Provide message timestamp to caller

This commit is contained in:
Franck Royer 2021-10-07 15:09:49 +11:00
parent c2b174ab53
commit 749c15cf91
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 11 additions and 8 deletions

View File

@ -14,7 +14,9 @@ export class Messenger {
waku: Waku; waku: Waku;
chatsById: Map<string, Chat>; chatsById: Map<string, Chat>;
observers: { observers: {
[chatId: string]: Set<(message: ApplicationMetadataMessage) => void>; [chatId: string]: Set<
(message: ApplicationMetadataMessage, timestamp: Date) => void
>;
}; };
identity: Identity; identity: Identity;
@ -49,13 +51,13 @@ export class Messenger {
this.waku.relay.addObserver( this.waku.relay.addObserver(
(wakuMessage: WakuMessage) => { (wakuMessage: WakuMessage) => {
if (!wakuMessage.payload) return; if (!wakuMessage.payload || !wakuMessage.timestamp) return;
const message = ApplicationMetadataMessage.decode(wakuMessage.payload); const message = ApplicationMetadataMessage.decode(wakuMessage.payload);
switch (message.type) { switch (message.type) {
case ApplicationMetadataMessage_Type.TYPE_CHAT_MESSAGE: case ApplicationMetadataMessage_Type.TYPE_CHAT_MESSAGE:
this._handleNewChatMessage(chat, message); this._handleNewChatMessage(chat, message, wakuMessage.timestamp);
break; break;
default: default:
dbg("Received unsupported message type", message.type); 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]. * @throws string If the chat has not been joined first using [joinChat].
*/ */
public addObserver( public addObserver(
observer: (message: ApplicationMetadataMessage) => void, observer: (message: ApplicationMetadataMessage, timestamp: Date) => void,
chatId: string chatId: string
): void { ): void {
// Not sure this is the best design here. Maybe `addObserver` and `joinChat` should be merged. // 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) => { const messages = wakuMessages.map((wakuMessage: WakuMessage) => {
if (!wakuMessage.payload) return; if (!wakuMessage.payload || !wakuMessage.timestamp) return;
const message = ApplicationMetadataMessage.decode(wakuMessage.payload); const message = ApplicationMetadataMessage.decode(wakuMessage.payload);
switch (message.type) { switch (message.type) {
case ApplicationMetadataMessage_Type.TYPE_CHAT_MESSAGE: case ApplicationMetadataMessage_Type.TYPE_CHAT_MESSAGE:
this._handleNewChatMessage(chat, message); this._handleNewChatMessage(chat, message, wakuMessage.timestamp);
return message; return message;
default: default:
dbg("Retrieved unsupported message type", message.type); dbg("Retrieved unsupported message type", message.type);
@ -186,7 +188,8 @@ export class Messenger {
private _handleNewChatMessage( private _handleNewChatMessage(
chat: Chat, chat: Chat,
message: ApplicationMetadataMessage message: ApplicationMetadataMessage,
timestamp: Date
): void { ): void {
if (!message.payload || !message.type || !message.signature) return; if (!message.payload || !message.type || !message.signature) return;
@ -195,7 +198,7 @@ export class Messenger {
if (this.observers[chat.id]) { if (this.observers[chat.id]) {
this.observers[chat.id].forEach((observer) => { this.observers[chat.id].forEach((observer) => {
observer(message); observer(message, timestamp);
}); });
} }
} }