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 = {
|
connectOptions: WAConnectOptions = {
|
||||||
timeoutMs: 60*1000,
|
timeoutMs: 60*1000,
|
||||||
maxIdleTimeMs: 10*1000,
|
maxIdleTimeMs: 10*1000,
|
||||||
|
waitOnlyForLastMessage: false,
|
||||||
waitForChats: true,
|
waitForChats: true,
|
||||||
maxRetries: 5,
|
maxRetries: 5,
|
||||||
connectCooldownMs: 2250,
|
connectCooldownMs: 2250,
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ export class WAConnection extends Base {
|
|||||||
|
|
||||||
// add wait for chats promise if required
|
// add wait for chats promise if required
|
||||||
if (typeof options?.waitForChats === 'undefined' ? true : options?.waitForChats) {
|
if (typeof options?.waitForChats === 'undefined' ? true : options?.waitForChats) {
|
||||||
const {waitForChats, cancelChats} = this.receiveChatsAndContacts()
|
const {waitForChats, cancelChats} = this.receiveChatsAndContacts(this.connectOptions.waitOnlyForLastMessage)
|
||||||
task = waitForChats
|
task = waitForChats
|
||||||
cancel = cancelChats
|
cancel = cancelChats
|
||||||
}
|
}
|
||||||
@@ -159,9 +159,8 @@ export class WAConnection extends Base {
|
|||||||
/**
|
/**
|
||||||
* Sets up callbacks to receive chats, contacts & messages.
|
* Sets up callbacks to receive chats, contacts & messages.
|
||||||
* Must be called immediately after connect
|
* 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 chats = new KeyedDB<WAChat>(Utils.waChatUniqueKey, c => c.jid)
|
||||||
const contacts = {}
|
const contacts = {}
|
||||||
|
|
||||||
@@ -172,9 +171,11 @@ export class WAConnection extends Base {
|
|||||||
const deregisterCallbacks = () => {
|
const deregisterCallbacks = () => {
|
||||||
// wait for actual messages to load, "last" is the most recent message, "before" contains prior messages
|
// 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:last'])
|
||||||
this.deregisterCallback(['action', 'add:before'])
|
if (!waitOnlyForLast) {
|
||||||
this.deregisterCallback(['action', 'add:unread'])
|
this.deregisterCallback(['action', 'add:before'])
|
||||||
|
this.deregisterCallback(['action', 'add:unread'])
|
||||||
|
}
|
||||||
|
|
||||||
this.deregisterCallback(['response', 'type:chat'])
|
this.deregisterCallback(['response', 'type:chat'])
|
||||||
this.deregisterCallback(['response', 'type:contacts'])
|
this.deregisterCallback(['response', 'type:contacts'])
|
||||||
}
|
}
|
||||||
@@ -184,7 +185,7 @@ export class WAConnection extends Base {
|
|||||||
const chatUpdate = json => {
|
const chatUpdate = json => {
|
||||||
receivedMessages = true
|
receivedMessages = true
|
||||||
|
|
||||||
const isLast = json[1].last
|
const isLast = json[1].last || waitOnlyForLast
|
||||||
const messages = json[2] as WANode[]
|
const messages = json[2] as WANode[]
|
||||||
|
|
||||||
if (messages) {
|
if (messages) {
|
||||||
|
|||||||
@@ -13,9 +13,16 @@ export class WAConnection extends Base {
|
|||||||
this.chatAddMessageAppropriate (message)
|
this.chatAddMessageAppropriate (message)
|
||||||
})
|
})
|
||||||
// presence updates
|
// presence updates
|
||||||
this.registerCallback('Presence', json => (
|
this.registerCallback('Presence', json => {
|
||||||
this.emit('user-presence-update', json[1] as PresenceUpdate)
|
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)
|
// If a message has been updated (usually called when a video message gets its upload url)
|
||||||
this.registerCallback (['action', 'add:update', 'message'], json => {
|
this.registerCallback (['action', 'add:update', 'message'], json => {
|
||||||
const message: WAMessage = json[2][0][2]
|
const message: WAMessage = json[2][0][2]
|
||||||
|
|||||||
@@ -67,12 +67,14 @@ export enum ReconnectMode {
|
|||||||
export type WAConnectOptions = {
|
export type WAConnectOptions = {
|
||||||
/** timeout after which the connect attempt will fail, set to null for default timeout value */
|
/** timeout after which the connect attempt will fail, set to null for default timeout value */
|
||||||
timeoutMs?: number
|
timeoutMs?: number
|
||||||
/** */
|
/** fails the connection if no data is received for X seconds */
|
||||||
maxIdleTimeMs?: number
|
maxIdleTimeMs?: number
|
||||||
/** maximum attempts to connect */
|
/** maximum attempts to connect */
|
||||||
maxRetries?: number
|
maxRetries?: number
|
||||||
/** should the chats be waited for */
|
/** should the chats be waited for */
|
||||||
waitForChats?: boolean
|
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 */
|
/** max time for the phone to respond to a connectivity test */
|
||||||
phoneResponseTime?: number
|
phoneResponseTime?: number
|
||||||
|
|
||||||
@@ -174,6 +176,9 @@ export interface WAContact {
|
|||||||
index?: string
|
index?: string
|
||||||
/** short name for the contact */
|
/** short name for the contact */
|
||||||
short?: string
|
short?: string
|
||||||
|
// Baileys Added
|
||||||
|
lastKnownPresence?: Presence
|
||||||
|
lastSeen?: number
|
||||||
}
|
}
|
||||||
export interface WAUser extends WAContact {
|
export interface WAUser extends WAContact {
|
||||||
phone: any
|
phone: any
|
||||||
|
|||||||
Reference in New Issue
Block a user