2021-01-11 10:32:51 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-05-08 17:02:54 +00:00
|
|
|
"strings"
|
2021-03-05 14:38:32 +00:00
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
"github.com/status-im/status-go/deprecation"
|
2021-01-11 10:32:51 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2021-09-07 14:05:36 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2021-01-11 10:32:51 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
|
|
|
)
|
|
|
|
|
2024-11-28 10:55:02 +00:00
|
|
|
type ChatPreviewFilterType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ChatPreviewFilterTypeCommunity ChatPreviewFilterType = iota
|
|
|
|
ChatPreviewFilterTypeNonCommunity
|
|
|
|
)
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (m *Messenger) getOneToOneAndNextClock(contact *Contact) (*Chat, uint64, error) {
|
|
|
|
chat, ok := m.allChats.Load(contact.ID)
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
|
|
|
|
|
|
|
// We don't want to show the chat to the user by default
|
|
|
|
chat.Active = false
|
|
|
|
|
|
|
|
if err := m.saveChat(chat); err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
|
|
|
}
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
|
|
|
|
return chat, clock, nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (m *Messenger) Chats() []*Chat {
|
|
|
|
var chats []*Chat
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allChats.Range(func(chatID string, chat *Chat) (shouldContinue bool) {
|
|
|
|
chats = append(chats, chat)
|
|
|
|
return true
|
|
|
|
})
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
return chats
|
|
|
|
}
|
|
|
|
|
2024-11-28 10:55:02 +00:00
|
|
|
func (m *Messenger) ChatsPreview(filter ChatPreviewFilterType) []*ChatPreview {
|
2021-09-07 14:05:36 +00:00
|
|
|
var chats []*ChatPreview
|
|
|
|
m.allChats.Range(func(chatID string, chat *Chat) (shouldContinue bool) {
|
2024-11-28 10:55:02 +00:00
|
|
|
// Skip if chat doesn't match the filter
|
|
|
|
isCommunityChat := chat.ChatType == ChatTypeCommunityChat
|
|
|
|
if filter == ChatPreviewFilterTypeCommunity && !isCommunityChat {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if filter == ChatPreviewFilterTypeNonCommunity && isCommunityChat {
|
|
|
|
return true
|
|
|
|
}
|
2022-04-01 08:50:38 +00:00
|
|
|
if chat.Active || chat.Muted {
|
2021-09-07 14:05:36 +00:00
|
|
|
chatPreview := &ChatPreview{
|
|
|
|
ID: chat.ID,
|
|
|
|
Name: chat.Name,
|
|
|
|
Description: chat.Description,
|
|
|
|
Color: chat.Color,
|
2021-10-04 13:02:25 +00:00
|
|
|
Emoji: chat.Emoji,
|
2021-09-07 14:05:36 +00:00
|
|
|
Active: chat.Active,
|
|
|
|
ChatType: chat.ChatType,
|
|
|
|
Timestamp: chat.Timestamp,
|
|
|
|
LastClockValue: chat.LastClockValue,
|
|
|
|
DeletedAtClockValue: chat.DeletedAtClockValue,
|
|
|
|
UnviewedMessagesCount: chat.UnviewedMessagesCount,
|
|
|
|
UnviewedMentionsCount: chat.UnviewedMentionsCount,
|
|
|
|
Alias: chat.Alias,
|
|
|
|
Identicon: chat.Identicon,
|
|
|
|
Muted: chat.Muted,
|
2023-06-22 05:06:32 +00:00
|
|
|
MuteTill: chat.MuteTill,
|
2021-09-07 14:05:36 +00:00
|
|
|
Profile: chat.Profile,
|
|
|
|
CommunityID: chat.CommunityID,
|
|
|
|
CategoryID: chat.CategoryID,
|
|
|
|
Joined: chat.Joined,
|
|
|
|
SyncedTo: chat.SyncedTo,
|
|
|
|
SyncedFrom: chat.SyncedFrom,
|
2021-10-21 17:04:56 +00:00
|
|
|
Highlight: chat.Highlight,
|
2022-11-03 08:57:51 +00:00
|
|
|
Members: chat.Members,
|
2024-04-09 15:39:14 +00:00
|
|
|
Base64Image: chat.Base64Image,
|
2021-09-07 14:05:36 +00:00
|
|
|
}
|
2023-05-08 17:02:54 +00:00
|
|
|
|
2021-09-07 14:05:36 +00:00
|
|
|
if chat.LastMessage != nil {
|
2023-05-08 17:02:54 +00:00
|
|
|
|
|
|
|
chatPreview.OutgoingStatus = chat.LastMessage.OutgoingStatus
|
|
|
|
chatPreview.ResponseTo = chat.LastMessage.ResponseTo
|
2021-09-07 14:05:36 +00:00
|
|
|
chatPreview.ContentType = chat.LastMessage.ContentType
|
2023-05-08 17:02:54 +00:00
|
|
|
chatPreview.From = chat.LastMessage.From
|
|
|
|
chatPreview.Deleted = chat.LastMessage.Deleted
|
|
|
|
chatPreview.DeletedForMe = chat.LastMessage.DeletedForMe
|
|
|
|
|
|
|
|
if chat.LastMessage.ContentType == protobuf.ChatMessage_IMAGE {
|
|
|
|
chatPreview.ParsedText = chat.LastMessage.ParsedText
|
|
|
|
|
|
|
|
image := chat.LastMessage.GetImage()
|
|
|
|
if image != nil {
|
|
|
|
chatPreview.AlbumImagesCount = image.AlbumImagesCount
|
|
|
|
chatPreview.ParsedText = chat.LastMessage.ParsedText
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:05:36 +00:00
|
|
|
if chat.LastMessage.ContentType == protobuf.ChatMessage_TEXT_PLAIN {
|
2021-10-01 09:59:42 +00:00
|
|
|
|
|
|
|
simplifiedText, err := chat.LastMessage.GetSimplifiedText("", nil)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if len(simplifiedText) > 100 {
|
|
|
|
chatPreview.Text = simplifiedText[:100]
|
|
|
|
} else {
|
|
|
|
chatPreview.Text = simplifiedText
|
|
|
|
}
|
|
|
|
if strings.Contains(chatPreview.Text, "0x") {
|
|
|
|
//if there is a mention, we would like to send parsed text as well
|
|
|
|
chatPreview.ParsedText = chat.LastMessage.ParsedText
|
|
|
|
}
|
2021-09-07 14:05:36 +00:00
|
|
|
}
|
2021-10-20 08:39:25 +00:00
|
|
|
} else if chat.LastMessage.ContentType == protobuf.ChatMessage_EMOJI ||
|
|
|
|
chat.LastMessage.ContentType == protobuf.ChatMessage_TRANSACTION_COMMAND {
|
|
|
|
|
|
|
|
chatPreview.Text = chat.LastMessage.Text
|
|
|
|
chatPreview.ParsedText = chat.LastMessage.ParsedText
|
2021-09-07 14:05:36 +00:00
|
|
|
}
|
2021-10-21 17:04:56 +00:00
|
|
|
if chat.LastMessage.ContentType == protobuf.ChatMessage_COMMUNITY {
|
|
|
|
chatPreview.ContentCommunityID = chat.LastMessage.CommunityID
|
|
|
|
}
|
2021-09-07 14:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chats = append(chats, chatPreview)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return chats
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) Chat(chatID string) *Chat {
|
|
|
|
chat, _ := m.allChats.Load(chatID)
|
|
|
|
|
|
|
|
return chat
|
2021-08-16 11:04:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
func (m *Messenger) ActiveChats() []*Chat {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var chats []*Chat
|
|
|
|
|
|
|
|
m.allChats.Range(func(chatID string, c *Chat) bool {
|
|
|
|
if c.Active {
|
|
|
|
chats = append(chats, c)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return chats
|
|
|
|
}
|
|
|
|
|
2021-06-01 09:29:37 +00:00
|
|
|
func (m *Messenger) initChatSyncFields(chat *Chat) error {
|
|
|
|
defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod
|
|
|
|
chat.SyncedTo = timestamp
|
|
|
|
chat.SyncedFrom = timestamp
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:56:38 +00:00
|
|
|
func (m *Messenger) createPublicChat(chatID string, response *MessengerResponse) (*MessengerResponse, error) {
|
2021-03-25 15:15:22 +00:00
|
|
|
chat, ok := m.allChats.Load(chatID)
|
|
|
|
if !ok {
|
|
|
|
chat = CreatePublicChat(chatID, m.getTimesource())
|
2021-05-14 10:55:42 +00:00
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
}
|
|
|
|
chat.Active = true
|
2021-05-28 11:05:01 +00:00
|
|
|
chat.DeletedAtClockValue = 0
|
2021-03-25 15:15:22 +00:00
|
|
|
|
|
|
|
// Save topics
|
|
|
|
_, err := m.Join(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
// Store chat
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
|
|
|
|
|
|
|
willSync, err := m.scheduleSyncChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We set the synced to, synced from to the default time
|
|
|
|
if !willSync {
|
2021-06-01 09:29:37 +00:00
|
|
|
if err := m.initChatSyncFields(chat); err != nil {
|
2021-05-28 11:05:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-14 10:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
err = m.saveChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-01 09:03:45 +00:00
|
|
|
err = m.reregisterForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
response.AddChat(chat)
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:56:38 +00:00
|
|
|
func (m *Messenger) CreatePublicChat(request *requests.CreatePublicChat) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID := request.ID
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
return m.createPublicChat(chatID, response)
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
// Deprecated: CreateProfileChat shouldn't be used
|
|
|
|
// and is only left here in case profile chat feature is re-introduced.
|
2021-03-25 15:15:22 +00:00
|
|
|
func (m *Messenger) CreateProfileChat(request *requests.CreateProfileChat) (*MessengerResponse, error) {
|
2023-08-03 14:16:11 +00:00
|
|
|
// Return error to prevent usage of deprecated function
|
|
|
|
if deprecation.ChatProfileDeprecated {
|
|
|
|
return nil, errors.New("profile chats are deprecated")
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := common.HexToPubkey(request.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat := m.buildProfileChat(request.ID)
|
|
|
|
|
|
|
|
chat.Active = true
|
|
|
|
|
|
|
|
// Save topics
|
|
|
|
_, err = m.Join(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check contact code
|
|
|
|
filter, err := m.transport.JoinPrivate(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
// Store chat
|
|
|
|
m.allChats.Store(chat.ID, chat)
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
response.AddChat(chat)
|
|
|
|
|
|
|
|
willSync, err := m.scheduleSyncChat(chat)
|
2021-03-25 15:15:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
// We set the synced to, synced from to the default time
|
|
|
|
if !willSync {
|
2021-06-01 09:29:37 +00:00
|
|
|
if err := m.initChatSyncFields(chat); err != nil {
|
2021-05-28 11:05:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-14 10:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.scheduleSyncFilters([]*transport.Filter{filter})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-25 15:15:22 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
err = m.saveChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-25 15:15:22 +00:00
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (m *Messenger) CreateOneToOneChat(request *requests.CreateOneToOneChat) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID := request.ID.String()
|
|
|
|
pk, err := common.HexToPubkey(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-05 15:11:10 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
ensName := request.ENSName
|
|
|
|
if ensName != "" {
|
|
|
|
clock := m.getTimesource().GetCurrentTime()
|
|
|
|
err := m.ensVerifier.ENSVerified(chatID, ensName, clock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-14 15:34:35 +00:00
|
|
|
contact, err := m.BuildContact(&requests.BuildContact{PublicKey: chatID})
|
2023-01-20 14:28:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-05 15:11:10 +00:00
|
|
|
}
|
2023-01-20 14:28:30 +00:00
|
|
|
|
2022-02-17 15:13:10 +00:00
|
|
|
contact.EnsName = ensName
|
2021-11-05 15:11:10 +00:00
|
|
|
contact.ENSVerified = true
|
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.Contacts = []*Contact{contact}
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := m.allChats.Load(chatID)
|
2021-01-11 10:32:51 +00:00
|
|
|
if !ok {
|
|
|
|
chat = CreateOneToOneChat(chatID, pk, m.getTimesource())
|
|
|
|
}
|
|
|
|
chat.Active = true
|
|
|
|
|
|
|
|
filters, err := m.Join(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(Samyoul) remove storing of an updated reference pointer?
|
|
|
|
m.allChats.Store(chatID, chat)
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
response.AddChat(chat)
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
willSync, err := m.scheduleSyncFilters(filters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We set the synced to, synced from to the default time
|
|
|
|
if !willSync {
|
2021-06-01 09:29:37 +00:00
|
|
|
if err := m.initChatSyncFields(chat); err != nil {
|
2021-05-28 11:05:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-14 10:55:42 +00:00
|
|
|
}
|
2021-03-25 15:15:22 +00:00
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
err = m.saveChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DeleteChat(chatID string) error {
|
|
|
|
return m.deleteChat(chatID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) deleteChat(chatID string) error {
|
|
|
|
err := m.persistence.DeleteChat(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-30 13:43:34 +00:00
|
|
|
|
|
|
|
// We clean the cache to be able to receive the messages again later
|
|
|
|
err = m.transport.ClearProcessedMessageIDsCache()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := m.allChats.Load(chatID)
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
if ok && chat.Active && chat.Public() {
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allChats.Delete(chatID)
|
2021-01-11 10:32:51 +00:00
|
|
|
return m.reregisterForPushNotifications()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) SaveChat(chat *Chat) error {
|
|
|
|
return m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
func (m *Messenger) DeactivateChat(request *requests.DeactivateChat) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-11 12:29:56 +00:00
|
|
|
doClearHistory := !request.PreserveHistory
|
|
|
|
|
|
|
|
return m.deactivateChat(request.ID, 0, true, doClearHistory)
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 19:34:48 +00:00
|
|
|
func (m *Messenger) deactivateChat(chatID string, deactivationClock uint64, shouldBeSynced bool, doClearHistory bool) (*MessengerResponse, error) {
|
2021-01-11 10:32:51 +00:00
|
|
|
var response MessengerResponse
|
2021-03-29 15:41:30 +00:00
|
|
|
chat, ok := m.allChats.Load(chatID)
|
2021-01-11 10:32:51 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
2021-12-09 21:55:00 +00:00
|
|
|
// Reset mailserver last request to allow re-fetching messages if joining a chat again
|
|
|
|
filters, err := m.filtersForChat(chatID)
|
|
|
|
if err != nil && err != ErrNoFiltersForChat {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.mailserversDatabase != nil {
|
|
|
|
for _, filter := range filters {
|
|
|
|
if !filter.Listen || filter.Ephemeral {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
err := m.mailserversDatabase.ResetLastRequest(filter.PubsubTopic, filter.ContentTopic.String())
|
2021-12-09 21:55:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 13:57:54 +00:00
|
|
|
if deactivationClock == 0 {
|
|
|
|
deactivationClock, _ = chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
2022-12-07 19:34:48 +00:00
|
|
|
err = m.persistence.DeactivateChat(chat, deactivationClock, doClearHistory)
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We re-register as our options have changed and we don't want to
|
|
|
|
// receive PN from mentions in this chat anymore
|
2021-09-15 08:00:19 +00:00
|
|
|
if chat.Public() || chat.ProfileUpdates() {
|
2021-01-11 10:32:51 +00:00
|
|
|
err := m.reregisterForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-25 15:15:22 +00:00
|
|
|
|
|
|
|
err = m.transport.ClearProcessedMessageIDsCache()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:41:30 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
m.allChats.Store(chatID, chat)
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
response.AddChat(chat)
|
|
|
|
// TODO: Remove filters
|
|
|
|
|
2021-10-05 17:26:02 +00:00
|
|
|
if shouldBeSynced {
|
2023-01-06 12:21:14 +00:00
|
|
|
err := m.syncChatRemoving(context.Background(), chat.ID, m.dispatchMessage)
|
2021-10-05 17:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) saveChats(chats []*Chat) error {
|
|
|
|
err := m.persistence.SaveChats(chats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, chat := range chats {
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allChats.Store(chat.ID, chat)
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) saveChat(chat *Chat) error {
|
2021-09-01 09:03:45 +00:00
|
|
|
_, ok := m.allChats.Load(chat.ID)
|
2021-01-11 10:32:51 +00:00
|
|
|
if chat.OneToOne() {
|
|
|
|
name, identicon, err := generateAliasAndIdenticon(chat.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.Alias = name
|
|
|
|
chat.Identicon = identicon
|
|
|
|
}
|
2023-05-16 16:11:52 +00:00
|
|
|
|
2023-10-17 13:24:15 +00:00
|
|
|
// Sync chat if it's a new public, 1-1 or group chat, but not a timeline chat
|
|
|
|
if !ok && chat.shouldBeSynced() {
|
|
|
|
if err := m.syncChat(context.Background(), chat, m.dispatchMessage); err != nil {
|
2021-01-11 10:32:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.persistence.SaveChat(*chat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-03 11:31:11 +00:00
|
|
|
// We store the chat has it might not have been in the store in the first place
|
2021-03-29 15:41:30 +00:00
|
|
|
m.allChats.Store(chat.ID, chat)
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) Join(chat *Chat) ([]*transport.Filter, error) {
|
|
|
|
switch chat.ChatType {
|
|
|
|
case ChatTypeOneToOne:
|
|
|
|
pk, err := chat.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := m.transport.JoinPrivate(pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*transport.Filter{f}, nil
|
|
|
|
case ChatTypePrivateGroupChat:
|
|
|
|
members, err := chat.MembersAsPublicKeys()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return m.transport.JoinGroup(members)
|
|
|
|
case ChatTypePublic, ChatTypeProfile, ChatTypeTimeline:
|
|
|
|
f, err := m.transport.JoinPublic(chat.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*transport.Filter{f}, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("chat is neither public nor private")
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 15:15:22 +00:00
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
// Deprecated: buildProfileChat shouldn't be used
|
|
|
|
// and is only left here in case profile chat feature is re-introduced.
|
2021-03-25 15:15:22 +00:00
|
|
|
func (m *Messenger) buildProfileChat(id string) *Chat {
|
2023-08-03 14:16:11 +00:00
|
|
|
// Return nil to prevent usage of deprecated function
|
|
|
|
if deprecation.ChatProfileDeprecated {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
// Create the corresponding profile chat
|
|
|
|
profileChatID := buildProfileChatID(id)
|
|
|
|
profileChat, ok := m.allChats.Load(profileChatID)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
profileChat = CreateProfileChat(id, m.getTimesource())
|
|
|
|
}
|
|
|
|
|
|
|
|
return profileChat
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
// Deprecated: ensureTimelineChat shouldn't be used
|
|
|
|
// and is only left here in case profile chat feature is re-introduced.
|
2021-03-25 15:15:22 +00:00
|
|
|
func (m *Messenger) ensureTimelineChat() error {
|
2023-08-03 14:16:11 +00:00
|
|
|
// Return error to prevent usage of deprecated function
|
|
|
|
if deprecation.ChatProfileDeprecated {
|
|
|
|
return errors.New("timeline chats are deprecated")
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
chat, err := m.persistence.Chat(timelineChatID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if chat != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
chat = CreateTimelineChat(m.getTimesource())
|
|
|
|
m.allChats.Store(timelineChatID, chat)
|
|
|
|
return m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:16:11 +00:00
|
|
|
// Deprecated: ensureMyOwnProfileChat shouldn't be used
|
|
|
|
// and is only left here in case profile chat feature is re-introduced.
|
2021-03-25 15:15:22 +00:00
|
|
|
func (m *Messenger) ensureMyOwnProfileChat() error {
|
2023-08-03 14:16:11 +00:00
|
|
|
// Return error to prevent usage of deprecated function
|
|
|
|
if deprecation.ChatProfileDeprecated {
|
|
|
|
return errors.New("profile chats are deprecated")
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
chatID := common.PubkeyToHex(&m.identity.PublicKey)
|
2021-05-14 10:55:42 +00:00
|
|
|
_, ok := m.allChats.Load(chatID)
|
2021-03-25 15:15:22 +00:00
|
|
|
if ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
chat := m.buildProfileChat(chatID)
|
2021-03-25 15:15:22 +00:00
|
|
|
|
|
|
|
chat.Active = true
|
|
|
|
|
|
|
|
// Save topics
|
|
|
|
_, err := m.Join(chat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ClearHistory(request *requests.ClearHistory) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.clearHistory(request.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) clearHistory(id string) (*MessengerResponse, error) {
|
|
|
|
chat, ok := m.allChats.Load(id)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.transport)
|
|
|
|
|
|
|
|
err := m.persistence.ClearHistory(chat, clock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if chat.Public() {
|
|
|
|
err = m.transport.ClearProcessedMessageIDsCache()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
err = m.syncClearHistory(context.Background(), chat, m.dispatchMessage)
|
2022-02-10 10:00:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
m.allChats.Store(id, chat)
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
response.AddChat(chat)
|
|
|
|
return response, nil
|
|
|
|
}
|
2023-11-24 14:51:09 +00:00
|
|
|
|
2023-11-27 12:38:45 +00:00
|
|
|
func (m *Messenger) FetchMessages(request *requests.FetchMessages) error {
|
2023-11-24 14:51:09 +00:00
|
|
|
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := request.ID
|
|
|
|
|
|
|
|
chat, ok := m.allChats.Load(id)
|
|
|
|
if !ok {
|
|
|
|
return ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
2024-02-12 12:20:56 +00:00
|
|
|
_, err := m.fetchMessages(chat.ID, oneMonthDuration)
|
2023-11-24 14:51:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|