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