feat(messenger): add `createDefaultChannel` flag to `CreateCommunity` API

This introduces a flag to configure whether `Messenger.CreateCommunity`
should create a default channel.

As discussed in https://github.com/status-im/status-go/issues/2758, this
is needed for the upcoming functionality to import discord communities.

Closes #2758
This commit is contained in:
Pascal Precht 2022-07-19 11:31:52 +02:00 committed by r4bbit.eth
parent 17efc5e782
commit 1ac88975b0
5 changed files with 71 additions and 37 deletions

View File

@ -142,6 +142,37 @@ func (s *MessengerCommunitiesSuite) newMessenger() *Messenger {
return s.newMessengerWithKey(s.shh, privateKey) return s.newMessengerWithKey(s.shh, privateKey)
} }
func (s *MessengerCommunitiesSuite) TestCreateCommunity() {
description := &requests.CreateCommunity{
Membership: protobuf.CommunityPermissions_NO_MEMBERSHIP,
Name: "status",
Color: "#ffffff",
Description: "status community description",
}
response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1)
s.Require().Len(response.Chats(), 1)
}
func (s *MessengerCommunitiesSuite) TestCreateCommunity_WithoutDefaultChannel() {
description := &requests.CreateCommunity{
Membership: protobuf.CommunityPermissions_NO_MEMBERSHIP,
Name: "status",
Color: "#ffffff",
Description: "status community description",
}
response, err := s.bob.CreateCommunity(description, false)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1)
s.Require().Len(response.Chats(), 0)
}
func (s *MessengerCommunitiesSuite) TestRetrieveCommunity() { func (s *MessengerCommunitiesSuite) TestRetrieveCommunity() {
alice := s.newMessenger() alice := s.newMessenger()
@ -152,7 +183,7 @@ func (s *MessengerCommunitiesSuite) TestRetrieveCommunity() {
Description: "status community description", Description: "status community description",
} }
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -210,7 +241,7 @@ func (s *MessengerCommunitiesSuite) TestJoinCommunity() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -408,7 +439,7 @@ func (s *MessengerCommunitiesSuite) TestCommunityContactCodeAdvertisement() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
@ -494,7 +525,7 @@ func (s *MessengerCommunitiesSuite) TestInviteUsersToCommunity() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -548,7 +579,7 @@ func (s *MessengerCommunitiesSuite) TestPostToCommunityChat() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -669,7 +700,7 @@ func (s *MessengerCommunitiesSuite) TestImportCommunity() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -740,7 +771,7 @@ func (s *MessengerCommunitiesSuite) TestRequestAccess() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -903,7 +934,7 @@ func (s *MessengerCommunitiesSuite) TestRequestAccessAgain() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -1156,7 +1187,7 @@ func (s *MessengerCommunitiesSuite) TestShareCommunity() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -1204,7 +1235,7 @@ func (s *MessengerCommunitiesSuite) TestBanUser() {
} }
// Create an community chat // Create an community chat
response, err := s.bob.CreateCommunity(description) response, err := s.bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)
@ -1274,7 +1305,7 @@ func (s *MessengerCommunitiesSuite) TestSyncCommunitySettings() {
Description: "new community description", Description: "new community description",
} }
mr, err := s.alice.CreateCommunity(createCommunityReq) mr, err := s.alice.CreateCommunity(createCommunityReq, true)
s.Require().NoError(err, "s.alice.CreateCommunity") s.Require().NoError(err, "s.alice.CreateCommunity")
var newCommunity *communities.Community var newCommunity *communities.Community
for _, com := range mr.Communities() { for _, com := range mr.Communities() {
@ -1335,7 +1366,7 @@ func (s *MessengerCommunitiesSuite) TestSyncCommunitySettings_EditCommunity() {
Description: "new community description", Description: "new community description",
} }
mr, err := s.alice.CreateCommunity(createCommunityReq) mr, err := s.alice.CreateCommunity(createCommunityReq, true)
s.Require().NoError(err, "s.alice.CreateCommunity") s.Require().NoError(err, "s.alice.CreateCommunity")
var newCommunity *communities.Community var newCommunity *communities.Community
for _, com := range mr.Communities() { for _, com := range mr.Communities() {
@ -1436,7 +1467,7 @@ func (s *MessengerCommunitiesSuite) TestSyncCommunity() {
Description: "new community description", Description: "new community description",
} }
mr, err := s.alice.CreateCommunity(createCommunityReq) mr, err := s.alice.CreateCommunity(createCommunityReq, true)
s.Require().NoError(err, "s.alice.CreateCommunity") s.Require().NoError(err, "s.alice.CreateCommunity")
var newCommunity *communities.Community var newCommunity *communities.Community
for _, com := range mr.Communities() { for _, com := range mr.Communities() {
@ -1531,7 +1562,7 @@ func (s *MessengerCommunitiesSuite) TestSyncCommunity_RequestToJoin() {
Color: "#000000", Color: "#000000",
Description: "new community description", Description: "new community description",
} }
mr, err := s.bob.CreateCommunity(createCommunityReq) mr, err := s.bob.CreateCommunity(createCommunityReq, true)
s.Require().NoError(err, "CreateCommunity") s.Require().NoError(err, "CreateCommunity")
s.Require().NotNil(mr) s.Require().NotNil(mr)
s.Len(mr.Communities(), 1) s.Len(mr.Communities(), 1)
@ -1767,7 +1798,7 @@ func (s *MessengerCommunitiesSuite) TestSyncCommunity_Leave() {
Color: "#000000", Color: "#000000",
Description: "new community description", Description: "new community description",
} }
mr, err := s.bob.CreateCommunity(createCommunityReq) mr, err := s.bob.CreateCommunity(createCommunityReq, true)
s.Require().NoError(err, "CreateCommunity") s.Require().NoError(err, "CreateCommunity")
s.Require().NotNil(mr) s.Require().NotNil(mr)
s.Len(mr.Communities(), 1) s.Len(mr.Communities(), 1)
@ -1863,7 +1894,7 @@ func (s *MessengerCommunitiesSuite) TestSetMutePropertyOnChatsByCategory() {
Description: "new community description", Description: "new community description",
} }
mr, err := s.alice.CreateCommunity(createCommunityReq) mr, err := s.alice.CreateCommunity(createCommunityReq, true)
s.Require().NoError(err, "s.alice.CreateCommunity") s.Require().NoError(err, "s.alice.CreateCommunity")
var newCommunity *communities.Community var newCommunity *communities.Community
for _, com := range mr.Communities() { for _, com := range mr.Communities() {

View File

@ -404,7 +404,7 @@ func (s *MessengerBackupSuite) TestBackupCommunities() {
} }
// Create a community chat // Create a community chat
response, err := bob1.CreateCommunity(description) response, err := bob1.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)

View File

@ -739,11 +739,13 @@ func (m *Messenger) DeleteCommunityChat(communityID types.HexBytes, chatID strin
return response, nil return response, nil
} }
func (m *Messenger) CreateCommunity(request *requests.CreateCommunity) (*MessengerResponse, error) { func (m *Messenger) CreateCommunity(request *requests.CreateCommunity, createDefaultChannel bool) (*MessengerResponse, error) {
if err := request.Validate(); err != nil { if err := request.Validate(); err != nil {
return nil, err return nil, err
} }
response := &MessengerResponse{}
community, err := m.communitiesManager.CreateCommunity(request) community, err := m.communitiesManager.CreateCommunity(request)
if err != nil { if err != nil {
return nil, err return nil, err
@ -770,22 +772,25 @@ func (m *Messenger) CreateCommunity(request *requests.CreateCommunity) (*Messeng
return nil, err return nil, err
} }
chatResponse, err := m.CreateCommunityChat(community.ID(), &protobuf.CommunityChat{ if createDefaultChannel {
Identity: &protobuf.ChatIdentity{ chatResponse, err := m.CreateCommunityChat(community.ID(), &protobuf.CommunityChat{
DisplayName: "general", Identity: &protobuf.ChatIdentity{
Description: "General channel for the community", DisplayName: "general",
Color: community.Description().Identity.Color, Description: "General channel for the community",
}, Color: community.Description().Identity.Color,
Permissions: &protobuf.CommunityPermissions{ },
Access: protobuf.CommunityPermissions_NO_MEMBERSHIP, Permissions: &protobuf.CommunityPermissions{
}, Access: protobuf.CommunityPermissions_NO_MEMBERSHIP,
}) },
if err != nil { })
return nil, err if err != nil {
} return nil, err
}
// updating community so it contains the general chat // updating community so it contains the general chat
community = chatResponse.Communities()[0] community = chatResponse.Communities()[0]
response.AddChat(chatResponse.Chats()[0])
}
if request.Encrypted { if request.Encrypted {
// Init hash ratchet for community // Init hash ratchet for community
@ -796,10 +801,8 @@ func (m *Messenger) CreateCommunity(request *requests.CreateCommunity) (*Messeng
} }
} }
response := &MessengerResponse{}
response.AddCommunity(community) response.AddCommunity(community)
response.AddCommunitySettings(&communitySettings) response.AddCommunitySettings(&communitySettings)
response.AddChat(chatResponse.Chats()[0])
err = m.syncCommunity(context.Background(), community) err = m.syncCommunity(context.Background(), community)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -906,7 +906,7 @@ func (s *MessengerPushNotificationSuite) TestReceivePushNotificationCommunityReq
Description: "status community description", Description: "status community description",
} }
response, err := bob.CreateCommunity(description) response, err := bob.CreateCommunity(description, true)
s.Require().NoError(err) s.Require().NoError(err)
s.Require().NotNil(response) s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1) s.Require().Len(response.Communities(), 1)

View File

@ -385,7 +385,7 @@ func (api *PublicAPI) LeaveCommunity(parent context.Context, communityID types.H
// CreateCommunity creates a new community with the provided description // CreateCommunity creates a new community with the provided description
func (api *PublicAPI) CreateCommunity(request *requests.CreateCommunity) (*protocol.MessengerResponse, error) { func (api *PublicAPI) CreateCommunity(request *requests.CreateCommunity) (*protocol.MessengerResponse, error) {
return api.service.messenger.CreateCommunity(request) return api.service.messenger.CreateCommunity(request, true)
} }
// EditCommunity edits an existing community with the provided description // EditCommunity edits an existing community with the provided description