diff --git a/chatto/src/main/javascript/ts/src/main.ts b/chatto/src/main/javascript/ts/src/main.ts index 5710a84..8fc0c09 100644 --- a/chatto/src/main/javascript/ts/src/main.ts +++ b/chatto/src/main/javascript/ts/src/main.ts @@ -31,6 +31,7 @@ import { UserViewDeps } from "./view/UserViewDeps"; import { ChatViewDeps } from "./view/ChatViewDeps"; import { MarkDownItMarkDownService } from "./service/MarkDownItMarkDownService"; import { Sprintf } from "./singleton/Sprintf"; +import { EncryptionServiceFactory } from "./service/EncryptionServiceFactory"; const usersListElement = document.getElementById('contacts-box'); const userSearchButton = document.getElementById('user-search'); @@ -84,7 +85,7 @@ const cvDeps: ChatViewDeps = { messageSendTemplate: TemplateFactory.getTemplate('msg_container_send_template'), messageReceiveTemplate: TemplateFactory.getTemplate('msg_container_template'), markdownService: new MarkDownItMarkDownService, - encryptionService: new SJCLEncryptionService + encryptionService: EncryptionServiceFactory.getEncryptionService() } const chatView = new ChatView(cvDeps); @@ -109,7 +110,7 @@ var msgContainerTemplate = Handlebars.compile(source); JsonAPI.ACTIVE_USERS_GET + 'aef'; -const encryptionService: EncryptionService = new SJCLEncryptionService(); +const encryptionService: EncryptionService = EncryptionServiceFactory.getEncryptionService(); let messageCipherDTO: MessageCipherDTO = encryptionService.encrypt("password", "data"); console.log(encryptionService.decrypt("password", messageCipherDTO)); diff --git a/chatto/src/main/javascript/ts/src/model/ChatModel.ts b/chatto/src/main/javascript/ts/src/model/ChatModel.ts index 5ae8b42..1f14929 100644 --- a/chatto/src/main/javascript/ts/src/model/ChatModel.ts +++ b/chatto/src/main/javascript/ts/src/model/ChatModel.ts @@ -75,7 +75,7 @@ export class ChatModel implements Subject { // log.debug('page number after = ' + this._messagePageMap.get(contactName)!) // } const pageNumber = this._messagePageMap.get(contactName) - const cVMs = await ChatModelHelper.getMessages(contactName, passphrase, pageNumber!, lastMessageTime, this); + const cVMs = await ChatModelHelper.getMessages(contactName, passphrase, pageNumber!, lastMessageTime); if (cVMs != null) { log.info('Subject: My state has just changed') diff --git a/chatto/src/main/javascript/ts/src/model/ChatModelHelper.ts b/chatto/src/main/javascript/ts/src/model/ChatModelHelper.ts index 21b60d4..ed44a5f 100644 --- a/chatto/src/main/javascript/ts/src/model/ChatModelHelper.ts +++ b/chatto/src/main/javascript/ts/src/model/ChatModelHelper.ts @@ -1,23 +1,21 @@ import * as log from "loglevel"; import { ChatMessageDTO } from "../dto/ChatMessageDTO"; import { EncryptionService } from "../service/EncryptionService"; -import { SJCLEncryptionService } from "../service/SJCLEncryptionService"; +import { EncryptionServiceFactory } from "../service/EncryptionServiceFactory"; import { JsonAPI } from "../singleton/JsonAPI"; import { Sprintf } from "../singleton/Sprintf"; import { ChatMessageViewModel } from "../viewmodel/ChatMessageViewModel"; -import { ChatModel } from "./ChatModel"; import { fetchErrorHandler } from "./FetchErrorHandler"; export class ChatModelHelper { - private static readonly _encryptionService: EncryptionService = new SJCLEncryptionService(); + private static readonly _encryptionService: EncryptionService = EncryptionServiceFactory.getEncryptionService(); - public static async getMessages(userName: string, passphrase: string, page: number | null, lastMessageTime: string | null, chatModel: ChatModel): Promise { + public static async getMessages(userName: string, passphrase: string, page: number | null, lastMessageTime: string | null): Promise { switch (lastMessageTime) { case null: { const data: ChatMessageDTO[] = await this.getPaginatedMessagesAjax(userName, page!); - const data2 = Promise.all(data.map(vm => this.toChatMessageVMAsync(vm, passphrase)).reverse()); - return data2; - // return data.map(vm => this.toChatMessageVM(vm, passphrase)).reverse(); + const cVMs = Promise.all(data.map(vm => this.toChatMessageVMAsync(vm, passphrase)).reverse()); + return cVMs; } default: { const data: ChatMessageDTO[] = await this.getNewMessagesAjax(userName, lastMessageTime); diff --git a/chatto/src/main/javascript/ts/src/service/EncryptionServiceFactory.ts b/chatto/src/main/javascript/ts/src/service/EncryptionServiceFactory.ts new file mode 100644 index 0000000..8d146b5 --- /dev/null +++ b/chatto/src/main/javascript/ts/src/service/EncryptionServiceFactory.ts @@ -0,0 +1,13 @@ +import { EncryptionService } from "./EncryptionService"; +import { SJCLEncryptionService } from "./SJCLEncryptionService"; +import PromiseWorker = require('promise-worker'); +// import PromiseWorker from "promise-worker"; + +export class EncryptionServiceFactory { + private static readonly _worker = new Worker('/js/worker.js'); + // @ts-ignore + private static readonly _promiseWorker = new PromiseWorker(EncryptionServiceFactory._worker); + public static getEncryptionService(): EncryptionService { + return new SJCLEncryptionService(this._promiseWorker) + } +} \ No newline at end of file diff --git a/chatto/src/main/javascript/ts/src/service/SJCLEncryptionService.ts b/chatto/src/main/javascript/ts/src/service/SJCLEncryptionService.ts index 6635de0..01f5164 100644 --- a/chatto/src/main/javascript/ts/src/service/SJCLEncryptionService.ts +++ b/chatto/src/main/javascript/ts/src/service/SJCLEncryptionService.ts @@ -1,12 +1,16 @@ import { EncryptionService } from "./EncryptionService"; import * as sjcl from "sjcl"; import { MessageCipherDTO } from "../dto/MessageCipherDTO"; -import PromiseWorker = require('promise-worker'); export class SJCLEncryptionService implements EncryptionService { - private readonly _params = { mode: "gcm", ts: 128, adata: "", iter: 10000 }; - private readonly _worker = new Worker('/js/worker.js'); + private _params = { mode: "gcm", ts: 128, adata: "", iter: 10000 }; // @ts-ignore - private readonly _promiseWorker = new PromiseWorker(this._worker); + private readonly _promiseWorker: any; + + constructor(promiseWorker: any ) { + this._promiseWorker = promiseWorker; + } + + public encrypt(passphrase: string, plainText: string): MessageCipherDTO { // @ts-ignore diff --git a/chatto/src/main/resources/static/js/worker.js b/chatto/src/main/resources/static/js/worker.js index fb11894..0088fc7 100644 --- a/chatto/src/main/resources/static/js/worker.js +++ b/chatto/src/main/resources/static/js/worker.js @@ -1,10 +1,10 @@ // worker.js importScripts('https://unpkg.com/promise-worker/dist/promise-worker.register.js'); +// importScripts('https://unpkg.com/promise-worker@2.0.1/dist/promise-worker.register.js') importScripts('https://cdnjs.cloudflare.com/ajax/libs/sjcl/1.0.8/sjcl.min.js'); registerPromiseWorker((payload) => { // console.log(payload) // console.log('decrypted = ' + sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher))); - // return 'pong'; return sjcl.decrypt(payload.passphrase, JSON.stringify(payload.cipher)) }); \ No newline at end of file