From 50c94f587dbc097464a429703b5c9298c743fb7a Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 20 Aug 2018 17:40:39 -0400 Subject: [PATCH] implement notifications service --- jslib | 2 +- package-lock.json | 5 +++++ package.json | 1 + src/_locales/en/messages.json | 3 +++ src/background/main.background.ts | 14 +++++++++++--- src/background/runtime.background.ts | 9 ++++++++- src/popup/accounts/environment.component.html | 5 +++++ src/popup/services/services.module.ts | 6 ++++++ src/popup/vault/ciphers.component.ts | 8 +++++--- src/popup/vault/groupings.component.ts | 8 +++++--- src/popup/vault/view.component.html | 2 +- src/popup/vault/view.component.ts | 16 ++++++++++------ 12 files changed, 61 insertions(+), 18 deletions(-) diff --git a/jslib b/jslib index b64757132f..21e0953589 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit b64757132faf1ebb5438bec00720c58604fd29f6 +Subproject commit 21e09535896078c4716205e8141f6d3b2182a981 diff --git a/package-lock.json b/package-lock.json index 8e27d601ef..40f71b7206 100644 --- a/package-lock.json +++ b/package-lock.json @@ -96,6 +96,11 @@ "tslib": "^1.7.1" } }, + "@aspnet/signalr": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@aspnet/signalr/-/signalr-1.0.2.tgz", + "integrity": "sha512-sXleqUCCbodCOqUA8MjLSvtAgDTvDhEq6j3JyAq/w4RMJhpZ+dXK9+6xEMbzag2hisq5e/8vDC82JYutkcOISQ==" + }, "@ngtools/webpack": { "version": "1.10.2", "resolved": "https://registry.npmjs.org/@ngtools/webpack/-/webpack-1.10.2.tgz", diff --git a/package.json b/package.json index 50c758ef13..37877164b1 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "@angular/platform-browser-dynamic": "5.2.0", "@angular/router": "5.2.0", "@angular/upgrade": "5.2.0", + "@aspnet/signalr": "1.0.2", "angular2-toaster": "4.0.2", "angulartics2": "5.0.1", "core-js": "2.4.1", diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index b9caaa8def..f6e770e82f 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -767,6 +767,9 @@ "identityUrl": { "message": "Identity Server URL" }, + "notificationsUrl": { + "message": "Notifications Server URL" + }, "iconsUrl": { "message": "Icons Server URL" }, diff --git a/src/background/main.background.ts b/src/background/main.background.ts index e79d6f9f6b..cd9187d8ba 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -20,6 +20,7 @@ import { UserService, } from 'jslib/services'; import { ExportService } from 'jslib/services/export.service'; +import { NotificationsService } from 'jslib/services/notifications.service'; import { SearchService } from 'jslib/services/search.service'; import { WebCryptoFunctionService } from 'jslib/services/webCryptoFunction.service'; @@ -45,6 +46,7 @@ import { UserService as UserServiceAbstraction, } from 'jslib/abstractions'; import { ExportService as ExportServiceAbstraction } from 'jslib/abstractions/export.service'; +import { NotificationsService as NotificationsServiceAbstraction } from 'jslib/abstractions/notifications.service'; import { SearchService as SearchServiceAbstraction } from 'jslib/abstractions/search.service'; import { Analytics } from 'jslib/misc'; @@ -93,6 +95,7 @@ export default class MainBackground { auditService: AuditServiceAbstraction; exportService: ExportServiceAbstraction; searchService: SearchServiceAbstraction; + notificationsService: NotificationsServiceAbstraction; analytics: Analytics; onUpdatedRan: boolean; @@ -128,7 +131,6 @@ export default class MainBackground { this.appIdService = new AppIdService(this.storageService); this.apiService = new ApiService(this.tokenService, this.platformUtilsService, async (expired: boolean) => await this.logout(expired)); - this.environmentService = new EnvironmentService(this.apiService, this.storageService); this.userService = new UserService(this.tokenService, this.storageService); this.settingsService = new SettingsService(this.userService, this.storageService); this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService, @@ -155,6 +157,10 @@ export default class MainBackground { this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService); this.auditService = new AuditService(cryptoFunctionService, this.apiService); this.exportService = new ExportService(this.folderService, this.cipherService, this.apiService); + this.notificationsService = new NotificationsService(this.userService, this.tokenService, + this.syncService, this.appIdService); + this.environmentService = new EnvironmentService(this.apiService, this.storageService, + this.notificationsService); this.analytics = new Analytics(window, () => BrowserApi.gaFilter(), this.platformUtilsService, this.storageService, this.appIdService); @@ -166,7 +172,7 @@ export default class MainBackground { // Background this.runtimeBackground = new RuntimeBackground(this, this.autofillService, this.cipherService, this.platformUtilsService as BrowserPlatformUtilsService, this.storageService, this.i18nService, - this.analytics); + this.analytics, this.notificationsService); this.tabsBackground = new TabsBackground(this, this.platformUtilsService); this.commandsBackground = new CommandsBackground(this, this.passwordGenerationService, this.platformUtilsService, this.analytics); @@ -202,7 +208,8 @@ export default class MainBackground { await this.environmentService.setUrlsFromStorage(); await this.setIcon(); this.cleanupNotificationQueue(); - await this.fullSync(true); + this.fullSync(true); + setTimeout(() => this.notificationsService.init(this.environmentService), 2500); resolve(); }, 500); }); @@ -271,6 +278,7 @@ export default class MainBackground { await this.setIcon(); await this.refreshBadgeAndMenu(); + this.notificationsService.updateConnection(); } collectPageDetailsForContentScript(tab: any, sender: string, frameId: number = null) { diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index a7123408ff..0cd5aa9203 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -22,6 +22,8 @@ import MainBackground from './main.background'; import { AutofillService } from '../services/abstractions/autofill.service'; import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service'; +import { NotificationsService } from 'jslib/abstractions/notifications.service'; + import { Utils } from 'jslib/misc/utils'; export default class RuntimeBackground { @@ -33,7 +35,8 @@ export default class RuntimeBackground { constructor(private main: MainBackground, private autofillService: AutofillService, private cipherService: CipherService, private platformUtilsService: BrowserPlatformUtilsService, - private storageService: StorageService, private i18nService: I18nService, private analytics: Analytics) { + private storageService: StorageService, private i18nService: I18nService, + private analytics: Analytics, private notificationsService: NotificationsService) { this.isSafari = this.platformUtilsService.isSafari(); this.runtime = this.isSafari ? safari.application : chrome.runtime; @@ -77,6 +80,10 @@ export default class RuntimeBackground { async processMessage(msg: any, sender: any, sendResponse: any) { switch (msg.command) { case 'loggedIn': + await this.main.setIcon(); + await this.main.refreshBadgeAndMenu(false); + this.notificationsService.updateConnection(); + break; case 'unlocked': case 'locked': await this.main.setIcon(); diff --git a/src/popup/accounts/environment.component.html b/src/popup/accounts/environment.component.html index d0ea49d81e..d518900b83 100644 --- a/src/popup/accounts/environment.component.html +++ b/src/popup/accounts/environment.component.html @@ -48,6 +48,11 @@ +
+ + +
('appIdService'), deps: [] }, { provide: AutofillService, useFactory: getBgService('autofillService'), deps: [] }, { provide: ExportService, useFactory: getBgService('exportService'), deps: [] }, + { + provide: NotificationsService, + useFactory: getBgService('notificationsService'), + deps: [], + }, { provide: APP_INITIALIZER, useFactory: initFactory, diff --git a/src/popup/vault/ciphers.component.ts b/src/popup/vault/ciphers.component.ts index 821bf8c502..236f039c14 100644 --- a/src/popup/vault/ciphers.component.ts +++ b/src/popup/vault/ciphers.component.ts @@ -121,9 +121,11 @@ export class CiphersComponent extends BaseCiphersComponent implements OnInit, On this.ngZone.run(async () => { switch (message.command) { case 'syncCompleted': - window.setTimeout(() => { - this.refresh(); - }, 500); + if (message.successfully) { + window.setTimeout(() => { + this.refresh(); + }, 500); + } break; default: break; diff --git a/src/popup/vault/groupings.component.ts b/src/popup/vault/groupings.component.ts index 2edc7eae56..c49e54bb5b 100644 --- a/src/popup/vault/groupings.component.ts +++ b/src/popup/vault/groupings.component.ts @@ -94,9 +94,11 @@ export class GroupingsComponent extends BaseGroupingsComponent implements OnInit this.ngZone.run(async () => { switch (message.command) { case 'syncCompleted': - window.setTimeout(() => { - this.load(); - }, 500); + if (message.successfully) { + window.setTimeout(() => { + this.load(); + }, 500); + } break; default: break; diff --git a/src/popup/vault/view.component.html b/src/popup/vault/view.component.html index d164dd26c2..05dc485847 100644 --- a/src/popup/vault/view.component.html +++ b/src/popup/vault/view.component.html @@ -268,7 +268,7 @@ diff --git a/src/popup/vault/view.component.ts b/src/popup/vault/view.component.ts index 6a523cc7a6..670b74a452 100644 --- a/src/popup/vault/view.component.ts +++ b/src/popup/vault/view.component.ts @@ -1,7 +1,8 @@ import { Location } from '@angular/common'; import { + ChangeDetectorRef, Component, - OnInit, + NgZone, } from '@angular/core'; import { ActivatedRoute, @@ -11,8 +12,6 @@ import { import { ToasterService } from 'angular2-toaster'; import { Angulartics2 } from 'angulartics2'; -import { BrowserApi } from '../../browser/browserApi'; - import { AuditService } from 'jslib/abstractions/audit.service'; import { CipherService } from 'jslib/abstractions/cipher.service'; import { CryptoService } from 'jslib/abstractions/crypto.service'; @@ -21,13 +20,15 @@ import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { TokenService } from 'jslib/abstractions/token.service'; import { TotpService } from 'jslib/abstractions/totp.service'; +import { BroadcasterService } from 'jslib/angular/services/broadcaster.service'; + import { ViewComponent as BaseViewComponent } from 'jslib/angular/components/view.component'; @Component({ selector: 'app-vault-view', templateUrl: 'view.component.html', }) -export class ViewComponent extends BaseViewComponent implements OnInit { +export class ViewComponent extends BaseViewComponent { showAttachments = true; constructor(cipherService: CipherService, totpService: TotpService, @@ -35,9 +36,11 @@ export class ViewComponent extends BaseViewComponent implements OnInit { cryptoService: CryptoService, platformUtilsService: PlatformUtilsService, i18nService: I18nService, analytics: Angulartics2, auditService: AuditService, private route: ActivatedRoute, - private router: Router, private location: Location) { + private router: Router, private location: Location, + broadcasterService: BroadcasterService, ngZone: NgZone, + changeDetectorRef: ChangeDetectorRef) { super(cipherService, totpService, tokenService, toasterService, cryptoService, platformUtilsService, - i18nService, analytics, auditService, window); + i18nService, analytics, auditService, window, broadcasterService, ngZone, changeDetectorRef); } ngOnInit() { @@ -51,6 +54,7 @@ export class ViewComponent extends BaseViewComponent implements OnInit { await this.load(); }); + super.ngOnInit(); } edit() {