2021-06-07 08:31:27 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-12-02 11:34:02 +00:00
|
|
|
"crypto/ecdsa"
|
2021-06-07 08:31:27 +00:00
|
|
|
"errors"
|
|
|
|
|
2022-12-02 11:34:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2022-09-28 11:42:17 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
2021-06-07 08:31:27 +00:00
|
|
|
"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-07-26 21:06:32 +00:00
|
|
|
var ErrInvalidEditOrDeleteAuthor = errors.New("sender is not the author of the message")
|
|
|
|
var ErrInvalidDeleteTypeAuthor = errors.New("message type cannot be deleted")
|
2022-10-05 17:54:47 +00:00
|
|
|
var ErrInvalidEditContentType = errors.New("only text or emoji messages can be replaced")
|
2022-12-02 11:34:02 +00:00
|
|
|
var ErrInvalidDeletePermission = errors.New("don't have enough permission to delete")
|
2021-06-07 08:31:27 +00:00
|
|
|
|
|
|
|
func (m *Messenger) EditMessage(ctx context.Context, request *requests.EditMessage) (*MessengerResponse, error) {
|
|
|
|
err := request.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message, err := m.persistence.MessageByID(request.ID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-08 06:07:45 +00:00
|
|
|
if message.From != common.PubkeyToHex(&m.identity.PublicKey) {
|
2021-07-26 21:06:32 +00:00
|
|
|
return nil, ErrInvalidEditOrDeleteAuthor
|
2021-06-07 08:31:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 11:10:17 +00:00
|
|
|
if message.ContentType != protobuf.ChatMessage_TEXT_PLAIN && message.ContentType != protobuf.ChatMessage_EMOJI && message.ContentType != protobuf.ChatMessage_IMAGE {
|
2021-06-07 08:31:27 +00:00
|
|
|
return nil, ErrInvalidEditContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats.Load(message.ChatId)
|
|
|
|
if !ok {
|
2023-04-26 11:10:17 +00:00
|
|
|
return nil, ErrChatNotFound
|
2021-06-07 08:31:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 07:21:25 +00:00
|
|
|
messages, err := m.getOtherMessagesInAlbum(message, message.LocalChatID)
|
2021-06-07 11:45:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-26 11:10:17 +00:00
|
|
|
response := &MessengerResponse{}
|
2021-06-07 08:31:27 +00:00
|
|
|
|
2023-04-26 11:10:17 +00:00
|
|
|
for _, message := range messages {
|
2023-08-31 07:21:25 +00:00
|
|
|
//Add LinkPreviews only to first message
|
|
|
|
if len(request.LinkPreviews) > 0 {
|
|
|
|
message.LinkPreviews = request.LinkPreviews
|
|
|
|
}
|
2023-10-13 12:25:34 +00:00
|
|
|
if len(request.StatusLinkPreviews) > 0 {
|
|
|
|
message.StatusLinkPreviews = request.StatusLinkPreviews
|
|
|
|
}
|
2023-04-26 11:10:17 +00:00
|
|
|
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2021-06-07 08:31:27 +00:00
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
editMessage := NewEditMessage()
|
2023-04-26 11:10:17 +00:00
|
|
|
|
|
|
|
editMessage.Text = request.Text
|
|
|
|
editMessage.ContentType = request.ContentType
|
|
|
|
editMessage.ChatId = message.ChatId
|
|
|
|
editMessage.MessageId = message.ID
|
|
|
|
editMessage.Clock = clock
|
2023-10-13 12:25:34 +00:00
|
|
|
|
2023-11-08 17:28:55 +00:00
|
|
|
// We consider link previews non-critical data, so we do not want to block
|
|
|
|
// messages from being sent.
|
|
|
|
|
2023-08-31 07:21:25 +00:00
|
|
|
unfurledLinks, err := message.ConvertLinkPreviewsToProto()
|
|
|
|
if err != nil {
|
2023-11-08 17:28:55 +00:00
|
|
|
m.logger.Error("failed to convert link previews", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
editMessage.UnfurledLinks = unfurledLinks
|
2023-08-31 07:21:25 +00:00
|
|
|
}
|
2023-04-26 11:10:17 +00:00
|
|
|
|
2023-10-13 12:25:34 +00:00
|
|
|
unfurledStatusLinks, err := message.ConvertStatusLinkPreviewsToProto()
|
|
|
|
if err != nil {
|
2023-11-08 17:28:55 +00:00
|
|
|
m.logger.Error("failed to convert status link previews", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
editMessage.UnfurledStatusLinks = unfurledStatusLinks
|
2023-10-13 12:25:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
err = m.applyEditMessage(editMessage.EditMessage, message)
|
2021-06-23 10:07:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-26 11:10:17 +00:00
|
|
|
encodedMessage, err := m.encodeChatEntity(chat, editMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage := common.RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_EDIT_MESSAGE,
|
|
|
|
SkipGroupMessageWrap: true,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
}
|
|
|
|
_, err = m.dispatchMessage(ctx, rawMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if chat.LastMessage != nil && chat.LastMessage.ID == message.ID {
|
|
|
|
chat.LastMessage = message
|
|
|
|
err := m.saveChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AddMessage(message)
|
|
|
|
}
|
2023-04-17 14:44:48 +00:00
|
|
|
|
|
|
|
// pull updated messages
|
|
|
|
updatedMessages, err := m.persistence.MessagesByResponseTo(request.ID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddMessages(updatedMessages)
|
2023-08-31 07:21:25 +00:00
|
|
|
m.prepareMessages(response.messages)
|
2021-06-23 10:07:26 +00:00
|
|
|
response.AddChat(chat)
|
2021-06-07 08:31:27 +00:00
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
2021-06-07 11:45:06 +00:00
|
|
|
|
2023-03-07 14:38:09 +00:00
|
|
|
func (m *Messenger) CanDeleteMessageForEveryoneInCommunity(communityID string, publicKey *ecdsa.PublicKey) bool {
|
2022-12-02 11:34:02 +00:00
|
|
|
if communityID != "" {
|
|
|
|
community, err := m.communitiesManager.GetByIDString(communityID)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to find community", zap.String("communityID", communityID), zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return community.CanDeleteMessageForEveryone(publicKey)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-07 14:38:09 +00:00
|
|
|
func (m *Messenger) CanDeleteMessageForEveryoneInPrivateGroupChat(chat *Chat, publicKey *ecdsa.PublicKey) bool {
|
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to find group", zap.String("chatID", chat.ID), zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
admins := group.Admins()
|
|
|
|
return stringSliceContains(admins, common.PubkeyToHex(publicKey))
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:06:32 +00:00
|
|
|
func (m *Messenger) DeleteMessageAndSend(ctx context.Context, messageID string) (*MessengerResponse, error) {
|
|
|
|
message, err := m.persistence.MessageByID(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats.Load(message.ChatId)
|
|
|
|
if !ok {
|
2023-04-26 11:10:17 +00:00
|
|
|
return nil, ErrChatNotFound
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 11:34:02 +00:00
|
|
|
var canDeleteMessageForEveryone = false
|
2023-02-01 00:57:35 +00:00
|
|
|
var deletedBy string
|
2022-12-02 11:34:02 +00:00
|
|
|
if message.From != common.PubkeyToHex(&m.identity.PublicKey) {
|
|
|
|
if message.MessageType == protobuf.MessageType_COMMUNITY_CHAT {
|
|
|
|
communityID := chat.CommunityID
|
2023-03-07 14:38:09 +00:00
|
|
|
canDeleteMessageForEveryone = m.CanDeleteMessageForEveryoneInCommunity(communityID, &m.identity.PublicKey)
|
|
|
|
if !canDeleteMessageForEveryone {
|
|
|
|
return nil, ErrInvalidDeletePermission
|
|
|
|
}
|
|
|
|
} else if message.MessageType == protobuf.MessageType_PRIVATE_GROUP {
|
|
|
|
canDeleteMessageForEveryone = m.CanDeleteMessageForEveryoneInPrivateGroupChat(chat, &m.identity.PublicKey)
|
2022-12-02 11:34:02 +00:00
|
|
|
if !canDeleteMessageForEveryone {
|
|
|
|
return nil, ErrInvalidDeletePermission
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 00:57:35 +00:00
|
|
|
|
|
|
|
// only add DeletedBy when not deleted by message.From
|
|
|
|
deletedBy = contactIDFromPublicKey(m.IdentityPublicKey())
|
|
|
|
|
2022-12-02 11:34:02 +00:00
|
|
|
if !canDeleteMessageForEveryone {
|
|
|
|
return nil, ErrInvalidEditOrDeleteAuthor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:06:32 +00:00
|
|
|
// Only certain types of messages can be deleted
|
|
|
|
if message.ContentType != protobuf.ChatMessage_TEXT_PLAIN &&
|
|
|
|
message.ContentType != protobuf.ChatMessage_STICKER &&
|
|
|
|
message.ContentType != protobuf.ChatMessage_EMOJI &&
|
|
|
|
message.ContentType != protobuf.ChatMessage_IMAGE &&
|
|
|
|
message.ContentType != protobuf.ChatMessage_AUDIO {
|
|
|
|
return nil, ErrInvalidDeleteTypeAuthor
|
|
|
|
}
|
|
|
|
|
2023-08-31 07:21:25 +00:00
|
|
|
messagesToDelete, err := m.getOtherMessagesInAlbum(message, message.ChatId)
|
2023-04-14 17:17:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:06:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
deleteMessage := NewDeleteMessage()
|
2021-07-26 21:06:32 +00:00
|
|
|
deleteMessage.ChatId = message.ChatId
|
|
|
|
deleteMessage.MessageId = messageID
|
|
|
|
deleteMessage.Clock = clock
|
2023-02-01 00:57:35 +00:00
|
|
|
deleteMessage.DeletedBy = deletedBy
|
2021-07-26 21:06:32 +00:00
|
|
|
|
|
|
|
encodedMessage, err := m.encodeChatEntity(chat, deleteMessage)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage := common.RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_DELETE_MESSAGE,
|
|
|
|
SkipGroupMessageWrap: true,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
}
|
2023-02-01 00:57:35 +00:00
|
|
|
|
2021-07-26 21:06:32 +00:00
|
|
|
_, err = m.dispatchMessage(ctx, rawMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-14 17:17:56 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
for _, messageToDelete := range messagesToDelete {
|
|
|
|
messageToDelete.Deleted = true
|
|
|
|
messageToDelete.DeletedBy = deletedBy
|
|
|
|
err = m.persistence.SaveMessages([]*common.Message{messageToDelete})
|
|
|
|
if err != nil {
|
2021-07-26 21:06:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-14 17:17:56 +00:00
|
|
|
response.AddMessage(messageToDelete)
|
|
|
|
response.AddRemovedMessage(&RemovedMessage{MessageID: messageToDelete.ID, ChatID: chat.ID, DeletedBy: deletedBy})
|
|
|
|
|
|
|
|
if chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
|
2023-05-08 17:02:54 +00:00
|
|
|
chat.LastMessage = messageToDelete
|
|
|
|
|
|
|
|
err = m.saveChat(chat)
|
|
|
|
if err != nil {
|
2023-04-14 17:17:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-04-17 14:44:48 +00:00
|
|
|
// pull updated messages
|
|
|
|
updatedMessages, err := m.persistence.MessagesByResponseTo(messageToDelete.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-08 12:49:08 +00:00
|
|
|
|
2023-04-17 14:44:48 +00:00
|
|
|
response.AddMessages(updatedMessages)
|
2023-06-08 12:49:08 +00:00
|
|
|
m.prepareMessages(response.messages)
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response.AddChat(chat)
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2023-07-10 19:26:32 +00:00
|
|
|
func (m *Messenger) DeleteMessageForMeAndSync(ctx context.Context, localChatID string, messageID string) (*MessengerResponse, error) {
|
2022-09-28 11:42:17 +00:00
|
|
|
message, err := m.persistence.MessageByID(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
2023-07-10 19:26:32 +00:00
|
|
|
chat, ok := m.allChats.Load(localChatID)
|
2022-09-28 11:42:17 +00:00
|
|
|
if !ok {
|
2023-04-26 11:10:17 +00:00
|
|
|
return nil, ErrChatNotFound
|
2022-09-28 11:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only certain types of messages can be deleted
|
|
|
|
if message.ContentType != protobuf.ChatMessage_TEXT_PLAIN &&
|
|
|
|
message.ContentType != protobuf.ChatMessage_STICKER &&
|
|
|
|
message.ContentType != protobuf.ChatMessage_EMOJI &&
|
|
|
|
message.ContentType != protobuf.ChatMessage_IMAGE &&
|
|
|
|
message.ContentType != protobuf.ChatMessage_AUDIO {
|
|
|
|
return nil, ErrInvalidDeleteTypeAuthor
|
|
|
|
}
|
|
|
|
|
2023-08-31 07:21:25 +00:00
|
|
|
messagesToDelete, err := m.getOtherMessagesInAlbum(message, localChatID)
|
2022-09-28 11:42:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-14 17:17:56 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
for _, messageToDelete := range messagesToDelete {
|
|
|
|
messageToDelete.DeletedForMe = true
|
|
|
|
err = m.persistence.SaveMessages([]*common.Message{messageToDelete})
|
|
|
|
if err != nil {
|
2022-11-17 10:11:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-14 17:17:56 +00:00
|
|
|
if chat.LastMessage != nil && chat.LastMessage.ID == messageToDelete.ID {
|
2023-05-08 17:02:54 +00:00
|
|
|
chat.LastMessage = messageToDelete
|
|
|
|
err = m.saveChat(chat)
|
|
|
|
if err != nil {
|
2023-04-14 17:17:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AddMessage(messageToDelete)
|
2023-04-17 14:44:48 +00:00
|
|
|
|
|
|
|
// pull updated messages
|
|
|
|
updatedMessages, err := m.persistence.MessagesByResponseTo(messageToDelete.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-28 14:57:50 +00:00
|
|
|
|
2023-04-17 14:44:48 +00:00
|
|
|
response.AddMessages(updatedMessages)
|
|
|
|
|
2023-06-28 14:57:50 +00:00
|
|
|
m.prepareMessages(response.messages)
|
|
|
|
|
2023-04-14 17:17:56 +00:00
|
|
|
}
|
2022-09-28 11:42:17 +00:00
|
|
|
response.AddChat(chat)
|
|
|
|
|
2023-05-12 08:31:34 +00:00
|
|
|
err = m.withChatClock(func(chatID string, clock uint64) error {
|
2023-08-18 11:39:59 +00:00
|
|
|
deletedForMeMessage := &protobuf.SyncDeleteForMeMessage{
|
2023-05-12 08:31:34 +00:00
|
|
|
MessageId: messageID,
|
|
|
|
Clock: clock,
|
|
|
|
}
|
2022-09-28 11:42:17 +00:00
|
|
|
|
2023-05-12 08:31:34 +00:00
|
|
|
if m.hasPairedDevices() {
|
2023-05-09 12:54:56 +00:00
|
|
|
encodedMessage, err2 := proto.Marshal(deletedForMeMessage)
|
2022-09-28 11:42:17 +00:00
|
|
|
|
2023-05-09 12:54:56 +00:00
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
2022-09-28 11:42:17 +00:00
|
|
|
|
2023-05-09 12:54:56 +00:00
|
|
|
rawMessage := common.RawMessage{
|
|
|
|
LocalChatID: chatID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_DELETE_FOR_ME_MESSAGE,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
}
|
|
|
|
_, err2 = m.dispatchMessage(ctx, rawMessage)
|
|
|
|
return err2
|
2022-09-28 11:42:17 +00:00
|
|
|
}
|
2023-05-12 08:31:34 +00:00
|
|
|
|
|
|
|
return m.persistence.SaveOrUpdateDeleteForMeMessage(deletedForMeMessage)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
2022-09-28 11:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:45:06 +00:00
|
|
|
func (m *Messenger) applyEditMessage(editMessage *protobuf.EditMessage, message *common.Message) error {
|
2021-06-25 10:15:37 +00:00
|
|
|
if err := ValidateText(editMessage.Text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-07 11:45:06 +00:00
|
|
|
message.Text = editMessage.Text
|
|
|
|
message.EditedAt = editMessage.Clock
|
2023-08-31 07:21:25 +00:00
|
|
|
message.UnfurledLinks = editMessage.UnfurledLinks
|
2023-10-13 12:25:34 +00:00
|
|
|
message.UnfurledStatusLinks = editMessage.UnfurledStatusLinks
|
2022-11-16 11:43:40 +00:00
|
|
|
if editMessage.ContentType != protobuf.ChatMessage_UNKNOWN_CONTENT_TYPE {
|
|
|
|
message.ContentType = editMessage.ContentType
|
|
|
|
}
|
2021-06-07 11:45:06 +00:00
|
|
|
|
2021-06-23 10:07:26 +00:00
|
|
|
// Save original message as edit so we can retrieve history
|
|
|
|
if message.EditedAt == 0 {
|
2023-08-18 11:39:59 +00:00
|
|
|
originalEdit := NewEditMessage()
|
2021-06-23 10:07:26 +00:00
|
|
|
originalEdit.Clock = message.Clock
|
|
|
|
originalEdit.LocalChatID = message.LocalChatID
|
|
|
|
originalEdit.MessageId = message.ID
|
|
|
|
originalEdit.Text = message.Text
|
2022-10-05 17:54:47 +00:00
|
|
|
originalEdit.ContentType = message.ContentType
|
2021-06-23 10:07:26 +00:00
|
|
|
originalEdit.From = message.From
|
2023-08-31 07:21:25 +00:00
|
|
|
originalEdit.UnfurledLinks = message.UnfurledLinks
|
2023-10-13 12:25:34 +00:00
|
|
|
originalEdit.UnfurledStatusLinks = message.UnfurledStatusLinks
|
2021-06-23 10:07:26 +00:00
|
|
|
err := m.persistence.SaveEdit(originalEdit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:45:06 +00:00
|
|
|
err := message.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.persistence.SaveMessages([]*common.Message{message})
|
|
|
|
}
|
2021-07-26 21:06:32 +00:00
|
|
|
|
|
|
|
func (m *Messenger) applyDeleteMessage(messageDeletes []*DeleteMessage, message *common.Message) error {
|
|
|
|
if messageDeletes[0].From != message.From {
|
|
|
|
return ErrInvalidEditOrDeleteAuthor
|
|
|
|
}
|
|
|
|
|
|
|
|
message.Deleted = true
|
2023-02-01 00:57:35 +00:00
|
|
|
message.DeletedBy = messageDeletes[0].DeletedBy
|
2021-07-26 21:06:32 +00:00
|
|
|
|
|
|
|
err := message.PrepareContent(common.PubkeyToHex(&m.identity.PublicKey))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.persistence.SaveMessages([]*common.Message{message})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-17 10:11:58 +00:00
|
|
|
return nil
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
2022-09-28 11:42:17 +00:00
|
|
|
|
2023-05-12 08:31:34 +00:00
|
|
|
func (m *Messenger) applyDeleteForMeMessage(message *common.Message) error {
|
2022-09-28 11:42:17 +00:00
|
|
|
message.DeletedForMe = true
|
|
|
|
|
2023-05-12 08:31:34 +00:00
|
|
|
err := message.PrepareContent(m.myHexIdentity())
|
2022-09-28 11:42:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.persistence.SaveMessages([]*common.Message{message})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-02 18:12:20 +00:00
|
|
|
|
|
|
|
func (m *Messenger) addContactRequestPropagatedState(message *common.Message) error {
|
|
|
|
chat, ok := m.allChats.Load(message.LocalChatID)
|
|
|
|
if !ok {
|
|
|
|
return ErrChatNotFound
|
|
|
|
}
|
|
|
|
if !chat.OneToOne() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:34:35 +00:00
|
|
|
contact, err := m.BuildContact(&requests.BuildContact{PublicKey: chat.ID})
|
2023-02-02 18:12:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.ContactRequestPropagatedState = contact.ContactRequestPropagatedState()
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-14 08:50:36 +00:00
|
|
|
|
|
|
|
func (m *Messenger) SendOneToOneMessage(request *requests.SendOneToOneMessage) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID, err := request.HexID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := m.allChats.Load(chatID)
|
|
|
|
if !ok {
|
|
|
|
// Only one to one chan be muted when it's not in the database
|
|
|
|
publicKey, err := common.HexToPubkey(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a one to one chat
|
|
|
|
chat := CreateOneToOneChat(chatID, publicKey, m.getTimesource())
|
|
|
|
err = m.initChatSyncFields(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = m.saveChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
message := common.NewMessage()
|
2022-10-14 08:50:36 +00:00
|
|
|
message.Text = request.Message
|
|
|
|
message.ChatId = chatID
|
|
|
|
message.ContentType = protobuf.ChatMessage_TEXT_PLAIN
|
|
|
|
|
|
|
|
return m.sendChatMessage(context.Background(), message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) SendGroupChatMessage(request *requests.SendGroupChatMessage) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID := request.ID
|
|
|
|
|
|
|
|
_, ok := m.allChats.Load(chatID)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
message := common.NewMessage()
|
2022-10-14 08:50:36 +00:00
|
|
|
message.Text = request.Message
|
|
|
|
message.ChatId = chatID
|
|
|
|
message.ContentType = protobuf.ChatMessage_TEXT_PLAIN
|
|
|
|
|
|
|
|
return m.sendChatMessage(context.Background(), message)
|
|
|
|
}
|