2020-12-22 10:49:25 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2022-01-18 16:31:34 +00:00
|
|
|
"errors"
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2022-06-13 10:57:51 +00:00
|
|
|
"go.uber.org/zap"
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-12-22 10:49:25 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2021-10-25 10:25:37 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2021-02-17 23:14:48 +00:00
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
2020-12-22 10:49:25 +00:00
|
|
|
)
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
func (m *Messenger) AcceptContactRequest(ctx context.Context, request *requests.AcceptContactRequest) (*MessengerResponse, error) {
|
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contactRequest, err := m.persistence.MessageByID(request.ID.String())
|
|
|
|
if err != nil {
|
2022-06-13 10:57:51 +00:00
|
|
|
m.logger.Error("could not find contact request message", zap.Error(err))
|
2022-01-18 16:31:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.addContact(contactRequest.From, "", "", "", contactRequest.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) SendContactRequest(ctx context.Context, request *requests.SendContactRequest) (*MessengerResponse, error) {
|
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID := request.ID.String()
|
|
|
|
|
|
|
|
response, err := m.addContact(chatID, "", "", "", "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := common.HexToPubkey(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
2022-05-27 09:14:40 +00:00
|
|
|
_, ok := m.allChats.Load(chatID)
|
2022-01-18 16:31:34 +00:00
|
|
|
if !ok {
|
|
|
|
// Create a one to one chat and set active to false
|
2022-05-27 09:14:40 +00:00
|
|
|
chat := CreateOneToOneChat(chatID, publicKey, m.getTimesource())
|
2022-01-18 16:31:34 +00:00
|
|
|
chat.Active = false
|
|
|
|
err = m.initChatSyncFields(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = m.saveChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chatMessage := &common.Message{}
|
|
|
|
chatMessage.ChatId = chatID
|
|
|
|
chatMessage.Text = request.Message
|
|
|
|
chatMessage.ContentType = protobuf.ChatMessage_CONTACT_REQUEST
|
|
|
|
chatMessage.ContactRequestState = common.ContactRequestStatePending
|
|
|
|
|
|
|
|
messageResponse, err := m.sendChatMessage(ctx, chatMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = response.Merge(messageResponse)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
func (m *Messenger) DismissContactRequest(ctx context.Context, request *requests.DismissContactRequest) (*MessengerResponse, error) {
|
2021-10-25 10:25:37 +00:00
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
contactRequest, err := m.persistence.MessageByID(request.ID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contact, ok := m.allContacts.Load(contactRequest.From)
|
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
contact, err = buildContactFromPkString(contactRequest.From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
contact.DismissContactRequest()
|
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
response.AddContact(contact)
|
2021-10-25 10:25:37 +00:00
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
contactRequest.ContactRequestState = common.ContactRequestStateDismissed
|
2021-10-29 15:18:21 +00:00
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
err = m.persistence.SetContactRequestState(contactRequest.ID, contactRequest.ContactRequestState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
notification, err := m.persistence.GetActivityCenterNotificationByID(types.FromHex(contactRequest.ID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if notification != nil {
|
|
|
|
err := m.persistence.UpdateActivityCenterNotificationMessage(notification.ID, contactRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
notification.Message = contactRequest
|
|
|
|
|
|
|
|
response.AddActivityCenterNotification(notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AddMessage(contactRequest)
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) addContact(pubKey, ensName, nickname, displayName, contactRequestID 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 10:48:02 +00:00
|
|
|
if err := m.addENSNameToContact(contact); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
if len(nickname) != 0 {
|
|
|
|
contact.LocalNickname = nickname
|
2021-10-25 10:25:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
if len(displayName) != 0 {
|
|
|
|
contact.DisplayName = displayName
|
2022-02-17 15:13:10 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 14:50:16 +00:00
|
|
|
if !contact.Added {
|
2022-04-25 09:02:49 +00:00
|
|
|
contact.Add()
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
2021-10-22 14:20:42 +00:00
|
|
|
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
contact.ContactRequestSent()
|
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
// We sync the contact with the other devices
|
2022-01-18 16:31:34 +00:00
|
|
|
err := m.syncContact(context.Background(), contact)
|
2020-12-22 10:49:25 +00:00
|
|
|
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-02-17 23:14:48 +00:00
|
|
|
// Reset last published time for ChatIdentity so new contact can receive data
|
2021-10-07 15:02:01 +00:00
|
|
|
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)
|
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
|
|
|
|
}
|
2021-11-03 11:31:11 +00:00
|
|
|
if err := m.saveChat(profileChat); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2021-10-22 14:20:42 +00:00
|
|
|
// 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
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
_, err = m.scheduleSyncFilters([]*transport.Filter{filter})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-22 14:20:42 +00:00
|
|
|
|
2021-10-29 15:18:21 +00:00
|
|
|
ensName, err = m.settings.ENSName()
|
2020-12-22 10:49:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
displayName, err = m.settings.DisplayName()
|
2022-02-17 15:13:10 +00:00
|
|
|
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
|
2022-02-17 15:13:10 +00:00
|
|
|
response, err := m.sendContactUpdate(context.Background(), pubKey, displayName, ensName, "")
|
2021-10-22 14:20:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
if len(contactRequestID) != 0 {
|
|
|
|
contactRequest, err := m.persistence.MessageByID(contactRequestID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contactRequest.ContactRequestState = common.ContactRequestStateAccepted
|
|
|
|
|
|
|
|
err = m.persistence.SetContactRequestState(contactRequest.ID, contactRequest.ContactRequestState)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contact.AcceptContactRequest()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
acceptContactRequest := &protobuf.AcceptContactRequest{
|
|
|
|
Id: contactRequest.ID,
|
|
|
|
Clock: clock,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(acceptContactRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(context.Background(), common.RawMessage{
|
|
|
|
LocalChatID: pubKey,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_ACCEPT_CONTACT_REQUEST,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
notification, err := m.persistence.GetActivityCenterNotificationByID(types.FromHex(contactRequest.ID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if notification != nil {
|
|
|
|
err := m.persistence.UpdateActivityCenterNotificationMessage(notification.ID, contactRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
notification.Message = contactRequest
|
|
|
|
|
|
|
|
response.AddActivityCenterNotification(notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AddMessage(contactRequest)
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:20:42 +00:00
|
|
|
// 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)
|
2020-12-22 10:49:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:20:42 +00:00
|
|
|
response.AddChat(profileChat)
|
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
response.AddContact(contact)
|
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
return response, nil
|
|
|
|
}
|
2022-01-18 16:31:34 +00:00
|
|
|
func (m *Messenger) AddContact(ctx context.Context, request *requests.AddContact) (*MessengerResponse, error) {
|
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.addContact(request.ID.String(), request.ENSName, request.Nickname, request.DisplayName, "")
|
|
|
|
}
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2021-10-07 15:02:01 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
contact.RetractContactRequest()
|
2020-12-22 10:49:25 +00:00
|
|
|
contact.Remove()
|
2021-10-22 14:20:42 +00:00
|
|
|
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
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 {
|
2021-12-17 13:57:54 +00:00
|
|
|
chatResponse, err := m.deactivateChat(profileChatID, 0, false)
|
2020-12-22 10:49:25 +00:00
|
|
|
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
|
|
|
|
}
|
2022-01-18 16:31:34 +00:00
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
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) {
|
2021-03-24 08:04:03 +00:00
|
|
|
contacts = append(contacts, contact)
|
2021-03-29 15:41:30 +00:00
|
|
|
return true
|
|
|
|
})
|
2020-12-22 10:49:25 +00:00
|
|
|
return contacts
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:39:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
func (m *Messenger) MutualContacts() []*Contact {
|
|
|
|
var contacts []*Contact
|
|
|
|
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {
|
|
|
|
if contact.Added && contact.HasAddedUs {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-10-25 10:25:37 +00:00
|
|
|
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
|
2021-10-22 14:20:42 +00:00
|
|
|
|
|
|
|
contact, ok := m.allContacts.Load(pubKey)
|
|
|
|
if !ok {
|
2021-10-25 10:25:37 +00:00
|
|
|
var err error
|
|
|
|
contact, err = buildContactFromPkString(pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-22 14:20:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 10:48:02 +00:00
|
|
|
if err := m.addENSNameToContact(contact); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:20:42 +00:00
|
|
|
clock := m.getTimesource().GetCurrentTime()
|
|
|
|
contact.LocalNickname = nickname
|
|
|
|
contact.LastUpdatedLocally = clock
|
|
|
|
|
2021-10-25 10:25:37 +00:00
|
|
|
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)
|
|
|
|
|
2021-10-22 14:20:42 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
response.Contacts = []*Contact{contact}
|
|
|
|
|
2021-10-25 10:25:37 +00:00
|
|
|
err = m.syncContact(context.Background(), contact)
|
2021-10-22 14:20:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2022-04-08 20:14:37 +00:00
|
|
|
func (m *Messenger) blockContact(contactID string, isDesktopFunc bool) ([]*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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-04-08 20:14:37 +00:00
|
|
|
if isDesktopFunc {
|
|
|
|
contact.BlockDesktop()
|
|
|
|
} else {
|
|
|
|
contact.Block()
|
|
|
|
}
|
2021-10-22 14:20:42 +00:00
|
|
|
contact.LastUpdatedLocally = m.getTimesource().GetCurrentTime()
|
|
|
|
|
2022-04-08 20:14:37 +00:00
|
|
|
chats, err := m.persistence.BlockContact(contact, isDesktopFunc)
|
2020-12-22 10:49:25 +00:00
|
|
|
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
|
|
|
}
|
2022-04-08 20:14:37 +00:00
|
|
|
|
|
|
|
if !isDesktopFunc {
|
|
|
|
m.allChats.Delete(contact.ID)
|
|
|
|
m.allChats.Delete(buildProfileChatID(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
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:44:40 +00:00
|
|
|
func (m *Messenger) BlockContact(contactID string) (*MessengerResponse, error) {
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
2022-04-08 20:14:37 +00:00
|
|
|
chats, err := m.blockContact(contactID, false)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// The same function as the one above.
|
|
|
|
func (m *Messenger) BlockContactDesktop(contactID string) (*MessengerResponse, error) {
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
chats, err := m.blockContact(contactID, true)
|
2021-12-06 12:44:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
// 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)
|
|
|
|
|
2022-02-17 15:13:10 +00:00
|
|
|
displayName, err := m.settings.DisplayName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = m.sendContactUpdate(ctx, myID, displayName, 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) {
|
2021-10-01 14:50:16 +00:00
|
|
|
if contact.Added {
|
2022-02-17 15:13:10 +00:00
|
|
|
if _, err = m.sendContactUpdate(ctx, contact.ID, displayName, ensName, profileImage); err != nil {
|
2021-03-29 15:41:30 +00:00
|
|
|
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) {
|
2022-02-17 15:13:10 +00:00
|
|
|
displayName, err := m.settings.DisplayName()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.sendContactUpdate(ctx, chatID, displayName, ensName, profileImage)
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 15:13:10 +00:00
|
|
|
func (m *Messenger) sendContactUpdate(ctx context.Context, chatID, displayName, ensName, profileImage string) (*MessengerResponse, error) {
|
2020-12-22 10:49:25 +00:00
|
|
|
var response MessengerResponse
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
contact, ok := m.allContacts.Load(chatID)
|
2021-10-22 14:20:42 +00:00
|
|
|
if !ok || !contact.Added {
|
|
|
|
return nil, nil
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2022-02-17 15:13:10 +00:00
|
|
|
DisplayName: displayName,
|
2020-12-22 10:49:25 +00:00
|
|
|
EnsName: ensName,
|
2022-02-17 15:13:10 +00:00
|
|
|
ProfileImage: profileImage,
|
|
|
|
}
|
2020-12-22 10:49:25 +00:00
|
|
|
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
|
|
|
|
}
|
2021-10-22 14:20:42 +00:00
|
|
|
return &response, nil
|
2020-12-22 10:49:25 +00:00
|
|
|
}
|
2021-10-26 10:48:02 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:13:10 +00:00
|
|
|
contact.EnsName = ensRecord.Name
|
2021-10-26 10:48:02 +00:00
|
|
|
contact.ENSVerified = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-18 16:31:34 +00:00
|
|
|
|
|
|
|
func (m *Messenger) RetractContactRequest(request *requests.RetractContactRequest) (*MessengerResponse, error) {
|
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
contact, ok := m.allContacts.Load(request.ContactID.String())
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("contact not found")
|
|
|
|
}
|
|
|
|
contact.HasAddedUs = false
|
|
|
|
m.allContacts.Store(contact.ID, contact)
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
err = m.removeContact(context.Background(), response, contact.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chat, ok := m.allChats.Load(contact.ID)
|
|
|
|
if !ok {
|
|
|
|
pubKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat = OneToOneFromPublicKey(pubKey, m.getTimesource())
|
|
|
|
chat.Active = false
|
|
|
|
if err := m.saveChat(chat); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
retractContactRequest := &protobuf.RetractContactRequest{
|
|
|
|
Clock: clock,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(retractContactRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(context.Background(), common.RawMessage{
|
|
|
|
LocalChatID: contact.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_RETRACT_CONTACT_REQUEST,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2022-06-17 16:20:43 +00:00
|
|
|
func (m *Messenger) AcceptLatestContactRequestForContact(ctx context.Context, request *requests.AcceptLatestContactRequestForContact) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contactRequestID, err := m.persistence.LatestPendingContactRequestIDForContact(request.ID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.AcceptContactRequest(ctx, &requests.AcceptContactRequest{ID: types.Hex2Bytes(contactRequestID)})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DismissLatestContactRequestForContact(ctx context.Context, request *requests.DismissLatestContactRequestForContact) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contactRequestID, err := m.persistence.LatestPendingContactRequestIDForContact(request.ID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.DismissContactRequest(ctx, &requests.DismissContactRequest{ID: types.Hex2Bytes(contactRequestID)})
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
func (m *Messenger) PendingContactRequests(cursor string, limit int) ([]*common.Message, string, error) {
|
|
|
|
return m.persistence.PendingContactRequests(cursor, limit)
|
|
|
|
}
|
2022-06-13 10:57:51 +00:00
|
|
|
|
|
|
|
func defaultContactRequestID(contactID string) string {
|
|
|
|
return "0x" + types.Bytes2Hex(append(types.Hex2Bytes(contactID), 0x20))
|
|
|
|
}
|