public AddParticipantsResponse AddParticipants(ISession session, AddParticipantsRequest request)
        {
            var response  = request.CreateResponse <AddParticipantsResponse>();
            var groupChat = _groupChatsRepository.GetChat(request.GroupId);

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

            var addedParticipants = new List <GroupChatParticipant>();

            foreach (var participantId in request.ParticipantIds)
            {
                var participant = new GroupChatParticipant {
                    UserId = participantId, Devices = _devicesRepository.GetDevices(participantId)
                };
                if (groupChat.Participants.AddIfUnique(participant, r => r.UserId))
                {
                    _groupChatsRepository.AddGroupToUser(participant.UserId, request.GroupId);
                    addedParticipants.Add(participant);
                }
            }

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

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

            return(response);
        }
Ejemplo n.º 2
0
 public Task <AddParticipantsResponse> AddParticipants(AddParticipantsRequest request)
 {
     return(_connectionManager.SendRequestAndWaitResponse <AddParticipantsResponse>(request));
 }