예제 #1
0
        public async Task <ChatRoomDto> InviteToRoomAsync(string userId, string roomId)
        {
            if (String.IsNullOrEmpty(userId))
            {
                throw new ArgumentNullException(nameof(userId));
            }

            if (String.IsNullOrEmpty(roomId))
            {
                throw new ArgumentNullException(nameof(roomId));
            }

            if (userId == identityService.CurrentUserId)
            {
                throw new ArgumentException("User cannot invite himself to chat room", nameof(userId));
            }

            UserEntity user = await this.userRepo.FindByIdAsync(userId);

            if (user == null)
            {
                throw new Exception($"Cannot find user with Id {userId}");
            }

            //var room = await this.roomRepo.FindByIdAsync(roomId);

            //if (room == null)
            //{
            //    throw new Exception($"Cannot find room with Id {roomId}");
            //}

            await roomRepo.AddParticipantsAsync(roomId, new[] { userId });

            var room = await this.roomRepo.FindByIdAsync(roomId);

            ChatRoomDto model = mapper.Map <ChatRoomEntity, ChatRoomDto>(room);

            await notificator.Notificate(new ChatJoinNoitification(new NewChatUser
            {
                Room = model,
                User = mapper.Map <UserEntity, UserDto>(user)
            }, model.Participants.Select(x => x.Id).Where(x => String.Compare(x, user.Id, true) != 0).ToArray()));

            await notificator.Notificate(new NewChatNotification(model, new[] { user.Id }));

            return(model);
        }