frnt: Applied null pattern to chatdto

This commit is contained in:
Rohan Sircar 2020-05-27 18:18:57 +05:30
parent e32283a60b
commit aff632a091
2 changed files with 226 additions and 174 deletions

View File

@ -1,8 +1,8 @@
import { MessageCipherDTO } from "./MessageCipherDTO"; import { MessageCipherDTO } from "./MessageCipherDTO";
export class ChatMessageDTO { export class ChatMessageDTO {
public toUser: string | undefined; public toUser: string = "";
public fromUser: string | undefined; public fromUser: string = "";
public messageCipher!: MessageCipherDTO; public messageCipher!: MessageCipherDTO;
public messageTime: Date | undefined | null; public messageTime: Date = new Date();
} }

View File

@ -8,188 +8,240 @@ import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
import { fetchErrorHandler } from "./FetchErrorHandler"; import { fetchErrorHandler } from "./FetchErrorHandler";
export class ChatModelHelper { export class ChatModelHelper {
private readonly _encryptionService: EncryptionService; private readonly _encryptionService: EncryptionService;
private readonly _notificationService: NotificationService; private readonly _notificationService: NotificationService;
constructor(
encryptionService: EncryptionService,
notificationService: NotificationService
) {
this._encryptionService = encryptionService;
this._notificationService = notificationService;
}
constructor(encryptionService: EncryptionService, notificationService: NotificationService) { public async getMessages(
this._encryptionService = encryptionService; userName: string,
this._notificationService = notificationService; passphrase: string,
page: number | null,
lastMessageTime: Date | null,
op: string
): Promise<ChatMessageViewModel[]> {
switch (op) {
case "page": {
const data: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(
userName,
page!
);
const cVMs = Promise.all(
data.map((vm) => this._toChatMessageVMAsync(vm, passphrase)).reverse()
);
return cVMs;
}
case "new": {
const data: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(
userName,
page!
);
const cVMs = Promise.all(
data.map((vm) => this._toChatMessageVMAsync(vm, passphrase)).reverse()
);
return cVMs;
}
case "update": {
const data: ChatMessageDTO[] = await this._getNewMessagesAjax(
userName,
lastMessageTime!
);
const cVMs = Promise.all(
data.map((vm) => this._toChatMessageVMAsync(vm, passphrase)).reverse()
);
return cVMs;
}
default: {
log.error("Invalid operation");
return Array();
}
} }
}
public async isPassphraseValid(
public async getMessages(userName: string, passphrase: string, page: number | null, lastMessageTime: Date | null, op: string): Promise<ChatMessageViewModel[]> { passphrase: string,
switch (op) { userName: string
case "page": { ): Promise<boolean> {
const data: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(userName, page!); const messages: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(
const cVMs = Promise.all(data.map(vm => this._toChatMessageVMAsync(vm, passphrase)).reverse()); userName,
return cVMs; 0
} );
case "new": { if (messages.length === 0) return true;
const data: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(userName, page!); try {
const cVMs = Promise.all(data.map(vm => this._toChatMessageVMAsync(vm, passphrase)).reverse()); this._encryptionService.decrypt(passphrase, messages[0].messageCipher);
return cVMs; } catch (error) {
} return false;
case "update": {
const data: ChatMessageDTO[] = await this._getNewMessagesAjax(userName, lastMessageTime!);
const cVMs = Promise.all(data.map(vm => this._toChatMessageVMAsync(vm, passphrase)).reverse());
return cVMs;
}
default: {
log.error("Invalid operation");
return Array();
}
}
} }
return true;
}
public async isPassphraseValid(passphrase: string, userName: string): Promise<boolean> { private async _toChatMessageVMAsync(
const messages: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(userName, 0); chatMessageDTO: ChatMessageDTO,
if (messages.length === 0) return true; passphrase: string
try { ): Promise<ChatMessageViewModel> {
this._encryptionService.decrypt(passphrase, messages[0].messageCipher) const vm = new ChatMessageViewModel();
} catch (error) { vm.fromUser = chatMessageDTO.fromUser;
return false; vm.toUser = chatMessageDTO.toUser;
} // vm.messageTime = chatMessageDTO.messageTime;
return true chatMessageDTO.messageTime == null
? log.error("Message time somehow null")
: (vm.messageTime = chatMessageDTO.messageTime);
vm.message = await this._encryptionService.decryptAsPromise(
passphrase,
chatMessageDTO.messageCipher
);
return vm;
}
private _toChatMessageVM(
chatMessageDTO: ChatMessageDTO,
passphrase: string
): ChatMessageViewModel {
const vm = new ChatMessageViewModel();
vm.fromUser = chatMessageDTO.fromUser;
vm.toUser = chatMessageDTO.toUser;
// vm.messageTime = chatMessageDTO.messageTime;
chatMessageDTO.messageTime == null
? log.error("Message time somehow null")
: (vm.messageTime = chatMessageDTO.messageTime);
vm.message = this._encryptionService.decrypt(
passphrase,
chatMessageDTO.messageCipher
) as string;
return vm;
}
private async _getAllMessagesAjax(toUser: string): Promise<any> {
const headers = new Headers();
if (JsonAPI.authToken == null) {
log.error("authToken null");
return;
} }
headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
private async _toChatMessageVMAsync(chatMessageDTO: ChatMessageDTO, passphrase: string): Promise<ChatMessageViewModel> { const url = Sprintf(JsonAPI.CHAT_MESSAGES_GET, toUser);
const vm = new ChatMessageViewModel(); log.debug(url);
vm.fromUser = chatMessageDTO.fromUser; const response = await fetch(url, {
vm.toUser = chatMessageDTO.toUser; method: "GET",
// vm.messageTime = chatMessageDTO.messageTime; headers: headers,
chatMessageDTO.messageTime == null ? log.error("Message time somehow null") : vm.messageTime = chatMessageDTO.messageTime; });
vm.message = await this._encryptionService.decryptAsPromise(passphrase, chatMessageDTO.messageCipher); log.debug(response.clone());
return vm; if (fetchErrorHandler(response.clone(), this._notificationService)) {
return null;
} }
const data: Promise<any> = await response.json();
return data;
}
private _toChatMessageVM(chatMessageDTO: ChatMessageDTO, passphrase: string): ChatMessageViewModel { private async _getPaginatedMessagesAjax(
const vm = new ChatMessageViewModel(); toUser: string,
vm.fromUser = chatMessageDTO.fromUser; page: number
vm.toUser = chatMessageDTO.toUser; ): Promise<any> {
// vm.messageTime = chatMessageDTO.messageTime; const headers = new Headers();
chatMessageDTO.messageTime == null ? log.error("Message time somehow null") : vm.messageTime = chatMessageDTO.messageTime; if (JsonAPI.authToken == null) {
vm.message = this._encryptionService.decrypt(passphrase, chatMessageDTO.messageCipher) as string; log.error("authToken null");
return vm; return;
} }
headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
private async _getAllMessagesAjax(toUser: string): Promise<any> { const url = Sprintf(JsonAPI.CHAT_MESSAGE_PAGE_GET, toUser, page, 5);
const headers = new Headers(); log.debug(url);
if (JsonAPI.authToken == null) { const response = await fetch(url, {
log.error("authToken null"); method: "GET",
return; headers: headers,
}; });
headers.append('X-AUTH-TOKEN', JsonAPI.authToken); log.debug(response.clone());
const url = Sprintf(JsonAPI.CHAT_MESSAGES_GET, toUser); if (fetchErrorHandler(response.clone(), this._notificationService)) {
log.debug(url) return null;
const response = await fetch(url, {
method: 'GET',
headers: headers
});
log.debug(response.clone());
if (fetchErrorHandler(response.clone(), this._notificationService)) {
return null;
}
const data: Promise<any> = await response.json();
return data;
} }
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null) return null;
private async _getPaginatedMessagesAjax(toUser: string, page: number): Promise<any> { d.messageTime = new Date(d.messageTime);
const headers = new Headers(); return d;
if (JsonAPI.authToken == null) { });
log.error("authToken null"); return d1;
return;
};
headers.append('X-AUTH-TOKEN', JsonAPI.authToken);
const url = Sprintf(JsonAPI.CHAT_MESSAGE_PAGE_GET, toUser, page, 5);
log.debug(url)
const response = await fetch(url, {
method: 'GET',
headers: headers
});
log.debug(response.clone());
if (fetchErrorHandler(response.clone(), this._notificationService)) {
return null;
}
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null)
return null;
d.messageTime = new Date(d.messageTime)
return d;
});
return d1;
}
const data2 = func(data)
return data2;
} }
const data2 = func(data);
return data2;
}
private async _getMessagesAjax(toUser: string, lastMessageTimeStamp: Date): Promise<any> { private async _getMessagesAjax(
const headers = new Headers(); toUser: string,
if (JsonAPI.authToken == null) { lastMessageTimeStamp: Date
log.error("authToken null"); ): Promise<any> {
return; const headers = new Headers();
}; if (JsonAPI.authToken == null) {
headers.append('X-AUTH-TOKEN', JsonAPI.authToken); log.error("authToken null");
// const url = Sprintf(JsonAPI.CHAT_MESSAGES_GET, toUser, page, 5); return;
// log.debug(url)
const response = await fetch(`/api/chat/get/messages/${toUser}`, {
method: 'GET',
headers: headers
});
log.debug(response.clone());
if (fetchErrorHandler(response.clone(), this._notificationService)) {
return null;
}
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null)
return null;
d.messageTime = new Date(d.messageTime)
return d;
});
return d1;
}
const data2 = func(data)
return data2;
} }
headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
private async _getNewMessagesAjax(toUser: string, lastMessageTimeStamp: Date): Promise<any> { // const url = Sprintf(JsonAPI.CHAT_MESSAGES_GET, toUser, page, 5);
const headers = new Headers(); // log.debug(url)
if (JsonAPI.authToken == null) { const response = await fetch(`/api/chat/get/messages/${toUser}`, {
log.error("authToken null"); method: "GET",
return; headers: headers,
}; });
headers.append('X-AUTH-TOKEN', JsonAPI.authToken); log.debug(response.clone());
// const url = Sprintf(JsonAPI.CHAT_MESSAGES_GET, toUser, page, 5); if (fetchErrorHandler(response.clone(), this._notificationService)) {
// log.debug(url) return null;
// log.debug(lastMessageTimeStamp);
// log.debug(lastMessageTimeStamp.toISOString())
const response = await fetch(`/api/chat/get/messages/${toUser}/${lastMessageTimeStamp.toISOString()}`, {
method: 'GET',
headers: headers
});
log.debug(response.clone());
if (fetchErrorHandler(response.clone(), this._notificationService)) {
return null;
}
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null)
return null;
d.messageTime = new Date(d.messageTime)
return d;
});
return d1;
}
const data2 = func(data)
log.debug("LOOK HERE NOW ", data, data2)
return data2;
} }
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null) return null;
d.messageTime = new Date(d.messageTime);
return d;
});
return d1;
}
const data2 = func(data);
return data2;
}
private async _getNewMessagesAjax(
toUser: string,
lastMessageTimeStamp: Date
): Promise<any> {
const headers = new Headers();
if (JsonAPI.authToken == null) {
log.error("authToken null");
return;
}
headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
// const url = Sprintf(JsonAPI.CHAT_MESSAGES_GET, toUser, page, 5);
// log.debug(url)
// log.debug(lastMessageTimeStamp);
// log.debug(lastMessageTimeStamp.toISOString())
const response = await fetch(
`/api/chat/get/messages/${toUser}/${lastMessageTimeStamp.toISOString()}`,
{
method: "GET",
headers: headers,
}
);
log.debug(response.clone());
if (fetchErrorHandler(response.clone(), this._notificationService)) {
return null;
}
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null) return null;
d.messageTime = new Date(d.messageTime);
return d;
});
return d1;
}
const data2 = func(data);
return data2;
}
} }