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,
|
||||
},
|
||||
getMessage: async() => undefined,
|
||||
cachedGroupMetadata: async() => undefined,
|
||||
makeSignalRepository: makeLibSignalRepository
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
generateHighQualityLinkPreview,
|
||||
options: axiosOptions,
|
||||
patchMessageBeforeSending,
|
||||
cachedGroupMetadata,
|
||||
} = config
|
||||
const sock = makeGroupsSocket(config)
|
||||
const {
|
||||
@@ -29,7 +30,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
generateMessageTag,
|
||||
sendNode,
|
||||
groupMetadata,
|
||||
groupToggleEphemeral
|
||||
groupToggleEphemeral,
|
||||
} = sock
|
||||
|
||||
const userDevicesCache = config.userDevicesCache || new NodeCache({
|
||||
@@ -333,7 +334,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
const relayMessage = async(
|
||||
jid: string,
|
||||
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
|
||||
|
||||
@@ -347,6 +348,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
|
||||
msgId = msgId || generateMessageIDV2(sock.user?.id)
|
||||
useUserDevicesCache = useUserDevicesCache !== false
|
||||
useCachedGroupMetadata = useCachedGroupMetadata !== false && !isStatus
|
||||
|
||||
const participants: BinaryNode[] = []
|
||||
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) {
|
||||
const [groupData, senderKeyMap] = await Promise.all([
|
||||
(async() => {
|
||||
let groupData = cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined
|
||||
if(groupData) {
|
||||
let groupData = useCachedGroupMetadata && cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined
|
||||
if(groupData && Array.isArray(groupData?.participants)) {
|
||||
logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata')
|
||||
}
|
||||
|
||||
if(!groupData && !isStatus) {
|
||||
} else {
|
||||
groupData = await groupMetadata(jid)
|
||||
}
|
||||
|
||||
@@ -758,7 +758,11 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
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) {
|
||||
process.nextTick(() => {
|
||||
processingMutex.mutex(() => (
|
||||
|
||||
@@ -179,8 +179,8 @@ export type GroupMetadataParticipants = Pick<GroupMetadata, 'participants'>
|
||||
type MinimalRelayOptions = {
|
||||
/** override the message ID with a custom provided string */
|
||||
messageId?: string
|
||||
/** cached group metadata, use to prevent redundant requests to WA & speed up msg sending */
|
||||
cachedGroupMetadata?: (jid: string) => Promise<GroupMetadataParticipants | undefined>
|
||||
/** should we use group metadata cache, or fetch afresh from the server; default assumed to be "true" */
|
||||
useCachedGroupMetadata?: boolean
|
||||
}
|
||||
|
||||
export type MessageRelayOptions = MinimalRelayOptions & {
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { Logger } from 'pino'
|
||||
import type { URL } from 'url'
|
||||
import { proto } from '../../WAProto'
|
||||
import { AuthenticationState, SignalAuthState, TransactionCapabilityOptions } from './Auth'
|
||||
import { GroupMetadata } from './GroupMetadata'
|
||||
import { MediaConnInfo } from './Message'
|
||||
import { SignalRepository } from './Signal'
|
||||
|
||||
@@ -118,6 +119,9 @@ export type SocketConfig = {
|
||||
* */
|
||||
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
|
||||
|
||||
/** Socket passthrough */
|
||||
|
||||
Reference in New Issue
Block a user