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;
chatsById: Map<string, Chat>;
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);
});
}
}