Files
server/src/Notifications/NotificationsHub.cs

52 lines
1.8 KiB
C#
Raw Normal View History

2018-08-02 12:14:33 -04:00
using System;
using System.Threading.Tasks;
using Bit.Core.Context;
using Bit.Core.Settings;
2018-08-02 12:14:33 -04:00
using Microsoft.AspNetCore.Authorization;
2018-08-16 13:45:31 -04:00
namespace Bit.Notifications
2018-08-02 12:14:33 -04:00
{
[Authorize("Application")]
2018-08-16 13:45:31 -04:00
public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub
2018-08-02 12:14:33 -04:00
{
2018-08-23 15:48:40 -04:00
private readonly ConnectionCounter _connectionCounter;
2019-01-25 00:01:24 -05:00
private readonly GlobalSettings _globalSettings;
2018-08-23 15:48:40 -04:00
public NotificationsHub(ConnectionCounter connectionCounter, GlobalSettings globalSettings)
2018-08-23 15:48:40 -04:00
{
_connectionCounter = connectionCounter;
2019-01-25 00:01:24 -05:00
_globalSettings = globalSettings;
2018-08-23 15:48:40 -04:00
}
2018-08-02 12:14:33 -04:00
public override async Task OnConnectedAsync()
{
var currentContext = new CurrentContext(null);
await currentContext.BuildAsync(Context.User, _globalSettings);
if (currentContext.Organizations != null)
2018-08-02 12:14:33 -04:00
{
foreach (var org in currentContext.Organizations)
{
await Groups.AddToGroupAsync(Context.ConnectionId, $"Organization_{org.Id}");
}
2018-08-02 12:14:33 -04:00
}
2018-08-23 15:48:40 -04:00
_connectionCounter.Increment();
2018-08-02 12:14:33 -04:00
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
var currentContext = new CurrentContext(null);
await currentContext.BuildAsync(Context.User, _globalSettings);
if (currentContext.Organizations != null)
2018-08-02 12:14:33 -04:00
{
foreach (var org in currentContext.Organizations)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"Organization_{org.Id}");
}
2018-08-02 12:14:33 -04:00
}
2018-08-23 15:48:40 -04:00
_connectionCounter.Decrement();
2018-08-02 12:14:33 -04:00
await base.OnDisconnectedAsync(exception);
}
}
}