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
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
});
}
}

View File

@ -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;
}

View File

@ -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<ActiveUserViewModel> {
private readonly _model: UserModel;
@ -72,21 +73,7 @@ export class UserView implements Observer<ActiveUserViewModel> {
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<ActiveUserViewModel> {
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);