Files
Baileys/src/Socket/mex.ts
João Lucas de Oliveira Lopes 8391c02e0b [READY FOR MERGE] Implement newsletter (#1532)
* feat: implement basic newsletter functionality with socket integration and event handling

* feat: enhance media handling for newsletters with raw media upload support

* feat: working updatePicture, removePicure, adminCount, mute, Unmute

* fix: fetchMessages

* chore: cleanup

* fix: update newsletter metadata path and query ID for consistency. newsletterMetadata works now

* chore: enhance newsletter metadata parsing and error handling

* fix: correct DELETE QueryId value in Newsletter.ts

* chore: split mex stuffs to own file

* chore: remove as any
2025-06-30 23:22:09 -03:00

59 lines
1.7 KiB
TypeScript

import { Boom } from '@hapi/boom'
import { BinaryNode } from '../WABinary'
import { getBinaryNodeChild, S_WHATSAPP_NET } from '../WABinary'
const wMexQuery = (
variables: Record<string, unknown>,
queryId: string,
query: (node: BinaryNode) => Promise<BinaryNode>,
generateMessageTag: () => string
) => {
return query({
tag: 'iq',
attrs: {
id: generateMessageTag(),
type: 'get',
to: S_WHATSAPP_NET,
xmlns: 'w:mex'
},
content: [
{
tag: 'query',
attrs: { query_id: queryId },
content: Buffer.from(JSON.stringify({ variables }), 'utf-8')
}
]
})
}
export const executeWMexQuery = async <T>(
variables: Record<string, unknown>,
queryId: string,
dataPath: string,
query: (node: BinaryNode) => Promise<BinaryNode>,
generateMessageTag: () => string
): Promise<T> => {
const result = await wMexQuery(variables, queryId, query, generateMessageTag)
const child = getBinaryNodeChild(result, 'result')
if (child?.content) {
const data = JSON.parse(child.content.toString())
if (data.errors && data.errors.length > 0) {
const errorMessages = data.errors.map((err: Error) => err.message || 'Unknown error').join(', ')
const firstError = data.errors[0]
const errorCode = firstError.extensions?.error_code || 400
throw new Boom(`GraphQL server error: ${errorMessages}`, { statusCode: errorCode, data: firstError })
}
const response = dataPath ? data?.data?.[dataPath] : data?.data
if (typeof response !== 'undefined') {
return response as T
}
}
const action = (dataPath || '').startsWith('xwa2_')
? dataPath.substring(5).replace(/_/g, ' ')
: dataPath?.replace(/_/g, ' ')
throw new Boom(`Failed to ${action}, unexpected response structure.`, { statusCode: 400, data: result })
}