From 9d9c40b7cffa6af901c06836ef576ad5a91c51bc Mon Sep 17 00:00:00 2001 From: Dario Gabriel Lipicar Date: Mon, 8 Jan 2024 17:14:37 -0300 Subject: [PATCH] fix: make fetchCommunity return latest available community Fixes #13145 --- protocol/messenger_communities.go | 4 ++-- services/ext/service.go | 32 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 6544c2874..430ccd29d 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -2535,7 +2535,7 @@ func (m *Messenger) RemoveRoleFromMember(request *requests.RemoveRoleFromMember) return response, nil } -func (m *Messenger) findCommunityInfoFromDB(communityID string) (*communities.Community, error) { +func (m *Messenger) FindCommunityInfoFromDB(communityID string) (*communities.Community, error) { id, err := hexutil.Decode(communityID) if err != nil { return nil, err @@ -2564,7 +2564,7 @@ func (m *Messenger) FetchCommunity(request *FetchCommunityRequest) (*communities communityID := request.getCommunityID() if request.TryDatabase { - community, err := m.findCommunityInfoFromDB(communityID) + community, err := m.FindCommunityInfoFromDB(communityID) if err != nil { return nil, err } diff --git a/services/ext/service.go b/services/ext/service.go index 6f63e6831..4db3e24a0 100644 --- a/services/ext/service.go +++ b/services/ext/service.go @@ -549,7 +549,9 @@ func (s *Service) FillCollectibleMetadata(collectible *thirdparty.FullCollectibl return fmt.Errorf("invalid communityID") } - community, err := s.fetchCommunity(communityID, true) + // FetchCommunityInfo should have been previously called once to ensure + // that the latest version of the CommunityDescription is available in the DB + community, err := s.fetchCommunity(communityID, false) if err != nil { return err @@ -632,7 +634,7 @@ func communityToInfo(community *communities.Community) *thirdparty.CommunityInfo } func (s *Service) FetchCommunityInfo(communityID string) (*thirdparty.CommunityInfo, error) { - community, err := s.fetchCommunity(communityID, false) + community, err := s.fetchCommunity(communityID, true) if err != nil { return nil, err } @@ -640,7 +642,7 @@ func (s *Service) FetchCommunityInfo(communityID string) (*thirdparty.CommunityI return communityToInfo(community), nil } -func (s *Service) fetchCommunity(communityID string, tryDatabase bool) (*communities.Community, error) { +func (s *Service) fetchCommunity(communityID string, fetchLatest bool) (*communities.Community, error) { if s.messenger == nil { return nil, fmt.Errorf("messenger not ready") } @@ -650,14 +652,24 @@ func (s *Service) fetchCommunity(communityID string, tryDatabase bool) (*communi // TODO: we need the shard information in the collectible to be able to retrieve info for // communities that have specific shards - var shard *shard.Shard = nil // TODO: build this with info from token - community, err := s.messenger.FetchCommunity(&protocol.FetchCommunityRequest{ - CommunityKey: communityID, - Shard: shard, - TryDatabase: tryDatabase, - WaitForResponse: true, - }) + if fetchLatest { + // Try to fetch the latest version of the Community + var shard *shard.Shard = nil // TODO: build this with info from token + // NOTE: The community returned by this function will be nil if + // the version we have in the DB is the latest available. + _, err := s.messenger.FetchCommunity(&protocol.FetchCommunityRequest{ + CommunityKey: communityID, + Shard: shard, + TryDatabase: false, + WaitForResponse: true, + }) + if err != nil { + return nil, err + } + } + // Get the latest successfully fetched version of the Community + community, err := s.messenger.FindCommunityInfoFromDB(communityID) if err != nil { return nil, err }