mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: correctly handle presence being offline for receipts
When sendPresenceUpdate('unavailable') is called, it should allow notifications to be received on the phone
This commit is contained in:
@@ -564,6 +564,8 @@ type WAPresence = 'unavailable' | 'available' | 'composing' | 'recording' | 'pau
|
|||||||
|
|
||||||
The presence expires after about 10 seconds.
|
The presence expires after about 10 seconds.
|
||||||
|
|
||||||
|
**Note:** In the multi-device version of WhatsApp -- if a desktop client is active, WA doesn't send push notifications to the device. If you would like to receive said notifications -- mark your Baileys client offline using `sock.sendPresenceUpdate('unavailable')`
|
||||||
|
|
||||||
## Downloading Media Messages
|
## Downloading Media Messages
|
||||||
|
|
||||||
If you want to save the media you received
|
If you want to save the media you received
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ const BASE_CONNECTION_CONFIG: CommonSocketConfig<any> = {
|
|||||||
export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
|
export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
|
||||||
...BASE_CONNECTION_CONFIG,
|
...BASE_CONNECTION_CONFIG,
|
||||||
downloadHistory: true,
|
downloadHistory: true,
|
||||||
|
markOnlineOnConnect: true,
|
||||||
linkPreviewImageThumbnailWidth: 192,
|
linkPreviewImageThumbnailWidth: 192,
|
||||||
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
|
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
|
||||||
getMessage: async() => undefined
|
getMessage: async() => undefined
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import { makeMessagesSocket } from './messages-send'
|
|||||||
const MAX_SYNC_ATTEMPTS = 5
|
const MAX_SYNC_ATTEMPTS = 5
|
||||||
|
|
||||||
export const makeChatsSocket = (config: SocketConfig) => {
|
export const makeChatsSocket = (config: SocketConfig) => {
|
||||||
const { logger } = config
|
const { logger, markOnlineOnConnect } = config
|
||||||
const sock = makeMessagesSocket(config)
|
const sock = makeMessagesSocket(config)
|
||||||
const {
|
const {
|
||||||
ev,
|
ev,
|
||||||
@@ -379,6 +379,8 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ev.emit('connection.update', { isOnline: type === 'available' })
|
||||||
|
|
||||||
await sendNode({
|
await sendNode({
|
||||||
tag: 'presence',
|
tag: 'presence',
|
||||||
attrs: {
|
attrs: {
|
||||||
@@ -467,7 +469,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
const events = processSyncActions(actions, authState.creds.me!, logger)
|
const events = processSyncActions(actions, authState.creds.me!, logger)
|
||||||
emitEventsFromMap(events)
|
emitEventsFromMap(events)
|
||||||
// resend available presence to update name on servers
|
// resend available presence to update name on servers
|
||||||
if(events['creds.update']?.me?.name) {
|
if(events['creds.update']?.me?.name && markOnlineOnConnect) {
|
||||||
sendPresenceUpdate('available')
|
sendPresenceUpdate('available')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -610,7 +612,9 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
fetchProps(),
|
fetchProps(),
|
||||||
fetchBlocklist(),
|
fetchBlocklist(),
|
||||||
fetchPrivacySettings(),
|
fetchPrivacySettings(),
|
||||||
sendPresenceUpdate('available')
|
markOnlineOnConnect
|
||||||
|
? sendPresenceUpdate('available')
|
||||||
|
: undefined
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
const historyCache = new Set<string>()
|
const historyCache = new Set<string>()
|
||||||
|
|
||||||
|
let sendActiveReceipts = false
|
||||||
|
|
||||||
const sendMessageAck = async({ tag, attrs }: BinaryNode, extraAttrs: BinaryNodeAttributes = { }) => {
|
const sendMessageAck = async({ tag, attrs }: BinaryNode, extraAttrs: BinaryNodeAttributes = { }) => {
|
||||||
const stanza: BinaryNode = {
|
const stanza: BinaryNode = {
|
||||||
tag: 'ack',
|
tag: 'ack',
|
||||||
@@ -511,6 +513,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
if(isJidUser(msg.key.remoteJid)) {
|
if(isJidUser(msg.key.remoteJid)) {
|
||||||
participant = author
|
participant = author
|
||||||
}
|
}
|
||||||
|
} else if(!sendActiveReceipts) {
|
||||||
|
type = 'inactive'
|
||||||
}
|
}
|
||||||
|
|
||||||
await sendReceipt(msg.key.remoteJid!, participant, [msg.key.id!], type)
|
await sendReceipt(msg.key.remoteJid!, participant, [msg.key.id!], type)
|
||||||
@@ -617,6 +621,13 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ev.on('connection.update', ({ isOnline }) => {
|
||||||
|
if(typeof isOnline !== 'undefined') {
|
||||||
|
sendActiveReceipts = isOnline
|
||||||
|
logger.trace(`sendActiveReceipts set to "${sendActiveReceipts}"`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...sock,
|
...sock,
|
||||||
processMessage: processMessageLocal,
|
processMessage: processMessageLocal,
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export type MessageType = keyof proto.Message
|
|||||||
|
|
||||||
export type DownloadableMessage = { mediaKey?: Uint8Array, directPath?: string, url?: string }
|
export type DownloadableMessage = { mediaKey?: Uint8Array, directPath?: string, url?: string }
|
||||||
|
|
||||||
export type MessageReceiptType = 'read' | 'read-self' | 'hist_sync' | 'peer_msg' | 'sender' | undefined
|
export type MessageReceiptType = 'read' | 'read-self' | 'hist_sync' | 'peer_msg' | 'sender' | 'inactive' | undefined
|
||||||
|
|
||||||
export type MediaConnInfo = {
|
export type MediaConnInfo = {
|
||||||
auth: string
|
auth: string
|
||||||
|
|||||||
@@ -21,5 +21,9 @@ export type ConnectionState = {
|
|||||||
phoneConnected: boolean
|
phoneConnected: boolean
|
||||||
user?: Contact
|
user?: Contact
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* if the client is shown as an active, online client.
|
||||||
|
* If this is false, the primary phone and other devices will receive notifs
|
||||||
|
* */
|
||||||
|
isOnline?: boolean
|
||||||
}
|
}
|
||||||
@@ -24,6 +24,8 @@ export type SocketConfig = CommonSocketConfig<AuthenticationState> & {
|
|||||||
transactionOpts: TransactionCapabilityOptions
|
transactionOpts: TransactionCapabilityOptions
|
||||||
/** provide a cache to store a user's device list */
|
/** provide a cache to store a user's device list */
|
||||||
userDevicesCache?: NodeCache
|
userDevicesCache?: NodeCache
|
||||||
|
/** marks the client as online whenever the socket successfully connects */
|
||||||
|
markOnlineOnConnect: boolean
|
||||||
/**
|
/**
|
||||||
* map to store the retry counts for failed messages;
|
* map to store the retry counts for failed messages;
|
||||||
* used to determine whether to retry a message or not */
|
* used to determine whether to retry a message or not */
|
||||||
|
|||||||
Reference in New Issue
Block a user