Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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