@x4r1s3/baileys 0.0.0-stage → 1.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.
Files changed (185) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1245 -2
  3. package/SECURITY.md +47 -0
  4. package/WAProto/V +1 -0
  5. package/WAProto/WAProto.proto +8892 -0
  6. package/WAProto/index.d.ts +104128 -0
  7. package/WAProto/index.js +334920 -0
  8. package/assets/rolling-circle.svg +133 -0
  9. package/assets/x4r1s3-banner.svg +32 -0
  10. package/engine-requirements.js +27 -0
  11. package/lib/Builders/A2UI.js +245 -0
  12. package/lib/Builders/AIRich.js +179 -0
  13. package/lib/Builders/Button.js +108 -0
  14. package/lib/Builders/CFC.js +284 -0
  15. package/lib/Builders/Carousel.js +81 -0
  16. package/lib/Call/THIRD_PARTY_LICENSE.txt +21 -0
  17. package/lib/Call/astralena-v3.mjs +96 -0
  18. package/lib/Call/audio-feeder.mjs +201 -0
  19. package/lib/Call/audio-recorder.mjs +250 -0
  20. package/lib/Call/fetch-wasm-resources.mjs +104 -0
  21. package/lib/Call/index.mjs +707 -0
  22. package/lib/Call/relay-transport.mjs +563 -0
  23. package/lib/Call/signaling.mjs +660 -0
  24. package/lib/Call/types.mjs +17 -0
  25. package/lib/Call/video-feeder.mjs +94 -0
  26. package/lib/Call/voip-resources.mjs +259 -0
  27. package/lib/Call/wasm-engine.mjs +1372 -0
  28. package/lib/Call/worker-bootstrap.mjs +1042 -0
  29. package/lib/Defaults/ban-checker.js +56 -0
  30. package/lib/Defaults/index.js +170 -0
  31. package/lib/Signal/Group/ciphertext-message.js +23 -0
  32. package/lib/Signal/Group/group-session-builder.js +41 -0
  33. package/lib/Signal/Group/group_cipher.js +93 -0
  34. package/lib/Signal/Group/index.js +23 -0
  35. package/lib/Signal/Group/keyhelper.js +29 -0
  36. package/lib/Signal/Group/sender-chain-key.js +37 -0
  37. package/lib/Signal/Group/sender-key-distribution-message.js +74 -0
  38. package/lib/Signal/Group/sender-key-message.js +77 -0
  39. package/lib/Signal/Group/sender-key-name.js +59 -0
  40. package/lib/Signal/Group/sender-key-record.js +52 -0
  41. package/lib/Signal/Group/sender-key-state.js +95 -0
  42. package/lib/Signal/Group/sender-message-key.js +37 -0
  43. package/lib/Signal/libsignal.js +440 -0
  44. package/lib/Signal/lid-mapping.js +292 -0
  45. package/lib/Socket/Client/index.js +14 -0
  46. package/lib/Socket/Client/types.js +22 -0
  47. package/lib/Socket/Client/websocket.js +65 -0
  48. package/lib/Socket/aigroups.js +327 -0
  49. package/lib/Socket/ban-checker.js +165 -0
  50. package/lib/Socket/business.js +419 -0
  51. package/lib/Socket/chats.js +1921 -0
  52. package/lib/Socket/communities.js +443 -0
  53. package/lib/Socket/graphql.js +911 -0
  54. package/lib/Socket/groups.js +561 -0
  55. package/lib/Socket/index.js +49 -0
  56. package/lib/Socket/interactive-handler.js +612 -0
  57. package/lib/Socket/interop.js +575 -0
  58. package/lib/Socket/managed-account.js +220 -0
  59. package/lib/Socket/messages-recv.js +2193 -0
  60. package/lib/Socket/messages-send.js +4141 -0
  61. package/lib/Socket/mex.js +53 -0
  62. package/lib/Socket/newsletter.js +291 -0
  63. package/lib/Socket/privacy.js +458 -0
  64. package/lib/Socket/registration.js +440 -0
  65. package/lib/Socket/socket.js +1042 -0
  66. package/lib/Socket/status-actions.js +287 -0
  67. package/lib/Socket/username.js +241 -0
  68. package/lib/Store/index.js +16 -0
  69. package/lib/Store/make-cache-manager-store.js +101 -0
  70. package/lib/Store/make-in-memory-store.js +432 -0
  71. package/lib/Store/make-ordered-dictionary.js +90 -0
  72. package/lib/Store/object-repository.js +35 -0
  73. package/lib/Types/Auth.js +13 -0
  74. package/lib/Types/Bussines.js +13 -0
  75. package/lib/Types/Call.js +13 -0
  76. package/lib/Types/Chat.js +19 -0
  77. package/lib/Types/Contact.js +13 -0
  78. package/lib/Types/Events.js +13 -0
  79. package/lib/Types/GroupMetadata.js +13 -0
  80. package/lib/Types/Label.js +36 -0
  81. package/lib/Types/LabelAssociation.js +18 -0
  82. package/lib/Types/Message.js +30 -0
  83. package/lib/Types/Mex.js +53 -0
  84. package/lib/Types/Product.js +13 -0
  85. package/lib/Types/RichType.js +34 -0
  86. package/lib/Types/Signal.js +13 -0
  87. package/lib/Types/Socket.js +14 -0
  88. package/lib/Types/State.js +60 -0
  89. package/lib/Types/USync.js +13 -0
  90. package/lib/Types/index.js +37 -0
  91. package/lib/Utils/auth-utils.js +313 -0
  92. package/lib/Utils/ban-checker.js +48 -0
  93. package/lib/Utils/bot-capabilities.js +96 -0
  94. package/lib/Utils/bot-planning-replay.js +177 -0
  95. package/lib/Utils/browser-utils.js +40 -0
  96. package/lib/Utils/business.js +242 -0
  97. package/lib/Utils/c2582.js +147 -0
  98. package/lib/Utils/chat-utils.js +905 -0
  99. package/lib/Utils/companion-reg-client-utils.js +104 -0
  100. package/lib/Utils/consumer-application.js +114 -0
  101. package/lib/Utils/crypto.js +129 -0
  102. package/lib/Utils/decode-wa-message.js +496 -0
  103. package/lib/Utils/direct-chat-theme.js +164 -0
  104. package/lib/Utils/event-buffer.js +601 -0
  105. package/lib/Utils/experimental-proto.js +206 -0
  106. package/lib/Utils/games/miniapp.js +17 -0
  107. package/lib/Utils/games/slot-machine.js +270 -0
  108. package/lib/Utils/games/website-preview.js +306 -0
  109. package/lib/Utils/generics.js +402 -0
  110. package/lib/Utils/group-history.js +68 -0
  111. package/lib/Utils/group-wallpaper.js +168 -0
  112. package/lib/Utils/history.js +162 -0
  113. package/lib/Utils/identity-change-handler.js +60 -0
  114. package/lib/Utils/index.js +61 -0
  115. package/lib/Utils/jid-display-normalization.js +221 -0
  116. package/lib/Utils/link-preview.js +96 -0
  117. package/lib/Utils/logger.js +14 -0
  118. package/lib/Utils/lt-hash.js +19 -0
  119. package/lib/Utils/make-mutex.js +44 -0
  120. package/lib/Utils/message-composer.js +709 -0
  121. package/lib/Utils/message-inspect.js +79 -0
  122. package/lib/Utils/message-retry-manager.js +253 -0
  123. package/lib/Utils/message-security.js +167 -0
  124. package/lib/Utils/messages-media.js +1027 -0
  125. package/lib/Utils/messages.js +2467 -0
  126. package/lib/Utils/meta-ai-msmsg.js +215 -0
  127. package/lib/Utils/meta-compositing.js +239 -0
  128. package/lib/Utils/native-flow.js +158 -0
  129. package/lib/Utils/newsletter-live-update-utils.js +74 -0
  130. package/lib/Utils/newsletter-status-utils.js +152 -0
  131. package/lib/Utils/noise-handler.js +212 -0
  132. package/lib/Utils/offline-node-processor.js +51 -0
  133. package/lib/Utils/poll-manager.js +206 -0
  134. package/lib/Utils/portable-compat.js +12 -0
  135. package/lib/Utils/pre-key-manager.js +117 -0
  136. package/lib/Utils/process-message.js +610 -0
  137. package/lib/Utils/reporting-utils.js +269 -0
  138. package/lib/Utils/rich-message-utils.js +2841 -0
  139. package/lib/Utils/scheduled-message.js +107 -0
  140. package/lib/Utils/signal.js +212 -0
  141. package/lib/Utils/source-grep.js +57 -0
  142. package/lib/Utils/stanza-ack.js +49 -0
  143. package/lib/Utils/sticker-annotation.js +87 -0
  144. package/lib/Utils/sync-action-utils.js +59 -0
  145. package/lib/Utils/tc-token-utils.js +174 -0
  146. package/lib/Utils/use-multi-file-auth-state.js +132 -0
  147. package/lib/Utils/use-single-file-auth-state.js +108 -0
  148. package/lib/Utils/use-sqlite-auth-state.js +120 -0
  149. package/lib/Utils/validate-connection.js +221 -0
  150. package/lib/Utils/view-once-utils.js +218 -0
  151. package/lib/Utils/voip-rekey.js +32 -0
  152. package/lib/Utils/website-preview.js +27 -0
  153. package/lib/Utils/welcome-flow.js +228 -0
  154. package/lib/Utils/x4r1s3.js +144 -0
  155. package/lib/WABinary/constants.js +1475 -0
  156. package/lib/WABinary/decode.js +273 -0
  157. package/lib/WABinary/encode.js +231 -0
  158. package/lib/WABinary/generic-utils.js +242 -0
  159. package/lib/WABinary/index.js +17 -0
  160. package/lib/WABinary/jid-utils.js +107 -0
  161. package/lib/WABinary/types.js +13 -0
  162. package/lib/WAM/BinaryInfo.js +21 -0
  163. package/lib/WAM/constants.js +22864 -0
  164. package/lib/WAM/encode.js +161 -0
  165. package/lib/WAM/index.js +15 -0
  166. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +108 -0
  167. package/lib/WAUSync/Protocols/USyncContactProtocol.js +68 -0
  168. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
  169. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +38 -0
  170. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +82 -0
  171. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +40 -0
  172. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +37 -0
  173. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
  174. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +46 -0
  175. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +36 -0
  176. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -0
  177. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +40 -0
  178. package/lib/WAUSync/Protocols/index.js +24 -0
  179. package/lib/WAUSync/USyncQuery.js +213 -0
  180. package/lib/WAUSync/USyncUser.js +66 -0
  181. package/lib/WAUSync/index.js +15 -0
  182. package/lib/antiban.cjs +4726 -0
  183. package/lib/index.d.ts +1895 -0
  184. package/lib/index.js +41 -0
  185. package/package.json +125 -4
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @X4R1S3
3
+ *
4
+ * File: lib/Socket/mex.js
5
+ * About: Mex module for the X4R1S3 WhatsApp protocol client.
6
+ * Important exports: executeWMexQuery
7
+ * Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
8
+ *
9
+ * Copyright and attribution notice.
10
+ * Do not copy, reproduce, redistribute, modify, or create derivative works
11
+ * from this source without explicit written permission.
12
+ */
13
+ import { Boom } from '@hapi/boom';
14
+ import { getBinaryNodeChild, S_WHATSAPP_NET } from '../WABinary/index.js';
15
+ const wMexQuery = (variables, queryId, query, generateMessageTag) => {
16
+ return query({
17
+ tag: 'iq',
18
+ attrs: {
19
+ id: generateMessageTag(),
20
+ type: 'get',
21
+ to: S_WHATSAPP_NET,
22
+ xmlns: 'w:mex'
23
+ },
24
+ content: [
25
+ {
26
+ tag: 'query',
27
+ attrs: { query_id: queryId },
28
+ content: Buffer.from(JSON.stringify({ variables }), 'utf-8')
29
+ }
30
+ ]
31
+ });
32
+ };
33
+ export const executeWMexQuery = async (variables, queryId, dataPath, query, generateMessageTag) => {
34
+ const result = await wMexQuery(variables, queryId, query, generateMessageTag);
35
+ const child = getBinaryNodeChild(result, 'result');
36
+ if (child?.content) {
37
+ const data = JSON.parse(child.content.toString());
38
+ if (data.errors && data.errors.length > 0) {
39
+ const errorMessages = data.errors.map((err) => err.message || 'Unknown error').join(', ');
40
+ const firstError = data.errors[0];
41
+ const errorCode = firstError.extensions?.error_code || 400;
42
+ throw new Boom(`GraphQL server error: ${errorMessages}`, { statusCode: errorCode, data: firstError });
43
+ }
44
+ const response = dataPath ? data?.data?.[dataPath] : data?.data;
45
+ if (typeof response !== 'undefined') {
46
+ return response;
47
+ }
48
+ }
49
+ const action = (dataPath || '').startsWith('xwa2_')
50
+ ? dataPath.substring(5).replace(/_/g, ' ')
51
+ : dataPath?.replace(/_/g, ' ');
52
+ throw new Boom(`Failed to ${action}, unexpected response structure.`, { statusCode: 400, data: result });
53
+ };
@@ -0,0 +1,291 @@
1
+ /**
2
+ * @X4R1S3
3
+ *
4
+ * File: lib/Socket/newsletter.js
5
+ * About: Newsletter module for the X4R1S3 WhatsApp protocol client.
6
+ * Important exports: makeNewsletterSocket
7
+ * Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
8
+ *
9
+ * Copyright and attribution notice.
10
+ * Do not copy, reproduce, redistribute, modify, or create derivative works
11
+ * from this source without explicit written permission.
12
+ */
13
+ import { Boom } from '@hapi/boom';
14
+ import { proto } from '../../WAProto/index.js';
15
+ import { QueryIds, XWAPaths } from '../Types/index.js';
16
+ import { generateProfilePicture } from '../Utils/messages-media.js';
17
+ import { getBinaryNodeChild, getBinaryNodeChildren, isJidNewsletter, S_WHATSAPP_NET } from '../WABinary/index.js';
18
+ import { makeAIGroupsSocket } from './aigroups.js';
19
+ import { executeWMexQuery as genericExecuteWMexQuery } from './mex.js';
20
+ const parseNewsletterCreateResponse = (response) => {
21
+ const { id, thread_metadata: thread, viewer_metadata: viewer } = response;
22
+ return {
23
+ id: id,
24
+ owner: undefined,
25
+ name: thread.name.text,
26
+ creation_time: parseInt(thread.creation_time, 10),
27
+ description: thread.description.text,
28
+ invite: thread.invite,
29
+ subscribers: parseInt(thread.subscribers_count, 10),
30
+ verification: thread.verification,
31
+ picture: {
32
+ id: thread.picture?.id,
33
+ directPath: thread.picture?.direct_path
34
+ },
35
+ mute_state: viewer.mute
36
+ };
37
+ };
38
+ const parseNewsletterMetadata = (result) => {
39
+ if (typeof result !== 'object' || result === null) {
40
+ return null;
41
+ }
42
+ if ('id' in result && typeof result.id === 'string') {
43
+ return result;
44
+ }
45
+ if ('result' in result && typeof result.result === 'object' && result.result !== null && 'id' in result.result) {
46
+ return result.result;
47
+ }
48
+ return null;
49
+ };
50
+ const normalizeNewsletterInviteCode = (value) => {
51
+ if (typeof value !== 'string' || !value.trim()) {
52
+ throw new Boom('newsletterGetInviteInfo requires a non-empty invite code or URL', { statusCode: 400 });
53
+ }
54
+ return value.trim()
55
+ .replace(/^https?:\/\/(?:www\.)?whatsapp\.com\/channel\//i, '')
56
+ .replace(/^https?:\/\/wa\.me\/channel\//i, '')
57
+ .split(/[/?#]/, 1)[0];
58
+ };
59
+ export const makeNewsletterSocket = (config) => {
60
+ const sock = makeAIGroupsSocket(config);
61
+ const { query, generateMessageTag } = sock;
62
+ const executeWMexQuery = (variables, queryId, dataPath) => {
63
+ return genericExecuteWMexQuery(variables, queryId, dataPath, query, generateMessageTag);
64
+ };
65
+ const newsletterUpdate = async (jid, updates) => {
66
+ const variables = {
67
+ newsletter_id: jid,
68
+ updates: {
69
+ ...updates,
70
+ settings: null
71
+ }
72
+ };
73
+ return executeWMexQuery(variables, QueryIds.UPDATE_METADATA, 'xwa2_newsletter_update');
74
+ };
75
+ const newsletterAdminCapabilities = async jid => {
76
+ if (!isJidNewsletter(jid)) {
77
+ throw new Boom('Newsletter admin capabilities require a @newsletter JID', { statusCode: 400 });
78
+ }
79
+ const response = await executeWMexQuery(
80
+ { newsletter_id: jid },
81
+ QueryIds.ADMIN_CAPABILITIES,
82
+ XWAPaths.xwa2_newsletter_admin_capabilities
83
+ );
84
+ return Array.isArray(response?.capabilities) ? response.capabilities : [];
85
+ };
86
+ return {
87
+ ...sock,
88
+ executeWMexQuery,
89
+ newsletterCreate: async (name, description) => {
90
+ const variables = {
91
+ input: {
92
+ name,
93
+ description: description ?? null
94
+ }
95
+ };
96
+ const rawResponse = await executeWMexQuery(variables, QueryIds.CREATE, XWAPaths.xwa2_newsletter_create);
97
+ return parseNewsletterCreateResponse(rawResponse);
98
+ },
99
+ newsletterUpdate,
100
+ newsletterSubscribers: async (jid) => {
101
+ return executeWMexQuery({ newsletter_id: jid }, QueryIds.SUBSCRIBERS, XWAPaths.xwa2_newsletter_subscribers);
102
+ },
103
+ // Lia@Changes 29-01-26 --- Add newsletterSubscribed to fetch all subscribed newsletters (similar to groupFetchAllParticipating (⁠ ⁠╹⁠▽⁠╹⁠ ⁠))
104
+ newsletterSubscribed: async () => {
105
+ return executeWMexQuery({}, QueryIds.SUBSCRIBED, XWAPaths.xwa2_newsletter_subscribed);
106
+ },
107
+ newsletterMetadata: async (type, key) => {
108
+ if (typeof type !== 'string' || !type.trim()) {
109
+ throw new Boom('newsletterMetadata requires a metadata type', { statusCode: 400 });
110
+ }
111
+ if (typeof key !== 'string' || !key.trim()) {
112
+ throw new Boom('newsletterMetadata requires a metadata key', { statusCode: 400 });
113
+ }
114
+ const variables = {
115
+ fetch_creation_time: true,
116
+ fetch_full_image: true,
117
+ fetch_viewer_metadata: true,
118
+ input: {
119
+ key,
120
+ type: type.toUpperCase()
121
+ }
122
+ };
123
+ const result = await executeWMexQuery(variables, QueryIds.METADATA, XWAPaths.xwa2_newsletter_metadata);
124
+ return parseNewsletterMetadata(result);
125
+ },
126
+ newsletterGetInviteInfo: async (inviteCodeOrUrl) => {
127
+ const inviteCode = normalizeNewsletterInviteCode(inviteCodeOrUrl);
128
+ return executeWMexQuery(
129
+ { input: { key: inviteCode, type: 'INVITE' }, fetch_creation_time: true, fetch_full_image: true, fetch_viewer_metadata: true },
130
+ QueryIds.METADATA,
131
+ XWAPaths.xwa2_newsletter_metadata
132
+ ).then(parseNewsletterMetadata);
133
+ },
134
+ newsletterFollow: (jid) => {
135
+ return executeWMexQuery({ newsletter_id: jid }, QueryIds.FOLLOW, XWAPaths.xwa2_newsletter_join_v2);
136
+ },
137
+ newsletterUnfollow: (jid) => {
138
+ return executeWMexQuery({ newsletter_id: jid }, QueryIds.UNFOLLOW, XWAPaths.xwa2_newsletter_leave_v2);
139
+ },
140
+ newsletterMute: (jid) => {
141
+ return executeWMexQuery({ newsletter_id: jid }, QueryIds.MUTE, XWAPaths.xwa2_newsletter_mute_v2);
142
+ },
143
+ newsletterUnmute: (jid) => {
144
+ return executeWMexQuery({ newsletter_id: jid }, QueryIds.UNMUTE, XWAPaths.xwa2_newsletter_unmute_v2);
145
+ },
146
+ newsletterUpdateName: async (jid, name) => {
147
+ return await newsletterUpdate(jid, { name });
148
+ },
149
+ newsletterUpdateDescription: async (jid, description) => {
150
+ return await newsletterUpdate(jid, { description });
151
+ },
152
+ newsletterUpdatePicture: async (jid, content) => {
153
+ const { img } = await generateProfilePicture(content);
154
+ return await newsletterUpdate(jid, { picture: img.toString('base64') });
155
+ },
156
+ newsletterRemovePicture: async (jid) => {
157
+ return await newsletterUpdate(jid, { picture: '' });
158
+ },
159
+ newsletterReactMessage: async (jid, serverId, reaction) => {
160
+ if (typeof jid !== 'string' || !jid.trim()) {
161
+ throw new Boom('newsletterReactMessage requires a newsletter JID', { statusCode: 400 });
162
+ }
163
+ if (serverId === undefined || serverId === null || String(serverId).trim() === '') {
164
+ throw new Boom('newsletterReactMessage requires a server message ID', { statusCode: 400 });
165
+ }
166
+ if (reaction !== undefined && reaction !== null && typeof reaction !== 'string') {
167
+ throw new Boom('newsletterReactMessage reaction must be a string, null, or undefined', { statusCode: 400 });
168
+ }
169
+ const code = reaction || '';
170
+ await query({
171
+ tag: 'message',
172
+ attrs: {
173
+ to: jid,
174
+ ...(code ? {} : { edit: '7' }),
175
+ type: 'reaction',
176
+ server_id: serverId,
177
+ id: generateMessageTag()
178
+ },
179
+ content: [
180
+ {
181
+ tag: 'reaction',
182
+ attrs: code ? { code } : {}
183
+ }
184
+ ]
185
+ });
186
+ },
187
+ newsletterFetchMessages: async (type, key, count, after, before) => {
188
+ // WA Web: GetNewsletterMessages endpoint
189
+ // Request: iq(to=s.whatsapp.net) -> messages(count, type, jid|key, [after|before])
190
+ const messagesAttrs = {
191
+ count: count.toString(),
192
+ type,
193
+ [type === 'jid' ? 'jid' : 'key']: key
194
+ };
195
+ if (after) {
196
+ messagesAttrs.after = after.toString();
197
+ }
198
+ if (before) {
199
+ messagesAttrs.before = before.toString();
200
+ }
201
+ const result = await query({
202
+ tag: 'iq',
203
+ attrs: {
204
+ id: generateMessageTag(),
205
+ type: 'get',
206
+ xmlns: 'newsletter',
207
+ to: S_WHATSAPP_NET
208
+ },
209
+ content: [
210
+ {
211
+ tag: 'messages',
212
+ attrs: messagesAttrs
213
+ }
214
+ ]
215
+ });
216
+ // Response: iq -> messages(jid="newsletter_jid") -> message[]
217
+ const messagesNode = getBinaryNodeChild(result, 'messages');
218
+ if (!messagesNode) {
219
+ return [];
220
+ }
221
+ // The response messages node carries the newsletter JID
222
+ const newsletterJid = messagesNode.attrs.jid || (type === 'jid' ? key : undefined);
223
+ const messages = [];
224
+ // WA Web: mapChildrenWithTag(messages, "message", 0, 300, ...)
225
+ for (const child of getBinaryNodeChildren(messagesNode, 'message')) {
226
+ const plaintextNode = getBinaryNodeChild(child, 'plaintext');
227
+ if (!plaintextNode?.content) {
228
+ continue;
229
+ }
230
+ try {
231
+ const contentBuf =
232
+ typeof plaintextNode.content === 'string'
233
+ ? Buffer.from(plaintextNode.content, 'binary')
234
+ : Buffer.from(plaintextNode.content);
235
+ const messageProto = proto.Message.decode(contentBuf).toJSON();
236
+ const fullMessage = proto.WebMessageInfo.fromObject({
237
+ key: {
238
+ remoteJid: newsletterJid,
239
+ id: child.attrs.id || child.attrs.server_id,
240
+ server_id: child.attrs.server_id,
241
+ fromMe: false
242
+ },
243
+ message: messageProto,
244
+ messageTimestamp: child.attrs.t ? +child.attrs.t : undefined
245
+ }).toJSON();
246
+ messages.push(fullMessage);
247
+ }
248
+ catch (error) {
249
+ sock.logger?.error?.({ error }, 'Failed to decode newsletter message');
250
+ }
251
+ }
252
+ return messages;
253
+ },
254
+ subscribeNewsletterUpdates: async (jid) => {
255
+ const result = await query({
256
+ tag: 'iq',
257
+ attrs: {
258
+ id: generateMessageTag(),
259
+ type: 'set',
260
+ xmlns: 'newsletter',
261
+ to: jid
262
+ },
263
+ content: [{ tag: 'live_updates', attrs: {}, content: [] }]
264
+ });
265
+ const liveUpdatesNode = getBinaryNodeChild(result, 'live_updates');
266
+ const duration = liveUpdatesNode?.attrs?.duration;
267
+ return duration ? { duration: duration } : null;
268
+ },
269
+ newsletterAdminCount: async (jid) => {
270
+ const response = await executeWMexQuery({ newsletter_id: jid }, QueryIds.ADMIN_COUNT, XWAPaths.xwa2_newsletter_admin_count);
271
+ return response.admin_count;
272
+ },
273
+ newsletterAdminCapabilities,
274
+ newsletterCanPostStatus: async jid => {
275
+ const capabilities = await newsletterAdminCapabilities(jid);
276
+ return {
277
+ canPost: capabilities.includes('CHANNEL_STATUS_PRODUCER'),
278
+ capabilities
279
+ };
280
+ },
281
+ newsletterChangeOwner: async (jid, newOwnerJid) => {
282
+ await executeWMexQuery({ newsletter_id: jid, user_id: newOwnerJid }, QueryIds.CHANGE_OWNER, XWAPaths.xwa2_newsletter_change_owner);
283
+ },
284
+ newsletterDemote: async (jid, userJid) => {
285
+ await executeWMexQuery({ newsletter_id: jid, user_id: userJid }, QueryIds.DEMOTE, XWAPaths.xwa2_newsletter_demote);
286
+ },
287
+ newsletterDelete: async (jid) => {
288
+ await executeWMexQuery({ newsletter_id: jid }, QueryIds.DELETE, XWAPaths.xwa2_newsletter_delete_v2);
289
+ }
290
+ };
291
+ };