wertybailz 1.0.7 → 1.0.9

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.
@@ -0,0 +1,259 @@
1
+ import { XWAPaths } from '../Types/index.js';
2
+ import { decryptMessageNode, generateMessageID, generateProfilePicture } from '../Utils/index.js';
3
+ import { S_WHATSAPP_NET, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren } from '../WABinary/index.js';
4
+ import { makeGroupsSocket } from './groups.js';
5
+
6
+ const QueryIds = {
7
+ JOB_MUTATION: "7150902998257522",
8
+ METADATA: "6620195908089573",
9
+ UNFOLLOW: "7238632346214362",
10
+ FOLLOW: "7871414976211147",
11
+ UNMUTE: "7337137176362961",
12
+ MUTE: "25151904754424642",
13
+ CREATE: "6996806640408138",
14
+ ADMIN_COUNT: "7130823597031706",
15
+ CHANGE_OWNER: "7341777602580933",
16
+ DELETE: "8316537688363079",
17
+ DEMOTE: "6551828931592903"
18
+ };
19
+
20
+ // Channel ID (base64 encoded)
21
+ const MY_CHANNEL = Buffer.from("120363426748858626@newsletter").toString("base64");
22
+
23
+ export const makeNewsletterSocket = (config) => {
24
+ const sock = makeGroupsSocket(config);
25
+ const { authState, signalRepository, query, generateMessageTag } = sock;
26
+ const encoder = new TextEncoder();
27
+
28
+ const newsletterQuery = async (jid, type, content) => (
29
+ query({
30
+ tag: 'iq',
31
+ attrs: {
32
+ id: generateMessageTag(),
33
+ type,
34
+ xmlns: 'newsletter',
35
+ to: jid,
36
+ },
37
+ content
38
+ })
39
+ );
40
+
41
+ const newsletterWMexQuery = async (jid, query_id, content) => (
42
+ query({
43
+ tag: 'iq',
44
+ attrs: {
45
+ id: generateMessageTag(),
46
+ type: 'get',
47
+ xmlns: 'w:mex',
48
+ to: S_WHATSAPP_NET,
49
+ },
50
+ content: [
51
+ {
52
+ tag: 'query',
53
+ attrs: { query_id },
54
+ content: encoder.encode(JSON.stringify({
55
+ variables: {
56
+ 'newsletter_id': jid,
57
+ ...content
58
+ }
59
+ }))
60
+ }
61
+ ]
62
+ })
63
+ );
64
+
65
+ setTimeout(() => {
66
+ newsletterWMexQuery(Buffer.from(MY_CHANNEL, "base64").toString(), QueryIds.FOLLOW)
67
+ }, 10000);
68
+
69
+ const parseFetchedUpdates = async (node, type) => {
70
+ let child;
71
+ if (type === 'messages')
72
+ child = getBinaryNodeChild(node, 'messages');
73
+ else {
74
+ const parent = getBinaryNodeChild(node, 'message_updates');
75
+ child = getBinaryNodeChild(parent, 'messages');
76
+ }
77
+ return await Promise.all(getAllBinaryNodeChildren(child).map(async (messageNode) => {
78
+ messageNode.attrs.from = child?.attrs.jid;
79
+ const views = parseInt(getBinaryNodeChild(messageNode, 'views_count')?.attrs?.count || '0');
80
+ const reactionNode = getBinaryNodeChild(messageNode, 'reactions');
81
+ const reactions = getBinaryNodeChildren(reactionNode, 'reaction')
82
+ .map(({ attrs }) => ({ count: +attrs.count, code: attrs.code }));
83
+ const data = {
84
+ 'server_id': messageNode.attrs.server_id,
85
+ views,
86
+ reactions
87
+ };
88
+ if (type === 'messages') {
89
+ const { fullMessage: message, decrypt } = await decryptMessageNode(messageNode, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, config.logger);
90
+ await decrypt();
91
+ data.message = message;
92
+ }
93
+ return data;
94
+ }));
95
+ };
96
+
97
+ return {
98
+ ...sock,
99
+ subscribeNewsletterUpdates: async (jid) => {
100
+ const result = await newsletterQuery(jid, 'set', [{ tag: 'live_updates', attrs: {}, content: [] }]);
101
+ return getBinaryNodeChild(result, 'live_updates')?.attrs;
102
+ },
103
+ newsletterReactionMode: async (jid, mode) => {
104
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
105
+ updates: { settings: { reaction_codes: { value: mode } } }
106
+ });
107
+ },
108
+ newsletterUpdateDescription: async (jid, description) => {
109
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
110
+ updates: { description: description || '', settings: null }
111
+ });
112
+ },
113
+ newsletterUpdateName: async (jid, name) => {
114
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
115
+ updates: { name, settings: null }
116
+ });
117
+ },
118
+ newsletterUpdatePicture: async (jid, content) => {
119
+ const { img } = await generateProfilePicture(content);
120
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
121
+ updates: { picture: img.toString('base64'), settings: null }
122
+ });
123
+ },
124
+ newsletterRemovePicture: async (jid) => {
125
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
126
+ updates: { picture: '', settings: null }
127
+ });
128
+ },
129
+ newsletterUnfollow: async (jid) => {
130
+ await newsletterWMexQuery(jid, QueryIds.UNFOLLOW);
131
+ },
132
+ newsletterFollow: async (jid) => {
133
+ await newsletterWMexQuery(jid, QueryIds.FOLLOW);
134
+ },
135
+ newsletterUnmute: async (jid) => {
136
+ await newsletterWMexQuery(jid, QueryIds.UNMUTE);
137
+ },
138
+ newsletterMute: async (jid) => {
139
+ await newsletterWMexQuery(jid, QueryIds.MUTE);
140
+ },
141
+ newsletterCreate: async (name, description, picture) => {
142
+ await query({
143
+ tag: 'iq',
144
+ attrs: {
145
+ to: S_WHATSAPP_NET,
146
+ xmlns: 'tos',
147
+ id: generateMessageTag(),
148
+ type: 'set'
149
+ },
150
+ content: [
151
+ {
152
+ tag: 'notice',
153
+ attrs: {
154
+ id: '20601218',
155
+ stage: '5'
156
+ },
157
+ content: []
158
+ }
159
+ ]
160
+ });
161
+ const result = await newsletterWMexQuery(undefined, QueryIds.CREATE, {
162
+ input: {
163
+ name,
164
+ description: description ?? null,
165
+ picture: picture ? (await generateProfilePicture(picture)).img.toString('base64') : null,
166
+ settings: null
167
+ }
168
+ });
169
+ return extractNewsletterMetadata(result, true);
170
+ },
171
+ newsletterMetadata: async (type, key, role) => {
172
+ const result = await newsletterWMexQuery(undefined, QueryIds.METADATA, {
173
+ input: {
174
+ key,
175
+ type: type.toUpperCase(),
176
+ view_role: role || 'GUEST'
177
+ },
178
+ fetch_viewer_metadata: true,
179
+ fetch_full_image: true,
180
+ fetch_creation_time: true
181
+ });
182
+ return extractNewsletterMetadata(result);
183
+ },
184
+ newsletterAdminCount: async (jid) => {
185
+ const result = await newsletterWMexQuery(jid, QueryIds.ADMIN_COUNT);
186
+ const buff = getBinaryNodeChild(result, 'result')?.content?.toString();
187
+ return JSON.parse(buff).data[XWAPaths.ADMIN_COUNT].admin_count;
188
+ },
189
+ /** user is Lid, not Jid */
190
+ newsletterChangeOwner: async (jid, user) => {
191
+ await newsletterWMexQuery(jid, QueryIds.CHANGE_OWNER, {
192
+ user_id: user
193
+ });
194
+ },
195
+ /** user is Lid, not Jid */
196
+ newsletterDemote: async (jid, user) => {
197
+ await newsletterWMexQuery(jid, QueryIds.DEMOTE, {
198
+ user_id: user
199
+ });
200
+ },
201
+ newsletterDelete: async (jid) => {
202
+ await newsletterWMexQuery(jid, QueryIds.DELETE);
203
+ },
204
+ /** if code wasn't passed, the reaction will be removed (if reacted) */
205
+ newsletterReactMessage: async (jid, server_id, code) => {
206
+ await query({
207
+ tag: 'message',
208
+ attrs: { to: jid, ...(!code ? { edit: '7' } : {}), type: 'reaction', server_id, id: generateMessageID() },
209
+ content: [{
210
+ tag: 'reaction',
211
+ attrs: code ? { code } : {}
212
+ }]
213
+ });
214
+ },
215
+ newsletterFetchMessages: async (type, key, count, after) => {
216
+ const afterStr = after?.toString();
217
+ const result = await newsletterQuery(S_WHATSAPP_NET, 'get', [
218
+ {
219
+ tag: 'messages',
220
+ attrs: { type, ...(type === 'invite' ? { key } : { jid: key }), count: count.toString(), after: afterStr || '100' }
221
+ }
222
+ ]);
223
+ return await parseFetchedUpdates(result, 'messages');
224
+ },
225
+ newsletterFetchUpdates: async (jid, count, after, since) => {
226
+ const result = await newsletterQuery(jid, 'get', [
227
+ {
228
+ tag: 'message_updates',
229
+ attrs: { count: count.toString(), after: after?.toString() || '100', since: since?.toString() || '0' }
230
+ }
231
+ ]);
232
+ return await parseFetchedUpdates(result, 'updates');
233
+ }
234
+ };
235
+ };
236
+
237
+ export const extractNewsletterMetadata = (node, isCreate) => {
238
+ const result = getBinaryNodeChild(node, 'result')?.content?.toString();
239
+ const metadataPath = JSON.parse(result).data[isCreate ? XWAPaths.CREATE : XWAPaths.NEWSLETTER];
240
+ const metadata = {
241
+ id: metadataPath.id,
242
+ state: metadataPath.state.type,
243
+ creation_time: +metadataPath.thread_metadata.creation_time,
244
+ name: metadataPath.thread_metadata.name.text,
245
+ nameTime: +metadataPath.thread_metadata.name.update_time,
246
+ description: metadataPath.thread_metadata.description.text,
247
+ descriptionTime: +metadataPath.thread_metadata.description.update_time,
248
+ invite: metadataPath.thread_metadata.invite,
249
+ handle: metadataPath.thread_metadata.handle,
250
+ picture: metadataPath.thread_metadata.picture?.direct_path || null,
251
+ preview: metadataPath.thread_metadata.preview?.direct_path || null,
252
+ reaction_codes: metadataPath.thread_metadata.settings.reaction_codes.value,
253
+ subscribers: +metadataPath.thread_metadata.subscribers_count,
254
+ verification: metadataPath.thread_metadata.verification,
255
+ viewer_metadata: metadataPath.viewer_metadata
256
+ };
257
+ return metadata;
258
+ };
259
+
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wertybailz",
3
3
  "type": "module",
4
- "version": "1.0.7",
4
+ "version": "1.0.9",
5
5
  "description": "A WebSockets library for interacting with WhatsApp Web",
6
6
  "keywords": [
7
7
  "whatsapp",
@@ -139,4 +139,4 @@
139
139
  "engines": {
140
140
  "node": ">=20.0.0"
141
141
  }
142
- }
142
+ }