dibosockets 1.0.2 → 1.0.4

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 (40) hide show
  1. package/WAProto/WAProto.proto +5619 -5619
  2. package/lib/Api/Internet/growagarden.js +28 -28
  3. package/lib/Api/creator.json +13 -13
  4. package/lib/Api/downloader/tiktokdownload.js +132 -132
  5. package/lib/Api/index.js +38 -38
  6. package/lib/Api/nsfw/loli.js +79 -79
  7. package/lib/Api/stalker/githubstalk.js +46 -46
  8. package/lib/Api/stalker/stalkroblox.js +94 -94
  9. package/lib/Api/stalker/tiktokstalk.js +68 -68
  10. package/lib/Api/tools/ssweb.js +38 -38
  11. package/lib/Defaults/baileys-version.json +3 -3
  12. package/lib/Defaults/index.js +122 -122
  13. package/lib/Defaults/phonenumber_mcc.json +223 -223
  14. package/lib/Socket/RHandler.js +448 -448
  15. package/lib/Socket/business.js +266 -266
  16. package/lib/Socket/chats.js +1137 -1137
  17. package/lib/Socket/communities.js +441 -441
  18. package/lib/Socket/index.js +43 -43
  19. package/lib/Socket/messages-recv.js +1065 -1065
  20. package/lib/Socket/messages-send.js +881 -881
  21. package/lib/Socket/newsletter.js +286 -286
  22. package/lib/Socket/socket.js +666 -666
  23. package/lib/Store/make-in-memory-store.js +439 -439
  24. package/lib/Types/Newsletter.js +18 -18
  25. package/lib/Utils/async-iterable.js +41 -41
  26. package/lib/Utils/baileys-event-stream.js +63 -63
  27. package/lib/Utils/browser-utils.js +34 -34
  28. package/lib/Utils/event-buffer.js +518 -518
  29. package/lib/Utils/generics.js +431 -431
  30. package/lib/Utils/index.js +36 -36
  31. package/lib/Utils/message-retry-manager.js +127 -127
  32. package/lib/Utils/messages-media.js +926 -926
  33. package/lib/Utils/messages.js +1105 -1105
  34. package/lib/Utils/process-message.js +404 -404
  35. package/lib/Utils/resolveJid.js +52 -52
  36. package/lib/Utils/use-multi-file-auth-state.js +122 -122
  37. package/lib/Utils/validate-connection.js +228 -228
  38. package/lib/WABinary/jid-utils.js +85 -85
  39. package/lib/index.js +87 -74
  40. package/package.json +2 -2
@@ -1,441 +1,441 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.extractCommunityMetadata = exports.makeCommunitiesSocket = void 0;
7
- const WAProto_1 = require("../../WAProto/index.js");
8
- const Types_1 = require("../Types");
9
- const Utils_1 = require("../Utils");
10
- const logger_1 = __importDefault(require("../Utils/logger"));
11
- const WABinary_1 = require("../WABinary");
12
- const business_1 = require("./business");
13
-
14
- const makeCommunitiesSocket = (config) => {
15
- const sock = (0, business_1.makeBusinessSocket)(config);
16
- const { authState, ev, query, upsertMessage } = sock;
17
- const communityQuery = async (jid, type, content) => query({
18
- tag: 'iq',
19
- attrs: {
20
- type,
21
- xmlns: 'w:g2',
22
- to: jid
23
- },
24
- content
25
- });
26
- const communityMetadata = async (jid) => {
27
- const result = await communityQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
28
- return (0, exports.extractCommunityMetadata)(result);
29
- };
30
- const communityFetchAllParticipating = async () => {
31
- const result = await query({
32
- tag: 'iq',
33
- attrs: {
34
- to: '@g.us',
35
- xmlns: 'w:g2',
36
- type: 'get'
37
- },
38
- content: [
39
- {
40
- tag: 'participating',
41
- attrs: {},
42
- content: [
43
- { tag: 'participants', attrs: {} },
44
- { tag: 'description', attrs: {} }
45
- ]
46
- }
47
- ]
48
- });
49
- const data = {};
50
- const communitiesChild = (0, WABinary_1.getBinaryNodeChild)(result, 'communities');
51
- if (communitiesChild) {
52
- const communities = (0, WABinary_1.getBinaryNodeChildren)(communitiesChild, 'community');
53
- for (const communityNode of communities) {
54
- const meta = (0, exports.extractCommunityMetadata)({
55
- tag: 'result',
56
- attrs: {},
57
- content: [communityNode]
58
- });
59
- data[meta.id] = meta;
60
- }
61
- }
62
- sock.ev.emit('groups.update', Object.values(data));
63
- return data;
64
- };
65
- async function parseGroupResult(node) {
66
- logger_1.default.info({ node }, 'parseGroupResult');
67
- const groupNode = (0, WABinary_1.getBinaryNodeChild)(node, 'group');
68
- if (groupNode) {
69
- try {
70
- logger_1.default.info({ groupNode }, 'groupNode');
71
- const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`);
72
- return metadata ? metadata : Optional.empty();
73
- }
74
- catch (error) {
75
- console.error('Error parsing group metadata:', error);
76
- return Optional.empty();
77
- }
78
- }
79
- return Optional.empty();
80
- }
81
- const Optional = {
82
- empty: () => null,
83
- of: (value) => (value !== null ? { value } : null)
84
- };
85
- sock.ws.on('CB:ib,,dirty', async (node) => {
86
- const { attrs } = (0, WABinary_1.getBinaryNodeChild)(node, 'dirty');
87
- if (attrs.type !== 'communities') {
88
- return;
89
- }
90
- await communityFetchAllParticipating();
91
- await sock.cleanDirtyBits('groups');
92
- });
93
- return {
94
- ...sock,
95
- communityMetadata,
96
- communityCreate: async (subject, body) => {
97
- const descriptionId = (0, Utils_1.generateMessageID)().substring(0, 12);
98
- const result = await communityQuery('@g.us', 'set', [
99
- {
100
- tag: 'create',
101
- attrs: { subject },
102
- content: [
103
- {
104
- tag: 'description',
105
- attrs: { id: descriptionId },
106
- content: [
107
- {
108
- tag: 'body',
109
- attrs: {},
110
- content: Buffer.from(body || '', 'utf-8')
111
- }
112
- ]
113
- },
114
- {
115
- tag: 'parent',
116
- attrs: { default_membership_approval_mode: 'request_required' }
117
- },
118
- {
119
- tag: 'allow_non_admin_sub_group_creation',
120
- attrs: {}
121
- },
122
- {
123
- tag: 'create_general_chat',
124
- attrs: {}
125
- }
126
- ]
127
- }
128
- ]);
129
- return await parseGroupResult(result);
130
- },
131
- communityCreateGroup: async (subject, participants, parentCommunityJid) => {
132
- const key = (0, Utils_1.generateMessageIDV2)();
133
- const result = await communityQuery('@g.us', 'set', [
134
- {
135
- tag: 'create',
136
- attrs: {
137
- subject,
138
- key
139
- },
140
- content: [
141
- ...participants.map(jid => ({
142
- tag: 'participant',
143
- attrs: { jid }
144
- })),
145
- { tag: 'linked_parent', attrs: { jid: parentCommunityJid } }
146
- ]
147
- }
148
- ]);
149
- return await parseGroupResult(result);
150
- },
151
- communityLeave: async (id) => {
152
- await communityQuery('@g.us', 'set', [
153
- {
154
- tag: 'leave',
155
- attrs: {},
156
- content: [{ tag: 'community', attrs: { id } }]
157
- }
158
- ]);
159
- },
160
- communityUpdateSubject: async (jid, subject) => {
161
- await communityQuery(jid, 'set', [
162
- {
163
- tag: 'subject',
164
- attrs: {},
165
- content: Buffer.from(subject, 'utf-8')
166
- }
167
- ]);
168
- },
169
- communityLinkGroup: async (groupJid, parentCommunityJid) => {
170
- await communityQuery(parentCommunityJid, 'set', [
171
- {
172
- tag: 'links',
173
- attrs: {},
174
- content: [
175
- {
176
- tag: 'link',
177
- attrs: { link_type: 'sub_group' },
178
- content: [{ tag: 'group', attrs: { jid: groupJid } }]
179
- }
180
- ]
181
- }
182
- ]);
183
- },
184
- communityUnlinkGroup: async (groupJid, parentCommunityJid) => {
185
- await communityQuery(parentCommunityJid, 'set', [
186
- {
187
- tag: 'unlink',
188
- attrs: { unlink_type: 'sub_group' },
189
- content: [{ tag: 'group', attrs: { jid: groupJid } }]
190
- }
191
- ]);
192
- },
193
- communityFetchLinkedGroups: async (jid) => {
194
- let communityJid = jid;
195
- let isCommunity = false;
196
- // Try to determine if it is a subgroup or a community
197
- const metadata = await sock.groupMetadata(jid);
198
- if (metadata.linkedParent) {
199
- // It is a subgroup, get the community jid
200
- communityJid = metadata.linkedParent;
201
- }
202
- else {
203
- // It is a community
204
- isCommunity = true;
205
- }
206
- // Fetch all subgroups of the community
207
- const result = await communityQuery(communityJid, 'get', [{ tag: 'sub_groups', attrs: {} }]);
208
- const linkedGroupsData = [];
209
- const subGroupsNode = (0, WABinary_1.getBinaryNodeChild)(result, 'sub_groups');
210
- if (subGroupsNode) {
211
- const groupNodes = (0, WABinary_1.getBinaryNodeChildren)(subGroupsNode, 'group');
212
- for (const groupNode of groupNodes) {
213
- linkedGroupsData.push({
214
- id: groupNode.attrs.id ? (0, WABinary_1.jidEncode)(groupNode.attrs.id, 'g.us') : undefined,
215
- subject: groupNode.attrs.subject || '',
216
- creation: groupNode.attrs.creation ? Number(groupNode.attrs.creation) : undefined,
217
- owner: groupNode.attrs.creator ? (0, WABinary_1.jidNormalizedUser)(groupNode.attrs.creator) : undefined,
218
- size: groupNode.attrs.size ? Number(groupNode.attrs.size) : undefined
219
- });
220
- }
221
- }
222
- return {
223
- communityJid,
224
- isCommunity,
225
- linkedGroups: linkedGroupsData
226
- };
227
- },
228
- communityRequestParticipantsList: async (jid) => {
229
- const result = await communityQuery(jid, 'get', [
230
- {
231
- tag: 'membership_approval_requests',
232
- attrs: {}
233
- }
234
- ]);
235
- const node = (0, WABinary_1.getBinaryNodeChild)(result, 'membership_approval_requests');
236
- const participants = (0, WABinary_1.getBinaryNodeChildren)(node, 'membership_approval_request');
237
- return participants.map(v => v.attrs);
238
- },
239
- communityRequestParticipantsUpdate: async (jid, participants, action) => {
240
- const result = await communityQuery(jid, 'set', [
241
- {
242
- tag: 'membership_requests_action',
243
- attrs: {},
244
- content: [
245
- {
246
- tag: action,
247
- attrs: {},
248
- content: participants.map(jid => ({
249
- tag: 'participant',
250
- attrs: { jid }
251
- }))
252
- }
253
- ]
254
- }
255
- ]);
256
- const node = (0, WABinary_1.getBinaryNodeChild)(result, 'membership_requests_action');
257
- const nodeAction = (0, WABinary_1.getBinaryNodeChild)(node, action);
258
- const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(nodeAction, 'participant');
259
- return participantsAffected.map(p => {
260
- return { status: p.attrs.error || '200', jid: p.attrs.jid };
261
- });
262
- },
263
- communityParticipantsUpdate: async (jid, participants, action) => {
264
- const result = await communityQuery(jid, 'set', [
265
- {
266
- tag: action,
267
- attrs: action === 'remove' ? { linked_groups: 'true' } : {},
268
- content: participants.map(jid => ({
269
- tag: 'participant',
270
- attrs: { jid }
271
- }))
272
- }
273
- ]);
274
- const node = (0, WABinary_1.getBinaryNodeChild)(result, action);
275
- const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(node, 'participant');
276
- return participantsAffected.map(p => {
277
- return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
278
- });
279
- },
280
- communityUpdateDescription: async (jid, description) => {
281
- const metadata = await communityMetadata(jid);
282
- const prev = metadata.descId ?? null;
283
- await communityQuery(jid, 'set', [
284
- {
285
- tag: 'description',
286
- attrs: {
287
- ...(description ? { id: (0, Utils_1.generateMessageID)() } : { delete: 'true' }),
288
- ...(prev ? { prev } : {})
289
- },
290
- content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
291
- }
292
- ]);
293
- },
294
- communityInviteCode: async (jid) => {
295
- const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
296
- const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
297
- return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
298
- },
299
- communityRevokeInvite: async (jid) => {
300
- const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
301
- const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
302
- return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
303
- },
304
- communityAcceptInvite: async (code) => {
305
- const results = await communityQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
306
- const result = (0, WABinary_1.getBinaryNodeChild)(results, 'community');
307
- return result === null || result === void 0 ? void 0 : result.attrs.jid;
308
- },
309
- /**
310
- * revoke a v4 invite for someone
311
- * @param communityJid community jid
312
- * @param invitedJid jid of person you invited
313
- * @returns true if successful
314
- */
315
- communityRevokeInviteV4: async (communityJid, invitedJid) => {
316
- const result = await communityQuery(communityJid, 'set', [
317
- { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
318
- ]);
319
- return !!result;
320
- },
321
- /**
322
- * accept a CommunityInviteMessage
323
- * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
324
- * @param inviteMessage the message to accept
325
- */
326
- communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
327
- var _a;
328
- key = typeof key === 'string' ? { remoteJid: key } : key;
329
- const results = await communityQuery(inviteMessage.groupJid, 'set', [
330
- {
331
- tag: 'accept',
332
- attrs: {
333
- code: inviteMessage.inviteCode,
334
- expiration: inviteMessage.inviteExpiration.toString(),
335
- admin: key.remoteJid
336
- }
337
- }
338
- ]);
339
- // if we have the full message key
340
- // update the invite message to be expired
341
- if (key.id) {
342
- // create new invite message that is expired
343
- inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);
344
- inviteMessage.inviteExpiration = 0;
345
- inviteMessage.inviteCode = '';
346
- ev.emit('messages.update', [
347
- {
348
- key,
349
- update: {
350
- message: {
351
- groupInviteMessage: inviteMessage
352
- }
353
- }
354
- }
355
- ]);
356
- }
357
- // generate the community add message
358
- await upsertMessage({
359
- key: {
360
- remoteJid: inviteMessage.groupJid,
361
- id: (0, Utils_1.generateMessageIDV2)((_a = sock.user) === null || _a === void 0 ? void 0 : _a.id),
362
- fromMe: false,
363
- participant: key.remoteJid // TODO: investigate if this makes any sense at all
364
- },
365
- messageStubType: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,
366
- messageStubParameters: [JSON.stringify(authState.creds.me)],
367
- participant: key.remoteJid,
368
- messageTimestamp: (0, Utils_1.unixTimestampSeconds)()
369
- }, 'notify');
370
- return results.attrs.from;
371
- }),
372
- communityGetInviteInfo: async (code) => {
373
- const results = await communityQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
374
- return (0, exports.extractCommunityMetadata)(results);
375
- },
376
- communityToggleEphemeral: async (jid, ephemeralExpiration) => {
377
- const content = ephemeralExpiration
378
- ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
379
- : { tag: 'not_ephemeral', attrs: {} };
380
- await communityQuery(jid, 'set', [content]);
381
- },
382
- communitySettingUpdate: async (jid, setting) => {
383
- await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
384
- },
385
- communityMemberAddMode: async (jid, mode) => {
386
- await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
387
- },
388
- communityJoinApprovalMode: async (jid, mode) => {
389
- await communityQuery(jid, 'set', [
390
- { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'community_join', attrs: { state: mode } }] }
391
- ]);
392
- },
393
- communityFetchAllParticipating
394
- };
395
- };
396
- exports.makeCommunitiesSocket = makeCommunitiesSocket;
397
-
398
- const extractCommunityMetadata = (result) => {
399
- var _a, _b;
400
- const community = (0, WABinary_1.getBinaryNodeChild)(result, 'community');
401
- const descChild = (0, WABinary_1.getBinaryNodeChild)(community, 'description');
402
- let desc;
403
- let descId;
404
- if (descChild) {
405
- desc = (0, WABinary_1.getBinaryNodeChildString)(descChild, 'body');
406
- descId = descChild.attrs.id;
407
- }
408
- const communityId = ((_a = community.attrs.id) === null || _a === void 0 ? void 0 : _a.includes('@'))
409
- ? community.attrs.id
410
- : (0, WABinary_1.jidEncode)(community.attrs.id || '', 'g.us');
411
- const eph = (_b = (0, WABinary_1.getBinaryNodeChild)(community, 'ephemeral')) === null || _b === void 0 ? void 0 : _b.attrs.expiration;
412
- const memberAddMode = (0, WABinary_1.getBinaryNodeChildString)(community, 'member_add_mode') === 'all_member_add';
413
- const metadata = {
414
- id: communityId,
415
- subject: community.attrs.subject || '',
416
- subjectOwner: community.attrs.s_o,
417
- subjectTime: Number(community.attrs.s_t || 0),
418
- size: (0, WABinary_1.getBinaryNodeChildren)(community, 'participant').length,
419
- creation: Number(community.attrs.creation || 0),
420
- owner: community.attrs.creator ? (0, WABinary_1.jidNormalizedUser)(community.attrs.creator) : undefined,
421
- desc,
422
- descId,
423
- linkedParent: (0, WABinary_1.getBinaryNodeChild)(community, 'linked_parent')?.attrs.jid || undefined,
424
- restrict: !!(0, WABinary_1.getBinaryNodeChild)(community, 'locked'),
425
- announce: !!(0, WABinary_1.getBinaryNodeChild)(community, 'announcement'),
426
- isCommunity: !!(0, WABinary_1.getBinaryNodeChild)(community, 'parent'),
427
- isCommunityAnnounce: !!(0, WABinary_1.getBinaryNodeChild)(community, 'default_sub_community'),
428
- joinApprovalMode: !!(0, WABinary_1.getBinaryNodeChild)(community, 'membership_approval_mode'),
429
- memberAddMode,
430
- participants: (0, WABinary_1.getBinaryNodeChildren)(community, 'participant').map(({ attrs }) => {
431
- return {
432
- id: attrs.jid,
433
- admin: (attrs.type || null)
434
- };
435
- }),
436
- ephemeralDuration: eph ? +eph : undefined,
437
- addressingMode: (0, WABinary_1.getBinaryNodeChildString)(community, 'addressing_mode')
438
- };
439
- return metadata;
440
- };
441
- exports.extractCommunityMetadata = extractCommunityMetadata;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.extractCommunityMetadata = exports.makeCommunitiesSocket = void 0;
7
+ const WAProto_1 = require("../../WAProto/index.js");
8
+ const Types_1 = require("../Types");
9
+ const Utils_1 = require("../Utils");
10
+ const logger_1 = __importDefault(require("../Utils/logger"));
11
+ const WABinary_1 = require("../WABinary");
12
+ const business_1 = require("./business");
13
+
14
+ const makeCommunitiesSocket = (config) => {
15
+ const sock = (0, business_1.makeBusinessSocket)(config);
16
+ const { authState, ev, query, upsertMessage } = sock;
17
+ const communityQuery = async (jid, type, content) => query({
18
+ tag: 'iq',
19
+ attrs: {
20
+ type,
21
+ xmlns: 'w:g2',
22
+ to: jid
23
+ },
24
+ content
25
+ });
26
+ const communityMetadata = async (jid) => {
27
+ const result = await communityQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
28
+ return (0, exports.extractCommunityMetadata)(result);
29
+ };
30
+ const communityFetchAllParticipating = async () => {
31
+ const result = await query({
32
+ tag: 'iq',
33
+ attrs: {
34
+ to: '@g.us',
35
+ xmlns: 'w:g2',
36
+ type: 'get'
37
+ },
38
+ content: [
39
+ {
40
+ tag: 'participating',
41
+ attrs: {},
42
+ content: [
43
+ { tag: 'participants', attrs: {} },
44
+ { tag: 'description', attrs: {} }
45
+ ]
46
+ }
47
+ ]
48
+ });
49
+ const data = {};
50
+ const communitiesChild = (0, WABinary_1.getBinaryNodeChild)(result, 'communities');
51
+ if (communitiesChild) {
52
+ const communities = (0, WABinary_1.getBinaryNodeChildren)(communitiesChild, 'community');
53
+ for (const communityNode of communities) {
54
+ const meta = (0, exports.extractCommunityMetadata)({
55
+ tag: 'result',
56
+ attrs: {},
57
+ content: [communityNode]
58
+ });
59
+ data[meta.id] = meta;
60
+ }
61
+ }
62
+ sock.ev.emit('groups.update', Object.values(data));
63
+ return data;
64
+ };
65
+ async function parseGroupResult(node) {
66
+ logger_1.default.info({ node }, 'parseGroupResult');
67
+ const groupNode = (0, WABinary_1.getBinaryNodeChild)(node, 'group');
68
+ if (groupNode) {
69
+ try {
70
+ logger_1.default.info({ groupNode }, 'groupNode');
71
+ const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`);
72
+ return metadata ? metadata : Optional.empty();
73
+ }
74
+ catch (error) {
75
+ console.error('Error parsing group metadata:', error);
76
+ return Optional.empty();
77
+ }
78
+ }
79
+ return Optional.empty();
80
+ }
81
+ const Optional = {
82
+ empty: () => null,
83
+ of: (value) => (value !== null ? { value } : null)
84
+ };
85
+ sock.ws.on('CB:ib,,dirty', async (node) => {
86
+ const { attrs } = (0, WABinary_1.getBinaryNodeChild)(node, 'dirty');
87
+ if (attrs.type !== 'communities') {
88
+ return;
89
+ }
90
+ await communityFetchAllParticipating();
91
+ await sock.cleanDirtyBits('groups');
92
+ });
93
+ return {
94
+ ...sock,
95
+ communityMetadata,
96
+ communityCreate: async (subject, body) => {
97
+ const descriptionId = (0, Utils_1.generateMessageID)().substring(0, 12);
98
+ const result = await communityQuery('@g.us', 'set', [
99
+ {
100
+ tag: 'create',
101
+ attrs: { subject },
102
+ content: [
103
+ {
104
+ tag: 'description',
105
+ attrs: { id: descriptionId },
106
+ content: [
107
+ {
108
+ tag: 'body',
109
+ attrs: {},
110
+ content: Buffer.from(body || '', 'utf-8')
111
+ }
112
+ ]
113
+ },
114
+ {
115
+ tag: 'parent',
116
+ attrs: { default_membership_approval_mode: 'request_required' }
117
+ },
118
+ {
119
+ tag: 'allow_non_admin_sub_group_creation',
120
+ attrs: {}
121
+ },
122
+ {
123
+ tag: 'create_general_chat',
124
+ attrs: {}
125
+ }
126
+ ]
127
+ }
128
+ ]);
129
+ return await parseGroupResult(result);
130
+ },
131
+ communityCreateGroup: async (subject, participants, parentCommunityJid) => {
132
+ const key = (0, Utils_1.generateMessageIDV2)();
133
+ const result = await communityQuery('@g.us', 'set', [
134
+ {
135
+ tag: 'create',
136
+ attrs: {
137
+ subject,
138
+ key
139
+ },
140
+ content: [
141
+ ...participants.map(jid => ({
142
+ tag: 'participant',
143
+ attrs: { jid }
144
+ })),
145
+ { tag: 'linked_parent', attrs: { jid: parentCommunityJid } }
146
+ ]
147
+ }
148
+ ]);
149
+ return await parseGroupResult(result);
150
+ },
151
+ communityLeave: async (id) => {
152
+ await communityQuery('@g.us', 'set', [
153
+ {
154
+ tag: 'leave',
155
+ attrs: {},
156
+ content: [{ tag: 'community', attrs: { id } }]
157
+ }
158
+ ]);
159
+ },
160
+ communityUpdateSubject: async (jid, subject) => {
161
+ await communityQuery(jid, 'set', [
162
+ {
163
+ tag: 'subject',
164
+ attrs: {},
165
+ content: Buffer.from(subject, 'utf-8')
166
+ }
167
+ ]);
168
+ },
169
+ communityLinkGroup: async (groupJid, parentCommunityJid) => {
170
+ await communityQuery(parentCommunityJid, 'set', [
171
+ {
172
+ tag: 'links',
173
+ attrs: {},
174
+ content: [
175
+ {
176
+ tag: 'link',
177
+ attrs: { link_type: 'sub_group' },
178
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
179
+ }
180
+ ]
181
+ }
182
+ ]);
183
+ },
184
+ communityUnlinkGroup: async (groupJid, parentCommunityJid) => {
185
+ await communityQuery(parentCommunityJid, 'set', [
186
+ {
187
+ tag: 'unlink',
188
+ attrs: { unlink_type: 'sub_group' },
189
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
190
+ }
191
+ ]);
192
+ },
193
+ communityFetchLinkedGroups: async (jid) => {
194
+ let communityJid = jid;
195
+ let isCommunity = false;
196
+ // Try to determine if it is a subgroup or a community
197
+ const metadata = await sock.groupMetadata(jid);
198
+ if (metadata.linkedParent) {
199
+ // It is a subgroup, get the community jid
200
+ communityJid = metadata.linkedParent;
201
+ }
202
+ else {
203
+ // It is a community
204
+ isCommunity = true;
205
+ }
206
+ // Fetch all subgroups of the community
207
+ const result = await communityQuery(communityJid, 'get', [{ tag: 'sub_groups', attrs: {} }]);
208
+ const linkedGroupsData = [];
209
+ const subGroupsNode = (0, WABinary_1.getBinaryNodeChild)(result, 'sub_groups');
210
+ if (subGroupsNode) {
211
+ const groupNodes = (0, WABinary_1.getBinaryNodeChildren)(subGroupsNode, 'group');
212
+ for (const groupNode of groupNodes) {
213
+ linkedGroupsData.push({
214
+ id: groupNode.attrs.id ? (0, WABinary_1.jidEncode)(groupNode.attrs.id, 'g.us') : undefined,
215
+ subject: groupNode.attrs.subject || '',
216
+ creation: groupNode.attrs.creation ? Number(groupNode.attrs.creation) : undefined,
217
+ owner: groupNode.attrs.creator ? (0, WABinary_1.jidNormalizedUser)(groupNode.attrs.creator) : undefined,
218
+ size: groupNode.attrs.size ? Number(groupNode.attrs.size) : undefined
219
+ });
220
+ }
221
+ }
222
+ return {
223
+ communityJid,
224
+ isCommunity,
225
+ linkedGroups: linkedGroupsData
226
+ };
227
+ },
228
+ communityRequestParticipantsList: async (jid) => {
229
+ const result = await communityQuery(jid, 'get', [
230
+ {
231
+ tag: 'membership_approval_requests',
232
+ attrs: {}
233
+ }
234
+ ]);
235
+ const node = (0, WABinary_1.getBinaryNodeChild)(result, 'membership_approval_requests');
236
+ const participants = (0, WABinary_1.getBinaryNodeChildren)(node, 'membership_approval_request');
237
+ return participants.map(v => v.attrs);
238
+ },
239
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
240
+ const result = await communityQuery(jid, 'set', [
241
+ {
242
+ tag: 'membership_requests_action',
243
+ attrs: {},
244
+ content: [
245
+ {
246
+ tag: action,
247
+ attrs: {},
248
+ content: participants.map(jid => ({
249
+ tag: 'participant',
250
+ attrs: { jid }
251
+ }))
252
+ }
253
+ ]
254
+ }
255
+ ]);
256
+ const node = (0, WABinary_1.getBinaryNodeChild)(result, 'membership_requests_action');
257
+ const nodeAction = (0, WABinary_1.getBinaryNodeChild)(node, action);
258
+ const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(nodeAction, 'participant');
259
+ return participantsAffected.map(p => {
260
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
261
+ });
262
+ },
263
+ communityParticipantsUpdate: async (jid, participants, action) => {
264
+ const result = await communityQuery(jid, 'set', [
265
+ {
266
+ tag: action,
267
+ attrs: action === 'remove' ? { linked_groups: 'true' } : {},
268
+ content: participants.map(jid => ({
269
+ tag: 'participant',
270
+ attrs: { jid }
271
+ }))
272
+ }
273
+ ]);
274
+ const node = (0, WABinary_1.getBinaryNodeChild)(result, action);
275
+ const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(node, 'participant');
276
+ return participantsAffected.map(p => {
277
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
278
+ });
279
+ },
280
+ communityUpdateDescription: async (jid, description) => {
281
+ const metadata = await communityMetadata(jid);
282
+ const prev = metadata.descId ?? null;
283
+ await communityQuery(jid, 'set', [
284
+ {
285
+ tag: 'description',
286
+ attrs: {
287
+ ...(description ? { id: (0, Utils_1.generateMessageID)() } : { delete: 'true' }),
288
+ ...(prev ? { prev } : {})
289
+ },
290
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
291
+ }
292
+ ]);
293
+ },
294
+ communityInviteCode: async (jid) => {
295
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
296
+ const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
297
+ return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
298
+ },
299
+ communityRevokeInvite: async (jid) => {
300
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
301
+ const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
302
+ return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
303
+ },
304
+ communityAcceptInvite: async (code) => {
305
+ const results = await communityQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
306
+ const result = (0, WABinary_1.getBinaryNodeChild)(results, 'community');
307
+ return result === null || result === void 0 ? void 0 : result.attrs.jid;
308
+ },
309
+ /**
310
+ * revoke a v4 invite for someone
311
+ * @param communityJid community jid
312
+ * @param invitedJid jid of person you invited
313
+ * @returns true if successful
314
+ */
315
+ communityRevokeInviteV4: async (communityJid, invitedJid) => {
316
+ const result = await communityQuery(communityJid, 'set', [
317
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
318
+ ]);
319
+ return !!result;
320
+ },
321
+ /**
322
+ * accept a CommunityInviteMessage
323
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
324
+ * @param inviteMessage the message to accept
325
+ */
326
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
327
+ var _a;
328
+ key = typeof key === 'string' ? { remoteJid: key } : key;
329
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [
330
+ {
331
+ tag: 'accept',
332
+ attrs: {
333
+ code: inviteMessage.inviteCode,
334
+ expiration: inviteMessage.inviteExpiration.toString(),
335
+ admin: key.remoteJid
336
+ }
337
+ }
338
+ ]);
339
+ // if we have the full message key
340
+ // update the invite message to be expired
341
+ if (key.id) {
342
+ // create new invite message that is expired
343
+ inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);
344
+ inviteMessage.inviteExpiration = 0;
345
+ inviteMessage.inviteCode = '';
346
+ ev.emit('messages.update', [
347
+ {
348
+ key,
349
+ update: {
350
+ message: {
351
+ groupInviteMessage: inviteMessage
352
+ }
353
+ }
354
+ }
355
+ ]);
356
+ }
357
+ // generate the community add message
358
+ await upsertMessage({
359
+ key: {
360
+ remoteJid: inviteMessage.groupJid,
361
+ id: (0, Utils_1.generateMessageIDV2)((_a = sock.user) === null || _a === void 0 ? void 0 : _a.id),
362
+ fromMe: false,
363
+ participant: key.remoteJid // TODO: investigate if this makes any sense at all
364
+ },
365
+ messageStubType: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,
366
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
367
+ participant: key.remoteJid,
368
+ messageTimestamp: (0, Utils_1.unixTimestampSeconds)()
369
+ }, 'notify');
370
+ return results.attrs.from;
371
+ }),
372
+ communityGetInviteInfo: async (code) => {
373
+ const results = await communityQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
374
+ return (0, exports.extractCommunityMetadata)(results);
375
+ },
376
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
377
+ const content = ephemeralExpiration
378
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
379
+ : { tag: 'not_ephemeral', attrs: {} };
380
+ await communityQuery(jid, 'set', [content]);
381
+ },
382
+ communitySettingUpdate: async (jid, setting) => {
383
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
384
+ },
385
+ communityMemberAddMode: async (jid, mode) => {
386
+ await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
387
+ },
388
+ communityJoinApprovalMode: async (jid, mode) => {
389
+ await communityQuery(jid, 'set', [
390
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'community_join', attrs: { state: mode } }] }
391
+ ]);
392
+ },
393
+ communityFetchAllParticipating
394
+ };
395
+ };
396
+ exports.makeCommunitiesSocket = makeCommunitiesSocket;
397
+
398
+ const extractCommunityMetadata = (result) => {
399
+ var _a, _b;
400
+ const community = (0, WABinary_1.getBinaryNodeChild)(result, 'community');
401
+ const descChild = (0, WABinary_1.getBinaryNodeChild)(community, 'description');
402
+ let desc;
403
+ let descId;
404
+ if (descChild) {
405
+ desc = (0, WABinary_1.getBinaryNodeChildString)(descChild, 'body');
406
+ descId = descChild.attrs.id;
407
+ }
408
+ const communityId = ((_a = community.attrs.id) === null || _a === void 0 ? void 0 : _a.includes('@'))
409
+ ? community.attrs.id
410
+ : (0, WABinary_1.jidEncode)(community.attrs.id || '', 'g.us');
411
+ const eph = (_b = (0, WABinary_1.getBinaryNodeChild)(community, 'ephemeral')) === null || _b === void 0 ? void 0 : _b.attrs.expiration;
412
+ const memberAddMode = (0, WABinary_1.getBinaryNodeChildString)(community, 'member_add_mode') === 'all_member_add';
413
+ const metadata = {
414
+ id: communityId,
415
+ subject: community.attrs.subject || '',
416
+ subjectOwner: community.attrs.s_o,
417
+ subjectTime: Number(community.attrs.s_t || 0),
418
+ size: (0, WABinary_1.getBinaryNodeChildren)(community, 'participant').length,
419
+ creation: Number(community.attrs.creation || 0),
420
+ owner: community.attrs.creator ? (0, WABinary_1.jidNormalizedUser)(community.attrs.creator) : undefined,
421
+ desc,
422
+ descId,
423
+ linkedParent: (0, WABinary_1.getBinaryNodeChild)(community, 'linked_parent')?.attrs.jid || undefined,
424
+ restrict: !!(0, WABinary_1.getBinaryNodeChild)(community, 'locked'),
425
+ announce: !!(0, WABinary_1.getBinaryNodeChild)(community, 'announcement'),
426
+ isCommunity: !!(0, WABinary_1.getBinaryNodeChild)(community, 'parent'),
427
+ isCommunityAnnounce: !!(0, WABinary_1.getBinaryNodeChild)(community, 'default_sub_community'),
428
+ joinApprovalMode: !!(0, WABinary_1.getBinaryNodeChild)(community, 'membership_approval_mode'),
429
+ memberAddMode,
430
+ participants: (0, WABinary_1.getBinaryNodeChildren)(community, 'participant').map(({ attrs }) => {
431
+ return {
432
+ id: attrs.jid,
433
+ admin: (attrs.type || null)
434
+ };
435
+ }),
436
+ ephemeralDuration: eph ? +eph : undefined,
437
+ addressingMode: (0, WABinary_1.getBinaryNodeChildString)(community, 'addressing_mode')
438
+ };
439
+ return metadata;
440
+ };
441
+ exports.extractCommunityMetadata = extractCommunityMetadata;