1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-06-25 10:25:36 +02:00

added Send sync notification support (#250)

This commit is contained in:
Addison Beck 2021-01-22 16:52:08 -05:00 committed by GitHub
parent 6ac6df75d7
commit 68bd93e45b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 0 deletions

View File

@ -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<boolean>;
syncUpsertCipher: (notification: SyncCipherNotification, isEdit: boolean) => Promise<boolean>;
syncDeleteCipher: (notification: SyncFolderNotification) => Promise<boolean>;
syncUpsertSend: (notification: SyncSendNotification, isEdit: boolean) => Promise<boolean>;
syncDeleteSend: (notification: SyncSendNotification) => Promise<boolean>;
}

View File

@ -13,4 +13,8 @@ export enum NotificationType {
SyncSettings = 10,
LogOut = 11,
SyncSendCreate = 12,
SyncSendUpdate = 13,
SyncSendDelete = 14,
}

View File

@ -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'));
}
}

View File

@ -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;
}

View File

@ -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<boolean> {
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<boolean> {
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() {