From 151edb36077cdbb9f1f2b89f32cd939ee873ed98 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 30 Jul 2021 13:05:44 -0400 Subject: [PATCH] feat: add api to delete a community channel (#2266) --- protocol/communities/manager.go | 29 +++++++++++++++++++++++++++++ protocol/messenger_communities.go | 22 ++++++++++++++++++++++ services/ext/api.go | 7 ++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 5dfb369c2..aa87ca3d8 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -341,6 +341,35 @@ func (m *Manager) EditChat(communityID types.HexBytes, chatID string, chat *prot return community, changes, nil } +func (m *Manager) DeleteChat(communityID types.HexBytes, chatID string) (*Community, *protobuf.CommunityDescription, error) { + community, err := m.GetByID(communityID) + 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()) { + chatID = strings.TrimPrefix(chatID, communityID.String()) + } + description, err := community.DeleteChat(chatID) + if err != nil { + return nil, nil, err + } + + err = m.persistence.SaveCommunity(community) + if err != nil { + return nil, nil, err + } + + // Advertise changes + m.publish(&Subscription{Community: community}) + + return community, description, nil +} + func (m *Manager) CreateCategory(request *requests.CreateCommunityCategory) (*Community, *CommunityChanges, error) { community, err := m.GetByID(request.CommunityID) if err != nil { diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index c86bc9c5f..8dae8ec32 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -454,6 +454,28 @@ func (m *Messenger) EditCommunityChat(communityID types.HexBytes, chatID string, return &response, m.saveChats(chats) } +func (m *Messenger) DeleteCommunityChat(communityID types.HexBytes, chatID string) (*MessengerResponse, error) { + response := &MessengerResponse{} + + community, _, err := m.communitiesManager.DeleteChat(communityID, chatID) + if err != nil { + return nil, err + } + err = m.deleteChat(chatID) + if err != nil { + return nil, err + } + response.AddRemovedChat(chatID) + + _, err = m.transport.RemoveFilterByChatID(chatID) + if err != nil { + return nil, err + } + + response.AddCommunity(community) + return response, nil +} + func (m *Messenger) CreateCommunity(request *requests.CreateCommunity) (*MessengerResponse, error) { if err := request.Validate(); err != nil { return nil, err diff --git a/services/ext/api.go b/services/ext/api.go index b2f7e994f..916131adf 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -379,11 +379,16 @@ func (api *PublicAPI) CreateCommunityChat(communityID types.HexBytes, c *protobu return api.service.messenger.CreateCommunityChat(communityID, c) } -// CreateCommunityChat creates a community chat in the given community +// EditCommunityChat edits a community chat in the given community func (api *PublicAPI) EditCommunityChat(communityID types.HexBytes, chatID string, c *protobuf.CommunityChat) (*protocol.MessengerResponse, error) { return api.service.messenger.EditCommunityChat(communityID, chatID, c) } +// DeleteCommunityChat deletes a community chat in the given community +func (api *PublicAPI) DeleteCommunityChat(communityID types.HexBytes, chatID string) (*protocol.MessengerResponse, error) { + return api.service.messenger.DeleteCommunityChat(communityID, chatID) +} + // InviteUsersToCommunity invites the users with pks to the community with ID func (api *PublicAPI) InviteUsersToCommunity(request *requests.InviteUsersToCommunity) (*protocol.MessengerResponse, error) { return api.service.messenger.InviteUsersToCommunity(request)