@hiroosy/baileys-lite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +115856 -0
- package/engine-requirements.js +11 -0
- package/package.json +27 -0
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +649 -0
- package/src/constants.js +143 -0
- package/src/defaults.js +102 -0
- package/src/foundation/audio-duration.js +530 -0
- package/src/foundation/audio-samples.js +268 -0
- package/src/foundation/boom.js +46 -0
- package/src/foundation/cache.js +142 -0
- package/src/foundation/concurrency.js +87 -0
- package/src/foundation/curve25519.js +221 -0
- package/src/foundation/link-preview.js +320 -0
- package/src/foundation/logger.js +69 -0
- package/src/foundation/long.js +140 -0
- package/src/foundation/protobuf-minimal.js +619 -0
- package/src/foundation/wa-crypto.js +76 -0
- package/src/foundation/zip.js +124 -0
- package/src/index.js +48 -0
- package/src/signal/libsignal.js +1051 -0
- package/src/signal/signal-group.js +487 -0
- package/src/signal/signal-repository.js +646 -0
- package/src/socket/business-communities.js +830 -0
- package/src/socket/chats.js +674 -0
- package/src/socket/groups-newsletter.js +342 -0
- package/src/socket/messages-recv.js +1835 -0
- package/src/socket/messages-send.js +1202 -0
- package/src/socket/socket-core.js +833 -0
- package/src/socket/usync.js +291 -0
- package/src/socket-client/websocket-client.js +124 -0
- package/src/utils/auth-state-core.js +764 -0
- package/src/utils/auth-state-storage.js +316 -0
- package/src/utils/chat-sync.js +1045 -0
- package/src/utils/media.js +841 -0
- package/src/utils/message-compose.js +1609 -0
- package/src/utils/message-processing.js +958 -0
- package/src/utils/wa-protocol-core.js +1283 -0
- package/src/vendor/jpeg-js/LICENSE +25 -0
- package/src/vendor/jpeg-js/decoder.js +1152 -0
- package/src/vendor/jpeg-js/encoder.js +824 -0
- package/src/vendor/pngjs/LICENSE +20 -0
- package/src/vendor/pngjs/bitmapper.js +273 -0
- package/src/vendor/pngjs/bitpacker.js +167 -0
- package/src/vendor/pngjs/constants.js +38 -0
- package/src/vendor/pngjs/crc.js +47 -0
- package/src/vendor/pngjs/filter-pack.js +179 -0
- package/src/vendor/pngjs/filter-parse-sync.js +27 -0
- package/src/vendor/pngjs/filter-parse.js +185 -0
- package/src/vendor/pngjs/format-normaliser.js +101 -0
- package/src/vendor/pngjs/interlace.js +101 -0
- package/src/vendor/pngjs/packer-sync.js +65 -0
- package/src/vendor/pngjs/packer.js +138 -0
- package/src/vendor/pngjs/paeth-predictor.js +22 -0
- package/src/vendor/pngjs/parser-sync.js +121 -0
- package/src/vendor/pngjs/parser.js +299 -0
- package/src/vendor/pngjs/png-sync.js +19 -0
- package/src/vendor/pngjs/sync-inflate.js +174 -0
- package/src/vendor/pngjs/sync-reader.js +54 -0
- package/src/wam/wam-constants.js +22853 -0
- package/src/wam/wam.js +151 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups + newsletter socket layers. Combines what used to be Socket/groups.js
|
|
3
|
+
* and Socket/newsletter.js.
|
|
4
|
+
*
|
|
5
|
+
* Wrap order (must match upstream exactly): makeChatsSocket -> makeGroupsSocket
|
|
6
|
+
* -> makeNewsletterSocket. This file's export (makeNewsletterSocket) is what
|
|
7
|
+
* messages-send.js wraps next.
|
|
8
|
+
*/
|
|
9
|
+
import { proto } from '../../WAProto/index.js'
|
|
10
|
+
import { Boom } from '../foundation/boom.js'
|
|
11
|
+
import { WAMessageAddressingMode, WAMessageStubType, QueryIds, XWAPaths } from '../constants.js'
|
|
12
|
+
import { generateMessageIDV2, unixTimestampSeconds } from '../utils/wa-protocol-core.js'
|
|
13
|
+
import { generateProfilePicture } from '../utils/media.js'
|
|
14
|
+
import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isLidUser, isPnUser, jidEncode, jidNormalizedUser, S_WHATSAPP_NET } from '../binary/wa-binary.js'
|
|
15
|
+
import { executeWMexQuery as genericExecuteWMexQuery } from './socket-core.js'
|
|
16
|
+
import { makeChatsSocket } from './chats.js'
|
|
17
|
+
|
|
18
|
+
/* ------------------------------------------------------------------ */
|
|
19
|
+
/* Groups */
|
|
20
|
+
/* ------------------------------------------------------------------ */
|
|
21
|
+
|
|
22
|
+
export const extractGroupMetadata = result => {
|
|
23
|
+
const group = getBinaryNodeChild(result, 'group')
|
|
24
|
+
if (!group) {
|
|
25
|
+
const errorNode = getBinaryNodeChild(result, 'error')
|
|
26
|
+
if (errorNode) {
|
|
27
|
+
const code = errorNode.attrs.code ? +errorNode.attrs.code : 500
|
|
28
|
+
const text = errorNode.attrs.text || 'group metadata query failed'
|
|
29
|
+
throw new Boom(text, { statusCode: code, data: errorNode })
|
|
30
|
+
}
|
|
31
|
+
throw new Boom('Invalid group metadata response: missing <group> node', { data: result })
|
|
32
|
+
}
|
|
33
|
+
if (!group.attrs.id) throw new Boom('Invalid group metadata response: missing group id', { data: group })
|
|
34
|
+
const descChild = getBinaryNodeChild(group, 'description')
|
|
35
|
+
let desc, descId, descOwner, descOwnerPn, descOwnerUsername, descTime
|
|
36
|
+
if (descChild) {
|
|
37
|
+
desc = getBinaryNodeChildString(descChild, 'body')
|
|
38
|
+
descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined
|
|
39
|
+
descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined
|
|
40
|
+
descOwnerUsername = descChild.attrs.participant_username || undefined
|
|
41
|
+
descTime = +descChild.attrs.t
|
|
42
|
+
descId = descChild.attrs.id
|
|
43
|
+
}
|
|
44
|
+
const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us')
|
|
45
|
+
const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration
|
|
46
|
+
const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add'
|
|
47
|
+
return {
|
|
48
|
+
id: groupId,
|
|
49
|
+
notify: group.attrs.notify,
|
|
50
|
+
addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
|
|
51
|
+
subject: group.attrs.subject,
|
|
52
|
+
subjectOwner: group.attrs.s_o,
|
|
53
|
+
subjectOwnerPn: group.attrs.s_o_pn,
|
|
54
|
+
subjectOwnerUsername: group.attrs.s_o_username,
|
|
55
|
+
subjectTime: +group.attrs.s_t,
|
|
56
|
+
size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
|
|
57
|
+
creation: +group.attrs.creation,
|
|
58
|
+
owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
|
|
59
|
+
ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
|
|
60
|
+
ownerUsername: group.attrs.creator_username || undefined,
|
|
61
|
+
owner_country_code: group.attrs.creator_country_code,
|
|
62
|
+
desc,
|
|
63
|
+
descId,
|
|
64
|
+
descOwner,
|
|
65
|
+
descOwnerPn,
|
|
66
|
+
descOwnerUsername,
|
|
67
|
+
descTime,
|
|
68
|
+
linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
|
|
69
|
+
restrict: !!getBinaryNodeChild(group, 'locked'),
|
|
70
|
+
announce: !!getBinaryNodeChild(group, 'announcement'),
|
|
71
|
+
isCommunity: !!getBinaryNodeChild(group, 'parent'),
|
|
72
|
+
isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
|
|
73
|
+
joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
|
|
74
|
+
memberAddMode,
|
|
75
|
+
participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => ({
|
|
76
|
+
id: attrs.jid,
|
|
77
|
+
phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
|
|
78
|
+
lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
|
|
79
|
+
username: attrs.participant_username || attrs.username || undefined,
|
|
80
|
+
admin: attrs.type || null
|
|
81
|
+
})),
|
|
82
|
+
ephemeralDuration: eph ? +eph : undefined
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const makeGroupsSocket = config => {
|
|
87
|
+
const sock = makeChatsSocket(config)
|
|
88
|
+
const { authState, ev, query, upsertMessage } = sock
|
|
89
|
+
const groupQuery = async (jid, type, content) => query({ tag: 'iq', attrs: { type, xmlns: 'w:g2', to: jid }, content })
|
|
90
|
+
const groupMetadata = async jid => extractGroupMetadata(await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]))
|
|
91
|
+
|
|
92
|
+
const groupFetchAllParticipating = async () => {
|
|
93
|
+
const result = await query({
|
|
94
|
+
tag: 'iq',
|
|
95
|
+
attrs: { to: '@g.us', xmlns: 'w:g2', type: 'get' },
|
|
96
|
+
content: [{ tag: 'participating', attrs: {}, content: [{ tag: 'participants', attrs: {} }, { tag: 'description', attrs: {} }] }]
|
|
97
|
+
})
|
|
98
|
+
const data = {}
|
|
99
|
+
const groupsChild = getBinaryNodeChild(result, 'groups')
|
|
100
|
+
if (groupsChild) {
|
|
101
|
+
for (const groupNode of getBinaryNodeChildren(groupsChild, 'group')) {
|
|
102
|
+
const meta = extractGroupMetadata({ tag: 'result', attrs: {}, content: [groupNode] })
|
|
103
|
+
data[meta.id] = meta
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
sock.ev.emit('groups.update', Object.values(data))
|
|
107
|
+
return data
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
sock.ws.on('CB:ib,,dirty', async node => {
|
|
111
|
+
const { attrs } = getBinaryNodeChild(node, 'dirty')
|
|
112
|
+
if (attrs.type !== 'groups') return
|
|
113
|
+
await groupFetchAllParticipating()
|
|
114
|
+
await sock.cleanDirtyBits('groups')
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
...sock,
|
|
119
|
+
groupQuery,
|
|
120
|
+
groupMetadata,
|
|
121
|
+
groupCreate: async (subject, participants) => {
|
|
122
|
+
const key = generateMessageIDV2()
|
|
123
|
+
const result = await groupQuery('@g.us', 'set', [
|
|
124
|
+
{ tag: 'create', attrs: { subject, key }, content: participants.map(jid => ({ tag: 'participant', attrs: { jid } })) }
|
|
125
|
+
])
|
|
126
|
+
return extractGroupMetadata(result)
|
|
127
|
+
},
|
|
128
|
+
groupLeave: async id => {
|
|
129
|
+
await groupQuery('@g.us', 'set', [{ tag: 'leave', attrs: {}, content: [{ tag: 'group', attrs: { id } }] }])
|
|
130
|
+
},
|
|
131
|
+
groupUpdateSubject: async (jid, subject) => {
|
|
132
|
+
await groupQuery(jid, 'set', [{ tag: 'subject', attrs: {}, content: Buffer.from(subject, 'utf-8') }])
|
|
133
|
+
},
|
|
134
|
+
groupRequestParticipantsList: async jid => {
|
|
135
|
+
const result = await groupQuery(jid, 'get', [{ tag: 'membership_approval_requests', attrs: {} }])
|
|
136
|
+
const node = getBinaryNodeChild(result, 'membership_approval_requests')
|
|
137
|
+
return getBinaryNodeChildren(node, 'membership_approval_request').map(v => v.attrs)
|
|
138
|
+
},
|
|
139
|
+
groupRequestParticipantsUpdate: async (jid, participants, action) => {
|
|
140
|
+
const result = await groupQuery(jid, 'set', [
|
|
141
|
+
{ tag: 'membership_requests_action', attrs: {}, content: [{ tag: action, attrs: {}, content: participants.map(jid => ({ tag: 'participant', attrs: { jid } })) }] }
|
|
142
|
+
])
|
|
143
|
+
const node = getBinaryNodeChild(result, 'membership_requests_action')
|
|
144
|
+
const nodeAction = getBinaryNodeChild(node, action)
|
|
145
|
+
return getBinaryNodeChildren(nodeAction, 'participant').map(p => ({ status: p.attrs.error || '200', jid: p.attrs.jid }))
|
|
146
|
+
},
|
|
147
|
+
groupParticipantsUpdate: async (jid, participants, action) => {
|
|
148
|
+
const result = await groupQuery(jid, 'set', [{ tag: action, attrs: {}, content: participants.map(jid => ({ tag: 'participant', attrs: { jid } })) }])
|
|
149
|
+
const node = getBinaryNodeChild(result, action)
|
|
150
|
+
return getBinaryNodeChildren(node, 'participant').map(p => ({ status: p.attrs.error || '200', jid: p.attrs.jid, content: p }))
|
|
151
|
+
},
|
|
152
|
+
groupUpdateDescription: async (jid, description) => {
|
|
153
|
+
const metadata = await groupMetadata(jid)
|
|
154
|
+
const prev = metadata.descId ?? null
|
|
155
|
+
await groupQuery(jid, 'set', [
|
|
156
|
+
{
|
|
157
|
+
tag: 'description',
|
|
158
|
+
attrs: { ...(description ? { id: generateMessageIDV2() } : { delete: 'true' }), ...(prev ? { prev } : {}) },
|
|
159
|
+
content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
|
|
160
|
+
}
|
|
161
|
+
])
|
|
162
|
+
},
|
|
163
|
+
groupInviteCode: async jid => {
|
|
164
|
+
const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }])
|
|
165
|
+
return getBinaryNodeChild(result, 'invite')?.attrs.code
|
|
166
|
+
},
|
|
167
|
+
groupRevokeInvite: async jid => {
|
|
168
|
+
const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }])
|
|
169
|
+
return getBinaryNodeChild(result, 'invite')?.attrs.code
|
|
170
|
+
},
|
|
171
|
+
groupAcceptInvite: async code => {
|
|
172
|
+
const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }])
|
|
173
|
+
return getBinaryNodeChild(results, 'group')?.attrs.jid
|
|
174
|
+
},
|
|
175
|
+
groupRevokeInviteV4: async (groupJid, invitedJid) => {
|
|
176
|
+
const result = await groupQuery(groupJid, 'set', [{ tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }])
|
|
177
|
+
return !!result
|
|
178
|
+
},
|
|
179
|
+
groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
|
|
180
|
+
key = typeof key === 'string' ? { remoteJid: key } : key
|
|
181
|
+
const results = await groupQuery(inviteMessage.groupJid, 'set', [
|
|
182
|
+
{ tag: 'accept', attrs: { code: inviteMessage.inviteCode, expiration: inviteMessage.inviteExpiration.toString(), admin: key.remoteJid } }
|
|
183
|
+
])
|
|
184
|
+
if (key.id) {
|
|
185
|
+
inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage)
|
|
186
|
+
inviteMessage.inviteExpiration = 0
|
|
187
|
+
inviteMessage.inviteCode = ''
|
|
188
|
+
ev.emit('messages.update', [{ key, update: { message: { groupInviteMessage: inviteMessage } } }])
|
|
189
|
+
}
|
|
190
|
+
await upsertMessage(
|
|
191
|
+
{
|
|
192
|
+
key: { remoteJid: inviteMessage.groupJid, id: generateMessageIDV2(sock.user?.id), fromMe: false, participant: key.remoteJid },
|
|
193
|
+
messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
|
|
194
|
+
messageStubParameters: [JSON.stringify(authState.creds.me)],
|
|
195
|
+
participant: key.remoteJid,
|
|
196
|
+
messageTimestamp: unixTimestampSeconds()
|
|
197
|
+
},
|
|
198
|
+
'notify'
|
|
199
|
+
)
|
|
200
|
+
return results.attrs.from
|
|
201
|
+
}),
|
|
202
|
+
groupGetInviteInfo: async code => extractGroupMetadata(await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }])),
|
|
203
|
+
groupToggleEphemeral: async (jid, ephemeralExpiration) => {
|
|
204
|
+
const content = ephemeralExpiration ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } : { tag: 'not_ephemeral', attrs: {} }
|
|
205
|
+
await groupQuery(jid, 'set', [content])
|
|
206
|
+
},
|
|
207
|
+
groupSettingUpdate: async (jid, setting) => {
|
|
208
|
+
await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }])
|
|
209
|
+
},
|
|
210
|
+
groupMemberAddMode: async (jid, mode) => {
|
|
211
|
+
await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }])
|
|
212
|
+
},
|
|
213
|
+
groupJoinApprovalMode: async (jid, mode) => {
|
|
214
|
+
await groupQuery(jid, 'set', [{ tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }])
|
|
215
|
+
},
|
|
216
|
+
groupFetchAllParticipating
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* ------------------------------------------------------------------ */
|
|
221
|
+
/* Newsletters (wraps makeGroupsSocket) */
|
|
222
|
+
/* ------------------------------------------------------------------ */
|
|
223
|
+
|
|
224
|
+
const parseNewsletterCreateResponse = response => {
|
|
225
|
+
const { id, thread_metadata: thread, viewer_metadata: viewer } = response
|
|
226
|
+
return {
|
|
227
|
+
id,
|
|
228
|
+
owner: undefined,
|
|
229
|
+
name: thread.name.text,
|
|
230
|
+
creation_time: parseInt(thread.creation_time, 10),
|
|
231
|
+
description: thread.description.text,
|
|
232
|
+
invite: thread.invite,
|
|
233
|
+
subscribers: parseInt(thread.subscribers_count, 10),
|
|
234
|
+
verification: thread.verification,
|
|
235
|
+
picture: { id: thread.picture?.id, directPath: thread.picture?.direct_path },
|
|
236
|
+
mute_state: viewer.mute
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const parseNewsletterMetadata = result => {
|
|
241
|
+
if (typeof result !== 'object' || result === null) return null
|
|
242
|
+
if ('id' in result && typeof result.id === 'string') return result
|
|
243
|
+
if ('result' in result && typeof result.result === 'object' && result.result !== null && 'id' in result.result) return result.result
|
|
244
|
+
return null
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export const makeNewsletterSocket = config => {
|
|
248
|
+
const sock = makeGroupsSocket(config)
|
|
249
|
+
const { query, generateMessageTag, logger } = sock
|
|
250
|
+
const executeWMexQuery = (variables, queryId, dataPath) => genericExecuteWMexQuery(variables, queryId, dataPath, query, generateMessageTag)
|
|
251
|
+
const newsletterUpdate = async (jid, updates) =>
|
|
252
|
+
executeWMexQuery({ newsletter_id: jid, updates: { ...updates, settings: null } }, QueryIds.UPDATE_METADATA, 'xwa2_newsletter_update')
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
...sock,
|
|
256
|
+
executeWMexQuery,
|
|
257
|
+
newsletterCreate: async (name, description) => {
|
|
258
|
+
const rawResponse = await executeWMexQuery({ input: { name, description: description ?? null } }, QueryIds.CREATE, XWAPaths.xwa2_newsletter_create)
|
|
259
|
+
return parseNewsletterCreateResponse(rawResponse)
|
|
260
|
+
},
|
|
261
|
+
newsletterUpdate,
|
|
262
|
+
newsletterSubscribers: async jid => executeWMexQuery({ newsletter_id: jid }, QueryIds.SUBSCRIBERS, XWAPaths.xwa2_newsletter_subscribers),
|
|
263
|
+
newsletterSubscribed: async () => executeWMexQuery({}, QueryIds.SUBSCRIBED, XWAPaths.xwa2_newsletter_subscribed),
|
|
264
|
+
newsletterMetadata: async (type, key) => {
|
|
265
|
+
const variables = { fetch_creation_time: true, fetch_full_image: true, fetch_viewer_metadata: true, input: { key, type: type.toUpperCase() } }
|
|
266
|
+
const result = await executeWMexQuery(variables, QueryIds.METADATA, XWAPaths.xwa2_newsletter_metadata)
|
|
267
|
+
return parseNewsletterMetadata(result)
|
|
268
|
+
},
|
|
269
|
+
newsletterFollow: jid => executeWMexQuery({ newsletter_id: jid }, QueryIds.FOLLOW, XWAPaths.xwa2_newsletter_join_v2),
|
|
270
|
+
newsletterUnfollow: jid => executeWMexQuery({ newsletter_id: jid }, QueryIds.UNFOLLOW, XWAPaths.xwa2_newsletter_leave_v2),
|
|
271
|
+
newsletterMute: jid => executeWMexQuery({ newsletter_id: jid }, QueryIds.MUTE, XWAPaths.xwa2_newsletter_mute_v2),
|
|
272
|
+
newsletterUnmute: jid => executeWMexQuery({ newsletter_id: jid }, QueryIds.UNMUTE, XWAPaths.xwa2_newsletter_unmute_v2),
|
|
273
|
+
newsletterUpdateName: async (jid, name) => newsletterUpdate(jid, { name }),
|
|
274
|
+
newsletterUpdateDescription: async (jid, description) => newsletterUpdate(jid, { description }),
|
|
275
|
+
newsletterUpdatePicture: async (jid, content) => {
|
|
276
|
+
const { img } = await generateProfilePicture(content)
|
|
277
|
+
return newsletterUpdate(jid, { picture: img.toString('base64') })
|
|
278
|
+
},
|
|
279
|
+
newsletterRemovePicture: async jid => newsletterUpdate(jid, { picture: '' }),
|
|
280
|
+
newsletterReactMessage: async (jid, serverId, reaction) => {
|
|
281
|
+
await query({
|
|
282
|
+
tag: 'message',
|
|
283
|
+
attrs: { to: jid, ...(reaction ? {} : { edit: '7' }), type: 'reaction', server_id: serverId, id: generateMessageTag() },
|
|
284
|
+
content: [{ tag: 'reaction', attrs: reaction ? { code: reaction } : {} }]
|
|
285
|
+
})
|
|
286
|
+
},
|
|
287
|
+
newsletterFetchMessages: async (type, key, count, after, before) => {
|
|
288
|
+
const messagesAttrs = { count: count.toString(), type, [type === 'jid' ? 'jid' : 'key']: key }
|
|
289
|
+
if (after) messagesAttrs.after = after.toString()
|
|
290
|
+
if (before) messagesAttrs.before = before.toString()
|
|
291
|
+
const result = await query({
|
|
292
|
+
tag: 'iq',
|
|
293
|
+
attrs: { id: generateMessageTag(), type: 'get', xmlns: 'newsletter', to: S_WHATSAPP_NET },
|
|
294
|
+
content: [{ tag: 'messages', attrs: messagesAttrs }]
|
|
295
|
+
})
|
|
296
|
+
const messagesNode = getBinaryNodeChild(result, 'messages')
|
|
297
|
+
if (!messagesNode) return []
|
|
298
|
+
const newsletterJid = messagesNode.attrs.jid || (type === 'jid' ? key : undefined)
|
|
299
|
+
const messages = []
|
|
300
|
+
for (const child of getBinaryNodeChildren(messagesNode, 'message')) {
|
|
301
|
+
const plaintextNode = getBinaryNodeChild(child, 'plaintext')
|
|
302
|
+
if (!plaintextNode?.content) continue
|
|
303
|
+
try {
|
|
304
|
+
const contentBuf = typeof plaintextNode.content === 'string' ? Buffer.from(plaintextNode.content, 'binary') : Buffer.from(plaintextNode.content)
|
|
305
|
+
const messageProto = proto.Message.decode(contentBuf).toJSON()
|
|
306
|
+
const fullMessage = proto.WebMessageInfo.fromObject({
|
|
307
|
+
key: { remoteJid: newsletterJid, id: child.attrs.id || child.attrs.server_id, server_id: child.attrs.server_id, fromMe: false },
|
|
308
|
+
message: messageProto,
|
|
309
|
+
messageTimestamp: child.attrs.t ? +child.attrs.t : undefined
|
|
310
|
+
}).toJSON()
|
|
311
|
+
messages.push(fullMessage)
|
|
312
|
+
} catch (error) {
|
|
313
|
+
logger.error({ error }, 'Failed to decode newsletter message')
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return messages
|
|
317
|
+
},
|
|
318
|
+
subscribeNewsletterUpdates: async jid => {
|
|
319
|
+
const result = await query({
|
|
320
|
+
tag: 'iq',
|
|
321
|
+
attrs: { id: generateMessageTag(), type: 'set', xmlns: 'newsletter', to: jid },
|
|
322
|
+
content: [{ tag: 'live_updates', attrs: {}, content: [] }]
|
|
323
|
+
})
|
|
324
|
+
const liveUpdatesNode = getBinaryNodeChild(result, 'live_updates')
|
|
325
|
+
const duration = liveUpdatesNode?.attrs?.duration
|
|
326
|
+
return duration ? { duration } : null
|
|
327
|
+
},
|
|
328
|
+
newsletterAdminCount: async jid => {
|
|
329
|
+
const response = await executeWMexQuery({ newsletter_id: jid }, QueryIds.ADMIN_COUNT, XWAPaths.xwa2_newsletter_admin_count)
|
|
330
|
+
return response.admin_count
|
|
331
|
+
},
|
|
332
|
+
newsletterChangeOwner: async (jid, newOwnerJid) => {
|
|
333
|
+
await executeWMexQuery({ newsletter_id: jid, user_id: newOwnerJid }, QueryIds.CHANGE_OWNER, XWAPaths.xwa2_newsletter_change_owner)
|
|
334
|
+
},
|
|
335
|
+
newsletterDemote: async (jid, userJid) => {
|
|
336
|
+
await executeWMexQuery({ newsletter_id: jid, user_id: userJid }, QueryIds.DEMOTE, XWAPaths.xwa2_newsletter_demote)
|
|
337
|
+
},
|
|
338
|
+
newsletterDelete: async jid => {
|
|
339
|
+
await executeWMexQuery({ newsletter_id: jid }, QueryIds.DELETE, XWAPaths.xwa2_newsletter_delete_v2)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|