Skip to content

Commit

Permalink
Fix: cachedGroupMetadata (#846)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
bobslavtriev and PurpShell authored Aug 14, 2024
1 parent 1f9cfb1 commit 2dc1afa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Defaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = {
snapshot: false,
},
getMessage: async() => undefined,
cachedGroupMetadata: async() => undefined,
makeSignalRepository: makeLibSignalRepository
}

Expand Down
20 changes: 12 additions & 8 deletions src/Socket/messages-send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
generateHighQualityLinkPreview,
options: axiosOptions,
patchMessageBeforeSending,
cachedGroupMetadata,
} = config
const sock = makeGroupsSocket(config)
const {
Expand All @@ -29,7 +30,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
generateMessageTag,
sendNode,
groupMetadata,
groupToggleEphemeral
groupToggleEphemeral,
} = sock

const userDevicesCache = config.userDevicesCache || new NodeCache({
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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(() => (
Expand Down
4 changes: 2 additions & 2 deletions src/Types/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand Down
4 changes: 4 additions & 0 deletions src/Types/Socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 2dc1afa

Please sign in to comment.