1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-06 05:28:15 -05:00

centralize type deserialization

This commit is contained in:
Kyle Spearrin 2018-08-25 17:21:42 -04:00
parent 6f7d07530a
commit f74dfb5fbb
3 changed files with 5 additions and 13 deletions

View File

@ -3,13 +3,11 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.Core; using Bit.Core;
using Bit.Core.Models;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue; using Microsoft.WindowsAzure.Storage.Queue;
using Newtonsoft.Json;
namespace Bit.Notifications namespace Bit.Notifications
{ {
@ -68,11 +66,7 @@ namespace Bit.Notifications
{ {
foreach(var message in messages) foreach(var message in messages)
{ {
var notificationJson = message.AsString; await HubHelpers.SendNotificationToHubAsync(message.AsString, _hubContext, cancellationToken);
var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(
notificationJson);
await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson,
_hubContext, cancellationToken);
await _queue.DeleteMessageAsync(message); await _queue.DeleteMessageAsync(message);
} }
} }

View File

@ -1,12 +1,10 @@
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.Core.Models;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
namespace Bit.Notifications namespace Bit.Notifications
{ {
@ -29,8 +27,7 @@ namespace Bit.Notifications
var notificationJson = await reader.ReadToEndAsync(); var notificationJson = await reader.ReadToEndAsync();
if(!string.IsNullOrWhiteSpace(notificationJson)) if(!string.IsNullOrWhiteSpace(notificationJson))
{ {
var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(notificationJson); await HubHelpers.SendNotificationToHubAsync(notificationJson, _hubContext);
await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson, _hubContext);
} }
} }
} }

View File

@ -9,10 +9,11 @@ namespace Bit.Notifications
{ {
public static class HubHelpers public static class HubHelpers
{ {
public static async Task SendNotificationToHubAsync(PushType type, string notificationJson, public static async Task SendNotificationToHubAsync(string notificationJson,
IHubContext<NotificationsHub> hubContext, CancellationToken cancellationToken = default(CancellationToken)) IHubContext<NotificationsHub> hubContext, CancellationToken cancellationToken = default(CancellationToken))
{ {
switch(type) var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(notificationJson);
switch(notification.Type)
{ {
case PushType.SyncCipherUpdate: case PushType.SyncCipherUpdate:
case PushType.SyncCipherCreate: case PushType.SyncCipherCreate: