Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Diligent.WebAPI.Data.Entities;
  2. using Diligent.WebAPI.Host.DTOs.Notification;
  3. using Diligent.WebAPI.Host.DTOs.Chat;
  4. using Diligent.WebAPI.Host.Mediator.Messages.Commands;
  5. using Diligent.WebAPI.Host.Mediator.Notifications.Commands;
  6. using MediatR;
  7. using Microsoft.AspNetCore.SignalR;
  8. using Diligent.WebAPI.Host.Middlewares;
  9. using Diligent.WebAPI.Host.Mediator.Rooms.Commands;
  10. namespace Diligent.WebAPI.Host.Hubs
  11. {
  12. public class ChatHub : Hub<IChatClient>
  13. {
  14. private readonly IDictionary<string, UserConnection> _connections;
  15. private readonly IMediator _mediator;
  16. public ChatHub(IDictionary<string, UserConnection> connections,IMediator mediator)
  17. {
  18. _connections = connections;
  19. _mediator = mediator;
  20. }
  21. [HubMethodName("SendMessageToGroup")]
  22. [AuthorizationHubFilter(Roles = "Customer,Support")]
  23. public async Task SendMessageToGroup(ChatMessage message)
  24. {
  25. // Find user's room and send message to everyone
  26. // All other users will receive notification
  27. if (_connections.TryGetValue(message.ConnId, out UserConnection room))
  28. {
  29. await Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { UserId = room.UserId, User = room.UserId, Message = message.Message, RoomId = room.RoomId });
  30. await _mediator.Send(new AddMessageCommand(room.RoomId, new Message { Content = message.Message, SenderId = message.UserId, Username = room.Username }));
  31. // Find other users in room and save notification in database
  32. foreach (KeyValuePair<string, UserConnection> conn in _connections)
  33. {
  34. if (conn.Value.RoomId == room.RoomId && conn.Key != message.ConnId)
  35. {
  36. await _mediator.Send(new AddNotificationCommand(new NotificationSaveDTO { RoomId = room.RoomId, ReceiverId = conn.Value.UserId }));
  37. }
  38. }
  39. await Clients.OthersInGroup(room.RoomId).ReceiveNotifications(room.UserId, room.RoomId);
  40. }
  41. }
  42. [HubMethodName("JoinRoom")]
  43. [AuthorizationHubFilter(Roles = "Customer,Support")]
  44. public async Task JoinRoom(UserConnection userConnection)
  45. {
  46. // Check is user in requested room
  47. var result = _connections.Where(x => x.Value.UserId == userConnection.UserId && x.Value.RoomId == userConnection.RoomId).FirstOrDefault();
  48. // If user is not in room, add him
  49. // Return message "User has joined room" to all users in room
  50. if (result.Value == null)
  51. {
  52. await Groups.AddToGroupAsync(Context.ConnectionId, userConnection.RoomId);
  53. _connections[Context.ConnectionId] = userConnection;
  54. await Clients.Group(userConnection.RoomId).ReceiveMessage(new ChatMessage { UserId = userConnection.UserId, User = userConnection.UserId, RoomId = userConnection.RoomId, Message = $"{userConnection.Username} has joined room", ConnId = Context.ConnectionId,IsAccessMessage = true });
  55. }
  56. else
  57. {
  58. await _mediator.Send(new DeleteNotificationCommand(new NotificationDeleteDTO { UserId = userConnection.UserId, RoomId = userConnection.RoomId }));
  59. }
  60. }
  61. [HubMethodName("LeaveRoom")]
  62. [AuthorizationHubFilter(Roles = "Customer,Support")]
  63. public async Task LeaveRoom(LeaveChatRoomDTO connection)
  64. {
  65. if (_connections.TryGetValue(connection.ConnId, out UserConnection room))
  66. {
  67. // Find user connection in connections dictionary and delete it
  68. _connections.Remove(Context.ConnectionId);
  69. await Clients.OthersInGroup(room.RoomId).ReceiveMessage(new ChatMessage { UserId = room.UserId, User = room.UserId, Message = $"{room.Username} has left room", RoomId = room.RoomId,IsAccessMessage = true });
  70. await _mediator.Send(new RemoveUserFromGroupCommand(room.RoomId, room.UserId));
  71. }
  72. }
  73. [HubMethodName("SeeWhoIsTyping")]
  74. public async Task SeeWhoIsTyping(TypingInvokeMessage message)
  75. {
  76. // Find user's room and send message to everyone
  77. if (_connections.TryGetValue(message.ConnId, out UserConnection room))
  78. {
  79. // send the typing info to all in group except the caller
  80. await Clients.GroupExcept(message.RoomId, message.ConnId).ViewTyping(new TypingMessage { RoomId = room.RoomId, Message = message.Message });
  81. }
  82. }
  83. }
  84. }