Esempio n. 1
0
        public async Task <ChatMessageResult> CreateForEmptyRoomAsync(Guid profileId, MessageDto model)
        {
            var user = await _userRepository.GetWithId(profileId);

            var userTemp = await _userRepository.GetProfileForUserName(model.account);

            var allRoomLists = await _chatRoomListRepository.GetAllAsync();

            var roomList    = allRoomLists.Where(e => e.UserId == user.Id).ToList();
            var roomListTwo = allRoomLists.Where(e => e.UserId == userTemp.Id).ToList();

            //var uniqRoom = await _domainContext.ChatRooms.FirstOrDefaultAsync(e => e.Id == (roomList.Union));

            var dto     = new ChatMessageResult();
            var message = new ChatMessage();

            if (roomList.Count > 0 && roomListTwo.Count > 0)
            {
                var temp = roomList.FirstOrDefault(e => roomListTwo.FirstOrDefault(a => a.ChatRoomId == e.ChatRoomId) != null);
                //var temp = roomList.Intersect(roomListTwo).FirstOrDefault();
                if (temp != null)
                {
                    return(await CreateMessage(
                               model : model,
                               userTo : userTemp,
                               user : user
                               ));
                }
            }
            var room           = new ChatRoom(model.chatRoomId);
            var chatRoomListTo = new ChatRoomList();

            chatRoomListTo.ChatRoomId = room.Id;
            chatRoomListTo.UserId     = user.Id;
            var chatRoomListFrom = new ChatRoomList();

            chatRoomListFrom.ChatRoomId = room.Id;
            chatRoomListFrom.UserId     = userTemp.Id;

            await _chatRoomRepository.CreateAsync(room);

            await _chatRoomListRepository.CreateAsync(chatRoomListFrom);

            await _chatRoomListRepository.CreateAsync(chatRoomListTo);

            return(await CreateMessage(
                       model : model,
                       userTo : userTemp,
                       user : user
                       ));
        }
Esempio n. 2
0
        public async Task <ChatRoomDto> GetOrCreatePrivateRoomWith(string userId)
        {
            string currentUserId = identityService.CurrentUserId;

            if (currentUserId == userId)
            {
                throw new ArgumentException("Same user error");
            }

            if (!(await this.userRepo.FindByIdAsync(userId) is UserEntity user))
            {
                throw new ArgumentException("User does not exists");
            }

            var privateRoom = await roomRepo.GetPrivateRoomAsync(currentUserId, userId);

            if (privateRoom == null)
            {
                ChatRoomEntity newChatRoom = new ChatRoomEntity
                {
                    IsPrivate   = true,
                    OwnerUserId = currentUserId.ToLower(),
                    UserRooms   = new List <UsersChatRoomsEntity> {
                        new UsersChatRoomsEntity {
                            UserId = currentUserId
                        }, new UsersChatRoomsEntity {
                            UserId = userId
                        }
                    }
                };
                string id = await roomRepo.CreateAsync(newChatRoom);

                privateRoom = await roomRepo.FindByIdAsync(id);
            }

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

            await notificator.Notificate(new NewChatNotification(model, model.Participants.Select(x => x.Id)));

            return(model);
        }