mirror of
https://github.com/bitwarden/browser
synced 2025-12-29 14:43:31 +00:00
notification service
This commit is contained in:
@@ -12,6 +12,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
|
||||
apiUrl: string;
|
||||
identityUrl: string;
|
||||
iconsUrl: string;
|
||||
notificationsUrl: string;
|
||||
|
||||
constructor(private apiService: ApiService, private storageService: StorageService) {}
|
||||
|
||||
@@ -31,6 +32,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
|
||||
api: null,
|
||||
identity: null,
|
||||
icons: null,
|
||||
notifications: null,
|
||||
webVault: null,
|
||||
};
|
||||
|
||||
@@ -46,6 +48,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
|
||||
this.apiUrl = envUrls.api = urls.api;
|
||||
this.identityUrl = envUrls.identity = urls.identity;
|
||||
this.iconsUrl = urls.icons;
|
||||
this.notificationsUrl = urls.notifications;
|
||||
await this.apiService.setUrls(envUrls);
|
||||
}
|
||||
|
||||
@@ -55,6 +58,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
|
||||
urls.api = this.formatUrl(urls.api);
|
||||
urls.identity = this.formatUrl(urls.identity);
|
||||
urls.icons = this.formatUrl(urls.icons);
|
||||
urls.notifications = this.formatUrl(urls.notifications);
|
||||
|
||||
await this.storageService.save(ConstantsService.environmentUrlsKey, {
|
||||
base: urls.base,
|
||||
@@ -62,6 +66,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
|
||||
identity: urls.identity,
|
||||
webVault: urls.webVault,
|
||||
icons: urls.icons,
|
||||
notifications: urls.notifications,
|
||||
});
|
||||
|
||||
this.baseUrl = urls.base;
|
||||
@@ -69,6 +74,7 @@ export class EnvironmentService implements EnvironmentServiceAbstraction {
|
||||
this.apiUrl = urls.api;
|
||||
this.identityUrl = urls.identity;
|
||||
this.iconsUrl = urls.icons;
|
||||
this.notificationsUrl = urls.notifications;
|
||||
|
||||
const envUrls = new EnvironmentUrls();
|
||||
if (this.baseUrl) {
|
||||
|
||||
90
src/services/notifications.service.ts
Normal file
90
src/services/notifications.service.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import * as signalR from '@aspnet/signalr';
|
||||
|
||||
import { NotificationType } from '../enums/notificationType';
|
||||
|
||||
import { CipherService } from '../abstractions/cipher.service';
|
||||
import { CollectionService } from '../abstractions/collection.service';
|
||||
import { EnvironmentService } from '../abstractions/environment.service';
|
||||
import { FolderService } from '../abstractions/folder.service';
|
||||
import { NotificationsService as NotificationsServiceAbstraction } from '../abstractions/notifications.service';
|
||||
import { SettingsService } from '../abstractions/settings.service';
|
||||
import { SyncService } from '../abstractions/sync.service';
|
||||
import { TokenService } from '../abstractions/token.service';
|
||||
import { UserService } from '../abstractions/user.service';
|
||||
|
||||
import { NotificationResponse } from '../models/response/notificationResponse';
|
||||
|
||||
export class NotificationsService implements NotificationsServiceAbstraction {
|
||||
private signalrConnection: signalR.HubConnection;
|
||||
|
||||
constructor(private userService: UserService, private tokenService: TokenService,
|
||||
private syncService: SyncService) { }
|
||||
|
||||
async init(environmentService: EnvironmentService): Promise<void> {
|
||||
let url = 'https://notifications.bitwarden.com';
|
||||
if (environmentService.notificationsUrl != null) {
|
||||
url = environmentService.notificationsUrl;
|
||||
} else if (environmentService.baseUrl != null) {
|
||||
url = environmentService.baseUrl + '/notifications';
|
||||
}
|
||||
|
||||
if (this.signalrConnection != null) {
|
||||
await this.signalrConnection.stop();
|
||||
this.signalrConnection = null;
|
||||
}
|
||||
|
||||
this.signalrConnection = new signalR.HubConnectionBuilder()
|
||||
.withUrl(url + '/hub', {
|
||||
accessTokenFactory: () => this.tokenService.getToken(),
|
||||
})
|
||||
.configureLogging(signalR.LogLevel.Information)
|
||||
.build();
|
||||
|
||||
this.signalrConnection.on('ReceiveMessage', async (data: any) => {
|
||||
await this.processNotification(new NotificationResponse(data));
|
||||
});
|
||||
|
||||
this.updateConnection();
|
||||
}
|
||||
|
||||
async updateConnection(): Promise<void> {
|
||||
try {
|
||||
if (await this.userService.isAuthenticated()) {
|
||||
await this.signalrConnection.start();
|
||||
} else {
|
||||
await this.signalrConnection.stop();
|
||||
}
|
||||
} catch (e) {
|
||||
// tslint:disable-next-line
|
||||
console.error(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private async processNotification(notification: NotificationResponse) {
|
||||
if (notification == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (notification.type) {
|
||||
case NotificationType.SyncCipherCreate:
|
||||
case NotificationType.SyncCipherDelete:
|
||||
case NotificationType.SyncCipherUpdate:
|
||||
case NotificationType.SyncLoginDelete:
|
||||
this.syncService.fullSync(false);
|
||||
break;
|
||||
case NotificationType.SyncFolderCreate:
|
||||
case NotificationType.SyncFolderDelete:
|
||||
case NotificationType.SyncFolderUpdate:
|
||||
this.syncService.fullSync(false);
|
||||
break;
|
||||
case NotificationType.SyncVault:
|
||||
case NotificationType.SyncCiphers:
|
||||
case NotificationType.SyncOrgKeys:
|
||||
case NotificationType.SyncSettings:
|
||||
this.syncService.fullSync(false);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user