From 8bf03609fc0f76eb8488869db1a7c22c68408c01 Mon Sep 17 00:00:00 2001 From: Michal Iskierko Date: Wed, 28 Feb 2024 12:48:37 +0100 Subject: [PATCH] feat: Add HideIfPermissionsNotMet to CommunityChat struct Issue #13291 --- VERSION | 2 +- protocol/chat.go | 4 ++++ protocol/communities/community.go | 3 +++ protocol/communities/community_test.go | 11 +++++++---- protocol/communities_messenger_test.go | 2 ++ protocol/messenger_communities.go | 2 ++ protocol/messenger_communities_import_discord.go | 6 ++++-- protocol/protobuf/communities.proto | 1 + services/chat/api.go | 2 ++ 9 files changed, 26 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index a1cd603dc..eca6fc9b2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.176.11 +0.177.0 diff --git a/protocol/chat.go b/protocol/chat.go index 66e87f374..762d93458 100644 --- a/protocol/chat.go +++ b/protocol/chat.go @@ -160,6 +160,9 @@ type Chat struct { // Image of the chat in Base64 format Base64Image string `json:"image,omitempty"` + + // If true, the chat is invisible if permissions are not met + HideIfPermissionsNotMet bool `json:"hideIfPermissionsNotMet,omitempty"` } type ChatPreview struct { @@ -492,6 +495,7 @@ func CreateCommunityChat(orgID, chatID string, orgChat *protobuf.CommunityChat, return &Chat{ CommunityID: orgID, CategoryID: orgChat.CategoryId, + HideIfPermissionsNotMet: orgChat.HideIfPermissionsNotMet, Name: orgChat.Identity.DisplayName, Description: orgChat.Identity.Description, Active: true, diff --git a/protocol/communities/community.go b/protocol/communities/community.go index c55a3e2d7..604901c47 100644 --- a/protocol/communities/community.go +++ b/protocol/communities/community.go @@ -108,6 +108,7 @@ type CommunityChat struct { Position int `json:"position"` CategoryID string `json:"categoryID"` TokenGated bool `json:"tokenGated"` + HideIfPermissionsNotMet bool `json:"hideIfPermissionsNotMet"` } type CommunityCategory struct { @@ -197,6 +198,7 @@ func (o *Community) MarshalPublicAPIJSON() ([]byte, error) { ViewersCanPostReactions: c.ViewersCanPostReactions, TokenGated: o.channelEncrypted(id), CategoryID: c.CategoryId, + HideIfPermissionsNotMet: c.HideIfPermissionsNotMet, Position: int(c.Position), } communityItem.Chats[id] = chat @@ -337,6 +339,7 @@ func (o *Community) MarshalJSON() ([]byte, error) { ViewersCanPostReactions: c.ViewersCanPostReactions, TokenGated: o.channelEncrypted(id), CategoryID: c.CategoryId, + HideIfPermissionsNotMet: c.HideIfPermissionsNotMet, Position: int(c.Position), } communityItem.Chats[id] = chat diff --git a/protocol/communities/community_test.go b/protocol/communities/community_test.go index 605d9adc0..43616a235 100644 --- a/protocol/communities/community_test.go +++ b/protocol/communities/community_test.go @@ -176,8 +176,9 @@ func (s *CommunitySuite) TestEditChat() { } _, err := org.CreateChat(newChatID, &protobuf.CommunityChat{ - Identity: identity, - Permissions: permissions, + Identity: identity, + Permissions: permissions, + HideIfPermissionsNotMet: false, }) s.Require().NoError(err) @@ -203,8 +204,9 @@ func (s *CommunitySuite) TestEditChat() { org.config.PrivateKey = s.identity org.config.ID = &s.identity.PublicKey editChanges, err := org.EditChat(newChatID, &protobuf.CommunityChat{ - Identity: editedIdentity, - Permissions: editedPermissions, + Identity: editedIdentity, + Permissions: editedPermissions, + HideIfPermissionsNotMet: true, }) s.Require().NoError(err) @@ -218,6 +220,7 @@ func (s *CommunitySuite) TestEditChat() { s.Require().NotNil(editChanges.ChatsModified[newChatID]) s.Require().Equal(editChanges.ChatsModified[newChatID].ChatModified.Identity, editedIdentity) s.Require().Equal(editChanges.ChatsModified[newChatID].ChatModified.Permissions, editedPermissions) + s.Require().Equal(editChanges.ChatsModified[newChatID].ChatModified.HideIfPermissionsNotMet, true) } func (s *CommunitySuite) TestDeleteChat() { diff --git a/protocol/communities_messenger_test.go b/protocol/communities_messenger_test.go index 8f78426ea..52feb598c 100644 --- a/protocol/communities_messenger_test.go +++ b/protocol/communities_messenger_test.go @@ -322,6 +322,7 @@ func (s *MessengerCommunitiesSuite) TestJoinCommunity() { Emoji: "😎", Description: "status-core community chat", }, + HideIfPermissionsNotMet: true, } response, err = s.bob.CreateCommunityChat(community.ID(), orgChat) s.Require().NoError(err) @@ -338,6 +339,7 @@ func (s *MessengerCommunitiesSuite) TestJoinCommunity() { s.Require().True(createdChat.Active) s.Require().NotEmpty(createdChat.Timestamp) s.Require().True(strings.HasPrefix(createdChat.ID, community.IDString())) + s.Require().True(createdChat.HideIfPermissionsNotMet) // Make sure the changes are reflect in the community community = response.Communities()[0] diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index dd84852be..fbd75188d 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -2950,11 +2950,13 @@ func (m *Messenger) handleCommunityResponse(state *ReceivedMessageState, communi oldChat.Description != chat.Description || oldChat.Emoji != chat.Emoji || oldChat.Color != chat.Color || + oldChat.HideIfPermissionsNotMet != chat.HideIfPermissionsNotMet || oldChat.UpdateFirstMessageTimestamp(chat.FirstMessageTimestamp) { oldChat.Name = chat.Name oldChat.Description = chat.Description oldChat.Emoji = chat.Emoji oldChat.Color = chat.Color + oldChat.HideIfPermissionsNotMet = chat.HideIfPermissionsNotMet // TODO(samyoul) remove storing of an updated reference pointer? state.AllChats.Store(chat.ID, oldChat) state.Response.AddChat(chat) diff --git a/protocol/messenger_communities_import_discord.go b/protocol/messenger_communities_import_discord.go index e34236d2b..f9e0f61d9 100644 --- a/protocol/messenger_communities_import_discord.go +++ b/protocol/messenger_communities_import_discord.go @@ -500,7 +500,8 @@ func createCommunityChannelForImport(request *requests.ImportDiscordChannel) *pr Description: request.Description, Color: request.Color, }, - CategoryId: "", + CategoryId: "", + HideIfPermissionsNotMet: false, } } @@ -1210,7 +1211,8 @@ func (m *Messenger) RequestImportDiscordCommunity(request *requests.ImportDiscor Description: channel.Channel.Description, Color: discordCommunity.Color(), }, - CategoryId: processedCategoriesIds[channel.Channel.CategoryID], + CategoryId: processedCategoriesIds[channel.Channel.CategoryID], + HideIfPermissionsNotMet: false, } // We call `CreateChat` on `communitiesManager` directly to get more control diff --git a/protocol/protobuf/communities.proto b/protocol/protobuf/communities.proto index fd3f8d573..fb7dd550c 100644 --- a/protocol/protobuf/communities.proto +++ b/protocol/protobuf/communities.proto @@ -129,6 +129,7 @@ message CommunityChat { string category_id = 4; int32 position = 5; bool viewers_can_post_reactions = 6; + bool hide_if_permissions_not_met = 7; } message CommunityCategory { diff --git a/services/chat/api.go b/services/chat/api.go index 3dccf3fe4..3a6d8875f 100644 --- a/services/chat/api.go +++ b/services/chat/api.go @@ -84,6 +84,7 @@ type Chat struct { CanPostReactions bool `json:"canPostReactions"` ViewersCanPostReactions bool `json:"viewersCanPostReactions"` Base64Image string `json:"image,omitempty"` + HideIfPermissionsNotMet bool `json:"hideIfPermissionsNotMet,omitempty"` } type ChannelGroup struct { @@ -501,6 +502,7 @@ func (chat *Chat) populateCommunityFields(community *communities.Community) erro } chat.CategoryID = commChat.CategoryId + chat.HideIfPermissionsNotMet = commChat.HideIfPermissionsNotMet chat.Position = commChat.Position chat.Permissions = commChat.Permissions chat.Emoji = commChat.Identity.Emoji