-
Notifications
You must be signed in to change notification settings - Fork 242
/
messenger_contacts.go
365 lines (301 loc) · 8.96 KB
/
messenger_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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package protocol
import (
"context"
"crypto/ecdsa"
"github.com/golang/protobuf/proto"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/transport"
)
func (m *Messenger) SaveContact(contact *Contact) error {
return m.saveContact(contact)
}
func (m *Messenger) AddContact(ctx context.Context, pubKey string) (*MessengerResponse, error) {
contact, ok := m.allContacts.Load(pubKey)
if !ok {
var err error
contact, err = buildContactFromPkString(pubKey)
if err != nil {
return nil, err
}
}
if !contact.Added {
contact.Added = true
}
// We sync the contact with the other devices
err := m.syncContact(context.Background(), contact)
if err != nil {
return nil, err
}
err = m.persistence.SaveContact(contact, nil)
if err != nil {
return nil, err
}
// TODO(samyoul) remove storing of an updated reference pointer?
m.allContacts.Store(contact.ID, contact)
// And we re-register for push notications
err = m.reregisterForPushNotifications()
if err != nil {
return nil, err
}
// Reset last published time for ChatIdentity so new contact can receive data
err = m.resetLastPublishedTimeForChatIdentity()
if err != nil {
return nil, err
}
// Create the corresponding chat
profileChat := m.buildProfileChat(contact.ID)
_, err = m.Join(profileChat)
if err != nil {
return nil, err
}
// Finally we send a contact update so they are notified we added them
// TODO: ens and picture are both blank for now
response, err := m.sendContactUpdate(context.Background(), pubKey, "", "")
if err != nil {
return nil, err
}
response.AddChat(profileChat)
publicKey, err := contact.PublicKey()
if err != nil {
return nil, err
}
// TODO: Add filters to response
_, err = m.transport.InitFilters([]string{profileChat.ID}, []*ecdsa.PublicKey{publicKey})
if err != nil {
return nil, err
}
// Publish contact code
err = m.publishContactCode()
if err != nil {
return nil, err
}
return response, nil
}
func (m *Messenger) resetLastPublishedTimeForChatIdentity() error {
// Reset last published time for ChatIdentity so new contact can receive data
contactCodeTopic := transport.ContactCodeTopic(&m.identity.PublicKey)
m.logger.Debug("contact state changed ResetWhenChatIdentityLastPublished")
return m.persistence.ResetWhenChatIdentityLastPublished(contactCodeTopic)
}
func (m *Messenger) removeContact(ctx context.Context, response *MessengerResponse, pubKey string) error {
contact, ok := m.allContacts.Load(pubKey)
if !ok {
return ErrContactNotFound
}
contact.Remove()
err := m.persistence.SaveContact(contact, nil)
if err != nil {
return err
}
err = m.syncContact(context.Background(), contact)
if err != nil {
return err
}
// TODO(samyoul) remove storing of an updated reference pointer?
m.allContacts.Store(contact.ID, contact)
// And we re-register for push notications
err = m.reregisterForPushNotifications()
if err != nil {
return err
}
// Create the corresponding profile chat
profileChatID := buildProfileChatID(contact.ID)
_, ok = m.allChats.Load(profileChatID)
if ok {
chatResponse, err := m.deactivateChat(profileChatID, false)
if err != nil {
return err
}
err = response.Merge(chatResponse)
if err != nil {
return err
}
}
response.Contacts = []*Contact{contact}
return nil
}
func (m *Messenger) RemoveContact(ctx context.Context, pubKey string) (*MessengerResponse, error) {
response := new(MessengerResponse)
err := m.removeContact(ctx, response, pubKey)
if err != nil {
return nil, err
}
return response, nil
}
func (m *Messenger) Contacts() []*Contact {
var contacts []*Contact
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
contacts = append(contacts, contact)
return true
})
return contacts
}
// GetContactByID assumes pubKey includes 0x prefix
func (m *Messenger) GetContactByID(pubKey string) *Contact {
contact, _ := m.allContacts.Load(pubKey)
return contact
}
func (m *Messenger) BlockContact(contact *Contact) ([]*Chat, error) {
contact.Block()
chats, err := m.persistence.BlockContact(contact)
if err != nil {
return nil, err
}
m.allContacts.Store(contact.ID, contact)
for _, chat := range chats {
m.allChats.Store(chat.ID, chat)
}
m.allChats.Delete(contact.ID)
err = m.syncContact(context.Background(), contact)
if err != nil {
return nil, err
}
// re-register for push notifications
err = m.reregisterForPushNotifications()
if err != nil {
return nil, err
}
return chats, nil
}
func (m *Messenger) saveContact(contact *Contact) error {
name, identicon, err := generateAliasAndIdenticon(contact.ID)
if err != nil {
return err
}
contact.Identicon = identicon
contact.Alias = name
if m.shouldSyncContact(contact) {
if m.isNewContact(contact) {
publicKey, err := contact.PublicKey()
if err != nil {
return err
}
filter, err := m.transport.JoinPrivate(publicKey)
if err != nil {
return err
}
m.scheduleSyncFilter(filter)
}
err := m.syncContact(context.Background(), contact)
if err != nil {
return err
}
}
// We check if it should re-register with the push notification server
shouldReregisterForPushNotifications := (m.isNewContact(contact) || m.removedContact(contact))
err = m.persistence.SaveContact(contact, nil)
if err != nil {
return err
}
m.allContacts.Store(contact.ID, contact)
// Reregister only when data has changed
if shouldReregisterForPushNotifications {
err := m.resetLastPublishedTimeForChatIdentity()
if err != nil {
return err
}
// Publish contact code
err = m.publishContactCode()
if err != nil {
return err
}
return m.reregisterForPushNotifications()
}
return nil
}
// Send contact updates to all contacts added by us
func (m *Messenger) SendContactUpdates(ctx context.Context, ensName, profileImage string) (err error) {
myID := contactIDFromPublicKey(&m.identity.PublicKey)
if _, err = m.sendContactUpdate(ctx, myID, ensName, profileImage); err != nil {
return err
}
// TODO: This should not be sending paired messages, as we do it above
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
if contact.Added {
if _, err = m.sendContactUpdate(ctx, contact.ID, ensName, profileImage); err != nil {
return false
}
}
return true
})
return err
}
// NOTE: this endpoint does not add the contact, the reason being is that currently
// that's left as a responsibility to the client, which will call both `SendContactUpdate`
// and `SaveContact` with the correct system tag.
// Ideally we have a single endpoint that does both, but probably best to bring `ENS` name
// on the messenger first.
// SendContactUpdate sends a contact update to a user and adds the user to contacts
func (m *Messenger) SendContactUpdate(ctx context.Context, chatID, ensName, profileImage string) (*MessengerResponse, error) {
return m.sendContactUpdate(ctx, chatID, ensName, profileImage)
}
func (m *Messenger) sendContactUpdate(ctx context.Context, chatID, ensName, profileImage string) (*MessengerResponse, error) {
var response MessengerResponse
contact, ok := m.allContacts.Load(chatID)
if !ok {
var err error
contact, err = buildContactFromPkString(chatID)
if err != nil {
return nil, err
}
}
chat, ok := m.allChats.Load(chatID)
if !ok {
publicKey, err := contact.PublicKey()
if err != nil {
return nil, err
}
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
// We don't want to show the chat to the user
chat.Active = false
}
// TODO(samyoul) remove storing of an updated reference pointer?
m.allChats.Store(chat.ID, chat)
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
contactUpdate := &protobuf.ContactUpdate{
Clock: clock,
EnsName: ensName,
ProfileImage: profileImage}
encodedMessage, err := proto.Marshal(contactUpdate)
if err != nil {
return nil, err
}
_, err = m.dispatchMessage(ctx, common.RawMessage{
LocalChatID: chatID,
Payload: encodedMessage,
MessageType: protobuf.ApplicationMetadataMessage_CONTACT_UPDATE,
ResendAutomatically: true,
})
if err != nil {
return nil, err
}
response.Contacts = []*Contact{contact}
response.AddChat(chat)
chat.LastClockValue = clock
err = m.saveChat(chat)
if err != nil {
return nil, err
}
return &response, m.saveContact(contact)
}
func (m *Messenger) isNewContact(contact *Contact) bool {
previousContact, ok := m.allContacts.Load(contact.ID)
return contact.Added && (!ok || !previousContact.Added)
}
func (m *Messenger) shouldSyncContact(contact *Contact) bool {
previousContact, ok := m.allContacts.Load(contact.ID)
if !ok {
return contact.Added
}
return contact.LocalNickname != previousContact.LocalNickname ||
contact.Added != previousContact.Added ||
previousContact.Blocked != contact.Blocked
}
func (m *Messenger) removedContact(contact *Contact) bool {
previousContact, ok := m.allContacts.Load(contact.ID)
if !ok {
return false
}
return previousContact.Added && !contact.Added
}