Declare and use types for notificationQueue

This commit is contained in:
Daniel James Smith 2021-10-15 19:47:48 +02:00
parent d1977f1f08
commit 92459c6098
No known key found for this signature in database
GPG Key ID: 03E4BD365FF06726
5 changed files with 30 additions and 22 deletions

View File

@ -1,9 +1,6 @@
export default class AddChangePasswordQueueMessage {
type: string;
import NotificationQueueMessage from "./notificationQueueMessage";
export default class AddChangePasswordQueueMessage extends NotificationQueueMessage {
cipherId: string;
newPassword: string;
domain: string;
tabId: string;
expires: Date;
wasVaultLocked: boolean;
}

View File

@ -1,10 +1,7 @@
export default class AddLoginQueueMessage {
type: string;
import NotificationQueueMessage from "./notificationQueueMessage";
export default class AddLoginQueueMessage extends NotificationQueueMessage {
username: string;
password: string;
domain: string;
uri: string;
tabId: string;
expires: Date;
wasVaultLocked: boolean;
}

View File

@ -0,0 +1,9 @@
import { NotificationQueueMessageType } from "./NotificationQueueMessageType";
export default class NotificationQueueMessage {
type: NotificationQueueMessageType;
domain: string;
tabId: number;
expires: Date;
wasVaultLocked: boolean;
}

View File

@ -0,0 +1,4 @@
export enum NotificationQueueMessageType {
addLogin = 'addLogin',
changePassword = 'changePassword',
}

View File

@ -22,10 +22,11 @@ import { PolicyType } from 'jslib-common/enums/policyType';
import AddChangePasswordQueueMessage from './models/addChangePasswordQueueMessage';
import AddLoginQueueMessage from './models/addLoginQueueMessage';
import { NotificationQueueMessageType } from './models/NotificationQueueMessageType';
export default class NotificationBackground {
private notificationQueue: any[] = [];
private notificationQueue: (AddLoginQueueMessage | AddChangePasswordQueueMessage)[] = [];
constructor(private main: MainBackground, private autofillService: AutofillService,
private cipherService: CipherService, private storageService: StorageService,
@ -131,14 +132,14 @@ export default class NotificationBackground {
continue;
}
if (this.notificationQueue[i].type === 'addLogin') {
if (this.notificationQueue[i].type === NotificationQueueMessageType.addLogin) {
BrowserApi.tabSendMessageData(tab, 'openNotificationBar', {
type: 'add',
typeData: {
isVaultLocked: this.notificationQueue[i].wasVaultLocked,
},
});
} else if (this.notificationQueue[i].type === 'changePassword') {
} else if (this.notificationQueue[i].type === NotificationQueueMessageType.changePassword) {
BrowserApi.tabSendMessageData(tab, 'openNotificationBar', {
type: 'change',
typeData: {
@ -204,7 +205,7 @@ export default class NotificationBackground {
// remove any old messages for this tab
this.removeTabFromNotificationQueue(tab);
const message: AddLoginQueueMessage = {
type: 'addLogin',
type: NotificationQueueMessageType.addLogin,
username: loginInfo.username,
password: loginInfo.password,
domain: loginDomain,
@ -247,7 +248,7 @@ export default class NotificationBackground {
// remove any old messages for this tab
this.removeTabFromNotificationQueue(tab);
const message: AddChangePasswordQueueMessage = {
type: 'changePassword',
type: NotificationQueueMessageType.changePassword,
cipherId: cipherId,
newPassword: newPassword,
domain: loginDomain,
@ -263,7 +264,7 @@ export default class NotificationBackground {
for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
const queueMessage = this.notificationQueue[i];
if (queueMessage.tabId !== tab.id ||
(queueMessage.type !== 'addLogin' && queueMessage.type !== 'changePassword')) {
(queueMessage.type !== NotificationQueueMessageType.addLogin && queueMessage.type !== NotificationQueueMessageType.changePassword)) {
continue;
}
@ -275,7 +276,7 @@ export default class NotificationBackground {
this.notificationQueue.splice(i, 1);
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
if (queueMessage.type === 'changePassword') {
if (queueMessage.type === NotificationQueueMessageType.changePassword) {
const message = (queueMessage as AddChangePasswordQueueMessage);
const cipher = await this.getDecryptedCipherById(message.cipherId);
if (cipher == null) {
@ -286,11 +287,11 @@ export default class NotificationBackground {
}
if (!queueMessage.wasVaultLocked) {
await this.createNewCipher(queueMessage, folderId);
await this.createNewCipher(queueMessage as AddLoginQueueMessage, folderId);
}
// If the vault was locked, check if a cipher needs updating instead of creating a new one
if (queueMessage.type === 'addLogin' && queueMessage.wasVaultLocked === true) {
if (queueMessage.type === NotificationQueueMessageType.addLogin && queueMessage.wasVaultLocked === true) {
const message = (queueMessage as AddLoginQueueMessage);
const ciphers = await this.cipherService.getAllDecryptedForUrl(message.uri);
const usernameMatches = ciphers.filter(c => c.login.username != null &&
@ -349,7 +350,7 @@ export default class NotificationBackground {
private async saveNever(tab: chrome.tabs.Tab) {
for (let i = this.notificationQueue.length - 1; i >= 0; i--) {
const queueMessage = this.notificationQueue[i];
if (queueMessage.tabId !== tab.id || queueMessage.type !== 'addLogin') {
if (queueMessage.tabId !== tab.id || queueMessage.type !== NotificationQueueMessageType.addLogin) {
continue;
}