From ac0eb6fc106a3f1144ccffaa0fbcbc70380fca33 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Fri, 18 Sep 2020 19:08:19 +0530 Subject: [PATCH] Cache presence updates + waitForLastMessage option --- src/WAConnection/0.Base.ts | 1 + src/WAConnection/3.Connect.ts | 15 ++++++++------- src/WAConnection/4.Events.ts | 13 ++++++++++--- src/WAConnection/Constants.ts | 7 ++++++- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index a5ddb7f..c956ec5 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -46,6 +46,7 @@ export class WAConnection extends EventEmitter { connectOptions: WAConnectOptions = { timeoutMs: 60*1000, maxIdleTimeMs: 10*1000, + waitOnlyForLastMessage: false, waitForChats: true, maxRetries: 5, connectCooldownMs: 2250, diff --git a/src/WAConnection/3.Connect.ts b/src/WAConnection/3.Connect.ts index 35be5a7..5eebe51 100644 --- a/src/WAConnection/3.Connect.ts +++ b/src/WAConnection/3.Connect.ts @@ -76,7 +76,7 @@ export class WAConnection extends Base { // add wait for chats promise if required if (typeof options?.waitForChats === 'undefined' ? true : options?.waitForChats) { - const {waitForChats, cancelChats} = this.receiveChatsAndContacts() + const {waitForChats, cancelChats} = this.receiveChatsAndContacts(this.connectOptions.waitOnlyForLastMessage) task = waitForChats cancel = cancelChats } @@ -159,9 +159,8 @@ export class WAConnection extends Base { /** * Sets up callbacks to receive chats, contacts & messages. * Must be called immediately after connect - * @returns [chats, contacts] */ - protected receiveChatsAndContacts() { + protected receiveChatsAndContacts(waitOnlyForLast: boolean) { const chats = new KeyedDB(Utils.waChatUniqueKey, c => c.jid) const contacts = {} @@ -172,9 +171,11 @@ export class WAConnection extends Base { const deregisterCallbacks = () => { // wait for actual messages to load, "last" is the most recent message, "before" contains prior messages this.deregisterCallback(['action', 'add:last']) - this.deregisterCallback(['action', 'add:before']) - this.deregisterCallback(['action', 'add:unread']) - + if (!waitOnlyForLast) { + this.deregisterCallback(['action', 'add:before']) + this.deregisterCallback(['action', 'add:unread']) + } + this.deregisterCallback(['response', 'type:chat']) this.deregisterCallback(['response', 'type:contacts']) } @@ -184,7 +185,7 @@ export class WAConnection extends Base { const chatUpdate = json => { receivedMessages = true - const isLast = json[1].last + const isLast = json[1].last || waitOnlyForLast const messages = json[2] as WANode[] if (messages) { diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 06abfd9..fc21e18 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -13,9 +13,16 @@ export class WAConnection extends Base { this.chatAddMessageAppropriate (message) }) // presence updates - this.registerCallback('Presence', json => ( - this.emit('user-presence-update', json[1] as PresenceUpdate) - )) + this.registerCallback('Presence', json => { + const update = json[1] as PresenceUpdate + const jid = whatsappID(update.participant || update.id) + const contact = this.contacts[jid] + if (!isGroupID(jid) && contact) { + contact.lastKnownPresence = update.type + if (update.t) contact.lastSeen = +update.t + } + this.emit('user-presence-update', update) + }) // If a message has been updated (usually called when a video message gets its upload url) this.registerCallback (['action', 'add:update', 'message'], json => { const message: WAMessage = json[2][0][2] diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index 1a106a4..4020c54 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -67,12 +67,14 @@ export enum ReconnectMode { export type WAConnectOptions = { /** timeout after which the connect attempt will fail, set to null for default timeout value */ timeoutMs?: number - /** */ + /** fails the connection if no data is received for X seconds */ maxIdleTimeMs?: number /** maximum attempts to connect */ maxRetries?: number /** should the chats be waited for */ waitForChats?: boolean + /** if set to true, the connect only waits for the last message of the chat */ + waitOnlyForLastMessage?: boolean /** max time for the phone to respond to a connectivity test */ phoneResponseTime?: number @@ -174,6 +176,9 @@ export interface WAContact { index?: string /** short name for the contact */ short?: string + // Baileys Added + lastKnownPresence?: Presence + lastSeen?: number } export interface WAUser extends WAContact { phone: any