From 749c15cf91c76e9dba665cc81c398df8b662ec2e Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 7 Oct 2021 15:09:49 +1100 Subject: [PATCH] Provide message timestamp to caller --- packages/status-communities/src/messenger.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/status-communities/src/messenger.ts b/packages/status-communities/src/messenger.ts index d5fe484c..45b4a78a 100644 --- a/packages/status-communities/src/messenger.ts +++ b/packages/status-communities/src/messenger.ts @@ -14,7 +14,9 @@ export class Messenger { waku: Waku; chatsById: Map; observers: { - [chatId: string]: Set<(message: ApplicationMetadataMessage) => void>; + [chatId: string]: Set< + (message: ApplicationMetadataMessage, timestamp: Date) => void + >; }; identity: Identity; @@ -49,13 +51,13 @@ export class Messenger { this.waku.relay.addObserver( (wakuMessage: WakuMessage) => { - if (!wakuMessage.payload) return; + if (!wakuMessage.payload || !wakuMessage.timestamp) return; const message = ApplicationMetadataMessage.decode(wakuMessage.payload); switch (message.type) { case ApplicationMetadataMessage_Type.TYPE_CHAT_MESSAGE: - this._handleNewChatMessage(chat, message); + this._handleNewChatMessage(chat, message, wakuMessage.timestamp); break; default: dbg("Received unsupported message type", message.type); @@ -100,7 +102,7 @@ export class Messenger { * @throws string If the chat has not been joined first using [joinChat]. */ public addObserver( - observer: (message: ApplicationMetadataMessage) => void, + observer: (message: ApplicationMetadataMessage, timestamp: Date) => void, chatId: string ): void { // Not sure this is the best design here. Maybe `addObserver` and `joinChat` should be merged. @@ -161,13 +163,13 @@ export class Messenger { }; const messages = wakuMessages.map((wakuMessage: WakuMessage) => { - if (!wakuMessage.payload) return; + if (!wakuMessage.payload || !wakuMessage.timestamp) return; const message = ApplicationMetadataMessage.decode(wakuMessage.payload); switch (message.type) { case ApplicationMetadataMessage_Type.TYPE_CHAT_MESSAGE: - this._handleNewChatMessage(chat, message); + this._handleNewChatMessage(chat, message, wakuMessage.timestamp); return message; default: dbg("Retrieved unsupported message type", message.type); @@ -186,7 +188,8 @@ export class Messenger { private _handleNewChatMessage( chat: Chat, - message: ApplicationMetadataMessage + message: ApplicationMetadataMessage, + timestamp: Date ): void { if (!message.payload || !message.type || !message.signature) return; @@ -195,7 +198,7 @@ export class Messenger { if (this.observers[chat.id]) { this.observers[chat.id].forEach((observer) => { - observer(message); + observer(message, timestamp); }); } }