Remove group members (#3011)

* remove list of members from group
This commit is contained in:
Omar Basem 2022-12-10 08:26:51 +04:00 committed by GitHub
parent cee5313e0e
commit d1a4b53d5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 20 deletions

View File

@ -1 +1 @@
0.115.6
0.116.0

View File

@ -114,10 +114,10 @@ func (m *Messenger) CreateGroupChatFromInvitation(name string, chatID string, ad
return &response, m.saveChat(&chat)
}
func (m *Messenger) RemoveMemberFromGroupChat(ctx context.Context, chatID string, member string) (*MessengerResponse, error) {
func (m *Messenger) RemoveMembersFromGroupChat(ctx context.Context, chatID string, members []string) (*MessengerResponse, error) {
var response MessengerResponse
logger := m.logger.With(zap.String("site", "RemoveMemberFromGroupChat"))
logger.Info("Removing member form group chat", zap.String("chatID", chatID), zap.String("member", member))
logger := m.logger.With(zap.String("site", "RemoveMembersFromGroupChat"))
logger.Info("Removing members form group chat", zap.String("chatID", chatID), zap.Any("members", members))
chat, ok := m.allChats.Load(chatID)
if !ok {
return nil, ErrChatNotFound
@ -136,6 +136,8 @@ func (m *Messenger) RemoveMemberFromGroupChat(ctx context.Context, chatID string
}
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
for _, member := range members {
// Remove member
event := v1protocol.NewMemberRemovedEvent(member, clock)
event.ChatID = chat.ID
@ -148,6 +150,7 @@ func (m *Messenger) RemoveMemberFromGroupChat(ctx context.Context, chatID string
if err != nil {
return nil, err
}
}
encodedMessage, err := m.sender.EncodeMembershipUpdate(group, nil)
if err != nil {

View File

@ -255,20 +255,26 @@ func (s *MessengerGroupChatSuite) TestGroupChatMembersRemoval() {
admin := s.startNewMessenger()
memberA := s.startNewMessenger()
memberB := s.startNewMessenger()
members := []string{common.PubkeyToHex(&memberA.identity.PublicKey), common.PubkeyToHex(&memberB.identity.PublicKey)}
memberC := s.startNewMessenger()
members := []string{common.PubkeyToHex(&memberA.identity.PublicKey), common.PubkeyToHex(&memberB.identity.PublicKey),
common.PubkeyToHex(&memberC.identity.PublicKey)}
s.makeMutualContacts(admin, memberA)
s.makeMutualContacts(admin, memberB)
s.makeMutualContacts(admin, memberC)
groupChat := s.createGroupChat(admin, "test_group_chat", members)
s.verifyGroupChatCreated(memberA, true)
s.verifyGroupChatCreated(memberB, true)
s.verifyGroupChatCreated(memberC, true)
_, err := memberA.RemoveMemberFromGroupChat(context.Background(), groupChat.ID, common.PubkeyToHex(&memberB.identity.PublicKey))
_, err := memberA.RemoveMembersFromGroupChat(context.Background(), groupChat.ID, []string{common.PubkeyToHex(&memberB.identity.PublicKey),
common.PubkeyToHex(&memberC.identity.PublicKey)})
s.Require().Error(err)
// only admin can remove members from the group
_, err = admin.RemoveMemberFromGroupChat(context.Background(), groupChat.ID, common.PubkeyToHex(&memberB.identity.PublicKey))
_, err = admin.RemoveMembersFromGroupChat(context.Background(), groupChat.ID, []string{common.PubkeyToHex(&memberB.identity.PublicKey),
common.PubkeyToHex(&memberC.identity.PublicKey)})
s.Require().NoError(err)
// ensure removal is propagated to other members
@ -285,6 +291,7 @@ func (s *MessengerGroupChatSuite) TestGroupChatMembersRemoval() {
defer s.NoError(admin.Shutdown())
defer s.NoError(memberA.Shutdown())
defer s.NoError(memberB.Shutdown())
defer s.NoError(memberC.Shutdown())
}
func (s *MessengerGroupChatSuite) TestGroupChatEdit() {

View File

@ -105,7 +105,7 @@ func (api *API) RemoveMember(ctx context.Context, communityID types.HexBytes, ch
}
return api.execAndGetGroupChatResponse(func() (*protocol.MessengerResponse, error) {
return api.s.messenger.RemoveMemberFromGroupChat(ctx, chatID, member)
return api.s.messenger.RemoveMembersFromGroupChat(ctx, chatID, []string{member})
})
}

View File

@ -219,7 +219,11 @@ func (api *PublicAPI) AddMembersToGroupChat(ctx Context, chatID string, members
}
func (api *PublicAPI) RemoveMemberFromGroupChat(ctx Context, chatID string, member string) (*protocol.MessengerResponse, error) {
return api.service.messenger.RemoveMemberFromGroupChat(ctx, chatID, member)
return api.service.messenger.RemoveMembersFromGroupChat(ctx, chatID, []string{member})
}
func (api *PublicAPI) RemoveMembersFromGroupChat(ctx Context, chatID string, members []string) (*protocol.MessengerResponse, error) {
return api.service.messenger.RemoveMembersFromGroupChat(ctx, chatID, members)
}
func (api *PublicAPI) AddAdminsToGroupChat(ctx Context, chatID string, members []string) (*protocol.MessengerResponse, error) {

View File

@ -289,7 +289,7 @@ func (api *NimbusPublicAPI) AddMembersToGroupChat(ctx Context, chatID string, me
}
func (api *NimbusPublicAPI) RemoveMemberFromGroupChat(ctx Context, chatID string, member string) (*protocol.MessengerResponse, error) {
return api.service.messenger.RemoveMemberFromGroupChat(ctx, chatID, member)
return api.service.messenger.RemoveMembersFromGroupChat(ctx, chatID, []string{member})
}
func (api *NimbusPublicAPI) AddAdminsToGroupChat(ctx Context, chatID string, members []string) (*protocol.MessengerResponse, error) {