mirror of
https://github.com/bitwarden/server
synced 2025-12-12 06:13:43 +00:00
update to new 2.0.0 preview for notification hub
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#if NET471
|
||||
using System;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Table;
|
||||
using Microsoft.Azure.NotificationHubs;
|
||||
@@ -13,16 +12,17 @@ namespace Bit.Core.Services
|
||||
{
|
||||
public class NotificationHubPushNotificationService : IPushNotificationService
|
||||
{
|
||||
private readonly NotificationHubClient _client;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
private NotificationHubClient _client = null;
|
||||
private DateTime? _clientExpires = null;
|
||||
|
||||
public NotificationHubPushNotificationService(
|
||||
GlobalSettings globalSettings,
|
||||
IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_client = NotificationHubClient.CreateClientFromConnectionString(globalSettings.NotificationHub.ConnectionString,
|
||||
globalSettings.NotificationHub.HubName);
|
||||
|
||||
_globalSettings = globalSettings;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
@@ -170,13 +170,25 @@ namespace Bit.Core.Services
|
||||
|
||||
private async Task SendPayloadAsync(string tag, PushType type, object payload)
|
||||
{
|
||||
await _client.SendTemplateNotificationAsync(
|
||||
await RenewClientAndExecuteAsync(async client => await client.SendTemplateNotificationAsync(
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
{ "type", ((byte)type).ToString() },
|
||||
{ "payload", JsonConvert.SerializeObject(payload) }
|
||||
}, tag);
|
||||
}, tag));
|
||||
}
|
||||
|
||||
private async Task RenewClientAndExecuteAsync(Func<NotificationHubClient, Task> task)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
if(_client == null || !_clientExpires.HasValue || _clientExpires.Value < now)
|
||||
{
|
||||
_clientExpires = now.Add(TimeSpan.FromMinutes(30));
|
||||
_client = NotificationHubClient.CreateClientFromConnectionString(
|
||||
_globalSettings.NotificationHub.ConnectionString,
|
||||
_globalSettings.NotificationHub.HubName);
|
||||
}
|
||||
await task(_client);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user