From 0950721a6d341ba5c28eeaee38af06f6fcfd9333 Mon Sep 17 00:00:00 2001 From: Mikhail Rogachev Date: Mon, 14 Aug 2023 12:59:02 +0400 Subject: [PATCH] feat: add 'GetCommunityMembersForWalletAddresses' endpoint (#3854) For https://github.com/status-im/status-desktop/issues/11143 --- protocol/messenger_communities.go | 25 +++++++++++++++++++++++++ services/ext/api.go | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 92623a645..a8848f242 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "golang.org/x/exp/slices" "golang.org/x/time/rate" gethcommon "github.com/ethereum/go-ethereum/common" @@ -4366,3 +4367,27 @@ func (m *Messenger) rekeyAllCommunities(logger *zap.Logger) { } } } + +func (m *Messenger) GetCommunityMembersForWalletAddresses(communityID types.HexBytes, chainID uint64) (map[string]*Contact, error) { + community, err := m.communitiesManager.GetByID(communityID) + if err != nil { + return nil, err + } + + membersForAddresses := map[string]*Contact{} + + for memberID, member := range community.Members() { + for _, revealedAddress := range member.RevealedAccounts { + if slices.Contains(revealedAddress.ChainIds, chainID) { + contact, ok := m.allContacts.Load(memberID) + if ok { + membersForAddresses[revealedAddress.Address] = contact + } else { + m.logger.Error("community member is not a contact", zap.String("contact ID", memberID)) + } + } + } + } + + return membersForAddresses, nil +} diff --git a/services/ext/api.go b/services/ext/api.go index efa9a73e3..4ad6eb482 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -459,7 +459,11 @@ func (api *PublicAPI) ImportCommunity(ctx context.Context, hexPrivateKey string) func (api *PublicAPI) GetCommunityPublicKeyFromPrivateKey(ctx context.Context, hexPrivateKey string) string { publicKey := api.service.messenger.GetCommunityIDFromKey(hexPrivateKey) return publicKey +} +// Get community members contact list for provided wallet addresses +func (api *PublicAPI) GetCommunityMembersForWalletAddresses(communityID types.HexBytes, chainID uint64) (map[string]*protocol.Contact, error) { + return api.service.messenger.GetCommunityMembersForWalletAddresses(communityID, chainID) } // Speeds up importing messages from archives