frnt: Simplified chatmodel getmessages

This commit is contained in:
Rohan Sircar 2020-05-27 18:18:43 +05:30
parent 7c6d4a5c05
commit e32283a60b

View File

@ -3,15 +3,15 @@ import { Observer } from "../observe/Observer";
import { JsonAPI } from "../singleton/JsonAPI"; import { JsonAPI } from "../singleton/JsonAPI";
import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel"; import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
import { ChatModelHelper } from "./ChatModelHelper"; import { ChatModelHelper } from "./ChatModelHelper";
import log = require('loglevel'); import log = require("loglevel");
import { ObserverData } from "../observe/ObserverData"; import { ObserverData } from "../observe/ObserverData";
import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel"; import { ActiveUserViewModel } from "../viewmodel/ActiveUserViewModel";
import moment = require("moment"); import moment = require("moment");
interface Params { interface Params {
userName: string, userName: string;
data: ChatMessageViewModel[], data: ChatMessageViewModel[];
op: string op: string;
} }
export class ChatModel implements Subject<ChatMessageViewModel> { export class ChatModel implements Subject<ChatMessageViewModel> {
@ -34,32 +34,52 @@ export class ChatModel implements Subject<ChatMessageViewModel> {
* The subscription management methods. * The subscription management methods.
*/ */
public attach(observer: Observer<ChatMessageViewModel>): void { public attach(observer: Observer<ChatMessageViewModel>): void {
log.info('Subject: Attached an observer.'); log.info("Subject: Attached an observer.");
this._observers.push(observer); this._observers.push(observer);
} }
public detach(observer: Observer<ChatMessageViewModel>): void { public detach(observer: Observer<ChatMessageViewModel>): void {
const observerIndex = this._observers.indexOf(observer); const observerIndex = this._observers.indexOf(observer);
this._observers.splice(observerIndex, 1); this._observers.splice(observerIndex, 1);
log.info('Subject: Detached an observer.'); log.info("Subject: Detached an observer.");
} }
private storeUserMessages(username: string, messages: ChatMessageViewModel[], op: string) { private storeUserMessages(
username: string,
messages: ChatMessageViewModel[],
op: string
) {
switch (op) { switch (op) {
case "clear": this._messagesMap.set(username, []); break; case "clear":
case "page": this._messagesMap.set(username, messages.concat(this.getStoredUserMessages(username))); break; this._messagesMap.set(username, []);
break;
case "page":
this._messagesMap.set(
username,
messages.concat(this.getStoredUserMessages(username))
);
break;
// case "page": this._messagesMap.set(username, messages); // case "page": this._messagesMap.set(username, messages);
case "new": this._messagesMap.set(username, this.getStoredUserMessages(username).concat(messages)); break; case "new":
case "update": this._messagesMap.set(username, this.getStoredUserMessages(username).concat(messages)); break; this._messagesMap.set(
default: new Error("Invalid option"); username,
this.getStoredUserMessages(username).concat(messages)
);
break;
case "update":
this._messagesMap.set(
username,
this.getStoredUserMessages(username).concat(messages)
);
break;
default:
new Error("Invalid option");
} }
} }
private getStoredUserMessages(username: string): ChatMessageViewModel[] { private getStoredUserMessages(username: string): ChatMessageViewModel[] {
let temp = this._messagesMap.get(username); let temp = this._messagesMap.get(username);
if (temp == null) if (temp == null) return [];
return [];
else { else {
return temp; return temp;
} }
@ -69,96 +89,131 @@ export class ChatModel implements Subject<ChatMessageViewModel> {
* Trigger an update in each subscriber. * Trigger an update in each subscriber.
*/ */
public notify(p: Params): void { public notify(p: Params): void {
log.info('Subject: Notifying observers...'); log.info("Subject: Notifying observers...");
switch (p.op) { switch (p.op) {
case "clear": { case "clear":
const od: ObserverData<ChatMessageViewModel> = { data: [], op: "clear" } {
const od: ObserverData<ChatMessageViewModel> = {
data: [],
op: "clear",
};
for (const observer of this._observers) { for (const observer of this._observers) {
observer.update(od); observer.update(od);
} }
} break; }
case "new": { break;
const od: ObserverData<ChatMessageViewModel> = { data: p.data, op: p.op } case "new":
{
const od: ObserverData<ChatMessageViewModel> = {
data: p.data,
op: p.op,
};
for (const observer of this._observers) { for (const observer of this._observers) {
observer.update(od); observer.update(od);
} }
} break; }
case "page": { break;
const od: ObserverData<ChatMessageViewModel> = { data: p.data, op: p.op } case "page":
{
const od: ObserverData<ChatMessageViewModel> = {
data: p.data,
op: p.op,
};
for (const observer of this._observers) { for (const observer of this._observers) {
observer.update(od); observer.update(od);
} }
} break; }
case "update": { break;
const od: ObserverData<ChatMessageViewModel> = { data: p.data, op: p.op } case "update":
{
const od: ObserverData<ChatMessageViewModel> = {
data: p.data,
op: p.op,
};
for (const observer of this._observers) { for (const observer of this._observers) {
observer.update(od); observer.update(od);
} }
} break; }
default: { log.error("error") } break;
default: {
log.error("error");
}
} }
} }
public someBusinessMethod(chatMessageList: ChatMessageViewModel[]): void { public someBusinessMethod(chatMessageList: ChatMessageViewModel[]): void {}
}
public clear(): void { public clear(): void {
log.info("Clearing model") log.info("Clearing model");
this._messagePageMap.set(JsonAPI.contactName!, 0); this._messagePageMap.set(JsonAPI.contactName!, 0);
this.storeUserMessages(JsonAPI.contactName!, Array(), "clear"); this.storeUserMessages(JsonAPI.contactName!, Array(), "clear");
this.notify({ userName: "", data: [], op: "clear" }) this.notify({ userName: "", data: [], op: "clear" });
} }
public async getMessages(vm: ActiveUserViewModel, op: string): Promise<ChatMessageViewModel[]> { public async getMessages(
if (this._messagePageMap.get(vm.userName!) == null) aVm: ActiveUserViewModel,
this._messagePageMap.set(vm.userName!, 0); op: string
): Promise<ChatMessageViewModel[]> {
if (this._messagePageMap.get(aVm.userName!) == null)
this._messagePageMap.set(aVm.userName!, 0);
const pageNumber = this._messagePageMap.get(vm.userName!) const pageNumber = this._messagePageMap.get(aVm.userName!);
const cVMs = await this._chatModelHelper.getMessages(vm.userName!, vm.passphrase, pageNumber!, vm.lastMessageTime!, op); const cVMs = await this._chatModelHelper.getMessages(
let cVMs2 = Array(); aVm.userName!,
if (cVMs != null) { aVm.passphrase,
log.info('Subject: My state has just changed') pageNumber!,
const existingMessages = this.getStoredUserMessages(vm.userName!); aVm.lastMessageTime!,
log.debug('existing message:') op
);
let cVMsFiltered = Array<ChatMessageViewModel>();
if (cVMs != null && cVMs.length != 0) {
log.info("Subject: My state has just changed");
const existingMessages = this.getStoredUserMessages(aVm.userName!);
log.debug("existing message:");
log.debug(existingMessages); log.debug(existingMessages);
log.debug('new messages:') log.debug("new messages:");
log.debug(cVMs); log.debug(cVMs);
cVMs2 = cVMs.filter(c => {
const res = existingMessages.filter(m => { // filter duplicates
if (moment(c.messageTime).isSame(moment(m.messageTime))) cVMsFiltered = cVMs.filter((c) => {
const res = existingMessages.filter((m) => {
if (moment(c.messageTime).isSame(moment(m.messageTime))) return true;
});
if (res.length > 0) return false;
return true; return true;
}) });
if (res.length > 0)
return false; if (op == "update") {
return true; // update the active user last message text and times for each user
}) const lastMessage = cVMsFiltered[cVMsFiltered.length - 1];
if (cVMs2.length != 0 && op == "update") { cVMsFiltered.forEach((v) => {
const lastMessageText = vm.lastMessageText = cVMs2[cVMs2.length - 1].message; if (v.fromUser == aVm.userName) {
cVMs2.forEach(v => { aVm.lastMessageTime = lastMessage.messageTime;
if (v.userName == vm.userName) { aVm.lastMessageText = lastMessage.message.slice(0, 15) + "...";
v.lastMessageTime = cVMs2[cVMs2.length - 1].messageTime;
v.lastMessageText = cVMs2[cVMs2.length - 1].message.slice(0, 15) + "..."
} }
}) });
}; } else {
this._messagePageMap.set(
if (cVMs2.length != 0 && op != "update") { aVm.userName!,
this._messagePageMap.get(aVm.userName!)! + 1
this._messagePageMap.set(vm.userName!, this._messagePageMap.get(vm.userName!)! + 1); );
} }
this.storeUserMessages(vm.userName!, cVMs2, op); this.storeUserMessages(aVm.userName!, cVMsFiltered, op);
this.notify({ userName: vm.userName!, data: cVMs2, op: op }); this.notify({ userName: aVm.userName!, data: cVMsFiltered, op: op });
}
else {
log.error('Messages were null');
} }
return cVMs2; return cVMsFiltered;
} }
public async isPassphraseValid(passphrase: string, userName: string): Promise<boolean> { public async isPassphraseValid(
let valid = await this._chatModelHelper.isPassphraseValid(passphrase, userName); passphrase: string,
userName: string
): Promise<boolean> {
let valid = await this._chatModelHelper.isPassphraseValid(
passphrase,
userName
);
return valid; return valid;
} }
} }