support new push models

This commit is contained in:
Kyle Spearrin 2017-05-26 22:55:48 -04:00
parent 8f7c4951b8
commit 6a1ff56e7b
2 changed files with 29 additions and 18 deletions

View File

@ -1,14 +1,20 @@
using System; using System;
using Bit.App.Enums; using Bit.App.Enums;
using Newtonsoft.Json.Linq;
namespace Bit.App.Models namespace Bit.App.Models
{ {
public class PushNotification public class PushNotificationData
{ {
public PushType Type { get; set; } public PushType Type { get; set; }
} }
public class SyncCipherPushNotification : PushNotification public class PushNotificationDataPayload : PushNotificationData
{
public string Payload { get; set; }
}
public class SyncCipherPushNotification
{ {
public string Id { get; set; } public string Id { get; set; }
public string UserId { get; set; } public string UserId { get; set; }
@ -16,14 +22,14 @@ namespace Bit.App.Models
public DateTime RevisionDate { get; set; } public DateTime RevisionDate { get; set; }
} }
public class SyncFolderPushNotification : PushNotification public class SyncFolderPushNotification
{ {
public string Id { get; set; } public string Id { get; set; }
public string UserId { get; set; } public string UserId { get; set; }
public DateTime RevisionDate { get; set; } public DateTime RevisionDate { get; set; }
} }
public class SyncUserPushNotification : PushNotification public class SyncUserPushNotification
{ {
public string UserId { get; set; } public string UserId { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }

View File

@ -33,33 +33,38 @@ namespace Bit.App.Services
_settings = settings; _settings = settings;
} }
public void OnMessage(JObject values, DeviceType deviceType) public void OnMessage(JObject value, DeviceType deviceType)
{ {
if(values == null) if(value == null)
{ {
return; return;
} }
_showNotification = false; _showNotification = false;
Debug.WriteLine("Message Arrived: {0}", JsonConvert.SerializeObject(values)); Debug.WriteLine("Message Arrived: {0}", JsonConvert.SerializeObject(value));
if(!_authService.IsAuthenticated) if(!_authService.IsAuthenticated)
{ {
return; return;
} }
JToken token; JToken dataToken;
if(!values.TryGetValue("type", StringComparison.OrdinalIgnoreCase, out token) || token == null) if(!value.TryGetValue("data", StringComparison.OrdinalIgnoreCase, out dataToken) || dataToken == null)
{ {
return; return;
} }
var type = (Enums.PushType)token.ToObject<short>(); var data = dataToken.ToObject<PushNotificationDataPayload>();
switch(type) if(data?.Payload == null)
{
return;
}
switch(data.Type)
{ {
case Enums.PushType.SyncCipherUpdate: case Enums.PushType.SyncCipherUpdate:
case Enums.PushType.SyncCipherCreate: case Enums.PushType.SyncCipherCreate:
var cipherCreateUpdateMessage = values.ToObject<SyncCipherPushNotification>(); var cipherCreateUpdateMessage = JsonConvert.DeserializeObject<SyncCipherPushNotification>(data.Payload);
if(cipherCreateUpdateMessage.OrganizationId == null && if(cipherCreateUpdateMessage.OrganizationId == null &&
cipherCreateUpdateMessage.UserId != _authService.UserId) cipherCreateUpdateMessage.UserId != _authService.UserId)
{ {
@ -74,7 +79,7 @@ namespace Bit.App.Services
break; break;
case Enums.PushType.SyncFolderUpdate: case Enums.PushType.SyncFolderUpdate:
case Enums.PushType.SyncFolderCreate: case Enums.PushType.SyncFolderCreate:
var folderCreateUpdateMessage = values.ToObject<SyncFolderPushNotification>(); var folderCreateUpdateMessage = JsonConvert.DeserializeObject<SyncFolderPushNotification>(data.Payload);
if(folderCreateUpdateMessage.UserId != _authService.UserId) if(folderCreateUpdateMessage.UserId != _authService.UserId)
{ {
break; break;
@ -82,7 +87,7 @@ namespace Bit.App.Services
_syncService.SyncFolderAsync(folderCreateUpdateMessage.Id); _syncService.SyncFolderAsync(folderCreateUpdateMessage.Id);
break; break;
case Enums.PushType.SyncFolderDelete: case Enums.PushType.SyncFolderDelete:
var folderDeleteMessage = values.ToObject<SyncFolderPushNotification>(); var folderDeleteMessage = JsonConvert.DeserializeObject<SyncFolderPushNotification>(data.Payload);
if(folderDeleteMessage.UserId != _authService.UserId) if(folderDeleteMessage.UserId != _authService.UserId)
{ {
break; break;
@ -90,7 +95,7 @@ namespace Bit.App.Services
_syncService.SyncDeleteFolderAsync(folderDeleteMessage.Id, folderDeleteMessage.RevisionDate); _syncService.SyncDeleteFolderAsync(folderDeleteMessage.Id, folderDeleteMessage.RevisionDate);
break; break;
case Enums.PushType.SyncLoginDelete: case Enums.PushType.SyncLoginDelete:
var loginDeleteMessage = values.ToObject<SyncCipherPushNotification>(); var loginDeleteMessage = JsonConvert.DeserializeObject<SyncCipherPushNotification>(data.Payload);
if(loginDeleteMessage.OrganizationId == null && if(loginDeleteMessage.OrganizationId == null &&
loginDeleteMessage.UserId != _authService.UserId) loginDeleteMessage.UserId != _authService.UserId)
{ {
@ -105,7 +110,7 @@ namespace Bit.App.Services
break; break;
case Enums.PushType.SyncCiphers: case Enums.PushType.SyncCiphers:
case Enums.PushType.SyncVault: case Enums.PushType.SyncVault:
var cipherMessage = values.ToObject<SyncUserPushNotification>(); var cipherMessage = JsonConvert.DeserializeObject<SyncUserPushNotification>(data.Payload);
if(cipherMessage.UserId != _authService.UserId) if(cipherMessage.UserId != _authService.UserId)
{ {
break; break;
@ -113,7 +118,7 @@ namespace Bit.App.Services
_syncService.FullSyncAsync(true); _syncService.FullSyncAsync(true);
break; break;
case Enums.PushType.SyncSettings: case Enums.PushType.SyncSettings:
var domainMessage = values.ToObject<SyncUserPushNotification>(); var domainMessage = JsonConvert.DeserializeObject<SyncUserPushNotification>(data.Payload);
if(domainMessage.UserId != _authService.UserId) if(domainMessage.UserId != _authService.UserId)
{ {
break; break;
@ -121,7 +126,7 @@ namespace Bit.App.Services
_syncService.SyncSettingsAsync(); _syncService.SyncSettingsAsync();
break; break;
case Enums.PushType.SyncOrgKeys: case Enums.PushType.SyncOrgKeys:
var orgKeysMessage = values.ToObject<SyncUserPushNotification>(); var orgKeysMessage = JsonConvert.DeserializeObject<SyncUserPushNotification>(data.Payload);
if(orgKeysMessage.UserId != _authService.UserId) if(orgKeysMessage.UserId != _authService.UserId)
{ {
break; break;