Esempio n. 1
0
        public override async Task OnDisconnectedAsync(Exception ex)
        {
            GetUserAndCurrentRoomChatView user = await _chatService.GetUserAndCurrentRoomByUserId(Context.User.Identity.Name);

            GetAllRoomsChatView userRooms = await _chatService.GetByUserId(Context.User.Identity.Name);

            List <string> roomNames = userRooms.Rooms
                                      .Select(x => x.Name)
                                      .ToList();

            await Clients.Groups(roomNames).SendAsync("UserDisconnected", $"{user.FirstName} {user.LastName} has left conversation", user);

            await base.OnDisconnectedAsync(ex);
        }
        public async Task <GetAllRoomsChatView> GetByUserId(string userId)
        {
            GetAllRoomsChatView response = new GetAllRoomsChatView();

            List <UserInRoom> userInRooms = await _userInRoomRepository.GetByUserId(userId);

            if (!userInRooms.Any())
            {
                throw new Exception("User in rooms is not found");
            }

            response.Rooms = _mapper.Map(userInRooms, response.Rooms);

            return(response);
        }
Esempio n. 3
0
        public ChatMenuView BuildChatMenu()
        {
            ChatMenuView result = new ChatMenuView();

            GetAllRoomsChatView rooms = Task
                                        .Run(async() => await _chatService.GetAllRooms())
                                        .Result;

            if (!rooms.Rooms.Any())
            {
                return(result);
            }
            result = _mapper.Map(rooms, result);

            return(result);
        }
Esempio n. 4
0
        public override async Task OnConnectedAsync()
        {
            GetUserAndCurrentRoomChatView user = await _chatService.GetUserAndCurrentRoomByUserId(Context.User.Identity.Name);

            GetAllRoomsChatView userRooms = await _chatService.GetByUserId(Context.User.Identity.Name);

            List <string> roomNames = userRooms.Rooms
                                      .Where(x => x.Id == user.CurrentRoomId)
                                      .Select(x => x.Name)
                                      .ToList();

            GetAllUsersChatView usersInRoom = _chatService.GetAllUsersByRoomId(user.CurrentRoomId);
            await Clients.Groups(roomNames).SendAsync("UserConnected", $"{user.FirstName} {user.LastName} join to conversation", user, usersInRoom);

            await base.OnConnectedAsync();
        }
        public async Task <GetAllRoomsChatView> GetAllRooms()
        {
            GetAllRoomsChatView  response     = new GetAllRoomsChatView();
            List <MessageInRoom> lastMessages = new List <MessageInRoom>();
            List <Room>          rooms        = await _roomRepository.GetAll();

            List <MessageInRoom> messageInRooms = await _messageInRoomRepository.GetAllRoomsAndMessages();

            if (!messageInRooms.Any())
            {
                throw new Exception("Message in rooms is not found");
            }

            if (!rooms.Any())
            {
                throw new Exception("Rooms is not found");
            }

            messageInRooms
            .GroupBy(x => x.RoomId)
            .ToList()
            .ForEach(item =>
            {
                MessageInRoom lastMessage = item
                                            .Where(x => x.RoomId == item.Key)
                                            .OrderByDescending(x => x.CreationAt)
                                            .FirstOrDefault();
                lastMessages.Add(lastMessage);
            });


            if (!messageInRooms.Any())
            {
                return(response);
            }

            response.Rooms = _mapper.Map(lastMessages, response.Rooms);

            if (rooms.Count > response.Rooms.Count)
            {
                List <Guid> roomsWithMessagesIds = response.Rooms
                                                   .Select(x => x.Id)
                                                   .ToList();

                var roomsWithotMessages = rooms
                                          .Where(x => !roomsWithMessagesIds
                                                 .Contains(x.Id))
                                          .ToList()
                                          .Select(x => new RoomGetAllRoomsChatViewtem()
                {
                    Id          = x.Id,
                    Name        = x.Name,
                    Photo       = x.Photo,
                    LastMessage = "No messages yet"
                })
                                          .ToList();

                response.Rooms.AddRange(roomsWithotMessages);
            }

            return(response);
        }