diff --git a/packages/kite-chat/src/kite-chat.ts b/packages/kite-chat/src/kite-chat.ts index a7e7440..3df2bad 100644 --- a/packages/kite-chat/src/kite-chat.ts +++ b/packages/kite-chat/src/kite-chat.ts @@ -32,7 +32,8 @@ import { getMessages, addMessage, modifyMessage, - deleteMessage + deleteMessage, + messageById } from './kite-storage'; export type KiteChatOptions = { @@ -142,6 +143,7 @@ export class KiteChat { return; } addMessage(msg, this.db); + this.element?.appendMsg(msg); } private update(messageId: string, updatedMsg: ContentMsg) { @@ -155,10 +157,13 @@ export class KiteChat { if(!this.db) { return; } + const msgElement = document.querySelector( + `${KiteMsgElement.TAG}[messageId="${messageId}"]` + ) as KiteFileElement | undefined; + msgElement?.remove(); deleteMessage(messageId, this.db); } - private restore() { if(!this.db) { return; @@ -209,7 +214,7 @@ export class KiteChat { throw new Error('Not connected'); } console.debug('outgoing', outgoing); - this.save(outgoing); + this.db && addMessage(outgoing, this.db); this.kiteWorker.port.postMessage(outgoing); } @@ -268,8 +273,16 @@ export class KiteChat { protected onContentMessage(incoming: ContentMsg) { console.debug('onContentMessage', incoming.messageId, incoming.timestamp); - this.save(incoming); - this.element?.appendMsg(incoming); + if(!this.db) { + return; + } + messageById(incoming.messageId, this.db).then(message => { + if(!message) { + this.db && this.save(incoming); + } else { + this.db && this.update(incoming.messageId, incoming); + } + }) } protected onConnected(payload: Connected) { @@ -305,13 +318,7 @@ export class KiteChat { this.update(e.messageId, { file: e.file, } as ContentMsg); - e.zippedIds.forEach(id => { - const msgElement = document.querySelector( - `${KiteMsgElement.TAG}[messageId="${id}"]` - ) as KiteFileElement | undefined; - msgElement?.remove(); - this.delete(id); - }) + e.zippedIds.forEach(id => this.delete(id)); } protected onFailedMessage(e: FailedMsg) { diff --git a/packages/kite-chat/src/kite-types.ts b/packages/kite-chat/src/kite-types.ts index 66866eb..e624da3 100644 --- a/packages/kite-chat/src/kite-types.ts +++ b/packages/kite-chat/src/kite-types.ts @@ -102,6 +102,7 @@ export type BinaryMsg = { fileType?: string; fileSize?: number; timestamp: Date; + status?: MsgStatus; }; export type Connected = { diff --git a/packages/kite-chat/src/serialization.test.ts b/packages/kite-chat/src/serialization.test.ts index 0fbab0b..aa8d361 100644 --- a/packages/kite-chat/src/serialization.test.ts +++ b/packages/kite-chat/src/serialization.test.ts @@ -19,4 +19,22 @@ describe('serialization', () => { expect(decoded).toStrictEqual(plaintext); }); + + it('should be able to encode kite message with status', () => { + const plaintext: PlaintextMsg = { + type: MsgType.PLAINTEXT, + messageId: 'ui4bJAV0Jo', + text: 'hello', + timestamp: new Date('2023-11-23T13:19:07.255Z'), + status: 2, + }; + + const encoded = JSON.stringify(["TXT","ui4bJAV0Jo","hello","2023-11-23T13:19:07.255Z",2]); + console.log(encoded); + + const decoded = decodeKiteMsg(encoded); + console.log(decoded); + + expect(decoded).toStrictEqual(plaintext); + }); }); diff --git a/packages/kite-chat/src/serialization.ts b/packages/kite-chat/src/serialization.ts index 09cebce..9676c8a 100644 --- a/packages/kite-chat/src/serialization.ts +++ b/packages/kite-chat/src/serialization.ts @@ -78,6 +78,16 @@ const BINARY_MESSAGE_FIELDS: Array = [ 'timestamp', ]; +const INCOMING_PLAINTEXT_MESSAGE_FIELDS: Array = [ + ...PLAINTEXT_MESSAGE_FIELDS, + 'status', +]; + +const INCOMING_BINARY_MESSAGE_FIELDS: Array = [ + ...BINARY_MESSAGE_FIELDS, + 'status', +]; + const UPLOAD_REQUEST_FIELDS: Array = [ 'type', 'messageId', @@ -104,9 +114,9 @@ const KITE_MSG_DECODERS: Partial> = { [MsgType.ACK]: decoderFactory(MESSAGE_ACK_FIELDS), [MsgType.OK]: decoderFactory(OK_FIELDS), [MsgType.ERROR]: decoderFactory(ERROR_RESPONSE_FIELDS), - [MsgType.PLAINTEXT]: decoderFactory(PLAINTEXT_MESSAGE_FIELDS), + [MsgType.PLAINTEXT]: decoderFactory(INCOMING_PLAINTEXT_MESSAGE_FIELDS), [MsgType.UPLOAD]: decoderFactory(UPLOAD_RESPONSE_FIELDS), - [MsgType.BIN]: decoderFactory(BINARY_MESSAGE_FIELDS), + [MsgType.BIN]: decoderFactory(INCOMING_BINARY_MESSAGE_FIELDS), [MsgType.PONG]: decoderFactory(PONG_FIELDS), };