@ -10,39 +10,44 @@ import { fetchErrorHandler } from "./FetchErrorHandler";
import { EncryptionService } from "../service/EncryptionService" ;
import { SJCLEncryptionService } from "../service/SJCLEncryptionService" ;
import { ChatModel } from "./ChatModel" ;
import { ChatModel } from "./ChatModel"
export class ChatModelHelper {
private static readonly encryptionService = new SJCLEncryptionService ( ) ;
public static getMessages ( userName : string , passphrase : string , lastMessageTime : string | null , chatModel : ChatModel ) : ChatMessageViewModel [ ] | null {
public static async getMessages ( userName : string , passphrase : string , lastMessageTime : string | null , chatModel : ChatModel ) : Promise < ChatMessageViewModel [ ] > {
switch ( lastMessageTime ) {
case null : {
this . getAllMessagesAjax ( userName )
. then ( ( data : ChatMessageDTO [ ] ) = > {
log . debug ( ` Subject: received all messages ` ) ;
// let userNames = data.map(ChatMessageViewModel => ChatMessageViewModel.fromUser)
// let sumt = data.map(chatMessageViewModel => { return this.encryptionService.decrypt(passphrase, chatMessageViewModel.messageCipher) });
// this.getAllMessagesAjax(userName)
// .then((data: ChatMessageDTO[]) => {
// log.debug(`Subject: received all messages`);
// // let userNames = data.map(ChatMessageViewModel => ChatMessageViewModel.fromUser)
// // let sumt = data.map(chatMessageViewModel => { return this.encryptionService.decrypt(passphrase, chatMessageViewModel.messageCipher) });
// return data.map(vm => this.toChatMessageVM(vm, passphrase));
// // chatModel.setUserMessages(userName, chatMessageVMs);
// // chatModel.notify();
// })
// break;
const data : ChatMessageDTO [ ] = await this . getAllMessagesAjax ( userName ) ;
return data . map ( vm = > this . toChatMessageVM ( vm , passphrase ) ) ;
// chatModel.setUserMessages(userName, chatMessageVMs);
// chatModel.notify();
} )
break ;
}
default : {
this . getNewMessagesAjax ( userName , lastMessageTime )
. then ( ( data : ChatMessageDTO [ ] ) = > {
log . debug ( ` Subject: received new messages ` ) ;
// this.getNewMessagesAjax(userName, lastMessageTime)
// .then((data: ChatMessageDTO[]) => {
// log.debug(`Subject: received new messages`);
// return data.map(vm => this.toChatMessageVM(vm, passphrase));
// // chatModel.setUserMessages(userName, chatMessageVMs);
// // this.state = data;
// // chatModel.notify();
// })
// break;
const data : ChatMessageDTO [ ] = await this . getNewMessagesAjax ( userName , lastMessageTime ) ;
return data . map ( vm = > this . toChatMessageVM ( vm , passphrase ) ) ;
// chatModel.setUserMessages(userName, chatMessageVMs);
// this.state = data;
// chatModel.notify();
} )
break ;
}
}
return null ;
// return null;
}
private static toChatMessageVM ( chatMessageDTO : ChatMessageDTO , passphrase : string ) : ChatMessageViewModel {
@ -55,13 +60,13 @@ export class ChatModelHelper {
}
private static async getAllMessagesAjax ( toUser : string ) : Promise < any > {
le t headers = new Headers ( ) ;
cons t headers = new Headers ( ) ;
if ( JsonAPI . authToken == null ) {
log . error ( "authToken null" ) ;
return ;
} ;
headers . append ( 'X-AUTH-TOKEN' , JsonAPI . authToken ) ;
le t response = await fetch ( ` ${ JsonAPI . CHAT_MESSAGES_GET } / ${ toUser } ` , {
cons t response = await fetch ( ` ${ JsonAPI . CHAT_MESSAGES_GET } / ${ toUser } ` , {
method : 'GET' ,
headers : headers
} ) ;
@ -69,18 +74,18 @@ export class ChatModelHelper {
if ( fetchErrorHandler ( response . clone ( ) ) ) {
return null ;
}
le t data : Promise < any > = await response . json ( ) ;
cons t data : Promise < any > = await response . json ( ) ;
return data ;
}
private static async getNewMessagesAjax ( toUser : string , lastMessageTimeStamp : string ) : Promise < any > {
le t headers = new Headers ( ) ;
cons t headers = new Headers ( ) ;
if ( JsonAPI . authToken == null ) {
log . error ( "authToken null" ) ;
return ;
} ;
headers . append ( 'X-AUTH-TOKEN' , JsonAPI . authToken ) ;
le t response = await fetch ( ` ${ JsonAPI . CHAT_MESSAGES_GET } / ${ toUser } / ${ lastMessageTimeStamp } ` , {
cons t response = await fetch ( ` ${ JsonAPI . CHAT_MESSAGES_GET } / ${ toUser } / ${ lastMessageTimeStamp } ` , {
method : 'GET' ,
headers : headers
} ) ;
@ -88,7 +93,7 @@ export class ChatModelHelper {
if ( fetchErrorHandler ( response . clone ( ) ) ) {
return null ;
}
le t data : Promise < any > = await response . json ( ) ;
cons t data : Promise < any > = await response . json ( ) ;
return data ;
}
}