fix(communities)_: spectated communities new channels member list (#5190)

This commit is contained in:
Mikhail Rogachev 2024-05-22 21:43:06 +02:00 committed by GitHub
parent 97af3f7e52
commit c8dfbe682d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 0 deletions

View File

@ -495,12 +495,24 @@ func CreateCommunityChat(orgID, chatID string, orgChat *protobuf.CommunityChat,
}
timestamp := timesource.GetCurrentTime()
// Populate community _channel_ members to _chat_ members
chatMembers := []ChatMember{}
for pubKey := range orgChat.Members {
chatMember := ChatMember{
ID: pubKey,
Admin: false,
}
chatMembers = append(chatMembers, chatMember)
}
return &Chat{
CommunityID: orgID,
CategoryID: orgChat.CategoryId,
HideIfPermissionsNotMet: orgChat.HideIfPermissionsNotMet,
Name: orgChat.Identity.DisplayName,
Description: orgChat.Identity.Description,
Members: chatMembers,
Active: true,
Color: color,
Emoji: orgChat.Identity.Emoji,
@ -570,6 +582,7 @@ func CreatePublicChat(name string, timesource common.TimeSource) *Chat {
ReadMessagesAtClockValue: 0,
Color: chatColors[rand.Intn(len(chatColors))], // nolint: gosec
ChatType: ChatTypePublic,
Members: []ChatMember{},
}
}

View File

@ -4337,3 +4337,73 @@ func (s *MessengerCommunitiesSuite) TestMemberMessagesHasImageLink() {
requireMessageWithImage(s.alice, alicePubKey, communityID)
requireMessageWithImage(s.bob, alicePubKey, communityID)
}
func (s *MessengerCommunitiesSuite) TestOpenAndNotJoinedCommunityNewChannelIsNotEmpty() {
// Create an open community
community, _ := s.createCommunity()
s.Require().Len(community.Chats(), 1)
s.Require().False(community.Encrypted())
// Bob joins the community
advertiseCommunityTo(&s.Suite, community, s.owner, s.bob)
request := &requests.RequestToJoinCommunity{CommunityID: community.ID()}
joinCommunity(&s.Suite, community, s.owner, s.bob, request, "")
// Alice just observes the community
advertiseCommunityTo(&s.Suite, community, s.owner, s.alice)
_, err := s.alice.SpectateCommunity(community.ID())
s.Require().NoError(err)
aliceCommunity, err := s.alice.GetCommunityByID(community.ID())
s.Require().NoError(err)
s.Require().Len(aliceCommunity.Chats(), 1)
// Owner creates a new channel
newChannel := &protobuf.CommunityChat{
Permissions: &protobuf.CommunityPermissions{
Access: protobuf.CommunityPermissions_AUTO_ACCEPT,
},
Identity: &protobuf.ChatIdentity{
DisplayName: "new channel",
Emoji: "",
Description: "chat created after joining the community",
},
}
response, err := s.owner.CreateCommunityChat(community.ID(), newChannel)
s.Require().NoError(err)
s.Require().Len(response.CommunityChanges, 1)
s.Require().Len(response.CommunityChanges[0].ChatsAdded, 1)
s.Require().Len(response.Communities(), 1)
s.Require().Len(response.Chats(), 1)
s.Require().Len(response.Chats()[0].Members, 2)
for _, chat := range response.Communities()[0].Chats() {
s.Require().Len(chat.Members, 2)
}
// Check Alice gets the correct member list for a new channel
_, err = WaitOnMessengerResponse(
s.alice,
func(r *MessengerResponse) bool {
if len(r.Chats()) == 1 && len(r.Communities()) > 0 {
for _, chat := range r.Chats() {
s.Require().Len(chat.Members, 2)
}
for _, chat := range r.Communities()[0].Chats() {
s.Require().Len(chat.Members, 2)
}
return true
}
return false
},
"no commiunity message for Alice",
)
s.Require().NoError(err)
aliceCommunity, err = s.alice.GetCommunityByID(community.ID())
s.Require().NoError(err)
s.Require().Len(aliceCommunity.Chats(), 2)
for _, chat := range aliceCommunity.Chats() {
s.Require().Len(chat.Members, 2)
}
}

View File

@ -517,6 +517,7 @@ func (db sqlitePersistence) Chat(chatID string) (*Chat, error) {
chat.UnviewedMentionsCount = uint(unviewedMentionsCount)
// Restore members
chat.Members = []ChatMember{}
membersDecoder := gob.NewDecoder(bytes.NewBuffer(encodedMembers))
err = membersDecoder.Decode(&chat.Members)
if err != nil {