From 858b6ce0359dfae48e4645a384e65c135b4d8087 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 3 Jun 2020 13:41:43 +0530 Subject: [PATCH] Refactored passphrase prompt --- .../service/AlertifyNotificationService.ts | 61 ++------------ .../ts/src/service/NotificationService.ts | 7 +- src/main/javascript/ts/src/view/UserView.ts | 81 ++++++++++++------- 3 files changed, 60 insertions(+), 89 deletions(-) diff --git a/src/main/javascript/ts/src/service/AlertifyNotificationService.ts b/src/main/javascript/ts/src/service/AlertifyNotificationService.ts index 6c7189f..622bbb3 100644 --- a/src/main/javascript/ts/src/service/AlertifyNotificationService.ts +++ b/src/main/javascript/ts/src/service/AlertifyNotificationService.ts @@ -1,10 +1,8 @@ -import { NotificationService } from "./NotificationService"; // @ts-ignore import * as alertify from "alertifyjs"; -import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel"; +import { NotificationService } from "./NotificationService"; import log = require("loglevel"); import bootbox = require("bootbox"); -import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel"; export class AlertifyNotificationService implements NotificationService { private readonly _alertify = alertify; constructor() { @@ -29,57 +27,12 @@ export class AlertifyNotificationService implements NotificationService { this._alertify.message(message); } passphrasePrompt( - vm: ActiveUserViewModel, - vms: ActiveUserViewModel[], - cb1: (v: ActiveUserViewModel, op: string) => ChatMessageViewModel[], - cb2: (...x: any) => any, - cb3: (...x: any) => any + cb: (result: string) => void ): void { - if (!vm.passphrase) { - bootbox.prompt({ - title: "Please enter the passphrase", - inputType: "password", - callback: async function (result) { - if (result) { - const valid = await cb3(result, vm.userName); - if (!valid) { - bootbox.alert("Some error occured. Please check your password"); - log.error("invalid password"); - return; - } - vm.unlocked = true; - vm.passphrase = result; - const chatMessages: ChatMessageViewModel[] = await cb1(vm, "new"); - - vms - .filter((v) => v.userName == vm.userName) - .map((v) => { - v.passphrase = result; - v.unlocked = true; - if (chatMessages.length > 0) { - v.lastMessageTime = new Date( - chatMessages[chatMessages.length - 1].messageTime - ); - const lastMessageText = (v.lastMessageText = - chatMessages[chatMessages.length - 1].message); - if (lastMessageText.length > 15) { - v.lastMessageText = - chatMessages[chatMessages.length - 1].message.slice( - 0, - 15 - ) + "..."; - } - } - }); - // vm.lastMessageTime = new Date(chatMessages[chatMessages.length - 1].messageTime); - cb2(vm, vms); - // log.debug(vm); - // log.debug(vms); - } - }, - }); - } else { - cb1(vm, "new"); - } + bootbox.prompt({ + title: "Please enter the passphrase", + inputType: "password", + callback: cb + }); } } diff --git a/src/main/javascript/ts/src/service/NotificationService.ts b/src/main/javascript/ts/src/service/NotificationService.ts index 101d59e..e9205a1 100644 --- a/src/main/javascript/ts/src/service/NotificationService.ts +++ b/src/main/javascript/ts/src/service/NotificationService.ts @@ -1,4 +1,3 @@ -import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel"; export interface NotificationService { success(message: string): void; @@ -6,8 +5,6 @@ export interface NotificationService { errorWithDelay(message: string, delay: number): void; warning(message: string): void; message(message: string): void; - passphrasePrompt(vm: ActiveUserViewModel, vms: ActiveUserViewModel[], - cb1: (...x: any) => any, - cb2: (...x: any) => any, - cb3: (...x: any) => any): void; + passphrasePrompt( + cb: (result: string) => void): void; } \ No newline at end of file diff --git a/src/main/javascript/ts/src/view/UserView.ts b/src/main/javascript/ts/src/view/UserView.ts index 6aefff7..1398b5a 100644 --- a/src/main/javascript/ts/src/view/UserView.ts +++ b/src/main/javascript/ts/src/view/UserView.ts @@ -9,6 +9,7 @@ import { UserViewDeps } from "./UserViewDeps"; import { ObserverData } from "../observe/ObserverData"; import { JsonAPI } from "../singleton/JsonAPI"; import { NotificationService } from "../service/NotificationService"; +import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel"; export class UserView implements Observer { private readonly _model: UserModel; @@ -72,21 +73,7 @@ export class UserView implements Observer { clearInterval(this._newMessagesLoop); let current = document.getElementsByClassName("user-box active"); - let passphrase: string = ""; if (current.length > 0) { - // let passphraseInput = document.getElementById('passphrase') as any; - - // if (passphraseInput == null) { - // log.error('passphraseInput element reference is null'); - // return; - // } - // passphrase = passphraseInput.value - // if (passphrase == '' || passphrase == null) { - // // alert('Please input passphrase') - // // alertify.error('Please enter a passphrase'); - // log.error('passphrase is empty or null'); - // return; - // } current[0].className = current[0].className.replace(" active", ""); } // Add the active class to the current/clicked button @@ -102,31 +89,65 @@ export class UserView implements Observer { JsonAPI.contactName = userName; // @ts-ignore: Object is possibly 'null'. document.getElementById("user-name-span").innerText = userName; - let vm = this._model.activeUsersList.find((vm) => vm.userName === userName); - if (!vm) { - vm = new ActiveUserViewModel(); - vm.userName = userName; + let currentUser = this._model.activeUsersList.find((vm) => vm.userName === userName) || new ActiveUserViewModel(); + currentUser.userName = userName; + + if (!currentUser?.passphrase) { + this._notificationService.passphrasePrompt( + async (result) => { + if (result) { + + const valid = await this._chatModel.isPassphraseValid(result, currentUser.userName!); + if (!valid) { + bootbox.alert("Some error occured. Please check your password"); + log.error("invalid password"); + return; + } + currentUser.unlocked = true; + currentUser.passphrase = result; + const chatMessages: ChatMessageViewModel[] = await this._chatModel.getMessages(currentUser, "new"); + + this._model.activeUsersList + .filter((v) => v.userName == currentUser!.userName) + .forEach((v) => { + v.passphrase = result; + v.unlocked = true; + if (chatMessages.length > 0) { + v.lastMessageTime = new Date( + chatMessages[chatMessages.length - 1].messageTime + ); + const lastMessageText = (v.lastMessageText = + chatMessages[chatMessages.length - 1].message); + if (lastMessageText.length > 15) { + v.lastMessageText = + chatMessages[chatMessages.length - 1].message.slice( + 0, + 15 + ) + "..."; + } + } + }); + this._promptHandler(currentUser); + + } + } + ); } - this._notificationService.passphrasePrompt( - vm, - this._model.activeUsersList, - this._chatModel.getMessages.bind(this._chatModel), - this._promptHandler.bind(this), - this._chatModel.isPassphraseValid.bind(this._chatModel) - ); - // this._chatModel.getMessages(userName, vm.passphrase, null, "new"); + else { + this._chatModel.getMessages(currentUser, "new"); + } + el.className += " active"; - log.debug("loop", this._newMessagesLoop); - if (vm.unlocked && vm.lastMessageTime) { + if (currentUser.unlocked && currentUser.lastMessageTime) { this._newMessagesLoop = setInterval( - this._chatModel.getMessages.bind(this._chatModel, vm, "update"), + this._chatModel.getMessages.bind(this._chatModel, currentUser, "update"), 10_000 ); this._model.notify(); } } - private _promptHandler(vm: ActiveUserViewModel, vms: ActiveUserViewModel[]) { + private _promptHandler(vm: ActiveUserViewModel) { // vms.filter(v => v.userName == vm.userName).map(v => v.userName = vm.userName) // log.debug(vms);