From fe0730b6a74e19f542cb0e03f086a9481204248d Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Thu, 3 Sep 2020 21:32:26 +0530 Subject: [PATCH] Update 7.MessagesExtra.ts --- src/WAConnection/7.MessagesExtra.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/WAConnection/7.MessagesExtra.ts b/src/WAConnection/7.MessagesExtra.ts index 0d8799c..fb24e0b 100644 --- a/src/WAConnection/7.MessagesExtra.ts +++ b/src/WAConnection/7.MessagesExtra.ts @@ -7,7 +7,7 @@ import { WAUrlInfo, WAMessageContent, WAMetric, WAFlag, WANode, WAMessage, WAMessageProto, BaileysError, MessageLogLevel, WA_MESSAGE_STATUS_TYPE } from './Constants' -import { whatsappID, delay } from './Utils' +import { whatsappID, delay, toNumber, unixTimestampSeconds } from './Utils' export class WAConnection extends Base { @@ -194,6 +194,27 @@ export class WAConnection extends Base { await delay (200) } } + /** + * Loads all messages sent after a specific date + */ + async messagesReceivedAfter (date: Date) { + const stamp = unixTimestampSeconds (date) + // find the index where the chat timestamp becomes greater + const idx = this.chats.all ().findIndex (c => c.t < stamp) + // all chats before that index -- i.e. all chats that were updated after that + const chats = this.chats.all ().slice (0, idx) + + const messages: WAMessage[] = [] + await Promise.all ( + chats.map (async chat => { + await this.findMessage (chat.jid, 5, m => { + if (toNumber(m.messageTimestamp) < stamp) return true + messages.push (m) + }) + }) + ) + return messages + } /** Load a single message specified by the ID */ async loadMessage (jid: string, messageID: string) { // load the message before the given message