From 68c349f72fb2a81fb232484dcc504f1c1d7103d2 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 23 Aug 2018 21:56:48 -0400 Subject: [PATCH] support message pack protocol for signalr --- src/Notifications/AzureQueueHostedService.cs | 6 +- .../Controllers/NotificationsController.cs | 17 ++++- src/Notifications/HubHelpers.cs | 62 ++++++++++--------- src/Notifications/Jobs/JobsHostedService.cs | 2 +- src/Notifications/Notifications.csproj | 1 + src/Notifications/Startup.cs | 11 +++- 6 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/Notifications/AzureQueueHostedService.cs b/src/Notifications/AzureQueueHostedService.cs index 332614d58..05baf77cf 100644 --- a/src/Notifications/AzureQueueHostedService.cs +++ b/src/Notifications/AzureQueueHostedService.cs @@ -68,9 +68,11 @@ namespace Bit.Notifications { foreach(var message in messages) { + var notificationJson = message.AsString; var notification = JsonConvert.DeserializeObject>( - message.AsString); - await HubHelpers.SendNotificationToHubAsync(notification, _hubContext, cancellationToken); + notificationJson); + await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson, + _hubContext, cancellationToken); await _queue.DeleteMessageAsync(message); } } diff --git a/src/Notifications/Controllers/NotificationsController.cs b/src/Notifications/Controllers/NotificationsController.cs index 1adebfffd..c57b56c13 100644 --- a/src/Notifications/Controllers/NotificationsController.cs +++ b/src/Notifications/Controllers/NotificationsController.cs @@ -1,9 +1,12 @@ -using System.Threading.Tasks; +using System.IO; +using System.Text; +using System.Threading.Tasks; using Bit.Core.Models; using Bit.Core.Utilities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; +using Newtonsoft.Json; namespace Bit.Notifications { @@ -19,9 +22,17 @@ namespace Bit.Notifications } [HttpPost("~/notifications")] - public async Task PostNotification([FromBody]PushNotificationData model) + public async Task PostNotification() { - await HubHelpers.SendNotificationToHubAsync(model, _hubContext); + using(var reader = new StreamReader(Request.Body, Encoding.UTF8)) + { + var notificationJson = await reader.ReadToEndAsync(); + if(!string.IsNullOrWhiteSpace(notificationJson)) + { + var notification = JsonConvert.DeserializeObject>(notificationJson); + await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson, _hubContext); + } + } } } } diff --git a/src/Notifications/HubHelpers.cs b/src/Notifications/HubHelpers.cs index 87370fc16..f955cc1ce 100644 --- a/src/Notifications/HubHelpers.cs +++ b/src/Notifications/HubHelpers.cs @@ -1,5 +1,6 @@ using System.Threading; using System.Threading.Tasks; +using Bit.Core.Enums; using Bit.Core.Models; using Microsoft.AspNetCore.SignalR; using Newtonsoft.Json; @@ -8,45 +9,48 @@ namespace Bit.Notifications { public static class HubHelpers { - public static async Task SendNotificationToHubAsync(PushNotificationData notification, + public static async Task SendNotificationToHubAsync(PushType type, string notificationJson, IHubContext hubContext, CancellationToken cancellationToken = default(CancellationToken)) { - switch(notification.Type) + switch(type) { - case Core.Enums.PushType.SyncCipherUpdate: - case Core.Enums.PushType.SyncCipherCreate: - case Core.Enums.PushType.SyncCipherDelete: - case Core.Enums.PushType.SyncLoginDelete: - var cipherPayload = JsonConvert.DeserializeObject( - JsonConvert.SerializeObject(notification.Payload)); - if(cipherPayload.UserId.HasValue) + case PushType.SyncCipherUpdate: + case PushType.SyncCipherCreate: + case PushType.SyncCipherDelete: + case PushType.SyncLoginDelete: + var cipherNotification = + JsonConvert.DeserializeObject>( + notificationJson); + if(cipherNotification.Payload.UserId.HasValue) { - await hubContext.Clients.User(cipherPayload.UserId.ToString()) - .SendAsync("ReceiveMessage", notification, cancellationToken); + await hubContext.Clients.User(cipherNotification.Payload.UserId.ToString()) + .SendAsync("ReceiveMessage", cipherNotification, cancellationToken); } - else if(cipherPayload.OrganizationId.HasValue) + else if(cipherNotification.Payload.OrganizationId.HasValue) { await hubContext.Clients.Group( - $"Organization_{cipherPayload.OrganizationId}") - .SendAsync("ReceiveMessage", notification, cancellationToken); + $"Organization_{cipherNotification.Payload.OrganizationId}") + .SendAsync("ReceiveMessage", cipherNotification, cancellationToken); } break; - case Core.Enums.PushType.SyncFolderUpdate: - case Core.Enums.PushType.SyncFolderCreate: - case Core.Enums.PushType.SyncFolderDelete: - var folderPayload = JsonConvert.DeserializeObject( - JsonConvert.SerializeObject(notification.Payload)); - await hubContext.Clients.User(folderPayload.UserId.ToString()) - .SendAsync("ReceiveMessage", notification, cancellationToken); + case PushType.SyncFolderUpdate: + case PushType.SyncFolderCreate: + case PushType.SyncFolderDelete: + var folderNotification = + JsonConvert.DeserializeObject>( + notificationJson); + await hubContext.Clients.User(folderNotification.Payload.UserId.ToString()) + .SendAsync("ReceiveMessage", folderNotification, cancellationToken); break; - case Core.Enums.PushType.SyncCiphers: - case Core.Enums.PushType.SyncVault: - case Core.Enums.PushType.SyncOrgKeys: - case Core.Enums.PushType.SyncSettings: - var userPayload = JsonConvert.DeserializeObject( - JsonConvert.SerializeObject(notification.Payload)); - await hubContext.Clients.User(userPayload.UserId.ToString()) - .SendAsync("ReceiveMessage", notification, cancellationToken); + case PushType.SyncCiphers: + case PushType.SyncVault: + case PushType.SyncOrgKeys: + case PushType.SyncSettings: + var userNotification = + JsonConvert.DeserializeObject>( + notificationJson); + await hubContext.Clients.User(userNotification.Payload.UserId.ToString()) + .SendAsync("ReceiveMessage", userNotification, cancellationToken); break; default: break; diff --git a/src/Notifications/Jobs/JobsHostedService.cs b/src/Notifications/Jobs/JobsHostedService.cs index 3cbcaf486..2ce6d347b 100644 --- a/src/Notifications/Jobs/JobsHostedService.cs +++ b/src/Notifications/Jobs/JobsHostedService.cs @@ -21,7 +21,7 @@ namespace Bit.Notifications.Jobs { var everyFiveMinutesTrigger = TriggerBuilder.Create() .StartNow() - .WithCronSchedule("0 */5 * * * ?") + .WithCronSchedule("0 */30 * * * ?") .Build(); Jobs = new List> diff --git a/src/Notifications/Notifications.csproj b/src/Notifications/Notifications.csproj index cf96e4086..8872e63a0 100644 --- a/src/Notifications/Notifications.csproj +++ b/src/Notifications/Notifications.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Notifications/Startup.cs b/src/Notifications/Startup.cs index 77c34998d..e7d110842 100644 --- a/src/Notifications/Startup.cs +++ b/src/Notifications/Startup.cs @@ -1,4 +1,5 @@ -using Bit.Core; +using System.Collections.Generic; +using Bit.Core; using Bit.Core.Utilities; using IdentityModel; using Microsoft.AspNetCore.Builder; @@ -54,7 +55,13 @@ namespace Bit.Notifications } else { - services.AddSignalR(); + services.AddSignalR().AddMessagePackProtocol(options => + { + options.FormatterResolvers = new List() + { + MessagePack.Resolvers.ContractlessStandardResolver.Instance + }; + }); } services.AddSingleton(); services.AddSingleton();