2023-11-28 03:19:07 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-11-22 16:27:55 +00:00
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.ChatHub;
|
|
|
|
|
|
2023-11-28 03:19:07 +00:00
|
|
|
[Authorize]
|
2023-11-22 16:27:55 +00:00
|
|
|
public class SignalRHub : Hub
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IUserIdentity _user;
|
2023-11-28 03:19:07 +00:00
|
|
|
private readonly IHttpContextAccessor _context;
|
2023-11-22 16:27:55 +00:00
|
|
|
|
|
|
|
|
public SignalRHub(IServiceProvider services,
|
|
|
|
|
ILogger<SignalRHub> logger,
|
2023-11-28 03:19:07 +00:00
|
|
|
IUserIdentity user,
|
|
|
|
|
IHttpContextAccessor context)
|
2023-11-22 16:27:55 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_user = user;
|
2023-11-28 03:19:07 +00:00
|
|
|
_context = context;
|
2023-11-22 16:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-28 03:19:07 +00:00
|
|
|
public override async Task OnConnectedAsync()
|
2023-11-22 16:27:55 +00:00
|
|
|
{
|
2024-01-05 03:20:50 +00:00
|
|
|
_logger.LogInformation($"SignalR Hub: {_user.FirstName} {_user.LastName} ({Context.User.Identity.Name}) connected in {Context.ConnectionId}");
|
2023-11-28 03:19:07 +00:00
|
|
|
|
|
|
|
|
var hooks = _services.GetServices<IConversationHook>();
|
|
|
|
|
var convService = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
_context.HttpContext.Request.Query.TryGetValue("conversationId", out var conversationId);
|
|
|
|
|
|
2023-12-04 23:42:46 +00:00
|
|
|
if (!string.IsNullOrEmpty(conversationId))
|
2023-11-28 03:19:07 +00:00
|
|
|
{
|
2024-01-05 03:20:50 +00:00
|
|
|
_logger.LogInformation($"Connection {Context.ConnectionId} is with conversation {conversationId}");
|
2025-02-27 22:19:43 +00:00
|
|
|
await AddGroup(conversationId);
|
2025-02-13 20:52:46 +00:00
|
|
|
|
2023-12-04 23:42:46 +00:00
|
|
|
var conv = await convService.GetConversation(conversationId);
|
2023-12-11 23:15:08 +00:00
|
|
|
if (conv != null)
|
2023-11-28 03:19:07 +00:00
|
|
|
{
|
2023-12-11 23:15:08 +00:00
|
|
|
foreach (var hook in hooks)
|
2023-12-04 23:42:46 +00:00
|
|
|
{
|
2023-12-11 23:15:08 +00:00
|
|
|
// Check if user connected with agent is the first time.
|
|
|
|
|
if (!conv.Dialogs.Any())
|
|
|
|
|
{
|
|
|
|
|
await hook.OnUserAgentConnectedInitially(conv);
|
|
|
|
|
}
|
2023-12-04 23:42:46 +00:00
|
|
|
}
|
2023-11-28 03:19:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await base.OnConnectedAsync();
|
2023-11-22 16:27:55 +00:00
|
|
|
}
|
2025-02-27 22:19:43 +00:00
|
|
|
|
|
|
|
|
private async Task AddGroup(string conversationId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var settings = _services.GetRequiredService<ChatHubSettings>();
|
|
|
|
|
if (settings.EventDispatchBy == EventDispatchType.Group)
|
|
|
|
|
{
|
|
|
|
|
await Groups.AddToGroupAsync(Context.ConnectionId, conversationId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-05-02 16:31:28 +00:00
|
|
|
_logger.LogWarning(ex, $"Failed to add chat group in {nameof(SignalRHub)} (conversation id: {conversationId}).");
|
2025-02-27 22:19:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-22 16:27:55 +00:00
|
|
|
}
|