-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontacts.go
76 lines (63 loc) · 1.36 KB
/
contacts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/boltdb/bolt"
)
type ContactTok struct {
Token string `json:"token"`
Relay string `json:"relay"`
SupportSMS bool `json:"supportsSms"`
}
type ContactsReq struct {
Contacts []string `json:"contacts"`
}
type ContactsResp struct {
Contacts []ContactTok `json:"contacts"`
}
func directory(w http.ResponseWriter, req *http.Request) {
uname, _, ok := req.BasicAuth()
if !ok {
http.Error(w, "auth fail", 401)
return
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "server error", 500)
return
}
var cReq ContactsReq
err = json.Unmarshal(body, &cReq)
if err != nil {
http.Error(w, "server error", 500)
return
}
log.Infof("number: %s, contacts: %s", uname, cReq.Contacts)
var cResp ContactsResp
err = db.View(func(tx *bolt.Tx) error {
for _, tok := range cReq.Contacts {
tokHash, err := base64.RawStdEncoding.DecodeString(tok)
if err != nil {
return err
}
id := encodeNumber(string(tokHash))
if tx.Bucket(id) != nil {
cResp.Contacts = append(cResp.Contacts, ContactTok{Token: tok})
}
}
return nil
})
if err != nil {
http.Error(w, "server error", 500)
return
}
resp, err := json.Marshal(cResp)
if err != nil {
http.Error(w, "server error", 500)
return
}
w.WriteHeader(200)
w.Write(resp)
}