|
|
@ -18,23 +18,32 @@ export class ChatModelHelper { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async getMessages(userName: string, passphrase: string, page: number | null, lastMessageTime: string | null, op: string): Promise<ChatMessageViewModel[]> { |
|
|
|
switch (lastMessageTime) { |
|
|
|
case null: { |
|
|
|
const data: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(userName, page!); |
|
|
|
const cVMs = Promise.all(data.map(vm => this._toChatMessageVMAsync(vm, passphrase)).reverse()); |
|
|
|
return cVMs; |
|
|
|
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()); |
|
|
|
return cVMs; |
|
|
|
} |
|
|
|
default: { |
|
|
|
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!); |
|
|
|
return data.map(vm => this._toChatMessageVM(vm, passphrase)); |
|
|
|
} |
|
|
|
default: { |
|
|
|
log.error("Invalid operation"); |
|
|
|
return Array(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async isPassphraseValid(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 { |
|
|
|
this._encryptionService.decrypt(passphrase, messages[0].messageCipher) |
|
|
|
} catch (error) { |
|
|
@ -102,17 +111,30 @@ export class ChatModelHelper { |
|
|
|
return null; |
|
|
|
} |
|
|
|
const data: Promise<any> = await response.json(); |
|
|
|
return data; |
|
|
|
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: string): 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); |
|
|
|
const response = await fetch(`${JsonAPI.CHAT_MESSAGES_GET}/${toUser}/${lastMessageTimeStamp}`, { |
|
|
|
// 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 |
|
|
|
}); |
|
|
@ -121,6 +143,52 @@ export class ChatModelHelper { |
|
|
|
return null; |
|
|
|
} |
|
|
|
const data: Promise<any> = await response.json(); |
|
|
|
return data; |
|
|
|
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) |
|
|
|
log.debug("LOOK HERE NOW ", data, data2) |
|
|
|
return data2; |
|
|
|
} |
|
|
|
} |