Repository navigation
Expand file tree
/
Copy pathSignalRHub.cs
More file actions
52 lines (45 loc) · 1.69 KB
/
Copy pathSignalRHub.cs
File metadata and controls
52 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
namespace BotSharp.Plugin.ChatHub;
[Authorize]
public class SignalRHub : Hub
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
private readonly IUserIdentity _user;
private readonly IHttpContextAccessor _context;
public SignalRHub(IServiceProvider services,
ILogger<SignalRHub> logger,
IUserIdentity user,
IHttpContextAccessor context)
{
_services = services;
_logger = logger;
_user = user;
_context = context;
}
public override async Task OnConnectedAsync()
{
_logger.LogInformation($"SignalR Hub: {_user.FirstName} {_user.LastName} ({Context.User.Identity.Name}) connected in {Context.ConnectionId}");
var hooks = _services.GetServices<IConversationHook>();
var convService = _services.GetRequiredService<IConversationService>();
_context.HttpContext.Request.Query.TryGetValue("conversationId", out var conversationId);
if (!string.IsNullOrEmpty(conversationId))
{
_logger.LogInformation($"Connection {Context.ConnectionId} is with conversation {conversationId}");
var conv = await convService.GetConversation(conversationId);
if (conv != null)
{
foreach (var hook in hooks)
{
// Check if user connected with agent is the first time.
if (!conv.Dialogs.Any())
{
await hook.OnUserAgentConnectedInitially(conv);
}
}
}
}
await base.OnConnectedAsync();
}
}