-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
250 lines (207 loc) · 7.6 KB
/
server.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package contact
import (
"context"
"time"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
contactpb "github.com/code-payments/code-protobuf-api/generated/go/contact/v1"
auth_util "github.com/code-payments/code-server/pkg/code/auth"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/user"
"github.com/code-payments/code-server/pkg/grpc/client"
)
const (
getContactsMaxPageSize = 1024
)
type contactListServer struct {
log *logrus.Entry
data code_data.Provider
auth *auth_util.RPCSignatureVerifier
contactpb.UnimplementedContactListServer
}
func NewContactListServer(
data code_data.Provider,
auth *auth_util.RPCSignatureVerifier,
) contactpb.ContactListServer {
return &contactListServer{
log: logrus.StandardLogger().WithField("type", "contact/server"),
data: data,
auth: auth,
}
}
func (s *contactListServer) AddContacts(ctx context.Context, req *contactpb.AddContactsRequest) (*contactpb.AddContactsResponse, error) {
log := s.log.WithField("method", "AddContacts")
log = client.InjectLoggingMetadata(ctx, log)
ownerAccount, err := common.NewAccountFromProto(req.OwnerAccountId)
if err != nil {
log.WithError(err).Warn("owner account is invalid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("owner_account", ownerAccount.PublicKey().ToBase58())
containerID, err := user.GetDataContainerIDFromProto(req.ContainerId)
if err != nil {
log.WithError(err).Warn("failure parsing data container id as uuid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("data_container", containerID.String())
signature := req.Signature
req.Signature = nil
if err := s.auth.AuthorizeDataAccess(ctx, containerID, ownerAccount, req, signature); err != nil {
return nil, err
}
contacts := make([]string, len(req.Contacts))
for i, contact := range req.Contacts {
contacts[i] = contact.Value
}
err = s.data.BatchAddContacts(ctx, containerID, contacts)
if err != nil {
log.WithError(err).Warn("failure adding contacts")
return nil, status.Error(codes.Internal, "")
}
contactStatusByNumber, err := s.batchGetContactStatus(ctx, contacts)
if err != nil {
return nil, status.Error(codes.Internal, "")
}
return &contactpb.AddContactsResponse{
Result: contactpb.AddContactsResponse_OK,
ContactStatus: contactStatusByNumber,
}, nil
}
func (s *contactListServer) RemoveContacts(ctx context.Context, req *contactpb.RemoveContactsRequest) (*contactpb.RemoveContactsResponse, error) {
log := s.log.WithField("method", "RemoveContacts")
log = client.InjectLoggingMetadata(ctx, log)
ownerAccount, err := common.NewAccountFromProto(req.OwnerAccountId)
if err != nil {
log.WithError(err).Warn("owner account is invalid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("owner_account", ownerAccount.PublicKey().ToBase58())
containerID, err := user.GetDataContainerIDFromProto(req.ContainerId)
if err != nil {
log.WithError(err).Warn("failure parsing data container id as uuid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("data_container", containerID.String())
signature := req.Signature
req.Signature = nil
if err := s.auth.AuthorizeDataAccess(ctx, containerID, ownerAccount, req, signature); err != nil {
return nil, err
}
contacts := make([]string, len(req.Contacts))
for i, contact := range req.Contacts {
contacts[i] = contact.Value
}
err = s.data.BatchRemoveContacts(ctx, containerID, contacts)
if err != nil {
log.WithError(err).Warn("failure removing contacts")
return nil, status.Error(codes.Internal, "")
}
return &contactpb.RemoveContactsResponse{
Result: contactpb.RemoveContactsResponse_OK,
}, nil
}
func (s *contactListServer) GetContacts(ctx context.Context, req *contactpb.GetContactsRequest) (*contactpb.GetContactsResponse, error) {
log := s.log.WithField("method", "GetContacts")
log = client.InjectLoggingMetadata(ctx, log)
ownerAccount, err := common.NewAccountFromProto(req.OwnerAccountId)
if err != nil {
log.WithError(err).Warn("owner account is invalid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("owner_account", ownerAccount.PublicKey().ToBase58())
containerID, err := user.GetDataContainerIDFromProto(req.ContainerId)
if err != nil {
log.WithError(err).Warn("failure parsing data container id as uuid")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("data_container", containerID.String())
signature := req.Signature
req.Signature = nil
if err := s.auth.AuthorizeDataAccess(ctx, containerID, ownerAccount, req, signature); err != nil {
return nil, err
}
var pageTokenBytes []byte
if req.PageToken != nil {
pageTokenBytes = req.PageToken.Value
}
var contacts []*contactpb.Contact
var nextPageToken *contactpb.PageToken
// We attempt to make as much meaningful progress in the contact list
// when IncludeOnlyInAppContacts is true to limit unneccessary network
// calls by clients with large address books. We also try to avoid
// taking too long by checkpointing after a certain amount of time,
// which eliminates the risk that these clients will endlessly timeout.
start := time.Now()
for {
limit := getContactsMaxPageSize - len(contacts) - 1
page, nextPageTokenBytes, err := s.data.GetContacts(ctx, containerID, uint32(limit), pageTokenBytes)
if err != nil {
log.WithError(err).Warn("failure fetching page of contacts")
return nil, status.Error(codes.Internal, "")
}
contactStatusByNumber, err := s.batchGetContactStatus(ctx, page)
if err != nil {
return nil, status.Error(codes.Internal, "")
}
for phoneNumber, contactStatus := range contactStatusByNumber {
if req.IncludeOnlyInAppContacts && !contactStatus.IsRegistered {
continue
}
contacts = append(contacts, &contactpb.Contact{
PhoneNumber: &commonpb.PhoneNumber{
Value: phoneNumber,
},
Status: contactStatus,
})
}
if len(nextPageTokenBytes) > 0 {
pageTokenBytes = nextPageTokenBytes
nextPageToken = &contactpb.PageToken{
Value: nextPageTokenBytes,
}
} else {
// Stop processing when we've reached the end of the contact list.
nextPageToken = nil
break
}
if len(contacts) > getContactsMaxPageSize/2 {
// Stop processing when we've packed sufficient numbers into the
// page. We prefer to checkpoint than to slowly make progress on
// the contact list.
break
}
if time.Since(start) > 500*time.Millisecond {
// Stop processing after a sufficient amount of time has passed.
// This eliminates potential timeouts at the client and allows it
// to checkpoint some progress via the page token.
break
}
}
return &contactpb.GetContactsResponse{
Result: contactpb.GetContactsResponse_OK,
NextPageToken: nextPageToken,
Contacts: contacts,
}, nil
}
func (s *contactListServer) batchGetContactStatus(ctx context.Context, phoneNumbers []string) (map[string]*contactpb.ContactStatus, error) {
log := s.log.WithField("method", "batchGetContactStatus")
result := make(map[string]*contactpb.ContactStatus)
for _, phoneNumber := range phoneNumbers {
result[phoneNumber] = &contactpb.ContactStatus{
IsRegistered: false,
IsInvited: true,
}
}
registered, err := s.data.FilterVerifiedPhoneNumbers(ctx, phoneNumbers)
if err != nil {
log.WithError(err).Warn("failure filtering registered users")
return nil, err
}
for _, phoneNumber := range registered {
result[phoneNumber].IsRegistered = true
}
return result, nil
}