BotSharp/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs

52 lines
1.6 KiB
C#
Raw Normal View History

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
{
2023-11-28 03:19:07 +00:00
_logger.LogInformation($"SignalR Hub: {_user.FirstName} {_user.LastName} ({Context.UserIdentifier}) connected in {Context.ConnectionId} [{DateTime.Now}]");
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
{
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
}
}