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";
export class ChatMessageDTO {
public toUser: string | undefined;
public fromUser: string | undefined;
public toUser: string = "";
public fromUser: string = "";
public messageCipher!: MessageCipherDTO;
public messageTime: Date | undefined | null;
public messageTime: Date = new Date();
}

View File

@ -11,28 +11,50 @@ export class ChatModelHelper {
private readonly _encryptionService: EncryptionService;
private readonly _notificationService: NotificationService;
constructor(encryptionService: EncryptionService, notificationService: NotificationService) {
constructor(
encryptionService: EncryptionService,
notificationService: NotificationService
) {
this._encryptionService = encryptionService;
this._notificationService = notificationService;
}
public async getMessages(userName: string, passphrase: string, page: number | null, lastMessageTime: Date | null, op: string): Promise<ChatMessageViewModel[]> {
public async getMessages(
userName: string,
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());
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());
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());
const data: ChatMessageDTO[] = await this._getNewMessagesAjax(
userName,
lastMessageTime!
);
const cVMs = Promise.all(
data.map((vm) => this._toChatMessageVMAsync(vm, passphrase)).reverse()
);
return cVMs;
}
default: {
@ -42,34 +64,56 @@ export class ChatModelHelper {
}
}
public async isPassphraseValid(passphrase: string, userName: string): Promise<boolean> {
const messages: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(userName, 0);
public async isPassphraseValid(
passphrase: string,
userName: string
): Promise<boolean> {
const messages: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(
userName,
0
);
if (messages.length === 0) return true;
try {
this._encryptionService.decrypt(passphrase, messages[0].messageCipher)
this._encryptionService.decrypt(passphrase, messages[0].messageCipher);
} catch (error) {
return false;
}
return true
return true;
}
private async _toChatMessageVMAsync(chatMessageDTO: ChatMessageDTO, passphrase: string): Promise<ChatMessageViewModel> {
private async _toChatMessageVMAsync(
chatMessageDTO: ChatMessageDTO,
passphrase: string
): Promise<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 = await this._encryptionService.decryptAsPromise(passphrase, chatMessageDTO.messageCipher);
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 {
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;
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;
}
@ -78,13 +122,13 @@ export class ChatModelHelper {
if (JsonAPI.authToken == null) {
log.error("authToken null");
return;
};
headers.append('X-AUTH-TOKEN', JsonAPI.authToken);
}
headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
const url = Sprintf(JsonAPI.CHAT_MESSAGES_GET, toUser);
log.debug(url)
log.debug(url);
const response = await fetch(url, {
method: 'GET',
headers: headers
method: "GET",
headers: headers,
});
log.debug(response.clone());
if (fetchErrorHandler(response.clone(), this._notificationService)) {
@ -94,18 +138,21 @@ export class ChatModelHelper {
return data;
}
private async _getPaginatedMessagesAjax(toUser: string, page: number): Promise<any> {
private async _getPaginatedMessagesAjax(
toUser: string,
page: number
): Promise<any> {
const headers = new Headers();
if (JsonAPI.authToken == null) {
log.error("authToken null");
return;
};
headers.append('X-AUTH-TOKEN', JsonAPI.authToken);
}
headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
const url = Sprintf(JsonAPI.CHAT_MESSAGE_PAGE_GET, toUser, page, 5);
log.debug(url)
log.debug(url);
const response = await fetch(url, {
method: 'GET',
headers: headers
method: "GET",
headers: headers,
});
log.debug(response.clone());
if (fetchErrorHandler(response.clone(), this._notificationService)) {
@ -114,30 +161,32 @@ export class ChatModelHelper {
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null)
return null;
if (d.messageTime == null) return null;
d.messageTime = new Date(d.messageTime)
d.messageTime = new Date(d.messageTime);
return d;
});
return d1;
}
const data2 = func(data)
const data2 = func(data);
return data2;
}
private async _getMessagesAjax(toUser: string, lastMessageTimeStamp: Date): Promise<any> {
private async _getMessagesAjax(
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);
}
headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
// const url = Sprintf(JsonAPI.CHAT_MESSAGES_GET, toUser, page, 5);
// log.debug(url)
const response = await fetch(`/api/chat/get/messages/${toUser}`, {
method: 'GET',
headers: headers
method: "GET",
headers: headers,
});
log.debug(response.clone());
if (fetchErrorHandler(response.clone(), this._notificationService)) {
@ -146,33 +195,38 @@ export class ChatModelHelper {
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null)
return null;
if (d.messageTime == null) return null;
d.messageTime = new Date(d.messageTime)
d.messageTime = new Date(d.messageTime);
return d;
});
return d1;
}
const data2 = func(data)
const data2 = func(data);
return data2;
}
private async _getNewMessagesAjax(toUser: string, lastMessageTimeStamp: Date): Promise<any> {
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);
}
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
});
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;
@ -180,16 +234,14 @@ export class ChatModelHelper {
const data: Promise<any> = await response.json();
function func(data: any) {
const d1 = data.map((d: any) => {
if (d.messageTime == null)
return null;
if (d.messageTime == null) return null;
d.messageTime = new Date(d.messageTime)
d.messageTime = new Date(d.messageTime);
return d;
});
return d1;
}
const data2 = func(data)
log.debug("LOOK HERE NOW ", data, data2)
const data2 = func(data);
return data2;
}
}