status-go/protocol/messenger_contacts.go

513 lines
12 KiB
Go
Raw Normal View History

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/requests"
2021-02-17 23:14:48 +00:00
"github.com/status-im/status-go/protocol/transport"
)
2021-11-22 17:21:27 +00:00
// NOTE: This sets HasAddedUs to false, so next time we receive a contact request it will be reset to true
func (m *Messenger) RejectContactRequest(ctx context.Context, request *requests.RejectContactRequest) (*MessengerResponse, error) {
err := request.Validate()
if err != nil {
return nil, err
}
pubKey := request.ID.String()
contact, ok := m.allContacts.Load(pubKey)
if !ok {
var err error
contact, err = buildContactFromPkString(pubKey)
if err != nil {
return nil, err
}
}
contact.HasAddedUs = false
err = m.persistence.SaveContact(contact, nil)
if err != nil {
return nil, err
}
m.allContacts.Store(contact.ID, contact)
response := &MessengerResponse{}
response.Contacts = []*Contact{contact}
return response, nil
}
func (m *Messenger) AddContact(ctx context.Context, request *requests.AddContact) (*MessengerResponse, error) {
err := request.Validate()
if err != nil {
return nil, err
}
pubKey := request.ID.String()
2021-10-29 15:18:21 +00:00
ensName := request.ENSName
2021-03-29 15:41:30 +00:00
contact, ok := m.allContacts.Load(pubKey)
if !ok {
var err error
contact, err = buildContactFromPkString(pubKey)
if err != nil {
return nil, err
}
}
2021-10-29 15:18:21 +00:00
if ensName != "" {
clock := m.getTimesource().GetCurrentTime()
err := m.ensVerifier.ENSVerified(pubKey, ensName, clock)
if err != nil {
return nil, err
}
}
if err := m.addENSNameToContact(contact); err != nil {
return nil, err
}
if len(request.Nickname) != 0 {
contact.LocalNickname = request.Nickname
}
2021-10-01 14:50:16 +00:00
if !contact.Added {
2021-03-24 08:04:03 +00:00
contact.Added = true
}
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
// 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)
// And we re-register for push notications
err = m.reregisterForPushNotifications()
if err != nil {
return nil, err
}
2021-02-17 23:14:48 +00:00
// Reset last published time for ChatIdentity so new contact can receive data
err = m.resetLastPublishedTimeForChatIdentity()
2021-02-17 23:14:48 +00:00
if err != nil {
return nil, err
}
2021-03-25 15:15:22 +00:00
// Create the corresponding chat
profileChat := m.buildProfileChat(contact.ID)
2021-03-25 15:15:22 +00:00
_, err = m.Join(profileChat)
if err != nil {
return nil, err
}
2021-11-03 11:31:11 +00:00
if err := m.saveChat(profileChat); err != nil {
return nil, err
}
// Fetch contact code
publicKey, err := contact.PublicKey()
if err != nil {
return nil, err
}
filter, err := m.transport.JoinPrivate(publicKey)
if err != nil {
return nil, err
}
m.scheduleSyncFilter(filter)
2021-10-29 15:18:21 +00:00
ensName, err = m.settings.ENSName()
if err != nil {
return nil, err
}
2021-10-29 15:18:21 +00:00
// Finally we send a contact update so they are notified we added them
response, err := m.sendContactUpdate(context.Background(), pubKey, ensName, "")
if err != nil {
return nil, err
}
// Send profile picture with contact request
chat, ok := m.allChats.Load(contact.ID)
if !ok {
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
chat.Active = false
if err := m.saveChat(chat); err != nil {
return nil, err
}
}
err = m.handleStandaloneChatIdentity(chat)
if err != nil {
return nil, err
}
response.AddChat(profileChat)
_, err = m.transport.InitFilters([]string{profileChat.ID}, []*ecdsa.PublicKey{publicKey})
if err != nil {
return nil, err
}
2021-09-29 08:21:05 +00:00
// 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 {
2021-03-29 15:41:30 +00:00
contact, ok := m.allContacts.Load(pubKey)
if !ok {
return ErrContactNotFound
}
contact.Remove()
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
err := m.persistence.SaveContact(contact, nil)
if err != nil {
return err
}
err = m.syncContact(context.Background(), contact)
if err != nil {
return err
}
2021-03-29 15:41:30 +00:00
// 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)
2021-03-29 15:41:30 +00:00
_, ok = m.allChats.Load(profileChatID)
if ok {
chatResponse, err := m.deactivateChat(profileChatID, 0, 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
2021-03-29 15:41:30 +00:00
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
2021-03-24 08:04:03 +00:00
contacts = append(contacts, contact)
2021-03-29 15:41:30 +00:00
return true
})
return contacts
}
func (m *Messenger) AddedContacts() []*Contact {
var contacts []*Contact
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
if contact.Added {
contacts = append(contacts, contact)
}
return true
})
return contacts
}
2021-11-05 15:11:10 +00:00
func (m *Messenger) BlockedContacts() []*Contact {
var contacts []*Contact
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
if contact.Blocked {
contacts = append(contacts, contact)
}
return true
})
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
}
func (m *Messenger) SetContactLocalNickname(request *requests.SetContactLocalNickname) (*MessengerResponse, error) {
if err := request.Validate(); err != nil {
return nil, err
}
pubKey := request.ID.String()
nickname := request.Nickname
contact, ok := m.allContacts.Load(pubKey)
if !ok {
var err error
contact, err = buildContactFromPkString(pubKey)
if err != nil {
return nil, err
}
}
if err := m.addENSNameToContact(contact); err != nil {
return nil, err
}
clock := m.getTimesource().GetCurrentTime()
contact.LocalNickname = nickname
contact.LastUpdatedLocally = clock
err := m.persistence.SaveContact(contact, nil)
if err != nil {
return nil, err
}
2021-11-05 15:11:10 +00:00
m.allContacts.Store(contact.ID, contact)
response := &MessengerResponse{}
response.Contacts = []*Contact{contact}
err = m.syncContact(context.Background(), contact)
if err != nil {
return nil, err
}
return response, nil
}
func (m *Messenger) blockContact(contactID string) ([]*Chat, error) {
2021-11-05 15:11:10 +00:00
contact, ok := m.allContacts.Load(contactID)
if !ok {
var err error
contact, err = buildContactFromPkString(contactID)
if err != nil {
return nil, err
}
}
contact.Block()
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
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)
for _, chat := range chats {
2021-03-29 15:41:30 +00:00
m.allChats.Store(chat.ID, chat)
}
2021-03-29 15:41:30 +00:00
m.allChats.Delete(contact.ID)
2021-11-03 11:31:11 +00:00
m.allChats.Delete(buildProfileChatID(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) BlockContact(contactID string) (*MessengerResponse, error) {
response := &MessengerResponse{}
chats, err := m.blockContact(contactID)
if err != nil {
return nil, err
}
response.AddChats(chats)
response, err = m.DeclineAllPendingGroupInvitesFromUser(response, contactID)
if err != nil {
return nil, err
}
err = m.persistence.DismissAllActivityCenterNotificationsFromUser(contactID)
if err != nil {
return nil, err
}
return response, nil
}
2021-10-25 07:18:28 +00:00
func (m *Messenger) UnblockContact(contactID string) error {
contact, ok := m.allContacts.Load(contactID)
2021-10-27 10:59:43 +00:00
if !ok || !contact.Blocked {
2021-10-25 07:18:28 +00:00
return nil
}
contact.Unblock()
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
2021-12-03 12:38:09 +00:00
err := m.persistence.SaveContact(contact, nil)
if err != nil {
return err
}
2021-10-25 07:18:28 +00:00
m.allContacts.Store(contact.ID, contact)
2021-12-03 12:38:09 +00:00
err = m.syncContact(context.Background(), contact)
2021-10-25 07:18:28 +00:00
if err != nil {
return err
}
// re-register for push notifications
err = m.reregisterForPushNotifications()
if err != nil {
return err
}
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) {
myID := contactIDFromPublicKey(&m.identity.PublicKey)
2021-03-29 15:41:30 +00:00
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
2021-03-29 15:41:30 +00:00
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
2021-10-01 14:50:16 +00:00
if contact.Added {
2021-03-29 15:41:30 +00:00
if _, err = m.sendContactUpdate(ctx, contact.ID, ensName, profileImage); err != nil {
return false
}
}
2021-03-29 15:41:30 +00:00
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
2021-03-29 15:41:30 +00:00
contact, ok := m.allContacts.Load(chatID)
if !ok || !contact.Added {
return nil, nil
}
2021-03-29 15:41:30 +00:00
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
}
2021-03-29 15:41:30 +00:00
// 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}
2021-01-11 10:32:51 +00:00
response.AddChat(chat)
chat.LastClockValue = clock
err = m.saveChat(chat)
if err != nil {
return nil, err
}
return &response, nil
}
func (m *Messenger) addENSNameToContact(contact *Contact) error {
// Check if there's already a verified record
ensRecord, err := m.ensVerifier.GetVerifiedRecord(contact.ID)
if err != nil {
return err
}
if ensRecord == nil {
return nil
}
contact.Name = ensRecord.Name
contact.ENSVerified = true
return nil
}