From 60de443e89bd6a5d4f5b71e7038528d27f26090b Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Tue, 1 Jun 2021 11:29:37 +0200 Subject: [PATCH] address feedback --- multiaccounts/accounts/database_test.go | 1 + protocol/message_handler.go | 52 ------------------------- protocol/messenger.go | 5 +-- protocol/messenger_chats.go | 30 +++++++------- protocol/messenger_communities.go | 52 +++++++++++++++++++++++++ protocol/messenger_mailserver.go | 12 +++--- 6 files changed, 75 insertions(+), 77 deletions(-) diff --git a/multiaccounts/accounts/database_test.go b/multiaccounts/accounts/database_test.go index fe03d75df..fa3cc24db 100644 --- a/multiaccounts/accounts/database_test.go +++ b/multiaccounts/accounts/database_test.go @@ -37,6 +37,7 @@ var ( SigningPhrase: "yurt joey vibe", SendPushNotifications: true, ProfilePicturesVisibility: ProfilePicturesVisibilityContactsOnly, + DefaultSyncPeriod: 86400, UseMailservers: true, LinkPreviewRequestEnabled: true, WalletRootAddress: types.HexToAddress("0x3B591fd819F86D0A6a2EF2Bcb94f77807a7De1a6")} diff --git a/protocol/message_handler.go b/protocol/message_handler.go index 49cb9d835..7369f105e 100644 --- a/protocol/message_handler.go +++ b/protocol/message_handler.go @@ -452,58 +452,6 @@ func (m *MessageHandler) HandlePairInstallation(state *ReceivedMessageState, mes return nil } -// HandleCommunityDescription handles an community description -func (m *MessageHandler) HandleCommunityDescription(state *ReceivedMessageState, signer *ecdsa.PublicKey, description protobuf.CommunityDescription, rawPayload []byte) error { - communityResponse, err := m.communitiesManager.HandleCommunityDescriptionMessage(signer, &description, rawPayload) - if err != nil { - return err - } - - community := communityResponse.Community - - state.Response.AddCommunity(community) - state.Response.CommunityChanges = append(state.Response.CommunityChanges, communityResponse.Changes) - - // If we haven't joined the org, nothing to do - if !community.Joined() { - return nil - } - - // Update relevant chats names and add new ones - // Currently removal is not supported - chats := CreateCommunityChats(community, state.Timesource) - var chatIDs []string - for i, chat := range chats { - - oldChat, ok := state.AllChats.Load(chat.ID) - if !ok { - // Beware, don't use the reference in the range (i.e chat) as it's a shallow copy - state.AllChats.Store(chat.ID, chats[i]) - - state.Response.AddChat(chat) - chatIDs = append(chatIDs, chat.ID) - // Update name, currently is the only field is mutable - } else if oldChat.Name != chat.Name { - oldChat.Name = chat.Name - // TODO(samyoul) remove storing of an updated reference pointer? - state.AllChats.Store(chat.ID, oldChat) - state.Response.AddChat(chat) - } - } - - // Load transport filters - filters, err := m.transport.InitPublicFilters(chatIDs) - if err != nil { - return err - } - - for _, filter := range filters { - state.AllFilters[filter.ChatID] = filter - } - - return nil -} - // HandleCommunityInvitation handles an community invitation func (m *MessageHandler) HandleCommunityInvitation(state *ReceivedMessageState, signer *ecdsa.PublicKey, invitation protobuf.CommunityInvitation, rawPayload []byte) error { if invitation.PublicKey == nil { diff --git a/protocol/messenger.go b/protocol/messenger.go index 46d8a0556..988fb71f7 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -2327,8 +2327,6 @@ type ReceivedMessageState struct { AllInstallations *installationMap // List of communities modified ModifiedInstallations *stringBoolMap - // List of filters - AllFilters map[string]*transport.Filter // Map of existing messages ExistingMessagesMap map[string]bool // EmojiReactions is a list of emoji reactions for the current batch @@ -2447,7 +2445,6 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte ModifiedInstallations: m.modifiedInstallations, ExistingMessagesMap: make(map[string]bool), EmojiReactions: make(map[string]*EmojiReaction), - AllFilters: make(map[string]*transport.Filter), GroupChatInvitations: make(map[string]*GroupChatInvitation), Response: &MessengerResponse{}, Timesource: m.getTimesource(), @@ -2794,7 +2791,7 @@ func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filte case protobuf.CommunityDescription: logger.Debug("Handling CommunityDescription") - err = m.handler.HandleCommunityDescription(messageState, publicKey, msg.ParsedMessage.Interface().(protobuf.CommunityDescription), msg.DecryptedPayload) + err = m.handleCommunityDescription(messageState, publicKey, msg.ParsedMessage.Interface().(protobuf.CommunityDescription), msg.DecryptedPayload) if err != nil { logger.Warn("failed to handle CommunityDescription", zap.Error(err)) allMessagesProcessed = false diff --git a/protocol/messenger_chats.go b/protocol/messenger_chats.go index 905e04ab3..0e77855f9 100644 --- a/protocol/messenger_chats.go +++ b/protocol/messenger_chats.go @@ -37,6 +37,18 @@ func (m *Messenger) ActiveChats() []*Chat { return chats } +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 +} + func (m *Messenger) CreatePublicChat(request *requests.CreatePublicChat) (*MessengerResponse, error) { if err := request.Validate(); err != nil { return nil, err @@ -68,13 +80,9 @@ func (m *Messenger) CreatePublicChat(request *requests.CreatePublicChat) (*Messe // We set the synced to, synced from to the default time if !willSync { - defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod() - if err != nil { + if err := m.initChatSyncFields(chat); err != nil { return nil, err } - timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod - chat.SyncedTo = timestamp - chat.SyncedFrom = timestamp } err = m.saveChat(chat) @@ -134,13 +142,9 @@ func (m *Messenger) CreateProfileChat(request *requests.CreateProfileChat) (*Mes // We set the synced to, synced from to the default time if !willSync { - defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod() - if err != nil { + if err := m.initChatSyncFields(chat); err != nil { return nil, err } - timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod - chat.SyncedTo = timestamp - chat.SyncedFrom = timestamp } _, err = m.scheduleSyncFilters([]*transport.Filter{filter}) @@ -191,13 +195,9 @@ func (m *Messenger) CreateOneToOneChat(request *requests.CreateOneToOneChat) (*M // We set the synced to, synced from to the default time if !willSync { - defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod() - if err != nil { + if err := m.initChatSyncFields(chat); err != nil { return nil, err } - timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod - chat.SyncedTo = timestamp - chat.SyncedFrom = timestamp } err = m.saveChat(chat) diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 6066835cd..7f2980e5b 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -690,3 +690,55 @@ func (m *Messenger) passStoredCommunityInfoToSignalHandler(communityID string) { m.config.messengerSignalsHandler.CommunityInfoFound(community) m.forgetCommunityRequest(communityID) } + +// handleCommunityDescription handles an community description +func (m *Messenger) handleCommunityDescription(state *ReceivedMessageState, signer *ecdsa.PublicKey, description protobuf.CommunityDescription, rawPayload []byte) error { + communityResponse, err := m.communitiesManager.HandleCommunityDescriptionMessage(signer, &description, rawPayload) + if err != nil { + return err + } + + community := communityResponse.Community + + state.Response.AddCommunity(community) + state.Response.CommunityChanges = append(state.Response.CommunityChanges, communityResponse.Changes) + + // If we haven't joined the org, nothing to do + if !community.Joined() { + return nil + } + + // Update relevant chats names and add new ones + // Currently removal is not supported + chats := CreateCommunityChats(community, state.Timesource) + var chatIDs []string + for i, chat := range chats { + + oldChat, ok := state.AllChats.Load(chat.ID) + if !ok { + // Beware, don't use the reference in the range (i.e chat) as it's a shallow copy + state.AllChats.Store(chat.ID, chats[i]) + + state.Response.AddChat(chat) + chatIDs = append(chatIDs, chat.ID) + // Update name, currently is the only field is mutable + } else if oldChat.Name != chat.Name { + oldChat.Name = chat.Name + // TODO(samyoul) remove storing of an updated reference pointer? + state.AllChats.Store(chat.ID, oldChat) + state.Response.AddChat(chat) + } + } + + // Load transport filters + filters, err := m.transport.InitPublicFilters(chatIDs) + if err != nil { + return err + } + _, err = m.scheduleSyncFilters(filters) + if err != nil { + return err + } + + return nil +} diff --git a/protocol/messenger_mailserver.go b/protocol/messenger_mailserver.go index 17b278656..77cc7e047 100644 --- a/protocol/messenger_mailserver.go +++ b/protocol/messenger_mailserver.go @@ -167,7 +167,7 @@ func (m *Messenger) syncChat(chatID string) (*MessengerResponse, error) { return m.syncFilters(filters) } -func (m *Messenger) defaultSyncPeriod() (uint32, error) { +func (m *Messenger) defaultSyncPeriodFromNow() (uint32, error) { defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod() if err != nil { return 0, err @@ -175,9 +175,9 @@ func (m *Messenger) defaultSyncPeriod() (uint32, error) { return uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod, nil } -// calculateSyncPeriod caps the sync period to the default -func (m *Messenger) calculateSyncPeriod(period uint32) (uint32, error) { - d, err := m.defaultSyncPeriod() +// capToDefaultSyncPeriod caps the sync period to the default +func (m *Messenger) capToDefaultSyncPeriod(period uint32) (uint32, error) { + d, err := m.defaultSyncPeriodFromNow() if err != nil { return 0, err } @@ -232,7 +232,7 @@ func (m *Messenger) syncFilters(filters []*transport.Filter) (*MessengerResponse topicData, ok := topicsData[filter.Topic.String()] if !ok { - lastRequest, err := m.defaultSyncPeriod() + lastRequest, err := m.defaultSyncPeriodFromNow() if err != nil { return nil, err } @@ -243,7 +243,7 @@ func (m *Messenger) syncFilters(filters []*transport.Filter) (*MessengerResponse } batch, ok := batches[topicData.LastRequest] if !ok { - from, err := m.calculateSyncPeriod(uint32(topicData.LastRequest)) + from, err := m.capToDefaultSyncPeriod(uint32(topicData.LastRequest)) if err != nil { return nil, err }