A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
import { ChatMessageDTO } from "../dto/ChatMessageDTO";
import * as log from "loglevel";
import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel";
import { JsonAPI } from "../singleton/JsonAPI";
import { fetchErrorHandler } from "./FetchErrorHandler";
import { EncryptionService } from "../service/EncryptionService"; import { SJCLEncryptionService } from "../service/SJCLEncryptionService"; import { ChatModel } from "./ChatModel" export class ChatModelHelper { private static readonly encryptionService = new SJCLEncryptionService();
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) });
// 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));
} default: { // 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)); } }
// return null;
}
private static toChatMessageVM(chatMessageDTO: ChatMessageDTO, passphrase: string): ChatMessageViewModel { const vm = new ChatMessageViewModel(); vm.fromUser = chatMessageDTO.fromUser; vm.toUser = chatMessageDTO.toUser; vm.messageTime = chatMessageDTO.messageTime; vm.message = this.encryptionService.decrypt(passphrase, chatMessageDTO.messageCipher) as string; return vm; }
private static 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); const response = await fetch(`${JsonAPI.CHAT_MESSAGES_GET}/${toUser}`, { method: 'GET', headers: headers }); console.log(response.clone()); if (fetchErrorHandler(response.clone())) { return null; } const data: Promise<any> = await response.json(); return data; }
private static async getNewMessagesAjax(toUser: string, lastMessageTimeStamp: string): 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}`, { method: 'GET', headers: headers }); console.log(response.clone()); if (fetchErrorHandler(response.clone())) { return null; } const data: Promise<any> = await response.json(); return data; } }
|