2020-12-01 09:43:46 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2021-01-11 10:32:51 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2022-01-17 03:42:11 +00:00
|
|
|
"github.com/status-im/status-go/services/browsers"
|
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
"github.com/status-im/status-go/appmetrics"
|
2020-12-01 09:43:46 +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-12-01 09:43:46 +00:00
|
|
|
"github.com/status-im/status-go/protocol/encryption/multidevice"
|
2021-03-31 16:23:45 +00:00
|
|
|
localnotifications "github.com/status-im/status-go/services/local-notifications"
|
2021-01-14 22:15:13 +00:00
|
|
|
"github.com/status-im/status-go/services/mailservers"
|
2020-12-01 09:43:46 +00:00
|
|
|
)
|
|
|
|
|
2021-08-20 14:26:13 +00:00
|
|
|
type RemovedMessage struct {
|
|
|
|
ChatID string `json:"chatId"`
|
|
|
|
MessageID string `json:"messageId"`
|
|
|
|
}
|
|
|
|
|
2022-02-10 10:00:59 +00:00
|
|
|
type ClearedHistory struct {
|
|
|
|
ChatID string `json:"chatId"`
|
|
|
|
ClearedAt uint64 `json:"clearedAt"`
|
|
|
|
}
|
|
|
|
|
2020-12-01 09:43:46 +00:00
|
|
|
type MessengerResponse struct {
|
2021-01-11 10:32:51 +00:00
|
|
|
Contacts []*Contact
|
|
|
|
Installations []*multidevice.Installation
|
|
|
|
EmojiReactions []*EmojiReaction
|
|
|
|
Invitations []*GroupChatInvitation
|
|
|
|
CommunityChanges []*communities.CommunityChanges
|
|
|
|
RequestsToJoinCommunity []*communities.RequestToJoin
|
2021-09-01 12:02:18 +00:00
|
|
|
AnonymousMetrics []*appmetrics.AppMetric
|
2021-01-11 10:32:51 +00:00
|
|
|
Mailservers []mailservers.Mailserver
|
2022-01-17 03:42:11 +00:00
|
|
|
Bookmarks []*browsers.Bookmark
|
2021-01-11 10:32:51 +00:00
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
// notifications a list of notifications derived from messenger events
|
|
|
|
// that are useful to notify the user about
|
2021-04-07 12:57:14 +00:00
|
|
|
notifications map[string]*localnotifications.Notification
|
|
|
|
chats map[string]*Chat
|
|
|
|
removedChats map[string]bool
|
2021-08-20 14:26:13 +00:00
|
|
|
removedMessages map[string]*RemovedMessage
|
2021-04-07 12:57:14 +00:00
|
|
|
communities map[string]*communities.Community
|
|
|
|
activityCenterNotifications map[string]*ActivityCenterNotification
|
2021-06-03 13:11:55 +00:00
|
|
|
messages map[string]*common.Message
|
2021-05-14 21:22:50 +00:00
|
|
|
pinMessages map[string]*common.PinMessage
|
2021-07-22 17:41:49 +00:00
|
|
|
currentStatus *UserStatus
|
|
|
|
statusUpdates map[string]UserStatus
|
2022-02-10 10:00:59 +00:00
|
|
|
clearedHistories map[string]*ClearedHistory
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) MarshalJSON() ([]byte, error) {
|
|
|
|
responseItem := struct {
|
|
|
|
Chats []*Chat `json:"chats,omitempty"`
|
|
|
|
RemovedChats []string `json:"removedChats,omitempty"`
|
2021-08-20 14:26:13 +00:00
|
|
|
RemovedMessages []*RemovedMessage `json:"removedMessages,omitempty"`
|
2021-01-11 10:32:51 +00:00
|
|
|
Messages []*common.Message `json:"messages,omitempty"`
|
|
|
|
Contacts []*Contact `json:"contacts,omitempty"`
|
|
|
|
Installations []*multidevice.Installation `json:"installations,omitempty"`
|
2021-03-25 15:15:22 +00:00
|
|
|
PinMessages []*common.PinMessage `json:"pinMessages,omitempty"`
|
2021-01-11 10:32:51 +00:00
|
|
|
EmojiReactions []*EmojiReaction `json:"emojiReactions,omitempty"`
|
|
|
|
Invitations []*GroupChatInvitation `json:"invitations,omitempty"`
|
|
|
|
CommunityChanges []*communities.CommunityChanges `json:"communityChanges,omitempty"`
|
|
|
|
RequestsToJoinCommunity []*communities.RequestToJoin `json:"requestsToJoinCommunity,omitempty"`
|
|
|
|
Mailservers []mailservers.Mailserver `json:"mailservers,omitempty"`
|
2022-01-17 03:42:11 +00:00
|
|
|
Bookmarks []*browsers.Bookmark `json:"bookmarks,omitempty"`
|
2022-02-10 10:00:59 +00:00
|
|
|
ClearedHistories []*ClearedHistory `json:"clearedHistories,omitempty"`
|
2021-03-31 16:23:45 +00:00
|
|
|
// Notifications a list of notifications derived from messenger events
|
|
|
|
// that are useful to notify the user about
|
2021-04-07 12:57:14 +00:00
|
|
|
Notifications []*localnotifications.Notification `json:"notifications"`
|
|
|
|
Communities []*communities.Community `json:"communities,omitempty"`
|
|
|
|
ActivityCenterNotifications []*ActivityCenterNotification `json:"activityCenterNotifications,omitempty"`
|
2021-07-22 17:41:49 +00:00
|
|
|
CurrentStatus *UserStatus `json:"currentStatus,omitempty"`
|
|
|
|
StatusUpdates []UserStatus `json:"statusUpdates,omitempty"`
|
2021-01-11 10:32:51 +00:00
|
|
|
}{
|
|
|
|
Contacts: r.Contacts,
|
|
|
|
Installations: r.Installations,
|
|
|
|
EmojiReactions: r.EmojiReactions,
|
|
|
|
Invitations: r.Invitations,
|
|
|
|
CommunityChanges: r.CommunityChanges,
|
|
|
|
RequestsToJoinCommunity: r.RequestsToJoinCommunity,
|
|
|
|
Mailservers: r.Mailservers,
|
2022-01-17 03:42:11 +00:00
|
|
|
Bookmarks: r.Bookmarks,
|
2021-07-22 17:41:49 +00:00
|
|
|
CurrentStatus: r.currentStatus,
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 13:11:55 +00:00
|
|
|
responseItem.Messages = r.Messages()
|
2021-03-31 16:23:45 +00:00
|
|
|
responseItem.Notifications = r.Notifications()
|
2021-01-11 10:32:51 +00:00
|
|
|
responseItem.Chats = r.Chats()
|
|
|
|
responseItem.Communities = r.Communities()
|
|
|
|
responseItem.RemovedChats = r.RemovedChats()
|
2021-07-26 21:06:32 +00:00
|
|
|
responseItem.RemovedMessages = r.RemovedMessages()
|
2022-02-10 10:00:59 +00:00
|
|
|
responseItem.ClearedHistories = r.ClearedHistories()
|
2021-04-07 12:57:14 +00:00
|
|
|
responseItem.ActivityCenterNotifications = r.ActivityCenterNotifications()
|
2021-05-14 21:22:50 +00:00
|
|
|
responseItem.PinMessages = r.PinMessages()
|
2021-07-22 17:41:49 +00:00
|
|
|
responseItem.StatusUpdates = r.StatusUpdates()
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
return json.Marshal(responseItem)
|
2020-12-01 09:43:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (r *MessengerResponse) Chats() []*Chat {
|
|
|
|
var chats []*Chat
|
|
|
|
for _, chat := range r.chats {
|
|
|
|
chats = append(chats, chat)
|
|
|
|
}
|
|
|
|
return chats
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) RemovedChats() []string {
|
|
|
|
var chats []string
|
|
|
|
for chatID := range r.removedChats {
|
|
|
|
chats = append(chats, chatID)
|
|
|
|
}
|
|
|
|
return chats
|
|
|
|
}
|
|
|
|
|
2021-08-20 14:26:13 +00:00
|
|
|
func (r *MessengerResponse) RemovedMessages() []*RemovedMessage {
|
|
|
|
var messages []*RemovedMessage
|
2021-07-26 21:06:32 +00:00
|
|
|
for messageID := range r.removedMessages {
|
2021-08-20 14:26:13 +00:00
|
|
|
messages = append(messages, r.removedMessages[messageID])
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
|
|
|
return messages
|
|
|
|
}
|
|
|
|
|
2022-02-10 10:00:59 +00:00
|
|
|
func (r *MessengerResponse) ClearedHistories() []*ClearedHistory {
|
|
|
|
var clearedHistories []*ClearedHistory
|
|
|
|
for chatID := range r.clearedHistories {
|
|
|
|
clearedHistories = append(clearedHistories, r.clearedHistories[chatID])
|
|
|
|
}
|
|
|
|
return clearedHistories
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (r *MessengerResponse) Communities() []*communities.Community {
|
|
|
|
var communities []*communities.Community
|
|
|
|
for _, c := range r.communities {
|
|
|
|
communities = append(communities, c)
|
|
|
|
}
|
|
|
|
return communities
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
func (r *MessengerResponse) Notifications() []*localnotifications.Notification {
|
|
|
|
var notifications []*localnotifications.Notification
|
|
|
|
for _, n := range r.notifications {
|
|
|
|
notifications = append(notifications, n)
|
|
|
|
}
|
|
|
|
return notifications
|
|
|
|
}
|
|
|
|
|
2021-05-14 21:22:50 +00:00
|
|
|
func (r *MessengerResponse) PinMessages() []*common.PinMessage {
|
|
|
|
var pinMessages []*common.PinMessage
|
|
|
|
for _, pm := range r.pinMessages {
|
|
|
|
pinMessages = append(pinMessages, pm)
|
|
|
|
}
|
|
|
|
return pinMessages
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
func (r *MessengerResponse) StatusUpdates() []UserStatus {
|
|
|
|
var userStatus []UserStatus
|
|
|
|
for pk, s := range r.statusUpdates {
|
|
|
|
s.PublicKey = pk
|
|
|
|
userStatus = append(userStatus, s)
|
|
|
|
}
|
|
|
|
return userStatus
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (r *MessengerResponse) IsEmpty() bool {
|
|
|
|
return len(r.chats)+
|
2021-06-03 13:11:55 +00:00
|
|
|
len(r.messages)+
|
2021-05-14 21:22:50 +00:00
|
|
|
len(r.pinMessages)+
|
2021-01-11 10:32:51 +00:00
|
|
|
len(r.Contacts)+
|
2022-01-17 03:42:11 +00:00
|
|
|
len(r.Bookmarks)+
|
2022-02-10 10:00:59 +00:00
|
|
|
len(r.clearedHistories)+
|
2021-01-11 10:32:51 +00:00
|
|
|
len(r.Installations)+
|
|
|
|
len(r.Invitations)+
|
|
|
|
len(r.EmojiReactions)+
|
|
|
|
len(r.communities)+
|
|
|
|
len(r.CommunityChanges)+
|
|
|
|
len(r.removedChats)+
|
2021-07-26 21:06:32 +00:00
|
|
|
len(r.removedMessages)+
|
2021-01-11 10:32:51 +00:00
|
|
|
len(r.Mailservers)+
|
2021-03-31 16:23:45 +00:00
|
|
|
len(r.notifications)+
|
2021-07-22 17:41:49 +00:00
|
|
|
len(r.statusUpdates)+
|
2021-04-07 12:57:14 +00:00
|
|
|
len(r.activityCenterNotifications)+
|
2021-07-22 17:41:49 +00:00
|
|
|
len(r.RequestsToJoinCommunity) == 0 &&
|
|
|
|
r.currentStatus == nil
|
2020-12-01 09:43:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 17:04:38 +00:00
|
|
|
// Merge takes another response and appends the new Chats & new Messages and replaces
|
|
|
|
// the existing Messages & Chats if they have the same ID
|
2021-01-11 10:32:51 +00:00
|
|
|
func (r *MessengerResponse) Merge(response *MessengerResponse) error {
|
|
|
|
if len(response.Contacts)+
|
|
|
|
len(response.Installations)+
|
|
|
|
len(response.EmojiReactions)+
|
|
|
|
len(response.Invitations)+
|
|
|
|
len(response.RequestsToJoinCommunity)+
|
|
|
|
len(response.Mailservers)+
|
|
|
|
len(response.EmojiReactions)+
|
2022-01-17 03:42:11 +00:00
|
|
|
len(response.Bookmarks)+
|
2022-02-10 10:00:59 +00:00
|
|
|
len(response.clearedHistories)+
|
2021-01-11 10:32:51 +00:00
|
|
|
len(response.CommunityChanges) != 0 {
|
2020-12-01 09:43:46 +00:00
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
r.AddChats(response.Chats())
|
|
|
|
r.AddRemovedChats(response.RemovedChats())
|
2021-07-26 21:06:32 +00:00
|
|
|
r.AddRemovedMessages(response.RemovedMessages())
|
2021-03-31 16:23:45 +00:00
|
|
|
r.AddNotifications(response.Notifications())
|
2021-06-03 13:11:55 +00:00
|
|
|
r.AddMessages(response.Messages())
|
2021-01-11 10:32:51 +00:00
|
|
|
r.AddCommunities(response.Communities())
|
2021-05-14 21:22:50 +00:00
|
|
|
r.AddPinMessages(response.PinMessages())
|
2021-12-02 14:23:02 +00:00
|
|
|
r.AddActivityCenterNotifications(response.ActivityCenterNotifications())
|
2020-12-03 17:04:38 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (r *MessengerResponse) AddCommunities(communities []*communities.Community) {
|
|
|
|
for _, overrideCommunity := range communities {
|
|
|
|
r.AddCommunity(overrideCommunity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddCommunity(c *communities.Community) {
|
|
|
|
if r.communities == nil {
|
|
|
|
r.communities = make(map[string]*communities.Community)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.communities[c.IDString()] = c
|
|
|
|
}
|
|
|
|
|
2022-01-17 03:42:11 +00:00
|
|
|
func (r *MessengerResponse) AddBookmark(bookmark *browsers.Bookmark) {
|
|
|
|
r.Bookmarks = append(r.Bookmarks, bookmark)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddBookmarks(bookmarks []*browsers.Bookmark) {
|
|
|
|
for _, b := range bookmarks {
|
|
|
|
r.AddBookmark(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (r *MessengerResponse) AddChat(c *Chat) {
|
|
|
|
if r.chats == nil {
|
|
|
|
r.chats = make(map[string]*Chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.chats[c.ID] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddChats(chats []*Chat) {
|
|
|
|
for _, c := range chats {
|
|
|
|
r.AddChat(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
func (r *MessengerResponse) AddNotification(n *localnotifications.Notification) {
|
|
|
|
if r.notifications == nil {
|
|
|
|
r.notifications = make(map[string]*localnotifications.Notification)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.notifications[n.ID.String()] = n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) ClearNotifications() {
|
|
|
|
r.notifications = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddNotifications(notifications []*localnotifications.Notification) {
|
|
|
|
for _, c := range notifications {
|
|
|
|
r.AddNotification(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (r *MessengerResponse) AddRemovedChats(chats []string) {
|
|
|
|
for _, c := range chats {
|
|
|
|
r.AddRemovedChat(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddRemovedChat(chatID string) {
|
|
|
|
if r.removedChats == nil {
|
|
|
|
r.removedChats = make(map[string]bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.removedChats[chatID] = true
|
|
|
|
}
|
2021-04-07 12:57:14 +00:00
|
|
|
|
2021-08-20 14:26:13 +00:00
|
|
|
func (r *MessengerResponse) AddRemovedMessages(messages []*RemovedMessage) {
|
2021-07-26 21:06:32 +00:00
|
|
|
for _, m := range messages {
|
|
|
|
r.AddRemovedMessage(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 14:26:13 +00:00
|
|
|
func (r *MessengerResponse) AddRemovedMessage(rm *RemovedMessage) {
|
2021-07-26 21:06:32 +00:00
|
|
|
if r.removedMessages == nil {
|
2021-08-20 14:26:13 +00:00
|
|
|
r.removedMessages = make(map[string]*RemovedMessage)
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 14:26:13 +00:00
|
|
|
r.removedMessages[rm.MessageID] = rm
|
2021-09-08 09:55:06 +00:00
|
|
|
// Remove original message from the map
|
|
|
|
|
|
|
|
if len(r.messages) != 0 && r.messages[rm.MessageID] != nil {
|
|
|
|
delete(r.messages, rm.MessageID)
|
|
|
|
}
|
2021-07-26 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 10:00:59 +00:00
|
|
|
func (r *MessengerResponse) AddClearedHistory(ch *ClearedHistory) {
|
|
|
|
if r.clearedHistories == nil {
|
|
|
|
r.clearedHistories = make(map[string]*ClearedHistory)
|
|
|
|
}
|
|
|
|
|
|
|
|
existingClearedHistory, ok := r.clearedHistories[ch.ChatID]
|
|
|
|
if !ok || existingClearedHistory.ClearedAt < ch.ClearedAt {
|
|
|
|
r.clearedHistories[ch.ChatID] = ch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
func (r *MessengerResponse) AddActivityCenterNotifications(ns []*ActivityCenterNotification) {
|
|
|
|
for _, n := range ns {
|
|
|
|
r.AddActivityCenterNotification(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddActivityCenterNotification(n *ActivityCenterNotification) {
|
|
|
|
if r.activityCenterNotifications == nil {
|
|
|
|
r.activityCenterNotifications = make(map[string]*ActivityCenterNotification)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.activityCenterNotifications[n.ID.String()] = n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) ActivityCenterNotifications() []*ActivityCenterNotification {
|
|
|
|
var ns []*ActivityCenterNotification
|
|
|
|
for _, n := range r.activityCenterNotifications {
|
|
|
|
ns = append(ns, n)
|
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
2021-05-14 21:22:50 +00:00
|
|
|
|
|
|
|
func (r *MessengerResponse) AddPinMessage(pm *common.PinMessage) {
|
|
|
|
if r.pinMessages == nil {
|
|
|
|
r.pinMessages = make(map[string]*common.PinMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.pinMessages[pm.ID] = pm
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddPinMessages(pms []*common.PinMessage) {
|
|
|
|
for _, pm := range pms {
|
|
|
|
r.AddPinMessage(pm)
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 15:15:22 +00:00
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
func (r *MessengerResponse) SetCurrentStatus(status UserStatus) {
|
|
|
|
r.currentStatus = &status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddStatusUpdate(upd UserStatus) {
|
|
|
|
if r.statusUpdates == nil {
|
|
|
|
r.statusUpdates = make(map[string]UserStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.statusUpdates[upd.PublicKey] = upd
|
|
|
|
}
|
|
|
|
|
2021-06-03 13:11:55 +00:00
|
|
|
func (r *MessengerResponse) Messages() []*common.Message {
|
|
|
|
var ms []*common.Message
|
|
|
|
for _, m := range r.messages {
|
|
|
|
ms = append(ms, m)
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) AddMessages(ms []*common.Message) {
|
|
|
|
for _, m := range ms {
|
|
|
|
r.AddMessage(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
func (r *MessengerResponse) AddMessage(message *common.Message) {
|
2021-06-03 13:11:55 +00:00
|
|
|
if r.messages == nil {
|
|
|
|
r.messages = make(map[string]*common.Message)
|
|
|
|
}
|
2021-08-13 17:10:32 +00:00
|
|
|
if message.Deleted {
|
|
|
|
return
|
|
|
|
}
|
2021-06-03 13:11:55 +00:00
|
|
|
r.messages[message.ID] = message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MessengerResponse) SetMessages(messages []*common.Message) {
|
|
|
|
r.messages = make(map[string]*common.Message)
|
|
|
|
r.AddMessages(messages)
|
2021-03-25 15:15:22 +00:00
|
|
|
}
|
2021-06-03 14:09:30 +00:00
|
|
|
|
|
|
|
func (r *MessengerResponse) GetMessage(messageID string) *common.Message {
|
|
|
|
if r.messages == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return r.messages[messageID]
|
|
|
|
}
|