public KickParticipantsResponse KickParticipants(ISession session, KickParticipantsRequest request)
        {
            var response             = request.CreateResponse <KickParticipantsResponse>();
            var groupChat            = _groupChatsRepository.GetChat(request.GroupId);
            var originalParticipants = groupChat.Participants.ToList();

            if (!HasAccess(groupChat, session.UserId))
            {
                response.Success = false;
                response.Error   = Errors.OperationIsNotPermitted;
                return(response);
            }

            var leftParticipants = new List <GroupChatParticipant>();

            foreach (var participantId in request.ParticipantIds)
            {
                if (session.UserId != participantId && groupChat.OwnerId != session.UserId)
                {
                    response.Success = false;
                    response.Error   = Errors.KickParticipants_OnlyOwnerCanKickParticipants;
                    return(response);
                }

                var participant = new GroupChatParticipant {
                    UserId = participantId
                };
                if (groupChat.Participants.RemoveAll(r => r.UserId == participantId) > 0)
                {
                    leftParticipants.Add(participant);
                    _groupChatsRepository.RemoveGroupFromUser(participantId, request.GroupId);
                }
            }

            foreach (var participant in originalParticipants)
            {
                _groupChangedEventManager.DeliverEventToDevices(participant.Devices,
                                                                () => new GroupChangedEvent
                {
                    ChangesAuthorId = session.UserId,
                    GroupId         = request.GroupId,
                    Participants    = leftParticipants,
                    Type            = GroupChangedEvent.ChangesType.ParticipantsLeft
                });
            }

            if (leftParticipants.Count > 0)
            {
                _groupChatsRepository.UpdateOrCreateGroup(groupChat);
            }

            return(response);
        }
Пример #2
0
 public Task <KickParticipantsResponse> KickParticipants(KickParticipantsRequest request)
 {
     return(_connectionManager.SendRequestAndWaitResponse <KickParticipantsResponse>(request));
 }