@nexustechpro/baileys 2.0.2 → 2.0.6

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 (108) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +924 -1299
  3. package/WAProto/index.js +22 -18
  4. package/lib/Defaults/baileys-version.json +6 -2
  5. package/lib/Defaults/index.js +173 -172
  6. package/lib/Signal/libsignal.js +395 -292
  7. package/lib/Signal/lid-mapping.js +264 -171
  8. package/lib/Socket/Client/index.js +2 -2
  9. package/lib/Socket/Client/types.js +10 -10
  10. package/lib/Socket/Client/websocket.js +45 -310
  11. package/lib/Socket/business.js +375 -375
  12. package/lib/Socket/chats.js +916 -963
  13. package/lib/Socket/communities.js +430 -430
  14. package/lib/Socket/groups.js +342 -342
  15. package/lib/Socket/index.js +21 -22
  16. package/lib/Socket/messages-recv.js +963 -743
  17. package/lib/Socket/messages-send.js +273 -321
  18. package/lib/Socket/mex.js +50 -50
  19. package/lib/Socket/newsletter.js +148 -148
  20. package/lib/Socket/nexus-handler.js +296 -247
  21. package/lib/Socket/registration.js +50 -33
  22. package/lib/Socket/socket.js +872 -1201
  23. package/lib/Store/index.js +5 -5
  24. package/lib/Store/make-cache-manager-store.js +81 -81
  25. package/lib/Store/make-in-memory-store.js +416 -416
  26. package/lib/Store/make-ordered-dictionary.js +81 -81
  27. package/lib/Store/object-repository.js +30 -30
  28. package/lib/Types/Auth.js +1 -1
  29. package/lib/Types/Bussines.js +1 -1
  30. package/lib/Types/Call.js +1 -1
  31. package/lib/Types/Chat.js +7 -7
  32. package/lib/Types/Contact.js +1 -1
  33. package/lib/Types/Events.js +1 -1
  34. package/lib/Types/GroupMetadata.js +1 -1
  35. package/lib/Types/Label.js +24 -24
  36. package/lib/Types/LabelAssociation.js +6 -6
  37. package/lib/Types/Message.js +10 -10
  38. package/lib/Types/Newsletter.js +37 -29
  39. package/lib/Types/Product.js +1 -1
  40. package/lib/Types/Signal.js +1 -1
  41. package/lib/Types/Socket.js +2 -2
  42. package/lib/Types/State.js +55 -12
  43. package/lib/Types/USync.js +1 -1
  44. package/lib/Types/index.js +25 -25
  45. package/lib/Utils/auth-utils.js +264 -256
  46. package/lib/Utils/baileys-event-stream.js +55 -55
  47. package/lib/Utils/browser-utils.js +27 -27
  48. package/lib/Utils/business.js +228 -230
  49. package/lib/Utils/chat-utils.js +726 -764
  50. package/lib/Utils/companion-reg-client-utils.js +34 -0
  51. package/lib/Utils/crypto.js +109 -135
  52. package/lib/Utils/decode-wa-message.js +342 -314
  53. package/lib/Utils/event-buffer.js +547 -547
  54. package/lib/Utils/generics.js +295 -297
  55. package/lib/Utils/history.js +91 -83
  56. package/lib/Utils/index.js +25 -20
  57. package/lib/Utils/key-store.js +17 -0
  58. package/lib/Utils/link-preview.js +107 -98
  59. package/lib/Utils/logger.js +2 -2
  60. package/lib/Utils/lt-hash.js +47 -47
  61. package/lib/Utils/make-mutex.js +39 -39
  62. package/lib/Utils/message-retry-manager.js +148 -148
  63. package/lib/Utils/messages-media.js +579 -535
  64. package/lib/Utils/messages.js +821 -706
  65. package/lib/Utils/noise-handler.js +255 -255
  66. package/lib/Utils/pre-key-manager.js +105 -105
  67. package/lib/Utils/process-message.js +430 -412
  68. package/lib/Utils/reporting-utils.js +155 -0
  69. package/lib/Utils/signal.js +191 -159
  70. package/lib/Utils/sync-action-utils.js +33 -0
  71. package/lib/Utils/tc-token-utils.js +162 -0
  72. package/lib/Utils/use-multi-file-auth-state.js +120 -120
  73. package/lib/Utils/validate-connection.js +194 -194
  74. package/lib/WABinary/constants.js +1306 -1300
  75. package/lib/WABinary/decode.js +237 -237
  76. package/lib/WABinary/encode.js +232 -232
  77. package/lib/WABinary/generic-utils.js +252 -211
  78. package/lib/WABinary/index.js +6 -5
  79. package/lib/WABinary/jid-utils.js +279 -95
  80. package/lib/WABinary/types.js +1 -1
  81. package/lib/WAM/BinaryInfo.js +9 -9
  82. package/lib/WAM/constants.js +22852 -22852
  83. package/lib/WAM/encode.js +149 -149
  84. package/lib/WAM/index.js +3 -3
  85. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
  86. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
  87. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
  88. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
  89. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  90. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
  91. package/lib/WAUSync/Protocols/index.js +4 -4
  92. package/lib/WAUSync/USyncQuery.js +93 -93
  93. package/lib/WAUSync/USyncUser.js +22 -22
  94. package/lib/WAUSync/index.js +3 -3
  95. package/lib/index.js +65 -66
  96. package/package.json +172 -143
  97. package/lib/Signal/Group/ciphertext-message.js +0 -12
  98. package/lib/Signal/Group/group-session-builder.js +0 -30
  99. package/lib/Signal/Group/group_cipher.js +0 -100
  100. package/lib/Signal/Group/index.js +0 -12
  101. package/lib/Signal/Group/keyhelper.js +0 -18
  102. package/lib/Signal/Group/sender-chain-key.js +0 -26
  103. package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
  104. package/lib/Signal/Group/sender-key-message.js +0 -66
  105. package/lib/Signal/Group/sender-key-name.js +0 -48
  106. package/lib/Signal/Group/sender-key-record.js +0 -41
  107. package/lib/Signal/Group/sender-key-state.js +0 -84
  108. package/lib/Signal/Group/sender-message-key.js +0 -26
@@ -1,343 +1,343 @@
1
- import { proto } from '../../WAProto/index.js';
2
- import { WAMessageAddressingMode, WAMessageStubType } from '../Types/index.js';
3
- import { generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
4
- import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isLidUser, isPnUser, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
5
- import { makeChatsSocket } from './chats.js';
6
- export const makeGroupsSocket = (config) => {
7
- const sock = makeChatsSocket(config);
8
- const { authState, ev, query, upsertMessage } = sock;
9
- const groupQuery = async (jid, type, content) => query({
10
- tag: 'iq',
11
- attrs: {
12
- type,
13
- xmlns: 'w:g2',
14
- to: jid
15
- },
16
- content
17
- });
18
- const groupMetadata = async (jid) => {
19
- for (let i = 0; i < 20; i++) {
20
- try {
21
- const result = await groupQuery(jid, 'get', [
22
- { tag: 'query', attrs: { request: 'interactive' } }
23
- ]);
24
- return extractGroupMetadata(result);
25
- } catch (error) {
26
- if (error?.data === 429 || error?.isRateLimit) {
27
- await new Promise(r => setTimeout(r, 300 + Math.random() * 700));
28
- continue;
29
- }
30
- throw error;
31
- }
32
- }
33
- };
34
- const groupFetchAllParticipating = async () => {
35
- const result = await query({
36
- tag: 'iq',
37
- attrs: {
38
- to: '@g.us',
39
- xmlns: 'w:g2',
40
- type: 'get'
41
- },
42
- content: [
43
- {
44
- tag: 'participating',
45
- attrs: {},
46
- content: [
47
- { tag: 'participants', attrs: {} },
48
- { tag: 'description', attrs: {} }
49
- ]
50
- }
51
- ]
52
- });
53
- const data = {};
54
- const groupsChild = getBinaryNodeChild(result, 'groups');
55
- if (groupsChild) {
56
- const groups = getBinaryNodeChildren(groupsChild, 'group');
57
- for (const groupNode of groups) {
58
- const meta = extractGroupMetadata({
59
- tag: 'result',
60
- attrs: {},
61
- content: [groupNode]
62
- });
63
- data[meta.id] = meta;
64
- }
65
- }
66
- // TODO: properly parse LID / PN DATA
67
- sock.ev.emit('groups.update', Object.values(data));
68
- return data;
69
- };
70
- sock.ws.on('CB:ib,,dirty', async (node) => {
71
- const { attrs } = getBinaryNodeChild(node, 'dirty');
72
- if (attrs.type !== 'groups') {
73
- return;
74
- }
75
- await groupFetchAllParticipating();
76
- await sock.cleanDirtyBits('groups');
77
- });
78
- return {
79
- ...sock,
80
- groupMetadata,
81
- groupCreate: async (subject, participants) => {
82
- const key = generateMessageIDV2();
83
- const result = await groupQuery('@g.us', 'set', [
84
- {
85
- tag: 'create',
86
- attrs: {
87
- subject,
88
- key
89
- },
90
- content: participants.map(jid => ({
91
- tag: 'participant',
92
- attrs: { jid }
93
- }))
94
- }
95
- ]);
96
- return extractGroupMetadata(result);
97
- },
98
- groupLeave: async (id) => {
99
- await groupQuery('@g.us', 'set', [
100
- {
101
- tag: 'leave',
102
- attrs: {},
103
- content: [{ tag: 'group', attrs: { id } }]
104
- }
105
- ]);
106
- },
107
- groupUpdateSubject: async (jid, subject) => {
108
- await groupQuery(jid, 'set', [
109
- {
110
- tag: 'subject',
111
- attrs: {},
112
- content: Buffer.from(subject, 'utf-8')
113
- }
114
- ]);
115
- },
116
- groupRequestParticipantsList: async (jid) => {
117
- const result = await groupQuery(jid, 'get', [
118
- {
119
- tag: 'membership_approval_requests',
120
- attrs: {}
121
- }
122
- ]);
123
- const node = getBinaryNodeChild(result, 'membership_approval_requests');
124
- const participants = getBinaryNodeChildren(node, 'membership_approval_request');
125
- return participants.map(v => v.attrs);
126
- },
127
- groupRequestParticipantsUpdate: async (jid, participants, action) => {
128
- const result = await groupQuery(jid, 'set', [
129
- {
130
- tag: 'membership_requests_action',
131
- attrs: {},
132
- content: [
133
- {
134
- tag: action,
135
- attrs: {},
136
- content: participants.map(jid => ({
137
- tag: 'participant',
138
- attrs: { jid }
139
- }))
140
- }
141
- ]
142
- }
143
- ]);
144
- const node = getBinaryNodeChild(result, 'membership_requests_action');
145
- const nodeAction = getBinaryNodeChild(node, action);
146
- const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
147
- return participantsAffected.map(p => {
148
- return { status: p.attrs.error || '200', jid: p.attrs.jid };
149
- });
150
- },
151
- groupParticipantsUpdate: async (jid, participants, action) => {
152
- const result = await groupQuery(jid, 'set', [
153
- {
154
- tag: action,
155
- attrs: {},
156
- content: participants.map(jid => ({
157
- tag: 'participant',
158
- attrs: { jid }
159
- }))
160
- }
161
- ]);
162
- const node = getBinaryNodeChild(result, action);
163
- const participantsAffected = getBinaryNodeChildren(node, 'participant');
164
- return participantsAffected.map(p => {
165
- return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
166
- });
167
- },
168
- groupUpdateDescription: async (jid, description) => {
169
- const metadata = await groupMetadata(jid);
170
- const prev = metadata.descId ?? null;
171
- await groupQuery(jid, 'set', [
172
- {
173
- tag: 'description',
174
- attrs: {
175
- ...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
176
- ...(prev ? { prev } : {})
177
- },
178
- content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
179
- }
180
- ]);
181
- },
182
- groupInviteCode: async (jid) => {
183
- const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
184
- const inviteNode = getBinaryNodeChild(result, 'invite');
185
- return inviteNode?.attrs.code;
186
- },
187
- groupRevokeInvite: async (jid) => {
188
- const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
189
- const inviteNode = getBinaryNodeChild(result, 'invite');
190
- return inviteNode?.attrs.code;
191
- },
192
- groupAcceptInvite: async (code) => {
193
- const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
194
- const result = getBinaryNodeChild(results, 'group');
195
- return result?.attrs.jid;
196
- },
197
- /**
198
- * revoke a v4 invite for someone
199
- * @param groupJid group jid
200
- * @param invitedJid jid of person you invited
201
- * @returns true if successful
202
- */
203
- groupRevokeInviteV4: async (groupJid, invitedJid) => {
204
- const result = await groupQuery(groupJid, 'set', [
205
- { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
206
- ]);
207
- return !!result;
208
- },
209
- /**
210
- * accept a GroupInviteMessage
211
- * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
212
- * @param inviteMessage the message to accept
213
- */
214
- groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
215
- key = typeof key === 'string' ? { remoteJid: key } : key;
216
- const results = await groupQuery(inviteMessage.groupJid, 'set', [
217
- {
218
- tag: 'accept',
219
- attrs: {
220
- code: inviteMessage.inviteCode,
221
- expiration: inviteMessage.inviteExpiration.toString(),
222
- admin: key.remoteJid
223
- }
224
- }
225
- ]);
226
- // if we have the full message key
227
- // update the invite message to be expired
228
- if (key.id) {
229
- // create new invite message that is expired
230
- inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
231
- inviteMessage.inviteExpiration = 0;
232
- inviteMessage.inviteCode = '';
233
- ev.emit('messages.update', [
234
- {
235
- key,
236
- update: {
237
- message: {
238
- groupInviteMessage: inviteMessage
239
- }
240
- }
241
- }
242
- ]);
243
- }
244
- // generate the group add message
245
- await upsertMessage({
246
- key: {
247
- remoteJid: inviteMessage.groupJid,
248
- id: generateMessageIDV2(sock.user?.id),
249
- fromMe: false,
250
- participant: key.remoteJid
251
- },
252
- messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
253
- messageStubParameters: [JSON.stringify(authState.creds.me)],
254
- participant: key.remoteJid,
255
- messageTimestamp: unixTimestampSeconds()
256
- }, 'notify');
257
- return results.attrs.from;
258
- }),
259
- groupGetInviteInfo: async (code) => {
260
- const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
261
- return extractGroupMetadata(results);
262
- },
263
- groupToggleEphemeral: async (jid, ephemeralExpiration) => {
264
- const content = ephemeralExpiration
265
- ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
266
- : { tag: 'not_ephemeral', attrs: {} };
267
- await groupQuery(jid, 'set', [content]);
268
- },
269
- groupSettingUpdate: async (jid, setting) => {
270
- await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
271
- },
272
- groupMemberAddMode: async (jid, mode) => {
273
- await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
274
- },
275
- groupJoinApprovalMode: async (jid, mode) => {
276
- await groupQuery(jid, 'set', [
277
- { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
278
- ]);
279
- },
280
- groupFetchAllParticipating
281
- };
282
- };
283
- export const extractGroupMetadata = (result) => {
284
- const group = getBinaryNodeChild(result, 'group');
285
- if (!group) {
286
- throw new Error('Group node not found in result');
287
- }
288
- const descChild = getBinaryNodeChild(group, 'description');
289
- let desc;
290
- let descId;
291
- let descOwner;
292
- let descOwnerPn;
293
- let descTime;
294
- if (descChild) {
295
- desc = getBinaryNodeChildString(descChild, 'body');
296
- descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
297
- descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
298
- descTime = +descChild.attrs.t;
299
- descId = descChild.attrs.id;
300
- }
301
- const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
302
- const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
303
- const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
304
- const metadata = {
305
- id: groupId,
306
- notify: group.attrs.notify,
307
- addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
308
- subject: group.attrs.subject,
309
- subjectOwner: group.attrs.s_o,
310
- subjectOwnerPn: group.attrs.s_o_pn,
311
- subjectTime: +group.attrs.s_t,
312
- size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
313
- creation: +group.attrs.creation,
314
- owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
315
- ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
316
- owner_country_code: group.attrs.creator_country_code,
317
- desc,
318
- descId,
319
- descOwner,
320
- descOwnerPn,
321
- descTime,
322
- linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
323
- restrict: !!getBinaryNodeChild(group, 'locked'),
324
- announce: !!getBinaryNodeChild(group, 'announcement'),
325
- isCommunity: !!getBinaryNodeChild(group, 'parent'),
326
- isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
327
- joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
328
- memberAddMode,
329
- participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
330
- // Store both ID and phone number for compatibility
331
- return {
332
- id: attrs.jid,
333
- jid: attrs.phone_number || attrs.jid, // jid field for backwards compatibility
334
- phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
335
- lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
336
- admin: (attrs.type || null)
337
- };
338
- }),
339
- ephemeralDuration: eph ? +eph : undefined
340
- };
341
- return metadata;
342
- };
1
+ import { proto } from '../../WAProto/index.js';
2
+ import { WAMessageAddressingMode, WAMessageStubType } from '../Types/index.js';
3
+ import { generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
4
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isLidUser, isPnUser, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
5
+ import { makeChatsSocket } from './chats.js';
6
+ export const makeGroupsSocket = (config) => {
7
+ const sock = makeChatsSocket(config);
8
+ const { authState, ev, query, upsertMessage } = sock;
9
+ const groupQuery = async (jid, type, content) => query({
10
+ tag: 'iq',
11
+ attrs: {
12
+ type,
13
+ xmlns: 'w:g2',
14
+ to: jid
15
+ },
16
+ content
17
+ });
18
+ const groupMetadata = async (jid) => {
19
+ for (let i = 0; i < 20; i++) {
20
+ try {
21
+ const result = await groupQuery(jid, 'get', [
22
+ { tag: 'query', attrs: { request: 'interactive' } }
23
+ ]);
24
+ return extractGroupMetadata(result);
25
+ } catch (error) {
26
+ if (error?.data === 429 || error?.isRateLimit) {
27
+ await new Promise(r => setTimeout(r, 300 + Math.random() * 700));
28
+ continue;
29
+ }
30
+ throw error;
31
+ }
32
+ }
33
+ };
34
+ const groupFetchAllParticipating = async () => {
35
+ const result = await query({
36
+ tag: 'iq',
37
+ attrs: {
38
+ to: '@g.us',
39
+ xmlns: 'w:g2',
40
+ type: 'get'
41
+ },
42
+ content: [
43
+ {
44
+ tag: 'participating',
45
+ attrs: {},
46
+ content: [
47
+ { tag: 'participants', attrs: {} },
48
+ { tag: 'description', attrs: {} }
49
+ ]
50
+ }
51
+ ]
52
+ });
53
+ const data = {};
54
+ const groupsChild = getBinaryNodeChild(result, 'groups');
55
+ if (groupsChild) {
56
+ const groups = getBinaryNodeChildren(groupsChild, 'group');
57
+ for (const groupNode of groups) {
58
+ const meta = extractGroupMetadata({
59
+ tag: 'result',
60
+ attrs: {},
61
+ content: [groupNode]
62
+ });
63
+ data[meta.id] = meta;
64
+ }
65
+ }
66
+ // TODO: properly parse LID / PN DATA
67
+ sock.ev.emit('groups.update', Object.values(data));
68
+ return data;
69
+ };
70
+ sock.ws.on('CB:ib,,dirty', async (node) => {
71
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
72
+ if (attrs.type !== 'groups') {
73
+ return;
74
+ }
75
+ await groupFetchAllParticipating();
76
+ await sock.cleanDirtyBits('groups');
77
+ });
78
+ return {
79
+ ...sock,
80
+ groupMetadata,
81
+ groupCreate: async (subject, participants) => {
82
+ const key = generateMessageIDV2();
83
+ const result = await groupQuery('@g.us', 'set', [
84
+ {
85
+ tag: 'create',
86
+ attrs: {
87
+ subject,
88
+ key
89
+ },
90
+ content: participants.map(jid => ({
91
+ tag: 'participant',
92
+ attrs: { jid }
93
+ }))
94
+ }
95
+ ]);
96
+ return extractGroupMetadata(result);
97
+ },
98
+ groupLeave: async (id) => {
99
+ await groupQuery('@g.us', 'set', [
100
+ {
101
+ tag: 'leave',
102
+ attrs: {},
103
+ content: [{ tag: 'group', attrs: { id } }]
104
+ }
105
+ ]);
106
+ },
107
+ groupUpdateSubject: async (jid, subject) => {
108
+ await groupQuery(jid, 'set', [
109
+ {
110
+ tag: 'subject',
111
+ attrs: {},
112
+ content: Buffer.from(subject, 'utf-8')
113
+ }
114
+ ]);
115
+ },
116
+ groupRequestParticipantsList: async (jid) => {
117
+ const result = await groupQuery(jid, 'get', [
118
+ {
119
+ tag: 'membership_approval_requests',
120
+ attrs: {}
121
+ }
122
+ ]);
123
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
124
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
125
+ return participants.map(v => v.attrs);
126
+ },
127
+ groupRequestParticipantsUpdate: async (jid, participants, action) => {
128
+ const result = await groupQuery(jid, 'set', [
129
+ {
130
+ tag: 'membership_requests_action',
131
+ attrs: {},
132
+ content: [
133
+ {
134
+ tag: action,
135
+ attrs: {},
136
+ content: participants.map(jid => ({
137
+ tag: 'participant',
138
+ attrs: { jid }
139
+ }))
140
+ }
141
+ ]
142
+ }
143
+ ]);
144
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
145
+ const nodeAction = getBinaryNodeChild(node, action);
146
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
147
+ return participantsAffected.map(p => {
148
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
149
+ });
150
+ },
151
+ groupParticipantsUpdate: async (jid, participants, action) => {
152
+ const result = await groupQuery(jid, 'set', [
153
+ {
154
+ tag: action,
155
+ attrs: {},
156
+ content: participants.map(jid => ({
157
+ tag: 'participant',
158
+ attrs: { jid }
159
+ }))
160
+ }
161
+ ]);
162
+ const node = getBinaryNodeChild(result, action);
163
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
164
+ return participantsAffected.map(p => {
165
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
166
+ });
167
+ },
168
+ groupUpdateDescription: async (jid, description) => {
169
+ const metadata = await groupMetadata(jid);
170
+ const prev = metadata.descId ?? null;
171
+ await groupQuery(jid, 'set', [
172
+ {
173
+ tag: 'description',
174
+ attrs: {
175
+ ...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
176
+ ...(prev ? { prev } : {})
177
+ },
178
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
179
+ }
180
+ ]);
181
+ },
182
+ groupInviteCode: async (jid) => {
183
+ const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
184
+ const inviteNode = getBinaryNodeChild(result, 'invite');
185
+ return inviteNode?.attrs.code;
186
+ },
187
+ groupRevokeInvite: async (jid) => {
188
+ const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
189
+ const inviteNode = getBinaryNodeChild(result, 'invite');
190
+ return inviteNode?.attrs.code;
191
+ },
192
+ groupAcceptInvite: async (code) => {
193
+ const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
194
+ const result = getBinaryNodeChild(results, 'group');
195
+ return result?.attrs.jid;
196
+ },
197
+ /**
198
+ * revoke a v4 invite for someone
199
+ * @param groupJid group jid
200
+ * @param invitedJid jid of person you invited
201
+ * @returns true if successful
202
+ */
203
+ groupRevokeInviteV4: async (groupJid, invitedJid) => {
204
+ const result = await groupQuery(groupJid, 'set', [
205
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
206
+ ]);
207
+ return !!result;
208
+ },
209
+ /**
210
+ * accept a GroupInviteMessage
211
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
212
+ * @param inviteMessage the message to accept
213
+ */
214
+ groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
215
+ key = typeof key === 'string' ? { remoteJid: key } : key;
216
+ const results = await groupQuery(inviteMessage.groupJid, 'set', [
217
+ {
218
+ tag: 'accept',
219
+ attrs: {
220
+ code: inviteMessage.inviteCode,
221
+ expiration: inviteMessage.inviteExpiration.toString(),
222
+ admin: key.remoteJid
223
+ }
224
+ }
225
+ ]);
226
+ // if we have the full message key
227
+ // update the invite message to be expired
228
+ if (key.id) {
229
+ // create new invite message that is expired
230
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
231
+ inviteMessage.inviteExpiration = 0;
232
+ inviteMessage.inviteCode = '';
233
+ ev.emit('messages.update', [
234
+ {
235
+ key,
236
+ update: {
237
+ message: {
238
+ groupInviteMessage: inviteMessage
239
+ }
240
+ }
241
+ }
242
+ ]);
243
+ }
244
+ // generate the group add message
245
+ await upsertMessage({
246
+ key: {
247
+ remoteJid: inviteMessage.groupJid,
248
+ id: generateMessageIDV2(sock.user?.id),
249
+ fromMe: false,
250
+ participant: key.remoteJid
251
+ },
252
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
253
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
254
+ participant: key.remoteJid,
255
+ messageTimestamp: unixTimestampSeconds()
256
+ }, 'notify');
257
+ return results.attrs.from;
258
+ }),
259
+ groupGetInviteInfo: async (code) => {
260
+ const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
261
+ return extractGroupMetadata(results);
262
+ },
263
+ groupToggleEphemeral: async (jid, ephemeralExpiration) => {
264
+ const content = ephemeralExpiration
265
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
266
+ : { tag: 'not_ephemeral', attrs: {} };
267
+ await groupQuery(jid, 'set', [content]);
268
+ },
269
+ groupSettingUpdate: async (jid, setting) => {
270
+ await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
271
+ },
272
+ groupMemberAddMode: async (jid, mode) => {
273
+ await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
274
+ },
275
+ groupJoinApprovalMode: async (jid, mode) => {
276
+ await groupQuery(jid, 'set', [
277
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
278
+ ]);
279
+ },
280
+ groupFetchAllParticipating
281
+ };
282
+ };
283
+ export const extractGroupMetadata = (result) => {
284
+ const group = getBinaryNodeChild(result, 'group');
285
+ if (!group) {
286
+ throw new Error('Group node not found in result');
287
+ }
288
+ const descChild = getBinaryNodeChild(group, 'description');
289
+ let desc;
290
+ let descId;
291
+ let descOwner;
292
+ let descOwnerPn;
293
+ let descTime;
294
+ if (descChild) {
295
+ desc = getBinaryNodeChildString(descChild, 'body');
296
+ descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
297
+ descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
298
+ descTime = +descChild.attrs.t;
299
+ descId = descChild.attrs.id;
300
+ }
301
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
302
+ const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
303
+ const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
304
+ const metadata = {
305
+ id: groupId,
306
+ notify: group.attrs.notify,
307
+ addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
308
+ subject: group.attrs.subject,
309
+ subjectOwner: group.attrs.s_o,
310
+ subjectOwnerPn: group.attrs.s_o_pn,
311
+ subjectTime: +group.attrs.s_t,
312
+ size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
313
+ creation: +group.attrs.creation,
314
+ owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
315
+ ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
316
+ owner_country_code: group.attrs.creator_country_code,
317
+ desc,
318
+ descId,
319
+ descOwner,
320
+ descOwnerPn,
321
+ descTime,
322
+ linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
323
+ restrict: !!getBinaryNodeChild(group, 'locked'),
324
+ announce: !!getBinaryNodeChild(group, 'announcement'),
325
+ isCommunity: !!getBinaryNodeChild(group, 'parent'),
326
+ isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
327
+ joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
328
+ memberAddMode,
329
+ participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
330
+ // Store both ID and phone number for compatibility
331
+ return {
332
+ id: attrs.jid,
333
+ jid: attrs.phone_number || attrs.jid, // jid field for backwards compatibility
334
+ phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
335
+ lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
336
+ admin: (attrs.type || null)
337
+ };
338
+ }),
339
+ ephemeralDuration: eph ? +eph : undefined
340
+ };
341
+ return metadata;
342
+ };
343
343
  //# sourceMappingURL=groups.js.map