2020-12-01 09:43:46 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2021-01-11 10:32:51 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
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"
|
2020-11-18 09:16:51 +00:00
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type MessengerResponse struct {
|
2021-01-11 10:32:51 +00:00
|
|
|
Messages []*common.Message
|
|
|
|
Contacts []*Contact
|
|
|
|
Installations []*multidevice.Installation
|
|
|
|
EmojiReactions []*EmojiReaction
|
|
|
|
Invitations []*GroupChatInvitation
|
|
|
|
CommunityChanges []*communities.CommunityChanges
|
|
|
|
RequestsToJoinCommunity []*communities.RequestToJoin
|
|
|
|
Filters []*transport.Filter
|
|
|
|
RemovedFilters []*transport.Filter
|
|
|
|
Mailservers []mailservers.Mailserver
|
|
|
|
MailserverTopics []mailservers.MailserverTopic
|
|
|
|
MailserverRanges []mailservers.ChatRequestRange
|
|
|
|
|
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
|
|
|
|
communities map[string]*communities.Community
|
|
|
|
activityCenterNotifications map[string]*ActivityCenterNotification
|
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"`
|
|
|
|
Messages []*common.Message `json:"messages,omitempty"`
|
|
|
|
Contacts []*Contact `json:"contacts,omitempty"`
|
|
|
|
Installations []*multidevice.Installation `json:"installations,omitempty"`
|
|
|
|
EmojiReactions []*EmojiReaction `json:"emojiReactions,omitempty"`
|
|
|
|
Invitations []*GroupChatInvitation `json:"invitations,omitempty"`
|
|
|
|
CommunityChanges []*communities.CommunityChanges `json:"communityChanges,omitempty"`
|
|
|
|
RequestsToJoinCommunity []*communities.RequestToJoin `json:"requestsToJoinCommunity,omitempty"`
|
|
|
|
Filters []*transport.Filter `json:"filters,omitempty"`
|
|
|
|
RemovedFilters []*transport.Filter `json:"removedFilters,omitempty"`
|
|
|
|
Mailservers []mailservers.Mailserver `json:"mailservers,omitempty"`
|
|
|
|
MailserverTopics []mailservers.MailserverTopic `json:"mailserverTopics,omitempty"`
|
|
|
|
MailserverRanges []mailservers.ChatRequestRange `json:"mailserverRanges,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-01-11 10:32:51 +00:00
|
|
|
}{
|
|
|
|
Messages: r.Messages,
|
|
|
|
Contacts: r.Contacts,
|
|
|
|
Installations: r.Installations,
|
|
|
|
EmojiReactions: r.EmojiReactions,
|
|
|
|
Invitations: r.Invitations,
|
|
|
|
CommunityChanges: r.CommunityChanges,
|
|
|
|
RequestsToJoinCommunity: r.RequestsToJoinCommunity,
|
|
|
|
Filters: r.Filters,
|
|
|
|
RemovedFilters: r.RemovedFilters,
|
|
|
|
Mailservers: r.Mailservers,
|
|
|
|
MailserverTopics: r.MailserverTopics,
|
|
|
|
MailserverRanges: r.MailserverRanges,
|
|
|
|
}
|
|
|
|
|
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-04-07 12:57:14 +00:00
|
|
|
responseItem.ActivityCenterNotifications = r.ActivityCenterNotifications()
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-11 10:32:51 +00:00
|
|
|
func (r *MessengerResponse) IsEmpty() bool {
|
|
|
|
return len(r.chats)+
|
|
|
|
len(r.Messages)+
|
|
|
|
len(r.Contacts)+
|
|
|
|
len(r.Installations)+
|
|
|
|
len(r.Invitations)+
|
|
|
|
len(r.EmojiReactions)+
|
|
|
|
len(r.communities)+
|
|
|
|
len(r.CommunityChanges)+
|
|
|
|
len(r.Filters)+
|
|
|
|
len(r.RemovedFilters)+
|
|
|
|
len(r.removedChats)+
|
|
|
|
len(r.MailserverTopics)+
|
|
|
|
len(r.Mailservers)+
|
|
|
|
len(r.MailserverRanges)+
|
2021-03-31 16:23:45 +00:00
|
|
|
len(r.notifications)+
|
2021-04-07 12:57:14 +00:00
|
|
|
len(r.activityCenterNotifications)+
|
2021-01-11 10:32:51 +00:00
|
|
|
len(r.RequestsToJoinCommunity) == 0
|
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.MailserverTopics)+
|
|
|
|
len(response.MailserverRanges)+
|
|
|
|
len(response.EmojiReactions)+
|
|
|
|
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-03-31 16:23:45 +00:00
|
|
|
r.AddNotifications(response.Notifications())
|
2021-01-11 10:32:51 +00:00
|
|
|
r.overrideMessages(response.Messages)
|
|
|
|
r.overrideFilters(response.Filters)
|
|
|
|
r.overrideRemovedFilters(response.Filters)
|
|
|
|
r.AddCommunities(response.Communities())
|
2020-12-03 17:04:38 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
// overrideMessages append new messages and override existing ones in response.Messages
|
|
|
|
func (r *MessengerResponse) overrideMessages(messages []*common.Message) {
|
|
|
|
for _, overrideMessage := range messages {
|
2020-12-01 09:43:46 +00:00
|
|
|
var found = false
|
2021-01-11 10:32:51 +00:00
|
|
|
for idx, chat := range r.Messages {
|
|
|
|
if chat.ID == overrideMessage.ID {
|
|
|
|
r.Messages[idx] = overrideMessage
|
2020-12-01 09:43:46 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2021-01-11 10:32:51 +00:00
|
|
|
r.Messages = append(r.Messages, overrideMessage)
|
2020-12-01 09:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-03 17:04:38 +00:00
|
|
|
}
|
2020-12-01 09:43:46 +00:00
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
// overrideFilters append new filters and override existing ones in response.Filters
|
|
|
|
func (r *MessengerResponse) overrideFilters(filters []*transport.Filter) {
|
|
|
|
for _, overrideFilter := range filters {
|
2020-12-01 09:43:46 +00:00
|
|
|
var found = false
|
2021-01-11 10:32:51 +00:00
|
|
|
for idx, filter := range r.Filters {
|
|
|
|
if filter.FilterID == overrideFilter.FilterID {
|
|
|
|
r.Filters[idx] = overrideFilter
|
2020-12-01 09:43:46 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2021-01-11 10:32:51 +00:00
|
|
|
r.Filters = append(r.Filters, overrideFilter)
|
2020-12-01 09:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
// overrideRemovedFilters append removed filters and override existing ones in response.Filters
|
|
|
|
func (r *MessengerResponse) overrideRemovedFilters(filters []*transport.Filter) {
|
|
|
|
for _, overrideFilter := range filters {
|
|
|
|
var found = false
|
|
|
|
for idx, filter := range r.RemovedFilters {
|
|
|
|
if filter.FilterID == overrideFilter.FilterID {
|
|
|
|
r.RemovedFilters[idx] = overrideFilter
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
r.RemovedFilters = append(r.RemovedFilters, overrideFilter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|