diff --git a/src/abstractions/sync.service.ts b/src/abstractions/sync.service.ts index ad6eb498e8..5d35cf20bc 100644 --- a/src/abstractions/sync.service.ts +++ b/src/abstractions/sync.service.ts @@ -1,6 +1,7 @@ import { SyncCipherNotification, SyncFolderNotification, + SyncSendNotification, } from '../models/response/notificationResponse'; export abstract class SyncService { @@ -13,4 +14,6 @@ export abstract class SyncService { syncDeleteFolder: (notification: SyncFolderNotification) => Promise; syncUpsertCipher: (notification: SyncCipherNotification, isEdit: boolean) => Promise; syncDeleteCipher: (notification: SyncFolderNotification) => Promise; + syncUpsertSend: (notification: SyncSendNotification, isEdit: boolean) => Promise; + syncDeleteSend: (notification: SyncSendNotification) => Promise; } diff --git a/src/enums/notificationType.ts b/src/enums/notificationType.ts index 4b655404e7..5adaac307d 100644 --- a/src/enums/notificationType.ts +++ b/src/enums/notificationType.ts @@ -13,4 +13,8 @@ export enum NotificationType { SyncSettings = 10, LogOut = 11, + + SyncSendCreate = 12, + SyncSendUpdate = 13, + SyncSendDelete = 14, } diff --git a/src/models/response/notificationResponse.ts b/src/models/response/notificationResponse.ts index 9187be71fd..b60f38a6d8 100644 --- a/src/models/response/notificationResponse.ts +++ b/src/models/response/notificationResponse.ts @@ -32,6 +32,10 @@ export class NotificationResponse extends BaseResponse { case NotificationType.LogOut: this.payload = new UserNotification(payload); break; + case NotificationType.SyncSendCreate: + case NotificationType.SyncSendUpdate: + case NotificationType.SyncSendDelete: + this.payload = new SyncSendNotification(payload); default: break; } @@ -78,3 +82,16 @@ export class UserNotification extends BaseResponse { this.date = new Date(this.getResponseProperty('Date')); } } + +export class SyncSendNotification extends BaseResponse { + id: string; + userId: string; + revisionDate: Date; + + constructor(response: any) { + super(response); + this.id = this.getResponseProperty('Id'); + this.userId = this.getResponseProperty('UserId'); + this.revisionDate = new Date(this.getResponseProperty('RevisionDate')); + } +} diff --git a/src/services/notifications.service.ts b/src/services/notifications.service.ts index a4df8299f5..b8ae1a07d8 100644 --- a/src/services/notifications.service.ts +++ b/src/services/notifications.service.ts @@ -16,6 +16,7 @@ import { NotificationResponse, SyncCipherNotification, SyncFolderNotification, + SyncSendNotification, } from '../models/response/notificationResponse'; export class NotificationsService implements NotificationsServiceAbstraction { @@ -159,6 +160,13 @@ export class NotificationsService implements NotificationsServiceAbstraction { this.logoutCallback(); } break; + case NotificationType.SyncSendCreate: + case NotificationType.SyncSendUpdate: + await this.syncService.syncUpsertSend(notification.payload as SyncSendNotification, + notification.type === NotificationType.SyncSendUpdate); + break; + case NotificationType.SyncSendDelete: + await this.syncService.syncDeleteSend(notification.payload as SyncSendNotification); default: break; } diff --git a/src/services/sync.service.ts b/src/services/sync.service.ts index 791d5f0240..a609748b58 100644 --- a/src/services/sync.service.ts +++ b/src/services/sync.service.ts @@ -25,6 +25,7 @@ import { FolderResponse } from '../models/response/folderResponse'; import { SyncCipherNotification, SyncFolderNotification, + SyncSendNotification, } from '../models/response/notificationResponse'; import { PolicyResponse } from '../models/response/policyResponse'; import { ProfileResponse } from '../models/response/profileResponse'; @@ -212,6 +213,37 @@ export class SyncService implements SyncServiceAbstraction { return this.syncCompleted(false); } + async syncUpsertSend(notification: SyncSendNotification, isEdit: boolean): Promise { + this.syncStarted(); + if (await this.userService.isAuthenticated()) { + try { + const localSend = await this.sendService.get(notification.id); + if ((!isEdit && localSend == null) || + (isEdit && localSend != null && localSend.revisionDate < notification.revisionDate)) { + const remoteSend = await this.apiService.getSend(notification.id); + if (remoteSend != null) { + const userId = await this.userService.getUserId(); + await this.sendService.upsert(new SendData(remoteSend, userId)); + this.messagingService.send('syncedUpsertedSend', { sendId: notification.id }); + return this.syncCompleted(true); + } + } + } catch { } + } + return this.syncCompleted(false); + } + + async syncDeleteSend(notification: SyncSendNotification): Promise { + this.syncStarted(); + if (await this.userService.isAuthenticated()) { + await this.sendService.delete(notification.id); + this.messagingService.send('syncedDeletedSend', { sendId: notification.id }); + this.syncCompleted(true); + return true; + } + return this.syncCompleted(false); + } + // Helpers private syncStarted() {