mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Fix: cachedGroupMetadata (#846)
* Update Socket.ts * Update Message.ts * Update messages-send.ts * Update index.ts * Update messages-send.ts * Update messages-send.ts * Update messages-send.ts * Update messages-send.ts * Update Message.ts * Update Socket.ts * Update messages-send.ts * Update messages-send.ts --------- Co-authored-by: Rajeh Taher <rajeh@reforward.dev>
This commit is contained in:
@@ -82,6 +82,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
|
|||||||
snapshot: false,
|
snapshot: false,
|
||||||
},
|
},
|
||||||
getMessage: async() => undefined,
|
getMessage: async() => undefined,
|
||||||
|
cachedGroupMetadata: async() => undefined,
|
||||||
makeSignalRepository: makeLibSignalRepository
|
makeSignalRepository: makeLibSignalRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
generateHighQualityLinkPreview,
|
generateHighQualityLinkPreview,
|
||||||
options: axiosOptions,
|
options: axiosOptions,
|
||||||
patchMessageBeforeSending,
|
patchMessageBeforeSending,
|
||||||
|
cachedGroupMetadata,
|
||||||
} = config
|
} = config
|
||||||
const sock = makeGroupsSocket(config)
|
const sock = makeGroupsSocket(config)
|
||||||
const {
|
const {
|
||||||
@@ -29,7 +30,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
generateMessageTag,
|
generateMessageTag,
|
||||||
sendNode,
|
sendNode,
|
||||||
groupMetadata,
|
groupMetadata,
|
||||||
groupToggleEphemeral
|
groupToggleEphemeral,
|
||||||
} = sock
|
} = sock
|
||||||
|
|
||||||
const userDevicesCache = config.userDevicesCache || new NodeCache({
|
const userDevicesCache = config.userDevicesCache || new NodeCache({
|
||||||
@@ -333,7 +334,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
const relayMessage = async(
|
const relayMessage = async(
|
||||||
jid: string,
|
jid: string,
|
||||||
message: proto.IMessage,
|
message: proto.IMessage,
|
||||||
{ messageId: msgId, participant, additionalAttributes, additionalNodes, useUserDevicesCache, cachedGroupMetadata, statusJidList }: MessageRelayOptions
|
{ messageId: msgId, participant, additionalAttributes, additionalNodes, useUserDevicesCache, useCachedGroupMetadata, statusJidList }: MessageRelayOptions
|
||||||
) => {
|
) => {
|
||||||
const meId = authState.creds.me!.id
|
const meId = authState.creds.me!.id
|
||||||
|
|
||||||
@@ -347,6 +348,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
msgId = msgId || generateMessageIDV2(sock.user?.id)
|
msgId = msgId || generateMessageIDV2(sock.user?.id)
|
||||||
useUserDevicesCache = useUserDevicesCache !== false
|
useUserDevicesCache = useUserDevicesCache !== false
|
||||||
|
useCachedGroupMetadata = useCachedGroupMetadata !== false && !isStatus
|
||||||
|
|
||||||
const participants: BinaryNode[] = []
|
const participants: BinaryNode[] = []
|
||||||
const destinationJid = (!isStatus) ? jidEncode(user, isLid ? 'lid' : isGroup ? 'g.us' : 's.whatsapp.net') : statusJid
|
const destinationJid = (!isStatus) ? jidEncode(user, isLid ? 'lid' : isGroup ? 'g.us' : 's.whatsapp.net') : statusJid
|
||||||
@@ -378,12 +380,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
if(isGroup || isStatus) {
|
if(isGroup || isStatus) {
|
||||||
const [groupData, senderKeyMap] = await Promise.all([
|
const [groupData, senderKeyMap] = await Promise.all([
|
||||||
(async() => {
|
(async() => {
|
||||||
let groupData = cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined
|
let groupData = useCachedGroupMetadata && cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined
|
||||||
if(groupData) {
|
if(groupData && Array.isArray(groupData?.participants)) {
|
||||||
logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata')
|
logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata')
|
||||||
}
|
} else {
|
||||||
|
|
||||||
if(!groupData && !isStatus) {
|
|
||||||
groupData = await groupMetadata(jid)
|
groupData = await groupMetadata(jid)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -758,7 +758,11 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
additionalAttributes.edit = '1'
|
additionalAttributes.edit = '1'
|
||||||
}
|
}
|
||||||
|
|
||||||
await relayMessage(jid, fullMsg.message!, { messageId: fullMsg.key.id!, cachedGroupMetadata: options.cachedGroupMetadata, additionalAttributes, statusJidList: options.statusJidList })
|
if('cachedGroupMetadata' in options) {
|
||||||
|
console.warn('cachedGroupMetadata in sendMessage are deprecated, now cachedGroupMetadata is part of the socket config.')
|
||||||
|
}
|
||||||
|
|
||||||
|
await relayMessage(jid, fullMsg.message!, { messageId: fullMsg.key.id!, useCachedGroupMetadata: options.useCachedGroupMetadata, additionalAttributes, statusJidList: options.statusJidList })
|
||||||
if(config.emitOwnEvents) {
|
if(config.emitOwnEvents) {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
processingMutex.mutex(() => (
|
processingMutex.mutex(() => (
|
||||||
|
|||||||
@@ -179,8 +179,8 @@ export type GroupMetadataParticipants = Pick<GroupMetadata, 'participants'>
|
|||||||
type MinimalRelayOptions = {
|
type MinimalRelayOptions = {
|
||||||
/** override the message ID with a custom provided string */
|
/** override the message ID with a custom provided string */
|
||||||
messageId?: string
|
messageId?: string
|
||||||
/** cached group metadata, use to prevent redundant requests to WA & speed up msg sending */
|
/** should we use group metadata cache, or fetch afresh from the server; default assumed to be "true" */
|
||||||
cachedGroupMetadata?: (jid: string) => Promise<GroupMetadataParticipants | undefined>
|
useCachedGroupMetadata?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MessageRelayOptions = MinimalRelayOptions & {
|
export type MessageRelayOptions = MinimalRelayOptions & {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import type { Logger } from 'pino'
|
|||||||
import type { URL } from 'url'
|
import type { URL } from 'url'
|
||||||
import { proto } from '../../WAProto'
|
import { proto } from '../../WAProto'
|
||||||
import { AuthenticationState, SignalAuthState, TransactionCapabilityOptions } from './Auth'
|
import { AuthenticationState, SignalAuthState, TransactionCapabilityOptions } from './Auth'
|
||||||
|
import { GroupMetadata } from './GroupMetadata'
|
||||||
import { MediaConnInfo } from './Message'
|
import { MediaConnInfo } from './Message'
|
||||||
import { SignalRepository } from './Signal'
|
import { SignalRepository } from './Signal'
|
||||||
|
|
||||||
@@ -118,6 +119,9 @@ export type SocketConfig = {
|
|||||||
* */
|
* */
|
||||||
getMessage: (key: proto.IMessageKey) => Promise<proto.IMessage | undefined>
|
getMessage: (key: proto.IMessageKey) => Promise<proto.IMessage | undefined>
|
||||||
|
|
||||||
|
/** cached group metadata, use to prevent redundant requests to WA & speed up msg sending */
|
||||||
|
cachedGroupMetadata: (jid: string) => Promise<GroupMetadata | undefined>
|
||||||
|
|
||||||
makeSignalRepository: (auth: SignalAuthState) => SignalRepository
|
makeSignalRepository: (auth: SignalAuthState) => SignalRepository
|
||||||
|
|
||||||
/** Socket passthrough */
|
/** Socket passthrough */
|
||||||
|
|||||||
Reference in New Issue
Block a user