mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Cache presence updates + waitForLastMessage option
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<WAChat>(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) {
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user