From 2a5dc6dec09d141c0a83b69835aac01391ea869a Mon Sep 17 00:00:00 2001 From: Patryk Osmaczko Date: Tue, 9 Jan 2024 22:47:37 +0100 Subject: [PATCH] chore: remove boilerplate check of `communities.GetByID` clients motivated by: https://github.com/status-im/status-go/pull/4514#discussion_r1445808116 --- protocol/communities/manager.go | 122 ++---------------- protocol/communities_messenger_test.go | 2 +- protocol/messenger.go | 4 - protocol/messenger_communities.go | 4 - .../messenger_communities_import_discord.go | 9 -- protocol/messenger_community_metrics.go | 5 - protocol/messenger_handler.go | 2 +- protocol/messenger_share_urls.go | 4 - .../messenger_store_node_request_manager.go | 2 +- 9 files changed, 15 insertions(+), 139 deletions(-) diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 1f217e1d2..681830766 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -834,9 +834,6 @@ func (m *Manager) EditCommunityTokenPermission(request *requests.EditCommunityTo if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } tokenPermission := request.ToCommunityTokenPermission() @@ -1023,9 +1020,6 @@ func (m *Manager) DeleteCommunityTokenPermission(request *requests.DeleteCommuni if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } changes, err := community.DeleteTokenPermission(request.PermissionID) if err != nil { @@ -1078,9 +1072,6 @@ func (m *Manager) SetShard(communityID types.HexBytes, shard *shard.Shard) (*Com if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } if !community.IsControlNode() { return nil, errors.New("not admin or owner") } @@ -1120,9 +1111,6 @@ func (m *Manager) EditCommunity(request *requests.EditCommunity) (*Community, er if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } newDescription, err := request.ToCommunityDescription() if err != nil { @@ -1215,7 +1203,7 @@ func (m *Manager) ImportCommunity(key *ecdsa.PrivateKey, clock uint64) (*Communi communityID := crypto.CompressPubkey(&key.PublicKey) community, err := m.GetByID(communityID) - if err != nil { + if err != nil && err != ErrOrgNotFound { return nil, err } @@ -1287,9 +1275,6 @@ func (m *Manager) CreateChat(communityID types.HexBytes, chat *protobuf.Communit if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } chatID := uuid.New().String() if thirdPartyID != "" { chatID = chatID + thirdPartyID @@ -1313,9 +1298,6 @@ func (m *Manager) EditChat(communityID types.HexBytes, chatID string, chat *prot if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } // Remove communityID prefix from chatID if exists if strings.HasPrefix(chatID, communityID.String()) { @@ -1340,9 +1322,6 @@ func (m *Manager) DeleteChat(communityID types.HexBytes, chatID string) (*Commun if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } // Remove communityID prefix from chatID if exists if strings.HasPrefix(chatID, communityID.String()) { @@ -1366,9 +1345,6 @@ func (m *Manager) CreateCategory(request *requests.CreateCommunityCategory, publ if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } categoryID := uuid.New().String() if request.ThirdPartyID != "" { @@ -1400,9 +1376,6 @@ func (m *Manager) EditCategory(request *requests.EditCommunityCategory) (*Commun if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } // Remove communityID prefix from chatID if exists for i, cid := range request.ChatIDs { @@ -1429,9 +1402,6 @@ func (m *Manager) EditChatFirstMessageTimestamp(communityID types.HexBytes, chat if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } // Remove communityID prefix from chatID if exists if strings.HasPrefix(chatID, communityID.String()) { @@ -1459,9 +1429,6 @@ func (m *Manager) ReorderCategories(request *requests.ReorderCommunityCategories if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } changes, err := community.ReorderCategories(request.CategoryID, request.Position) if err != nil { @@ -1481,9 +1448,6 @@ func (m *Manager) ReorderChat(request *requests.ReorderCommunityChat) (*Communit if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } // Remove communityID prefix from chatID if exists if strings.HasPrefix(request.ChatID, request.CommunityID.String()) { @@ -1508,9 +1472,6 @@ func (m *Manager) DeleteCategory(request *requests.DeleteCommunityCategory) (*Co if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } changes, err := community.DeleteCategory(request.CategoryID) if err != nil { @@ -1591,7 +1552,7 @@ func (m *Manager) HandleCommunityDescriptionMessage(signer *ecdsa.PublicKey, des } community, err := m.GetByID(id) - if err != nil { + if err != nil && err != ErrOrgNotFound { return nil, err } @@ -1826,10 +1787,6 @@ func (m *Manager) HandleCommunityEventsMessage(signer *ecdsa.PublicKey, message return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } - if !community.IsPrivilegedMember(signer) { return nil, errors.New("user has not permissions to send events") } @@ -1910,9 +1867,6 @@ func (m *Manager) HandleCommunityEventsMessageRejected(signer *ecdsa.PublicKey, if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } eventsMessage, err := CommunityEventsMessageFromProtobuf(message.Msg) if err != nil { @@ -2407,9 +2361,6 @@ func (m *Manager) HandleCommunityCancelRequestToJoin(signer *ecdsa.PublicKey, re if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } previousRequestToJoin, err := m.GetRequestToJoinByPkAndCommunityID(signer, community.ID()) if err != nil { @@ -2458,9 +2409,6 @@ func (m *Manager) HandleCommunityRequestToJoin(signer *ecdsa.PublicKey, receiver if err != nil { return nil, nil, err } - if community == nil { - return nil, nil, ErrOrgNotFound - } err = community.ValidateRequestToJoin(signer, request) if err != nil { @@ -2578,9 +2526,7 @@ func (m *Manager) HandleCommunityEditSharedAddresses(signer *ecdsa.PublicKey, re if err != nil { return err } - if community == nil { - return ErrOrgNotFound - } + if err := community.ValidateEditSharedAddresses(signer, request); err != nil { return err } @@ -2876,9 +2822,6 @@ func (m *Manager) HandleCommunityRequestToJoinResponse(signer *ecdsa.PublicKey, if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } communityDescriptionBytes, err := proto.Marshal(request.Community) if err != nil { @@ -2989,9 +2932,6 @@ func (m *Manager) JoinCommunity(id types.HexBytes, forceJoin bool) (*Community, if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } if !forceJoin && community.Joined() { // Nothing to do, we are already joined return community, ErrOrgAlreadyJoined @@ -3009,9 +2949,6 @@ func (m *Manager) SpectateCommunity(id types.HexBytes) (*Community, error) { if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } community.Spectate() if err = m.persistence.SaveCommunity(community); err != nil { return nil, err @@ -3071,9 +3008,6 @@ func (m *Manager) LeaveCommunity(id types.HexBytes) (*Community, error) { if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } community.RemoveOurselvesFromOrg(&m.identity.PublicKey) community.Leave() @@ -3091,9 +3025,6 @@ func (m *Manager) KickedOutOfCommunity(id types.HexBytes) (*Community, error) { if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } community.RemoveOurselvesFromOrg(&m.identity.PublicKey) community.Leave() @@ -3111,9 +3042,6 @@ func (m *Manager) AddMemberOwnerToCommunity(communityID types.HexBytes, pk *ecds if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } _, err = community.AddMember(pk, []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER}) if err != nil { @@ -3134,9 +3062,6 @@ func (m *Manager) RemoveUserFromCommunity(id types.HexBytes, pk *ecdsa.PublicKey if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } _, err = community.RemoveUserFromOrg(pk) if err != nil { @@ -3162,9 +3087,6 @@ func (m *Manager) UnbanUserFromCommunity(request *requests.UnbanUserFromCommunit if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } _, err = community.UnbanUserFromCommunity(publicKey) if err != nil { @@ -3190,9 +3112,6 @@ func (m *Manager) AddRoleToMember(request *requests.AddRoleToMember) (*Community if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } if !community.hasMember(publicKey) { return nil, ErrMemberNotFound @@ -3224,9 +3143,6 @@ func (m *Manager) RemoveRoleFromMember(request *requests.RemoveRoleFromMember) ( if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } if !community.hasMember(publicKey) { return nil, ErrMemberNotFound @@ -3259,9 +3175,6 @@ func (m *Manager) BanUserFromCommunity(request *requests.BanUserFromCommunity) ( if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } _, err = community.BanUserFromCommunity(publicKey) if err != nil { @@ -3307,7 +3220,14 @@ func (m *Manager) dbRecordBundleToCommunity(r *CommunityRecordBundle) (*Communit } func (m *Manager) GetByID(id []byte) (*Community, error) { - return m.persistence.GetByID(&m.identity.PublicKey, id) + community, err := m.persistence.GetByID(&m.identity.PublicKey, id) + if err != nil { + return nil, err + } + if community == nil { + return nil, ErrOrgNotFound + } + return community, nil } func (m *Manager) GetByIDString(idString string) (*Community, error) { @@ -3358,9 +3278,6 @@ func (m *Manager) CheckCommunityForJoining(communityID types.HexBytes) (*Communi if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } // We don't allow requesting access if already joined if community.Joined() { @@ -3469,9 +3386,6 @@ func (m *Manager) CanPost(pk *ecdsa.PublicKey, communityID string, chatID string if err != nil { return false, err } - if community == nil { - return false, nil - } return community.CanPost(pk, chatID, grant) } @@ -4548,13 +4462,10 @@ func (m *Manager) ImageToBase64(uri string) string { func (m *Manager) SaveCommunityToken(token *community_token.CommunityToken, croppedImage *images.CroppedImage) (*community_token.CommunityToken, error) { - community, err := m.GetByIDString(token.CommunityID) + _, err := m.GetByIDString(token.CommunityID) if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } if croppedImage != nil && croppedImage.ImagePath != "" { bytes, err := images.OpenAndAdjustImage(*croppedImage, true) @@ -4584,9 +4495,6 @@ func (m *Manager) AddCommunityToken(token *community_token.CommunityToken, clock if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } if !community.MemberCanManageToken(&m.identity.PublicKey, token) { return nil, ErrInvalidManageTokensPermission @@ -4669,9 +4577,6 @@ func (m *Manager) SetCommunityActiveMembersCount(communityID string, activeMembe if err != nil { return err } - if community == nil { - return ErrOrgNotFound - } updated, err := community.SetActiveMembersCount(activeMembersCount) if err != nil { @@ -5150,9 +5055,6 @@ func (m *Manager) CreateCommunityTokenDeploymentSignature(ctx context.Context, c if err != nil { return nil, err } - if community == nil { - return nil, ErrOrgNotFound - } if !community.IsControlNode() { return nil, ErrNotControlNode } diff --git a/protocol/communities_messenger_test.go b/protocol/communities_messenger_test.go index 21a79584c..f57bf2864 100644 --- a/protocol/communities_messenger_test.go +++ b/protocol/communities_messenger_test.go @@ -3090,7 +3090,7 @@ func (s *MessengerCommunitiesSuite) TestCheckCommunitiesToUnmute() { func (s *MessengerCommunitiesSuite) TestCommunityNotInDB() { community, err := s.alice.communitiesManager.GetByID([]byte("0x123")) - s.Require().Nil(err) + s.Require().ErrorIs(err, communities.ErrOrgNotFound) s.Require().Nil(community) } diff --git a/protocol/messenger.go b/protocol/messenger.go index acc462907..60e581a8e 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -2310,10 +2310,6 @@ func (m *Messenger) sendChatMessage(ctx context.Context, message *common.Message return nil, err } - if community == nil { - return nil, errors.New("community not found") - } - wrappedCommunity, err := community.ToProtocolMessageBytes() if err != nil { return nil, err diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index ce91dc916..c38f368f1 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -2837,10 +2837,6 @@ func (m *Messenger) handleCommunityPrivilegedUserSyncMessage(state *ReceivedMess return err } - if community == nil { - return communities.ErrOrgNotFound - } - // Currently this type of msg coming from the control node. // If it will change in the future, check that events types starting from // CONTROL_NODE were sent by a control node diff --git a/protocol/messenger_communities_import_discord.go b/protocol/messenger_communities_import_discord.go index c1e295d82..c396216a2 100644 --- a/protocol/messenger_communities_import_discord.go +++ b/protocol/messenger_communities_import_discord.go @@ -553,15 +553,6 @@ func (m *Messenger) RequestImportDiscordChannel(request *requests.ImportDiscordC return } - if community == nil { - errmsg := fmt.Sprintf("Couldn't get the community by id: '%s'", request.CommunityID) - importProgress.AddTaskError(discord.ChannelsCreationTask, discord.Error(errmsg)) - importProgress.StopTask(discord.ChannelsCreationTask) - progressUpdates <- importProgress - cancel <- []string{request.CommunityID.String(), "", request.DiscordChannelID} - return - } - importProgress.UpdateTaskProgress(discord.ChannelsCreationTask, progressValue) progressUpdates <- importProgress diff --git a/protocol/messenger_community_metrics.go b/protocol/messenger_community_metrics.go index 9e00ad06f..5bf3cd9b5 100644 --- a/protocol/messenger_community_metrics.go +++ b/protocol/messenger_community_metrics.go @@ -1,7 +1,6 @@ package protocol import ( - "errors" "fmt" "sort" @@ -27,10 +26,6 @@ func (m *Messenger) getChatIdsForCommunity(communityID types.HexBytes) ([]string if err != nil { return []string{}, err } - - if community == nil { - return []string{}, errors.New("no community found") - } return community.ChatIDs(), nil } diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index cc70c99f4..7f61132cd 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -1288,7 +1288,7 @@ func (m *Messenger) HandleHistoryArchiveMagnetlinkMessage(state *ReceivedMessage id := types.HexBytes(crypto.CompressPubkey(communityPubKey)) community, err := m.communitiesManager.GetByID(id) - if err != nil { + if err != nil && err != communities.ErrOrgNotFound { m.logger.Debug("Couldn't get community for community with id: ", zap.Any("id", id)) return err } diff --git a/protocol/messenger_share_urls.go b/protocol/messenger_share_urls.go index b8fd05874..666a98217 100644 --- a/protocol/messenger_share_urls.go +++ b/protocol/messenger_share_urls.go @@ -154,10 +154,6 @@ func (m *Messenger) ShareCommunityURLWithData(communityID types.HexBytes) (strin return "", err } - if community == nil { - return "", fmt.Errorf("community with communityID %s not found", communityID) - } - data, shortKey, err := m.prepareEncodedCommunityData(community) if err != nil { return "", err diff --git a/protocol/messenger_store_node_request_manager.go b/protocol/messenger_store_node_request_manager.go index 6702e13e4..3d8927a1c 100644 --- a/protocol/messenger_store_node_request_manager.go +++ b/protocol/messenger_store_node_request_manager.go @@ -400,7 +400,7 @@ func (r *storeNodeRequest) shouldFetchNextPage(envelopesCount int) (bool, uint32 community, err := r.manager.messenger.communitiesManager.GetByID(communityID) - if err != nil { + if err != nil && err != communities.ErrOrgNotFound { logger.Error("failed to read community from database", zap.String("communityID", r.requestID.DataID), zap.Error(err))