From 0073156dddaa11bf608c9374868d22551477f9e2 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Thu, 12 Dec 2024 12:01:55 +0530 Subject: [PATCH] feat_: use universal chatID for both send and receive filters for communities --- protocol/communities_messenger_test.go | 2 +- protocol/messenger_communities.go | 68 +------------------------- protocol/messenger_filter_init.go | 15 +++--- 3 files changed, 8 insertions(+), 77 deletions(-) diff --git a/protocol/communities_messenger_test.go b/protocol/communities_messenger_test.go index fab89c9c8a5..3492111f540 100644 --- a/protocol/communities_messenger_test.go +++ b/protocol/communities_messenger_test.go @@ -3630,7 +3630,7 @@ func (s *MessengerCommunitiesSuite) TestHandleImport() { message.Sig = crypto.FromECDSAPub(&s.owner.identity.PublicKey) message.Payload = wrappedPayload - filter := s.alice.transport.FilterByChatID(chat.ID) + filter := s.alice.transport.FilterByChatID(community.UniversalChatID()) importedMessages := make(map[transport.Filter][]*types.Message, 0) importedMessages[*filter] = append(importedMessages[*filter], message) diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index f5bed916f91..f3ac36c2969 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -953,11 +953,6 @@ func (m *Messenger) initCommunityChats(community *communities.Community) ([]*Cha chats := CreateCommunityChats(community, m.getTimesource()) - for _, chat := range chats { - publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ChatID: chat.ID, PubsubTopic: community.PubsubTopic()}) - - } - // Load transport filters filters, err := m.transport.InitPublicFilters(publicFiltersToInit) if err != nil { @@ -2400,25 +2395,12 @@ func (m *Messenger) CreateCommunityChat(communityID types.HexBytes, c *protobuf. response.CommunityChanges = []*communities.CommunityChanges{changes} var chats []*Chat - var publicFiltersToInit []transport.FiltersToInitialize for chatID, chat := range changes.ChatsAdded { c := CreateCommunityChat(changes.Community.IDString(), chatID, chat, m.getTimesource()) chats = append(chats, c) - publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ChatID: c.ID, PubsubTopic: changes.Community.PubsubTopic()}) - response.AddChat(c) } - // Load filters - filters, err := m.transport.InitPublicFilters(publicFiltersToInit) - if err != nil { - return nil, err - } - _, err = m.scheduleSyncFilters(filters) - if err != nil { - return nil, err - } - err = m.saveChats(chats) if err != nil { return nil, err @@ -2442,24 +2424,12 @@ func (m *Messenger) EditCommunityChat(communityID types.HexBytes, chatID string, response.CommunityChanges = []*communities.CommunityChanges{changes} var chats []*Chat - var publicFiltersToInit []transport.FiltersToInitialize for chatID, change := range changes.ChatsModified { c := CreateCommunityChat(community.IDString(), chatID, change.ChatModified, m.getTimesource()) chats = append(chats, c) - publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ChatID: c.ID, PubsubTopic: community.PubsubTopic()}) response.AddChat(c) } - // Load filters - filters, err := m.transport.InitPublicFilters(publicFiltersToInit) - if err != nil { - return nil, err - } - _, err = m.scheduleSyncFilters(filters) - if err != nil { - return nil, err - } - return &response, m.saveChats(chats) } @@ -2492,16 +2462,12 @@ func (m *Messenger) InitCommunityFilters(communityFiltersToInitialize []transpor func (m *Messenger) DefaultFilters(o *communities.Community) []transport.FiltersToInitialize { cID := o.IDString() uncompressedPubKey := common.PubkeyToHex(o.PublicKey())[2:] - updatesChannelID := o.StatusUpdatesChannelID() - mlChannelID := o.MagnetlinkMessageChannelID() memberUpdateChannelID := o.MemberUpdateChannelID() communityPubsubTopic := o.PubsubTopic() filters := []transport.FiltersToInitialize{ {ChatID: cID, PubsubTopic: communityPubsubTopic}, - {ChatID: updatesChannelID, PubsubTopic: communityPubsubTopic}, - {ChatID: mlChannelID, PubsubTopic: communityPubsubTopic}, {ChatID: memberUpdateChannelID, PubsubTopic: communityPubsubTopic}, {ChatID: uncompressedPubKey, PubsubTopic: shard.DefaultNonProtectedPubsubTopic()}, } @@ -2721,15 +2687,6 @@ func (m *Messenger) UpdateCommunityFilters(community *communities.Community) err return err } } - for chatID := range community.Chats() { - communityChatID := community.IDString() + chatID - _, err := m.transport.RemoveFilterByChatID(communityChatID) - if err != nil { - return err - } - publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ChatID: communityChatID, PubsubTopic: community.PubsubTopic()}) - } - _, err := m.transport.InitPublicFilters(publicFiltersToInit) if err != nil { return err @@ -3376,12 +3333,10 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi return nil } - removedChatIDs := make([]string, 0) for id := range communityResponse.Changes.ChatsRemoved { chatID := community.ChatID(id) _, ok := state.AllChats.Load(chatID) if ok { - removedChatIDs = append(removedChatIDs, chatID) state.AllChats.Delete(chatID) err := m.DeleteChat(chatID) if err != nil { @@ -3411,7 +3366,6 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi // Update relevant chats names and add new ones // Currently removal is not supported chats := CreateCommunityChats(community, state.Timesource) - var publicFiltersToInit []transport.FiltersToInitialize for i, chat := range chats { oldChat, ok := state.AllChats.Load(chat.ID) @@ -3420,10 +3374,7 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi state.AllChats.Store(chat.ID, chats[i]) state.Response.AddChat(chat) - publicFiltersToInit = append(publicFiltersToInit, transport.FiltersToInitialize{ - ChatID: chat.ID, - PubsubTopic: community.PubsubTopic(), - }) + // Update name, currently is the only field is mutable } else if oldChat.Name != chat.Name || oldChat.Description != chat.Description || @@ -3442,23 +3393,6 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi } } - for _, chatID := range removedChatIDs { - _, err := m.transport.RemoveFilterByChatID(chatID) - if err != nil { - m.logger.Error("couldn't remove filter", zap.Error(err)) - } - } - - // Load transport filters - filters, err := m.transport.InitPublicFilters(publicFiltersToInit) - if err != nil { - return err - } - _, err = m.scheduleSyncFilters(filters) - if err != nil { - return err - } - for _, requestToJoin := range communityResponse.RequestsToJoin { // Activity Center notification notification, err := m.persistence.GetActivityCenterNotificationByID(requestToJoin.ID) diff --git a/protocol/messenger_filter_init.go b/protocol/messenger_filter_init.go index e66030a0045..cb32ffd0e20 100644 --- a/protocol/messenger_filter_init.go +++ b/protocol/messenger_filter_init.go @@ -182,12 +182,12 @@ func (m *Messenger) processSingleChat(chat *Chat, communityInfo map[string]*comm filters = append(filters, transport.FiltersToInitialize{ChatID: chat.ID}) case ChatTypeCommunityChat: - filter, err := m.processCommunityChat(chat, communityInfo) + // Since universalChatID is being used, no specific filters needs to be registered for all community chats. + // Reasoning: https://github.com/status-im/status-go/pull/5993 + err := m.processCommunityChat(chat, communityInfo) if err != nil { return nil, nil, err } - filters = append(filters, filter) - case ChatTypeOneToOne: pk, err := chat.PublicKey() if err != nil { @@ -209,13 +209,13 @@ func (m *Messenger) processSingleChat(chat *Chat, communityInfo map[string]*comm return filters, publicKeys, nil } -func (m *Messenger) processCommunityChat(chat *Chat, communityInfo map[string]*communities.Community) (transport.FiltersToInitialize, error) { +func (m *Messenger) processCommunityChat(chat *Chat, communityInfo map[string]*communities.Community) error { community, ok := communityInfo[chat.CommunityID] if !ok { var err error community, err = m.communitiesManager.GetByIDString(chat.CommunityID) if err != nil { - return transport.FiltersToInitialize{}, err + return err } communityInfo[chat.CommunityID] = community } @@ -229,10 +229,7 @@ func (m *Messenger) processCommunityChat(chat *Chat, communityInfo map[string]*c } } - return transport.FiltersToInitialize{ - ChatID: chat.ID, - PubsubTopic: community.PubsubTopic(), - }, nil + return nil } func (m *Messenger) processPrivateGroupChat(chat *Chat) ([]*ecdsa.PublicKey, error) {