From 45c5fd1cdf04baf7ad2eaf25c9435b29d5f7e99c Mon Sep 17 00:00:00 2001 From: Mykhailo Prakhov Date: Mon, 23 Jan 2023 13:21:11 +0200 Subject: [PATCH] chore: allow GetMembers for the communty by communityID only --- services/chat/api.go | 67 ++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/services/chat/api.go b/services/chat/api.go index 0008c18ba..b3dfebe45 100644 --- a/services/chat/api.go +++ b/services/chat/api.go @@ -19,6 +19,7 @@ import ( var ( ErrChatNotFound = errors.New("can't find chat") + ErrCommunityNotFound = errors.New("can't find community") ErrCommunitiesNotSupported = errors.New("communities are not supported") ErrChatTypeNotSupported = errors.New("chat type not supported") ) @@ -226,6 +227,10 @@ func (api *API) GetChat(ctx context.Context, communityID types.HexBytes, chatID return nil, err } + if messengerChat == nil { + return nil, ErrChatNotFound + } + result, err := api.toAPIChat(messengerChat, community, pubKey) if err != nil { return nil, err @@ -324,24 +329,26 @@ func (api *API) toAPIChat(protocolChat *protocol.Chat, community *communities.Co func getChatMembers(sourceChat *protocol.Chat, community *communities.Community, userPubKey string) (map[string]Member, error) { result := make(map[string]Member) - if sourceChat.ChatType == protocol.ChatTypePrivateGroupChat && len(sourceChat.Members) > 0 { - for _, m := range sourceChat.Members { - result[m.ID] = Member{ - Admin: m.Admin, + if sourceChat != nil { + if sourceChat.ChatType == protocol.ChatTypePrivateGroupChat && len(sourceChat.Members) > 0 { + for _, m := range sourceChat.Members { + result[m.ID] = Member{ + Admin: m.Admin, + Joined: true, + } + } + return result, nil + } + + if sourceChat.ChatType == protocol.ChatTypeOneToOne { + result[sourceChat.ID] = Member{ Joined: true, } + result[userPubKey] = Member{ + Joined: true, + } + return result, nil } - return result, nil - } - - if sourceChat.ChatType == protocol.ChatTypeOneToOne { - result[sourceChat.ID] = Member{ - Joined: true, - } - result[userPubKey] = Member{ - Joined: true, - } - return result, nil } if community != nil { @@ -363,6 +370,20 @@ func getChatMembers(sourceChat *protocol.Chat, community *communities.Community, return nil, nil } +func (api *API) getCommunityByID(id string) (*communities.Community, error) { + communityID, err := hexutil.Decode(id) + if err != nil { + return nil, err + } + + community, err := api.s.messenger.GetCommunityByID(communityID) + if community == nil && err == nil { + return nil, ErrCommunityNotFound + } + + return community, err +} + func (chat *Chat) populateCommunityFields(community *communities.Community) error { if community == nil { return nil @@ -398,7 +419,14 @@ func (api *API) getChatAndCommunity(pubKey string, communityID types.HexBytes, c } if len(communityID) != 0 { - fullChatID = string(communityID.Bytes()) + chatID + id := string(communityID.Bytes()) + + if chatID == "" { + community, err := api.getCommunityByID(id) + return nil, community, err + } + + fullChatID = id + chatID } messengerChat := api.s.messenger.Chat(fullChatID) @@ -408,12 +436,9 @@ func (api *API) getChatAndCommunity(pubKey string, communityID types.HexBytes, c var community *communities.Community if messengerChat.CommunityID != "" { - communityID, err := hexutil.Decode(messengerChat.CommunityID) - if err != nil { - return nil, nil, err - } + var err error + community, err = api.getCommunityByID(messengerChat.CommunityID) - community, err = api.s.messenger.GetCommunityByID(communityID) if err != nil { return nil, nil, err }