2020-12-22 10:49:25 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Messenger) SaveContact(contact *Contact) error {
|
|
|
|
return m.saveContact(contact)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) AddContact(ctx context.Context, pubKey string) (*MessengerResponse, error) {
|
2021-03-29 15:41:30 +00:00
|
|
|
contact, ok := m.allContacts.Load(pubKey)
|
2020-12-22 10:49:25 +00:00
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
contact, err = buildContactFromPkString(pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !contact.IsAdded() {
|
|
|
|
contact.SystemTags = append(contact.SystemTags, contactAdded)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
m.allContacts.Store(contact.ID, contact)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
// And we re-register for push notications
|
|
|
|
err = m.reregisterForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
// Create the corresponding chat
|
|
|
|
profileChat := m.buildProfileChat(contact.ID)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
_, err = m.Join(profileChat)
|
2020-12-22 10:49:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
response.AddChat(profileChat)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
func (m *Messenger) removeContact(ctx context.Context, response *MessengerResponse, pubKey string) error {
|
2021-03-29 15:41:30 +00:00
|
|
|
contact, ok := m.allContacts.Load(pubKey)
|
2020-12-22 10:49:25 +00:00
|
|
|
if !ok {
|
2021-09-03 08:26:05 +00:00
|
|
|
return ErrContactNotFound
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contact.Remove()
|
|
|
|
|
|
|
|
err := m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
2021-09-03 08:26:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.syncContact(context.Background(), contact)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
m.allContacts.Store(contact.ID, contact)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
// And we re-register for push notications
|
|
|
|
err = m.reregisterForPushNotifications()
|
|
|
|
if err != nil {
|
2021-09-03 08:26:05 +00:00
|
|
|
return err
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the corresponding profile chat
|
|
|
|
profileChatID := buildProfileChatID(contact.ID)
|
2021-03-29 15:41:30 +00:00
|
|
|
_, ok = m.allChats.Load(profileChatID)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
chatResponse, err := m.deactivateChat(profileChatID)
|
|
|
|
if err != nil {
|
2021-09-03 08:26:05 +00:00
|
|
|
return err
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
err = response.Merge(chatResponse)
|
|
|
|
if err != nil {
|
2021-09-03 08:26:05 +00:00
|
|
|
return err
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Contacts = []*Contact{contact}
|
2021-09-03 08:26:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) Contacts() []*Contact {
|
|
|
|
var contacts []*Contact
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
|
2020-12-22 10:49:25 +00:00
|
|
|
if contact.HasCustomFields() {
|
|
|
|
contacts = append(contacts, contact)
|
|
|
|
}
|
2021-03-29 15:41:30 +00:00
|
|
|
return true
|
|
|
|
})
|
2020-12-22 10:49:25 +00:00
|
|
|
return contacts
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContactByID assumes pubKey includes 0x prefix
|
|
|
|
func (m *Messenger) GetContactByID(pubKey string) *Contact {
|
2021-03-29 15:41:30 +00:00
|
|
|
contact, _ := m.allContacts.Load(pubKey)
|
|
|
|
return contact
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) BlockContact(contact *Contact) ([]*Chat, error) {
|
2021-09-03 08:26:05 +00:00
|
|
|
contact.Block()
|
2020-12-22 10:49:25 +00:00
|
|
|
chats, err := m.persistence.BlockContact(contact)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allContacts.Store(contact.ID, contact)
|
2020-12-22 10:49:25 +00:00
|
|
|
for _, chat := range chats {
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allChats.Store(chat.ID, chat)
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allChats.Delete(contact.ID)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
err = m.syncContact(context.Background(), contact)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
// 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
|
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
if m.shouldSyncContact(contact) {
|
2021-03-25 15:15:22 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-12-22 10:49:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allContacts.Store(contact.ID, contact)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
// Reregister only when data has changed
|
|
|
|
if shouldReregisterForPushNotifications {
|
|
|
|
return m.reregisterForPushNotifications()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send contact updates to all contacts added by us
|
2021-03-29 15:41:30 +00:00
|
|
|
func (m *Messenger) SendContactUpdates(ctx context.Context, ensName, profileImage string) (err error) {
|
2020-12-22 10:49:25 +00:00
|
|
|
myID := contactIDFromPublicKey(&m.identity.PublicKey)
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
if _, err = m.sendContactUpdate(ctx, myID, ensName, profileImage); err != nil {
|
2020-12-22 10:49:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This should not be sending paired messages, as we do it above
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
|
2020-12-22 10:49:25 +00:00
|
|
|
if contact.IsAdded() {
|
2021-03-29 15:41:30 +00:00
|
|
|
if _, err = m.sendContactUpdate(ctx, contact.ID, ensName, profileImage); err != nil {
|
|
|
|
return false
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-29 15:41:30 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return err
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
contact, ok := m.allContacts.Load(chatID)
|
2020-12-22 10:49:25 +00:00
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
contact, err = buildContactFromPkString(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := m.allChats.Load(chatID)
|
2020-12-22 10:49:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
2020-12-22 10:49:25 +00:00
|
|
|
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}
|
2021-01-11 10:32:51 +00:00
|
|
|
response.AddChat(chat)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
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 {
|
2021-03-29 15:41:30 +00:00
|
|
|
previousContact, ok := m.allContacts.Load(contact.ID)
|
2020-12-22 10:49:25 +00:00
|
|
|
return contact.IsAdded() && (!ok || !previousContact.IsAdded())
|
|
|
|
}
|
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
func (m *Messenger) shouldSyncContact(contact *Contact) bool {
|
2021-03-29 15:41:30 +00:00
|
|
|
previousContact, ok := m.allContacts.Load(contact.ID)
|
2020-12-22 10:49:25 +00:00
|
|
|
if !ok {
|
2021-09-03 08:26:05 +00:00
|
|
|
return contact.IsAdded()
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
2021-09-03 08:26:05 +00:00
|
|
|
|
|
|
|
return contact.LocalNickname != previousContact.LocalNickname ||
|
|
|
|
contact.IsAdded() != previousContact.IsAdded() ||
|
|
|
|
previousContact.IsBlocked() != contact.IsBlocked()
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) removedContact(contact *Contact) bool {
|
2021-03-29 15:41:30 +00:00
|
|
|
previousContact, ok := m.allContacts.Load(contact.ID)
|
2020-12-22 10:49:25 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return previousContact.IsAdded() && !contact.IsAdded()
|
|
|
|
}
|