2018-08-02 12:14:33 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
using Bit.Core.Context;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
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
|
|
|
|
|
2021-08-30 18:19:46 +02: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()
|
|
|
|
|
|
{
|
2021-08-30 18:19:46 +02:00
|
|
|
|
var currentContext = new CurrentContext(null);
|
2020-06-04 14:14:43 -04:00
|
|
|
|
await currentContext.BuildAsync(Context.User, _globalSettings);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (currentContext.Organizations != null)
|
2018-08-02 12:14:33 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var org in currentContext.Organizations)
|
2018-08-31 17:05:27 -04:00
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2021-08-30 18:19:46 +02:00
|
|
|
|
var currentContext = new CurrentContext(null);
|
2020-06-04 14:14:43 -04:00
|
|
|
|
await currentContext.BuildAsync(Context.User, _globalSettings);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (currentContext.Organizations != null)
|
2018-08-02 12:14:33 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var org in currentContext.Organizations)
|
2018-08-31 17:05:27 -04:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|