public async Task <GetAllMessagesChatView> GetAllMessagesByRoomId(Guid roomId) { GetAllMessagesChatView response = new GetAllMessagesChatView(); List <MessageInRoom> messageInRooms = await _messageInRoomRepository.GetByRoomId(roomId); List <User> userInRooms = _userManager.Users .Where(x => x.CurrentRoomId == roomId) .ToList(); if (!userInRooms.Any()) { throw new Exception("User in rooms is not found"); } List <MessageInRoom> orderedMessageInRooms = messageInRooms .OrderBy(x => x.Message.CreationAt) .ToList(); if (messageInRooms.Where(x => x.Message.User == null).Any()) { foreach (var item in messageInRooms) { User user = await _userManager.FindByIdAsync(item.Message.UserId); item.Message.User = user; } } response.RoomId = roomId; response.Users = _mapper.Map(userInRooms, response.Users); response.Messages = _mapper.Map(orderedMessageInRooms, response.Messages); return(response); }
public async Task JoinRoom(string roomId, string group) { await _chatService.SetUserCurrentRoomByUserId(Context.User.Identity.Name, Guid.Parse(roomId)); GetUserAndCurrentRoomChatView user = await _chatService.GetUserAndCurrentRoomByUserId(Context.User.Identity.Name); GetAllMessagesChatView room = await _chatService.GetAllMessagesByRoomId(Guid.Parse(roomId)); GetAllUsersChatView usersInRoom = _chatService.GetAllUsersByRoomId(user.CurrentRoomId); await Groups.AddToGroupAsync(Context.ConnectionId, group); await Clients.Group(group).SendAsync("UserConnected", $"{user.FirstName} {user.LastName} join to conversation", user, usersInRoom); await Clients.Group(group).SendAsync("ReceiveRoomMessages", room); }