Refactored passphrase prompt

This commit is contained in:
Rohan Sircar 2020-06-03 13:41:43 +05:30
parent 7a7748cfa6
commit 858b6ce035
3 changed files with 60 additions and 89 deletions

View File

@ -1,10 +1,8 @@
import { NotificationService } from "./NotificationService";
// @ts-ignore // @ts-ignore
import * as alertify from "alertifyjs"; import * as alertify from "alertifyjs";
import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel"; import { NotificationService } from "./NotificationService";
import log = require("loglevel"); import log = require("loglevel");
import bootbox = require("bootbox"); import bootbox = require("bootbox");
import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
export class AlertifyNotificationService implements NotificationService { export class AlertifyNotificationService implements NotificationService {
private readonly _alertify = alertify; private readonly _alertify = alertify;
constructor() { constructor() {
@ -29,57 +27,12 @@ export class AlertifyNotificationService implements NotificationService {
this._alertify.message(message); this._alertify.message(message);
} }
passphrasePrompt( passphrasePrompt(
vm: ActiveUserViewModel, cb: (result: string) => void
vms: ActiveUserViewModel[],
cb1: (v: ActiveUserViewModel, op: string) => ChatMessageViewModel[],
cb2: (...x: any) => any,
cb3: (...x: any) => any
): void { ): void {
if (!vm.passphrase) {
bootbox.prompt({ bootbox.prompt({
title: "Please enter the passphrase", title: "Please enter the passphrase",
inputType: "password", inputType: "password",
callback: async function (result) { callback: cb
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");
}
} }
} }

View File

@ -1,4 +1,3 @@
import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel";
export interface NotificationService { export interface NotificationService {
success(message: string): void; success(message: string): void;
@ -6,8 +5,6 @@ export interface NotificationService {
errorWithDelay(message: string, delay: number): void; errorWithDelay(message: string, delay: number): void;
warning(message: string): void; warning(message: string): void;
message(message: string): void; message(message: string): void;
passphrasePrompt(vm: ActiveUserViewModel, vms: ActiveUserViewModel[], passphrasePrompt(
cb1: (...x: any) => any, cb: (result: string) => void): void;
cb2: (...x: any) => any,
cb3: (...x: any) => any): void;
} }

View File

@ -9,6 +9,7 @@ import { UserViewDeps } from "./UserViewDeps";
import { ObserverData } from "../observe/ObserverData"; import { ObserverData } from "../observe/ObserverData";
import { JsonAPI } from "../singleton/JsonAPI"; import { JsonAPI } from "../singleton/JsonAPI";
import { NotificationService } from "../service/NotificationService"; import { NotificationService } from "../service/NotificationService";
import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
export class UserView implements Observer<ActiveUserViewModel> { export class UserView implements Observer<ActiveUserViewModel> {
private readonly _model: UserModel; private readonly _model: UserModel;
@ -72,21 +73,7 @@ export class UserView implements Observer<ActiveUserViewModel> {
clearInterval(this._newMessagesLoop); clearInterval(this._newMessagesLoop);
let current = document.getElementsByClassName("user-box active"); let current = document.getElementsByClassName("user-box active");
let passphrase: string = "";
if (current.length > 0) { 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", ""); current[0].className = current[0].className.replace(" active", "");
} }
// Add the active class to the current/clicked button // Add the active class to the current/clicked button
@ -102,31 +89,65 @@ export class UserView implements Observer<ActiveUserViewModel> {
JsonAPI.contactName = userName; JsonAPI.contactName = userName;
// @ts-ignore: Object is possibly 'null'. // @ts-ignore: Object is possibly 'null'.
document.getElementById("user-name-span").innerText = userName; document.getElementById("user-name-span").innerText = userName;
let vm = this._model.activeUsersList.find((vm) => vm.userName === userName); let currentUser = this._model.activeUsersList.find((vm) => vm.userName === userName) || new ActiveUserViewModel();
if (!vm) { currentUser.userName = userName;
vm = new ActiveUserViewModel();
vm.userName = userName; if (!currentUser?.passphrase) {
}
this._notificationService.passphrasePrompt( this._notificationService.passphrasePrompt(
vm, async (result) => {
this._model.activeUsersList, if (result) {
this._chatModel.getMessages.bind(this._chatModel),
this._promptHandler.bind(this), const valid = await this._chatModel.isPassphraseValid(result, currentUser.userName!);
this._chatModel.isPassphraseValid.bind(this._chatModel) 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
); );
// this._chatModel.getMessages(userName, vm.passphrase, null, "new"); 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);
}
}
);
}
else {
this._chatModel.getMessages(currentUser, "new");
}
el.className += " active"; el.className += " active";
log.debug("loop", this._newMessagesLoop); if (currentUser.unlocked && currentUser.lastMessageTime) {
if (vm.unlocked && vm.lastMessageTime) {
this._newMessagesLoop = setInterval( this._newMessagesLoop = setInterval(
this._chatModel.getMessages.bind(this._chatModel, vm, "update"), this._chatModel.getMessages.bind(this._chatModel, currentUser, "update"),
10_000 10_000
); );
this._model.notify(); 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) // vms.filter(v => v.userName == vm.userName).map(v => v.userName = vm.userName)
// log.debug(vms); // log.debug(vms);