2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-10-14 14:10:48 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-10 18:59:01 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/hex"
|
2020-02-10 11:22:37 +00:00
|
|
|
"fmt"
|
2021-03-05 14:38:32 +00:00
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-05-14 21:22:50 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2020-08-07 13:49:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-11-25 00:34:32 +00:00
|
|
|
"github.com/status-im/status-go/images"
|
2021-04-07 12:57:14 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2020-07-22 07:41:40 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2020-11-18 09:16:51 +00:00
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
2020-01-10 18:59:01 +00:00
|
|
|
"github.com/status-im/status-go/protocol/encryption/multidevice"
|
2021-01-11 10:32:51 +00:00
|
|
|
"github.com/status-im/status-go/protocol/ens"
|
2019-12-02 15:34:05 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2020-11-25 13:57:34 +00:00
|
|
|
|
2021-05-14 21:22:50 +00:00
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
2019-10-14 14:10:48 +00:00
|
|
|
)
|
|
|
|
|
2020-02-21 14:48:53 +00:00
|
|
|
const (
|
|
|
|
transactionRequestDeclinedMessage = "Transaction request declined"
|
|
|
|
requestAddressForTransactionAcceptedMessage = "Request address for transaction accepted"
|
|
|
|
requestAddressForTransactionDeclinedMessage = "Request address for transaction declined"
|
|
|
|
)
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
var ErrMessageNotAllowed = errors.New("message from a non-contact")
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
type MessageHandler struct {
|
2020-11-18 09:16:51 +00:00
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
persistence *sqlitePersistence
|
2021-04-07 12:57:14 +00:00
|
|
|
settings *accounts.Database
|
2021-04-22 13:36:18 +00:00
|
|
|
transport *transport.Transport
|
2021-01-11 10:32:51 +00:00
|
|
|
ensVerifier *ens.Verifier
|
2020-11-18 09:16:51 +00:00
|
|
|
communitiesManager *communities.Manager
|
|
|
|
logger *zap.Logger
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 13:36:18 +00:00
|
|
|
func newMessageHandler(identity *ecdsa.PrivateKey, logger *zap.Logger, persistence *sqlitePersistence, communitiesManager *communities.Manager, transport *transport.Transport, ensVerifier *ens.Verifier, settings *accounts.Database) *MessageHandler {
|
2020-01-10 18:59:01 +00:00
|
|
|
return &MessageHandler{
|
2020-11-18 09:16:51 +00:00
|
|
|
identity: identity,
|
|
|
|
persistence: persistence,
|
|
|
|
communitiesManager: communitiesManager,
|
2021-04-07 12:57:14 +00:00
|
|
|
settings: settings,
|
2021-01-11 10:32:51 +00:00
|
|
|
ensVerifier: ensVerifier,
|
2020-11-18 09:16:51 +00:00
|
|
|
transport: transport,
|
|
|
|
logger: logger}
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
// HandleMembershipUpdate updates a Chat instance according to the membership updates.
|
|
|
|
// It retrieves chat, if exists, and merges membership updates from the message.
|
|
|
|
// Finally, the Chat is updated with the new group events.
|
2021-03-29 15:41:30 +00:00
|
|
|
func (m *MessageHandler) HandleMembershipUpdate(messageState *ReceivedMessageState, chat *Chat, rawMembershipUpdate protobuf.MembershipUpdateMessage, translations *systemMessageTranslationsMap) error {
|
2020-01-10 18:59:01 +00:00
|
|
|
var group *v1protocol.Group
|
|
|
|
var err error
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
logger := m.logger.With(zap.String("site", "HandleMembershipUpdate"))
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
message, err := v1protocol.MembershipUpdateMessageFromProtobuf(&rawMembershipUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
if err := ValidateMembershipUpdateMessage(message, messageState.Timesource.GetCurrentTime()); err != nil {
|
|
|
|
logger.Warn("failed to validate message", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
allowed, err := m.isMessageAllowedFrom(messageState.AllContacts, messageState.CurrentMessageState.Contact.ID, chat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return ErrMessageNotAllowed
|
|
|
|
}
|
|
|
|
|
2020-08-07 13:49:37 +00:00
|
|
|
//if chat.InvitationAdmin exists means we are waiting for invitation request approvement, and in that case
|
|
|
|
//we need to create a new chat instance like we don't have a chat and just use a regular invitation flow
|
|
|
|
if chat == nil || len(chat.InvitationAdmin) > 0 {
|
2020-01-10 18:59:01 +00:00
|
|
|
if len(message.Events) == 0 {
|
|
|
|
return errors.New("can't create new group chat without events")
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-08-07 13:49:37 +00:00
|
|
|
|
|
|
|
//approve invitations
|
|
|
|
if chat != nil && len(chat.InvitationAdmin) > 0 {
|
|
|
|
|
|
|
|
groupChatInvitation := &GroupChatInvitation{
|
|
|
|
GroupChatInvitation: protobuf.GroupChatInvitation{
|
|
|
|
ChatId: message.ChatID,
|
|
|
|
},
|
|
|
|
From: types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey)),
|
|
|
|
}
|
|
|
|
|
|
|
|
groupChatInvitation, err = m.persistence.InvitationByID(groupChatInvitation.ID())
|
2020-10-08 10:46:03 +00:00
|
|
|
if err != nil && err != common.ErrRecordNotFound {
|
2020-08-07 13:49:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if groupChatInvitation != nil {
|
|
|
|
groupChatInvitation.State = protobuf.GroupChatInvitation_APPROVED
|
|
|
|
|
|
|
|
err := m.persistence.SaveInvitation(groupChatInvitation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
messageState.GroupChatInvitations[groupChatInvitation.ID()] = groupChatInvitation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
group, err = v1protocol.NewGroupWithEvents(message.ChatID, message.Events)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
return err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
ourKey := contactIDFromPublicKey(&m.identity.PublicKey)
|
2019-12-02 15:34:05 +00:00
|
|
|
// A new chat must contain us
|
2021-04-07 12:57:14 +00:00
|
|
|
if !group.IsMember(ourKey) {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("can't create a new group chat without us being a member")
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-02-07 11:30:26 +00:00
|
|
|
newChat := CreateGroupChat(messageState.Timesource)
|
2021-04-07 12:57:14 +00:00
|
|
|
// We set group chat inactive and create a notification instead
|
|
|
|
// unless is coming from us or a contact
|
|
|
|
isActive := messageState.CurrentMessageState.Contact.IsAdded() || messageState.CurrentMessageState.Contact.ID == ourKey
|
|
|
|
newChat.Active = isActive
|
2020-01-10 18:59:01 +00:00
|
|
|
chat = &newChat
|
|
|
|
} else {
|
|
|
|
existingGroup, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create a Group from Chat")
|
|
|
|
}
|
|
|
|
updateGroup, err := v1protocol.NewGroupWithEvents(message.ChatID, message.Events)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "invalid membership update")
|
|
|
|
}
|
|
|
|
merged := v1protocol.MergeMembershipUpdateEvents(existingGroup.Events(), updateGroup.Events())
|
2020-03-09 06:19:23 +00:00
|
|
|
group, err = v1protocol.NewGroupWithEvents(chat.ID, merged)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create a group with new membership updates")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
chat.updateChatFromGroupMembershipChanges(group)
|
|
|
|
|
|
|
|
if !chat.Active {
|
2021-05-26 06:38:25 +00:00
|
|
|
m.createMessageNotification(chat, messageState)
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
2020-04-22 12:58:28 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
systemMessages := buildSystemMessages(message.Events, translations)
|
|
|
|
|
|
|
|
for _, message := range systemMessages {
|
|
|
|
messageID := message.ID
|
|
|
|
exists, err := m.messageExists(messageID, messageState.ExistingMessagesMap)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to check message exists", zap.Error(err))
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
messageState.Response.Messages = append(messageState.Response.Messages, message)
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
messageState.Response.AddChat(chat)
|
2020-01-10 18:59:01 +00:00
|
|
|
// Store in chats map as it might be a new one
|
2021-03-29 15:41:30 +00:00
|
|
|
messageState.AllChats.Store(chat.ID, chat)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-07-26 21:37:04 +00:00
|
|
|
if message.Message != nil {
|
|
|
|
messageState.CurrentMessageState.Message = *message.Message
|
2020-01-10 18:59:01 +00:00
|
|
|
return m.HandleChatMessage(messageState)
|
2020-07-28 14:34:49 +00:00
|
|
|
} else if message.EmojiReaction != nil {
|
|
|
|
return m.HandleEmojiReaction(messageState, *message.EmojiReaction)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 06:38:25 +00:00
|
|
|
func (m *MessageHandler) createMessageNotification(chat *Chat, messageState *ReceivedMessageState) {
|
|
|
|
|
|
|
|
var notificationType ActivityCenterType
|
|
|
|
if chat.OneToOne() {
|
|
|
|
notificationType = ActivityCenterNotificationTypeNewOneToOne
|
|
|
|
} else {
|
|
|
|
notificationType = ActivityCenterNotificationTypeNewPrivateGroupChat
|
|
|
|
}
|
|
|
|
notification := &ActivityCenterNotification{
|
|
|
|
ID: types.FromHex(chat.ID),
|
|
|
|
Name: chat.Name,
|
|
|
|
LastMessage: chat.LastMessage,
|
|
|
|
Type: notificationType,
|
|
|
|
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
|
|
|
|
ChatID: chat.ID,
|
|
|
|
}
|
|
|
|
err := m.addActivityCenterNotification(messageState, notification)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to create activity center notification", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 13:27:01 +00:00
|
|
|
func (m *MessageHandler) handleCommandMessage(state *ReceivedMessageState, message *common.Message) error {
|
2020-01-10 18:59:01 +00:00
|
|
|
message.ID = state.CurrentMessageState.MessageID
|
|
|
|
message.From = state.CurrentMessageState.Contact.ID
|
|
|
|
message.Alias = state.CurrentMessageState.Contact.Alias
|
|
|
|
message.SigPubKey = state.CurrentMessageState.PublicKey
|
|
|
|
message.Identicon = state.CurrentMessageState.Contact.Identicon
|
|
|
|
message.WhisperTimestamp = state.CurrentMessageState.WhisperTimestamp
|
|
|
|
|
2021-05-25 09:40:02 +00:00
|
|
|
if err := message.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey)); err != nil {
|
2020-02-10 11:22:37 +00:00
|
|
|
return fmt.Errorf("failed to prepare content: %v", err)
|
|
|
|
}
|
2021-04-07 12:57:14 +00:00
|
|
|
chat, err := m.matchChatEntity(message, state.AllChats, state.AllContacts, state.Timesource)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
return err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
allowed, err := m.isMessageAllowedFrom(state.AllContacts, state.CurrentMessageState.Contact.ID, chat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return ErrMessageNotAllowed
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// If deleted-at is greater, ignore message
|
|
|
|
if chat.DeletedAtClockValue >= message.Clock {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the LocalChatID for the message
|
|
|
|
message.LocalChatID = chat.ID
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
if c, ok := state.AllChats.Load(chat.ID); ok {
|
2020-01-10 18:59:01 +00:00
|
|
|
chat = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the LocalChatID for the message
|
|
|
|
message.LocalChatID = chat.ID
|
|
|
|
|
|
|
|
// Increase unviewed count
|
2020-07-06 08:54:22 +00:00
|
|
|
if !common.IsPubKeyEqual(message.SigPubKey, &m.identity.PublicKey) {
|
2021-05-26 06:38:25 +00:00
|
|
|
m.updateUnviewedCounts(chat, message.Mentioned)
|
2020-01-10 18:59:01 +00:00
|
|
|
message.OutgoingStatus = ""
|
|
|
|
} else {
|
|
|
|
// Our own message, mark as sent
|
2020-09-01 13:27:01 +00:00
|
|
|
message.OutgoingStatus = common.OutgoingStatusSent
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, state.Timesource)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
return err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
if !chat.Active {
|
2021-05-26 06:38:25 +00:00
|
|
|
m.createMessageNotification(chat, state)
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
// Add to response
|
2021-04-07 12:57:14 +00:00
|
|
|
state.Response.AddChat(chat)
|
2020-01-10 18:59:01 +00:00
|
|
|
if message != nil {
|
|
|
|
state.Response.Messages = append(state.Response.Messages, message)
|
|
|
|
}
|
2021-04-07 12:57:14 +00:00
|
|
|
|
|
|
|
// Set in the modified maps chat
|
|
|
|
state.AllChats.Store(chat.ID, chat)
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessageHandler) HandleSyncInstallationContact(state *ReceivedMessageState, message protobuf.SyncInstallationContact) error {
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := state.AllChats.Load(state.CurrentMessageState.Contact.ID)
|
2020-01-10 18:59:01 +00:00
|
|
|
if !ok {
|
2020-01-20 16:44:32 +00:00
|
|
|
chat = OneToOneFromPublicKey(state.CurrentMessageState.PublicKey, state.Timesource)
|
2020-01-10 18:59:01 +00:00
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
contact, ok := state.AllContacts.Load(message.Id)
|
2020-01-10 18:59:01 +00:00
|
|
|
if !ok {
|
2020-12-22 10:49:25 +00:00
|
|
|
var err error
|
|
|
|
contact, err = buildContactFromPkString(message.Id)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if contact.LastUpdated < message.Clock {
|
|
|
|
if !contact.IsAdded() {
|
|
|
|
contact.SystemTags = append(contact.SystemTags, contactAdded)
|
|
|
|
}
|
|
|
|
if contact.Name != message.EnsName {
|
|
|
|
contact.Name = message.EnsName
|
|
|
|
contact.ENSVerified = false
|
|
|
|
}
|
|
|
|
contact.LastUpdated = message.Clock
|
2020-08-20 14:06:38 +00:00
|
|
|
contact.LocalNickname = message.LocalNickname
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
state.ModifiedContacts.Store(contact.ID, true)
|
|
|
|
state.AllContacts.Store(contact.ID, contact)
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
state.AllChats.Store(chat.ID, chat)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
func (m *MessageHandler) HandleSyncInstallationPublicChat(state *ReceivedMessageState, message protobuf.SyncInstallationPublicChat) *Chat {
|
2020-01-15 07:25:09 +00:00
|
|
|
chatID := message.Id
|
2021-03-29 15:41:30 +00:00
|
|
|
_, ok := state.AllChats.Load(chatID)
|
2020-01-15 07:25:09 +00:00
|
|
|
if ok {
|
2021-05-14 10:55:42 +00:00
|
|
|
return nil
|
2020-01-15 07:25:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
chat := CreatePublicChat(chatID, state.Timesource)
|
2020-01-15 07:25:09 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
timestamp := uint32(state.Timesource.GetCurrentTime() / 1000)
|
|
|
|
chat.SyncedTo = timestamp
|
|
|
|
chat.SyncedFrom = timestamp
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
state.AllChats.Store(chat.ID, chat)
|
2020-01-15 07:25:09 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
state.Response.AddChat(chat)
|
|
|
|
return chat
|
2020-01-15 07:25:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:22:50 +00:00
|
|
|
func (m *MessageHandler) HandlePinMessage(state *ReceivedMessageState, message protobuf.PinMessage) error {
|
|
|
|
logger := m.logger.With(zap.String("site", "HandlePinMessage"))
|
|
|
|
|
|
|
|
logger.Info("Handling pin message")
|
|
|
|
|
|
|
|
pinMessage := &common.PinMessage{
|
|
|
|
PinMessage: message,
|
|
|
|
// MessageID: message.MessageId,
|
|
|
|
WhisperTimestamp: state.CurrentMessageState.WhisperTimestamp,
|
|
|
|
From: state.CurrentMessageState.Contact.ID,
|
|
|
|
SigPubKey: state.CurrentMessageState.PublicKey,
|
|
|
|
Identicon: state.CurrentMessageState.Contact.Identicon,
|
|
|
|
Alias: state.CurrentMessageState.Contact.Alias,
|
|
|
|
}
|
|
|
|
|
|
|
|
chat, err := m.matchChatEntity(pinMessage, state.AllChats, state.AllContacts, state.Timesource)
|
|
|
|
if err != nil {
|
|
|
|
return err // matchChatEntity returns a descriptive error message
|
|
|
|
}
|
|
|
|
|
|
|
|
pinMessage.ID, err = generatePinMessageID(&m.identity.PublicKey, pinMessage, chat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If deleted-at is greater, ignore message
|
|
|
|
if chat.DeletedAtClockValue >= pinMessage.Clock {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the LocalChatID for the message
|
|
|
|
pinMessage.LocalChatID = chat.ID
|
|
|
|
|
|
|
|
if c, ok := state.AllChats.Load(chat.ID); ok {
|
|
|
|
chat = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the LocalChatID for the message
|
|
|
|
pinMessage.LocalChatID = chat.ID
|
|
|
|
|
|
|
|
if chat.LastClockValue < message.Clock {
|
|
|
|
chat.LastClockValue = message.Clock
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Response.AddPinMessage(pinMessage)
|
|
|
|
|
|
|
|
// Set in the modified maps chat
|
|
|
|
state.Response.AddChat(chat)
|
|
|
|
state.AllChats.Store(chat.ID, chat)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *MessageHandler) HandleContactUpdate(state *ReceivedMessageState, message protobuf.ContactUpdate) error {
|
|
|
|
logger := m.logger.With(zap.String("site", "HandleContactUpdate"))
|
|
|
|
contact := state.CurrentMessageState.Contact
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := state.AllChats.Load(contact.ID)
|
2021-04-07 12:57:14 +00:00
|
|
|
|
|
|
|
allowed, err := m.isMessageAllowedFrom(state.AllContacts, state.CurrentMessageState.Contact.ID, chat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !allowed {
|
|
|
|
return ErrMessageNotAllowed
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
if !ok {
|
2020-01-20 16:44:32 +00:00
|
|
|
chat = OneToOneFromPublicKey(state.CurrentMessageState.PublicKey, state.Timesource)
|
2020-01-10 18:59:01 +00:00
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("Handling contact update")
|
|
|
|
|
|
|
|
if contact.LastUpdated < message.Clock {
|
|
|
|
logger.Info("Updating contact")
|
2020-01-15 07:25:09 +00:00
|
|
|
if !contact.HasBeenAdded() && contact.ID != contactIDFromPublicKey(&m.identity.PublicKey) {
|
2020-01-10 18:59:01 +00:00
|
|
|
contact.SystemTags = append(contact.SystemTags, contactRequestReceived)
|
|
|
|
}
|
|
|
|
if contact.Name != message.EnsName {
|
|
|
|
contact.Name = message.EnsName
|
|
|
|
contact.ENSVerified = false
|
|
|
|
}
|
|
|
|
contact.LastUpdated = message.Clock
|
2021-03-29 15:41:30 +00:00
|
|
|
state.ModifiedContacts.Store(contact.ID, true)
|
|
|
|
state.AllContacts.Store(contact.ID, contact)
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if chat.LastClockValue < message.Clock {
|
|
|
|
chat.LastClockValue = message.Clock
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
state.Response.AddChat(chat)
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
state.AllChats.Store(chat.ID, chat)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessageHandler) HandlePairInstallation(state *ReceivedMessageState, message protobuf.PairInstallation) error {
|
|
|
|
logger := m.logger.With(zap.String("site", "HandlePairInstallation"))
|
2020-01-20 16:44:32 +00:00
|
|
|
if err := ValidateReceivedPairInstallation(&message, state.CurrentMessageState.WhisperTimestamp); err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
logger.Warn("failed to validate message", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
installation, ok := state.AllInstallations.Load(message.InstallationId)
|
2020-01-10 18:59:01 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("installation not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := &multidevice.InstallationMetadata{
|
|
|
|
Name: message.Name,
|
|
|
|
DeviceType: message.DeviceType,
|
|
|
|
}
|
|
|
|
|
|
|
|
installation.InstallationMetadata = metadata
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
state.AllInstallations.Store(message.InstallationId, installation)
|
|
|
|
state.ModifiedInstallations.Store(message.InstallationId, true)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
// HandleCommunityInvitation handles an community invitation
|
|
|
|
func (m *MessageHandler) HandleCommunityInvitation(state *ReceivedMessageState, signer *ecdsa.PublicKey, invitation protobuf.CommunityInvitation, rawPayload []byte) error {
|
|
|
|
if invitation.PublicKey == nil {
|
|
|
|
return errors.New("invalid pubkey")
|
|
|
|
}
|
|
|
|
pk, err := crypto.DecompressPubkey(invitation.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !common.IsPubKeyEqual(pk, &m.identity.PublicKey) {
|
|
|
|
return errors.New("invitation not for us")
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
communityResponse, err := m.communitiesManager.HandleCommunityInvitation(signer, &invitation, rawPayload)
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
community := communityResponse.Community
|
|
|
|
|
|
|
|
state.Response.AddCommunity(community)
|
|
|
|
state.Response.CommunityChanges = append(state.Response.CommunityChanges, communityResponse.Changes)
|
2020-11-18 09:16:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
// HandleCommunityRequestToJoin handles an community request to join
|
|
|
|
func (m *MessageHandler) HandleCommunityRequestToJoin(state *ReceivedMessageState, signer *ecdsa.PublicKey, requestToJoinProto protobuf.CommunityRequestToJoin) error {
|
|
|
|
if requestToJoinProto.CommunityId == nil {
|
|
|
|
return errors.New("invalid community id")
|
|
|
|
}
|
|
|
|
|
|
|
|
requestToJoin, err := m.communitiesManager.HandleCommunityRequestToJoin(signer, &requestToJoinProto)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Response.RequestsToJoinCommunity = append(state.Response.RequestsToJoinCommunity, requestToJoin)
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
community, err := m.communitiesManager.GetByID(requestToJoinProto.CommunityId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
contactID := contactIDFromPublicKey(signer)
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
contact, _ := state.AllContacts.Load(contactID)
|
2021-03-31 16:23:45 +00:00
|
|
|
|
|
|
|
state.Response.AddNotification(NewCommunityRequestToJoinNotification(requestToJoin.ID.String(), community, contact))
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleWrappedCommunityDescriptionMessage handles a wrapped community description
|
|
|
|
func (m *MessageHandler) handleWrappedCommunityDescriptionMessage(payload []byte) (*communities.CommunityResponse, error) {
|
2020-11-18 09:16:51 +00:00
|
|
|
return m.communitiesManager.HandleWrappedCommunityDescriptionMessage(payload)
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *MessageHandler) HandleChatMessage(state *ReceivedMessageState) error {
|
|
|
|
logger := m.logger.With(zap.String("site", "handleChatMessage"))
|
2020-01-20 16:44:32 +00:00
|
|
|
if err := ValidateReceivedChatMessage(&state.CurrentMessageState.Message, state.CurrentMessageState.WhisperTimestamp); err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
logger.Warn("failed to validate message", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
2020-09-01 13:27:01 +00:00
|
|
|
receivedMessage := &common.Message{
|
2020-01-10 18:59:01 +00:00
|
|
|
ID: state.CurrentMessageState.MessageID,
|
|
|
|
ChatMessage: state.CurrentMessageState.Message,
|
|
|
|
From: state.CurrentMessageState.Contact.ID,
|
|
|
|
Alias: state.CurrentMessageState.Contact.Alias,
|
|
|
|
SigPubKey: state.CurrentMessageState.PublicKey,
|
|
|
|
Identicon: state.CurrentMessageState.Contact.Identicon,
|
|
|
|
WhisperTimestamp: state.CurrentMessageState.WhisperTimestamp,
|
|
|
|
}
|
|
|
|
|
2021-05-25 09:40:02 +00:00
|
|
|
err := receivedMessage.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
|
2020-02-10 11:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to prepare message content: %v", err)
|
|
|
|
}
|
2021-04-07 12:57:14 +00:00
|
|
|
chat, err := m.matchChatEntity(receivedMessage, state.AllChats, state.AllContacts, state.Timesource)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
2020-07-25 14:16:00 +00:00
|
|
|
return err // matchChatEntity returns a descriptive error message
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
2021-02-15 07:19:14 +00:00
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
allowed, err := m.isMessageAllowedFrom(state.AllContacts, state.CurrentMessageState.Contact.ID, chat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return ErrMessageNotAllowed
|
|
|
|
}
|
|
|
|
|
2021-03-04 13:02:08 +00:00
|
|
|
// It looks like status-react created profile chats as public chats
|
|
|
|
// so for now we need to check for the presence of "@" in their chatID
|
|
|
|
if chat.Public() && receivedMessage.ContentType == protobuf.ChatMessage_IMAGE && !chat.ProfileUpdates() {
|
2021-02-15 07:19:14 +00:00
|
|
|
return errors.New("images are not allowed in public chats")
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-10-20 15:10:28 +00:00
|
|
|
// If profile updates check if author is the same as chat profile public key
|
|
|
|
if chat.ProfileUpdates() && receivedMessage.From != chat.Profile {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// If deleted-at is greater, ignore message
|
2020-11-06 13:47:02 +00:00
|
|
|
if chat.DeletedAtClockValue >= receivedMessage.Clock {
|
2020-01-10 18:59:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the LocalChatID for the message
|
|
|
|
receivedMessage.LocalChatID = chat.ID
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
if c, ok := state.AllChats.Load(chat.ID); ok {
|
2020-01-10 18:59:01 +00:00
|
|
|
chat = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the LocalChatID for the message
|
|
|
|
receivedMessage.LocalChatID = chat.ID
|
|
|
|
|
|
|
|
// Increase unviewed count
|
2020-07-06 08:54:22 +00:00
|
|
|
if !common.IsPubKeyEqual(receivedMessage.SigPubKey, &m.identity.PublicKey) {
|
2021-05-26 06:38:25 +00:00
|
|
|
m.updateUnviewedCounts(chat, receivedMessage.Mentioned)
|
2020-01-10 18:59:01 +00:00
|
|
|
} else {
|
|
|
|
// Our own message, mark as sent
|
2020-09-01 13:27:01 +00:00
|
|
|
receivedMessage.OutgoingStatus = common.OutgoingStatusSent
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(receivedMessage, state.Timesource)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
// If the chat is not active, create a notification in the center
|
|
|
|
if chat.OneToOne() && !chat.Active {
|
2021-05-26 06:38:25 +00:00
|
|
|
m.createMessageNotification(chat, state)
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
2021-05-26 06:38:25 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// Set in the modified maps chat
|
2021-01-11 10:32:51 +00:00
|
|
|
state.Response.AddChat(chat)
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
state.AllChats.Store(chat.ID, chat)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-02-05 10:09:33 +00:00
|
|
|
contact := state.CurrentMessageState.Contact
|
2021-01-11 10:32:51 +00:00
|
|
|
if receivedMessage.EnsName != "" {
|
|
|
|
oldRecord, err := m.ensVerifier.Add(contact.ID, receivedMessage.EnsName, receivedMessage.Clock)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to verify ENS name", zap.Error(err))
|
|
|
|
} else if oldRecord == nil {
|
|
|
|
// If oldRecord is nil, a new verification process will take place
|
|
|
|
// so we reset the record
|
|
|
|
contact.ENSVerified = false
|
2021-03-29 15:41:30 +00:00
|
|
|
state.ModifiedContacts.Store(contact.ID, true)
|
|
|
|
state.AllContacts.Store(contact.ID, contact)
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
2020-02-05 10:09:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
if receivedMessage.ContentType == protobuf.ChatMessage_COMMUNITY {
|
|
|
|
m.logger.Debug("Handling community content type")
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
communityResponse, err := m.handleWrappedCommunityDescriptionMessage(receivedMessage.GetCommunity())
|
2020-11-18 09:16:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
community := communityResponse.Community
|
2020-11-18 09:16:51 +00:00
|
|
|
receivedMessage.CommunityID = community.IDString()
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
state.Response.AddCommunity(community)
|
|
|
|
state.Response.CommunityChanges = append(state.Response.CommunityChanges, communityResponse.Changes)
|
2020-11-18 09:16:51 +00:00
|
|
|
}
|
2021-04-07 12:57:14 +00:00
|
|
|
|
2020-07-25 14:16:00 +00:00
|
|
|
state.Response.Messages = append(state.Response.Messages, receivedMessage)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
func (m *MessageHandler) addActivityCenterNotification(state *ReceivedMessageState, notification *ActivityCenterNotification) error {
|
|
|
|
err := m.persistence.SaveActivityCenterNotification(notification)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to save notification", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
state.Response.AddActivityCenterNotification(notification)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *MessageHandler) HandleRequestAddressForTransaction(messageState *ReceivedMessageState, command protobuf.RequestAddressForTransaction) error {
|
2020-01-20 16:44:32 +00:00
|
|
|
err := ValidateReceivedRequestAddressForTransaction(&command, messageState.CurrentMessageState.WhisperTimestamp)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-01 13:27:01 +00:00
|
|
|
message := &common.Message{
|
2020-01-10 18:59:01 +00:00
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Clock: command.Clock,
|
|
|
|
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
|
|
|
|
Text: "Request address for transaction",
|
2020-01-15 07:25:09 +00:00
|
|
|
ChatId: contactIDFromPublicKey(&m.identity.PublicKey),
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-01-10 18:59:01 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TRANSACTION_COMMAND,
|
|
|
|
},
|
2020-09-01 13:27:01 +00:00
|
|
|
CommandParameters: &common.CommandParameters{
|
2020-01-10 18:59:01 +00:00
|
|
|
ID: messageState.CurrentMessageState.MessageID,
|
|
|
|
Value: command.Value,
|
|
|
|
Contract: command.Contract,
|
2020-09-01 13:27:01 +00:00
|
|
|
CommandState: common.CommandStateRequestAddressForTransaction,
|
2020-01-10 18:59:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return m.handleCommandMessage(messageState, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessageHandler) HandleRequestTransaction(messageState *ReceivedMessageState, command protobuf.RequestTransaction) error {
|
2020-01-20 16:44:32 +00:00
|
|
|
err := ValidateReceivedRequestTransaction(&command, messageState.CurrentMessageState.WhisperTimestamp)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-01 13:27:01 +00:00
|
|
|
message := &common.Message{
|
2020-01-10 18:59:01 +00:00
|
|
|
ChatMessage: protobuf.ChatMessage{
|
|
|
|
Clock: command.Clock,
|
|
|
|
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
|
|
|
|
Text: "Request transaction",
|
2020-01-15 07:25:09 +00:00
|
|
|
ChatId: contactIDFromPublicKey(&m.identity.PublicKey),
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-01-10 18:59:01 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TRANSACTION_COMMAND,
|
|
|
|
},
|
2020-09-01 13:27:01 +00:00
|
|
|
CommandParameters: &common.CommandParameters{
|
2020-01-10 18:59:01 +00:00
|
|
|
ID: messageState.CurrentMessageState.MessageID,
|
|
|
|
Value: command.Value,
|
|
|
|
Contract: command.Contract,
|
2020-09-01 13:27:01 +00:00
|
|
|
CommandState: common.CommandStateRequestTransaction,
|
2020-01-10 18:59:01 +00:00
|
|
|
Address: command.Address,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return m.handleCommandMessage(messageState, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessageHandler) HandleAcceptRequestAddressForTransaction(messageState *ReceivedMessageState, command protobuf.AcceptRequestAddressForTransaction) error {
|
2020-01-20 16:44:32 +00:00
|
|
|
err := ValidateReceivedAcceptRequestAddressForTransaction(&command, messageState.CurrentMessageState.WhisperTimestamp)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
initialMessage, err := m.persistence.MessageByID(command.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if initialMessage == nil {
|
|
|
|
return errors.New("message not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if initialMessage.LocalChatID != messageState.CurrentMessageState.Contact.ID {
|
|
|
|
return errors.New("From must match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if initialMessage.OutgoingStatus == "" {
|
|
|
|
return errors.New("Initial message must originate from us")
|
|
|
|
}
|
|
|
|
|
2020-09-01 13:27:01 +00:00
|
|
|
if initialMessage.CommandParameters.CommandState != common.CommandStateRequestAddressForTransaction {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("Wrong state for command")
|
|
|
|
}
|
|
|
|
|
|
|
|
initialMessage.Clock = command.Clock
|
|
|
|
initialMessage.Timestamp = messageState.CurrentMessageState.WhisperTimestamp
|
2020-02-21 14:48:53 +00:00
|
|
|
initialMessage.Text = requestAddressForTransactionAcceptedMessage
|
2020-01-10 18:59:01 +00:00
|
|
|
initialMessage.CommandParameters.Address = command.Address
|
2020-04-06 12:08:53 +00:00
|
|
|
initialMessage.Seen = false
|
2020-09-01 13:27:01 +00:00
|
|
|
initialMessage.CommandParameters.CommandState = common.CommandStateRequestAddressForTransactionAccepted
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
// Hide previous message
|
2020-01-17 12:39:09 +00:00
|
|
|
previousMessage, err := m.persistence.MessageByCommandID(messageState.CurrentMessageState.Contact.ID, command.Id)
|
2020-10-08 10:46:03 +00:00
|
|
|
if err != nil && err != common.ErrRecordNotFound {
|
2020-01-10 18:59:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if previousMessage != nil {
|
|
|
|
err = m.persistence.HideMessage(previousMessage.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
initialMessage.Replace = previousMessage.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.handleCommandMessage(messageState, initialMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessageHandler) HandleSendTransaction(messageState *ReceivedMessageState, command protobuf.SendTransaction) error {
|
2020-01-20 16:44:32 +00:00
|
|
|
err := ValidateReceivedSendTransaction(&command, messageState.CurrentMessageState.WhisperTimestamp)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
transactionToValidate := &TransactionToValidate{
|
|
|
|
MessageID: messageState.CurrentMessageState.MessageID,
|
|
|
|
CommandID: command.Id,
|
|
|
|
TransactionHash: command.TransactionHash,
|
|
|
|
FirstSeen: messageState.CurrentMessageState.WhisperTimestamp,
|
|
|
|
Signature: command.Signature,
|
|
|
|
Validate: true,
|
|
|
|
From: messageState.CurrentMessageState.PublicKey,
|
|
|
|
RetryCount: 0,
|
|
|
|
}
|
|
|
|
m.logger.Info("Saving transction to validate", zap.Any("transaction", transactionToValidate))
|
|
|
|
|
|
|
|
return m.persistence.SaveTransactionToValidate(transactionToValidate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessageHandler) HandleDeclineRequestAddressForTransaction(messageState *ReceivedMessageState, command protobuf.DeclineRequestAddressForTransaction) error {
|
2020-01-20 16:44:32 +00:00
|
|
|
err := ValidateReceivedDeclineRequestAddressForTransaction(&command, messageState.CurrentMessageState.WhisperTimestamp)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldMessage, err := m.persistence.MessageByID(command.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if oldMessage == nil {
|
|
|
|
return errors.New("message not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldMessage.LocalChatID != messageState.CurrentMessageState.Contact.ID {
|
|
|
|
return errors.New("From must match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldMessage.OutgoingStatus == "" {
|
|
|
|
return errors.New("Initial message must originate from us")
|
|
|
|
}
|
|
|
|
|
2020-09-01 13:27:01 +00:00
|
|
|
if oldMessage.CommandParameters.CommandState != common.CommandStateRequestAddressForTransaction {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("Wrong state for command")
|
|
|
|
}
|
|
|
|
|
|
|
|
oldMessage.Clock = command.Clock
|
|
|
|
oldMessage.Timestamp = messageState.CurrentMessageState.WhisperTimestamp
|
2020-02-21 14:48:53 +00:00
|
|
|
oldMessage.Text = requestAddressForTransactionDeclinedMessage
|
2020-04-06 12:08:53 +00:00
|
|
|
oldMessage.Seen = false
|
2020-09-01 13:27:01 +00:00
|
|
|
oldMessage.CommandParameters.CommandState = common.CommandStateRequestAddressForTransactionDeclined
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
// Hide previous message
|
|
|
|
err = m.persistence.HideMessage(command.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldMessage.Replace = command.Id
|
|
|
|
|
|
|
|
return m.handleCommandMessage(messageState, oldMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessageHandler) HandleDeclineRequestTransaction(messageState *ReceivedMessageState, command protobuf.DeclineRequestTransaction) error {
|
2020-01-20 16:44:32 +00:00
|
|
|
err := ValidateReceivedDeclineRequestTransaction(&command, messageState.CurrentMessageState.WhisperTimestamp)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldMessage, err := m.persistence.MessageByID(command.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if oldMessage == nil {
|
|
|
|
return errors.New("message not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldMessage.LocalChatID != messageState.CurrentMessageState.Contact.ID {
|
|
|
|
return errors.New("From must match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldMessage.OutgoingStatus == "" {
|
|
|
|
return errors.New("Initial message must originate from us")
|
|
|
|
}
|
|
|
|
|
2020-09-01 13:27:01 +00:00
|
|
|
if oldMessage.CommandParameters.CommandState != common.CommandStateRequestTransaction {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("Wrong state for command")
|
|
|
|
}
|
|
|
|
|
|
|
|
oldMessage.Clock = command.Clock
|
|
|
|
oldMessage.Timestamp = messageState.CurrentMessageState.WhisperTimestamp
|
2020-02-21 14:48:53 +00:00
|
|
|
oldMessage.Text = transactionRequestDeclinedMessage
|
2020-04-06 12:08:53 +00:00
|
|
|
oldMessage.Seen = false
|
2020-09-01 13:27:01 +00:00
|
|
|
oldMessage.CommandParameters.CommandState = common.CommandStateRequestTransactionDeclined
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
// Hide previous message
|
|
|
|
err = m.persistence.HideMessage(command.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldMessage.Replace = command.Id
|
|
|
|
|
|
|
|
return m.handleCommandMessage(messageState, oldMessage)
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
func (m *MessageHandler) matchChatEntity(chatEntity common.ChatEntity, chats *chatMap, contacts *contactMap, timesource common.TimeSource) (*Chat, error) {
|
2020-07-25 14:16:00 +00:00
|
|
|
if chatEntity.GetSigPubKey() == nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
m.logger.Error("public key can't be empty")
|
2020-07-25 14:16:00 +00:00
|
|
|
return nil, errors.New("received a chatEntity with empty public key")
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2020-07-25 14:16:00 +00:00
|
|
|
case chatEntity.GetMessageType() == protobuf.MessageType_PUBLIC_GROUP:
|
2020-01-10 18:59:01 +00:00
|
|
|
// For public messages, all outgoing and incoming messages have the same chatID
|
|
|
|
// equal to a public chat name.
|
2020-07-25 14:16:00 +00:00
|
|
|
chatID := chatEntity.GetChatId()
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := chats.Load(chatID)
|
|
|
|
if !ok {
|
2020-07-25 14:16:00 +00:00
|
|
|
return nil, errors.New("received a public chatEntity from non-existing chat")
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
return chat, nil
|
2020-07-27 10:13:22 +00:00
|
|
|
case chatEntity.GetMessageType() == protobuf.MessageType_ONE_TO_ONE && common.IsPubKeyEqual(chatEntity.GetSigPubKey(), &m.identity.PublicKey):
|
2020-02-10 11:22:37 +00:00
|
|
|
// It's a private message coming from us so we rely on Message.ChatID
|
2020-01-10 18:59:01 +00:00
|
|
|
// If chat does not exist, it should be created to support multidevice synchronization.
|
2020-07-25 14:16:00 +00:00
|
|
|
chatID := chatEntity.GetChatId()
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := chats.Load(chatID)
|
|
|
|
if !ok {
|
2020-01-10 18:59:01 +00:00
|
|
|
if len(chatID) != PubKeyStringLength {
|
|
|
|
return nil, errors.New("invalid pubkey length")
|
|
|
|
}
|
|
|
|
bytePubKey, err := hex.DecodeString(chatID[2:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decode hex chatID")
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := crypto.UnmarshalPubkey(bytePubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decode pubkey")
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
chat = CreateOneToOneChat(chatID[:8], pubKey, timesource)
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
return chat, nil
|
2020-07-25 14:16:00 +00:00
|
|
|
case chatEntity.GetMessageType() == protobuf.MessageType_ONE_TO_ONE:
|
|
|
|
// It's an incoming private chatEntity. ChatID is calculated from the signature.
|
2020-01-10 18:59:01 +00:00
|
|
|
// If a chat does not exist, a new one is created and saved.
|
2020-07-25 14:16:00 +00:00
|
|
|
chatID := contactIDFromPublicKey(chatEntity.GetSigPubKey())
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := chats.Load(chatID)
|
|
|
|
if !ok {
|
2020-01-10 18:59:01 +00:00
|
|
|
// TODO: this should be a three-word name used in the mobile client
|
2021-01-11 10:32:51 +00:00
|
|
|
chat = CreateOneToOneChat(chatID[:8], chatEntity.GetSigPubKey(), timesource)
|
2021-04-07 12:57:14 +00:00
|
|
|
chat.Active = false
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
2021-04-07 12:57:14 +00:00
|
|
|
// We set the chat as inactive and will create a notification
|
|
|
|
// if it's not coming from a contact
|
|
|
|
contact, ok := contacts.Load(chatID)
|
|
|
|
chat.Active = chat.Active || (ok && contact.IsAdded())
|
2020-11-18 09:16:51 +00:00
|
|
|
return chat, nil
|
|
|
|
case chatEntity.GetMessageType() == protobuf.MessageType_COMMUNITY_CHAT:
|
|
|
|
chatID := chatEntity.GetChatId()
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := chats.Load(chatID)
|
|
|
|
if !ok {
|
2020-11-18 09:16:51 +00:00
|
|
|
return nil, errors.New("received community chat chatEntity for non-existing chat")
|
|
|
|
}
|
|
|
|
|
|
|
|
if chat.CommunityID == "" || chat.ChatType != ChatTypeCommunityChat {
|
|
|
|
return nil, errors.New("not an community chat")
|
|
|
|
}
|
|
|
|
|
2020-12-22 16:20:12 +00:00
|
|
|
var emojiReaction bool
|
|
|
|
// We allow emoji reactions from anyone
|
|
|
|
switch chatEntity.(type) {
|
|
|
|
case *EmojiReaction:
|
|
|
|
emojiReaction = true
|
|
|
|
}
|
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
canPost, err := m.communitiesManager.CanPost(chatEntity.GetSigPubKey(), chat.CommunityID, chat.CommunityChatID(), chatEntity.GetGrant())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-22 16:20:12 +00:00
|
|
|
if !emojiReaction && !canPost {
|
2020-11-18 09:16:51 +00:00
|
|
|
return nil, errors.New("user can't post")
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
return chat, nil
|
2020-07-25 14:16:00 +00:00
|
|
|
case chatEntity.GetMessageType() == protobuf.MessageType_PRIVATE_GROUP:
|
|
|
|
// In the case of a group chatEntity, ChatID is the same for all messages belonging to a group.
|
2020-01-10 18:59:01 +00:00
|
|
|
// It needs to be verified if the signature public key belongs to the chat.
|
2020-07-25 14:16:00 +00:00
|
|
|
chatID := chatEntity.GetChatId()
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := chats.Load(chatID)
|
|
|
|
if !ok {
|
2020-07-25 14:16:00 +00:00
|
|
|
return nil, errors.New("received group chat chatEntity for non-existing chat")
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 14:16:00 +00:00
|
|
|
theirKeyHex := contactIDFromPublicKey(chatEntity.GetSigPubKey())
|
2020-01-15 07:25:09 +00:00
|
|
|
myKeyHex := contactIDFromPublicKey(&m.identity.PublicKey)
|
2020-01-10 18:59:01 +00:00
|
|
|
var theyJoined bool
|
|
|
|
var iJoined bool
|
|
|
|
for _, member := range chat.Members {
|
|
|
|
if member.ID == theirKeyHex && member.Joined {
|
|
|
|
theyJoined = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, member := range chat.Members {
|
|
|
|
if member.ID == myKeyHex && member.Joined {
|
|
|
|
iJoined = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if theyJoined && iJoined {
|
|
|
|
return chat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("did not find a matching group chat")
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can not match a chat because there is no valid case")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessageHandler) messageExists(messageID string, existingMessagesMap map[string]bool) (bool, error) {
|
|
|
|
if _, ok := existingMessagesMap[messageID]; ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
existingMessagesMap[messageID] = true
|
|
|
|
|
|
|
|
// Check against the database, this is probably a bit slow for
|
|
|
|
// each message, but for now might do, we'll make it faster later
|
|
|
|
existingMessage, err := m.persistence.MessageByID(messageID)
|
2020-10-08 10:46:03 +00:00
|
|
|
if err != nil && err != common.ErrRecordNotFound {
|
2020-01-10 18:59:01 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if existingMessage != nil {
|
|
|
|
return true, nil
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
return false, nil
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-07-24 13:47:58 +00:00
|
|
|
|
2020-07-25 14:16:00 +00:00
|
|
|
func (m *MessageHandler) HandleEmojiReaction(state *ReceivedMessageState, pbEmojiR protobuf.EmojiReaction) error {
|
2020-07-24 13:47:58 +00:00
|
|
|
logger := m.logger.With(zap.String("site", "HandleEmojiReaction"))
|
2020-07-28 07:53:32 +00:00
|
|
|
if err := ValidateReceivedEmojiReaction(&pbEmojiR, state.Timesource.GetCurrentTime()); err != nil {
|
|
|
|
logger.Error("invalid emoji reaction", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-27 12:27:48 +00:00
|
|
|
from := state.CurrentMessageState.Contact.ID
|
|
|
|
|
2020-07-28 07:53:32 +00:00
|
|
|
emojiReaction := &EmojiReaction{
|
|
|
|
EmojiReaction: pbEmojiR,
|
|
|
|
From: from,
|
|
|
|
SigPubKey: state.CurrentMessageState.PublicKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
existingEmoji, err := m.persistence.EmojiReactionByID(emojiReaction.ID())
|
2020-10-08 10:46:03 +00:00
|
|
|
if err != common.ErrRecordNotFound && err != nil {
|
2020-07-27 12:27:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingEmoji != nil && existingEmoji.Clock >= pbEmojiR.Clock {
|
|
|
|
// this is not a valid emoji, ignoring
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
chat, err := m.matchChatEntity(emojiReaction, state.AllChats, state.AllContacts, state.Timesource)
|
2020-07-25 14:16:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err // matchChatEntity returns a descriptive error message
|
2020-07-24 13:47:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 07:53:32 +00:00
|
|
|
// Set local chat id
|
|
|
|
emojiReaction.LocalChatID = chat.ID
|
|
|
|
|
|
|
|
logger.Debug("Handling emoji reaction")
|
2020-07-24 13:47:58 +00:00
|
|
|
|
2020-07-25 14:16:00 +00:00
|
|
|
if chat.LastClockValue < pbEmojiR.Clock {
|
|
|
|
chat.LastClockValue = pbEmojiR.Clock
|
2020-07-24 13:47:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
state.Response.AddChat(chat)
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
state.AllChats.Store(chat.ID, chat)
|
2020-07-24 13:47:58 +00:00
|
|
|
|
2020-07-27 12:27:48 +00:00
|
|
|
// save emoji reaction
|
|
|
|
err = m.persistence.SaveEmojiReaction(emojiReaction)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.EmojiReactions[emojiReaction.ID()] = emojiReaction
|
2020-07-27 10:13:22 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-07 13:49:37 +00:00
|
|
|
|
|
|
|
func (m *MessageHandler) HandleGroupChatInvitation(state *ReceivedMessageState, pbGHInvitations protobuf.GroupChatInvitation) error {
|
2021-04-07 12:57:14 +00:00
|
|
|
allowed, err := m.isMessageAllowedFrom(state.AllContacts, state.CurrentMessageState.Contact.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return ErrMessageNotAllowed
|
|
|
|
}
|
2020-08-07 13:49:37 +00:00
|
|
|
logger := m.logger.With(zap.String("site", "HandleGroupChatInvitation"))
|
|
|
|
if err := ValidateReceivedGroupChatInvitation(&pbGHInvitations); err != nil {
|
|
|
|
logger.Error("invalid group chat invitation", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
groupChatInvitation := &GroupChatInvitation{
|
|
|
|
GroupChatInvitation: pbGHInvitations,
|
|
|
|
SigPubKey: state.CurrentMessageState.PublicKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
//From is the PK of author of invitation request
|
|
|
|
if groupChatInvitation.State == protobuf.GroupChatInvitation_REJECTED {
|
|
|
|
//rejected so From is the current user who received this rejection
|
|
|
|
groupChatInvitation.From = types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey))
|
|
|
|
} else {
|
|
|
|
//invitation request, so From is the author of message
|
|
|
|
groupChatInvitation.From = state.CurrentMessageState.Contact.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
existingInvitation, err := m.persistence.InvitationByID(groupChatInvitation.ID())
|
2020-10-08 10:46:03 +00:00
|
|
|
if err != common.ErrRecordNotFound && err != nil {
|
2020-08-07 13:49:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingInvitation != nil && existingInvitation.Clock >= pbGHInvitations.Clock {
|
|
|
|
// this is not a valid invitation, ignoring
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// save invitation
|
|
|
|
err = m.persistence.SaveInvitation(groupChatInvitation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.GroupChatInvitations[groupChatInvitation.ID()] = groupChatInvitation
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-18 12:41:36 +00:00
|
|
|
|
|
|
|
// HandleChatIdentity handles an incoming protobuf.ChatIdentity
|
|
|
|
// extracts contact information stored in the protobuf and adds it to the user's contact for update.
|
|
|
|
func (m *MessageHandler) HandleChatIdentity(state *ReceivedMessageState, ci protobuf.ChatIdentity) error {
|
|
|
|
logger := m.logger.With(zap.String("site", "HandleChatIdentity"))
|
2021-04-07 12:57:14 +00:00
|
|
|
allowed, err := m.isMessageAllowedFrom(state.AllContacts, state.CurrentMessageState.Contact.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return ErrMessageNotAllowed
|
|
|
|
}
|
2020-11-18 12:41:36 +00:00
|
|
|
contact := state.CurrentMessageState.Contact
|
|
|
|
|
|
|
|
logger.Info("Handling contact update")
|
2020-12-15 15:28:05 +00:00
|
|
|
newImages, err := m.persistence.SaveContactChatIdentity(contact.ID, &ci)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if newImages {
|
|
|
|
for imageType, image := range ci.Images {
|
|
|
|
if contact.Images == nil {
|
|
|
|
contact.Images = make(map[string]images.IdentityImage)
|
2020-11-30 11:03:44 +00:00
|
|
|
}
|
2020-12-15 15:28:05 +00:00
|
|
|
contact.Images[imageType] = images.IdentityImage{Name: imageType, Payload: image.Payload}
|
2020-12-02 09:31:48 +00:00
|
|
|
|
2020-11-18 12:41:36 +00:00
|
|
|
}
|
2021-03-29 15:41:30 +00:00
|
|
|
state.ModifiedContacts.Store(contact.ID, true)
|
|
|
|
state.AllContacts.Store(contact.ID, contact)
|
2020-11-18 12:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-07 12:57:14 +00:00
|
|
|
|
|
|
|
func (m *MessageHandler) isMessageAllowedFrom(allContacts *contactMap, publicKey string, chat *Chat) (bool, error) {
|
|
|
|
onlyFromContacts, err := m.settings.GetMessagesFromContactsOnly()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !onlyFromContacts {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's from us, it's allowed
|
|
|
|
if contactIDFromPublicKey(&m.identity.PublicKey) == publicKey {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the chat is active, we allow it
|
|
|
|
if chat != nil && chat.Active {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the chat is public, we allow it
|
|
|
|
if chat != nil && chat.Public() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
contact, ok := allContacts.Load(publicKey)
|
|
|
|
if !ok {
|
|
|
|
// If it's not in contacts, we don't allow it
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we check if we added it
|
|
|
|
return contact.IsAdded(), nil
|
|
|
|
}
|
2021-05-26 06:38:25 +00:00
|
|
|
|
|
|
|
func (m *MessageHandler) updateUnviewedCounts(chat *Chat, mentioned bool) {
|
|
|
|
chat.UnviewedMessagesCount++
|
|
|
|
if mentioned {
|
|
|
|
chat.UnviewedMentionsCount++
|
|
|
|
}
|
|
|
|
}
|