@rizzkezik/bails 6.1.0 → 6.1.2

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,392 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeCommunitiesSocket = exports.extractCommunityMetadata = void 0;
4
+ const { proto } = require("../../WAProto");
5
+ const { WAMessageStubType, WAMessageAddressingMode } = require("../Types");
6
+ const { generateMessageID, unixTimestampSeconds } = require("../Utils");
7
+ const {
8
+ getBinaryNodeChild,
9
+ getBinaryNodeChildren,
10
+ getBinaryNodeChildString,
11
+ jidEncode,
12
+ jidNormalizedUser,
13
+ } = require("../WABinary");
14
+ const { makeBusinessSocket } = require("./business");
15
+ const makeCommunitiesSocket = (config) => {
16
+ const conn = makeBusinessSocket(config);
17
+ const { authState, ev, query, groupMetadata, upsertMessage, cleanDirtyBits } = conn;
18
+ const communityQuery = async (jid, type, content) => query({
19
+ tag: 'iq',
20
+ attrs: { type, xmlns: 'w:g2', to: jid },
21
+ content
22
+ });
23
+ const communityMetadata = async (jid) => {
24
+ const result = await communityQuery(jid, 'get', [
25
+ { tag: 'query', attrs: { request: 'interactive' } }
26
+ ]);
27
+ return extractCommunityMetadata(result);
28
+ };
29
+ const communityFetchAllParticipating = async () => {
30
+ const result = await query({
31
+ tag: 'iq',
32
+ attrs: { to: '@g.us', xmlns: 'w:g2', type: 'get' },
33
+ content: [
34
+ {
35
+ tag: 'participating',
36
+ attrs: {},
37
+ content: [
38
+ { tag: 'participants', attrs: {} },
39
+ { tag: 'description', attrs: {} }
40
+ ]
41
+ }
42
+ ]
43
+ });
44
+ const data = {};
45
+ const communitiesChild = getBinaryNodeChild(result, 'communities');
46
+ if (communitiesChild) {
47
+ const communities = getBinaryNodeChildren(communitiesChild, 'community');
48
+ for (const communityNode of communities) {
49
+ const meta = extractCommunityMetadata({
50
+ tag: 'result',
51
+ attrs: {},
52
+ content: [communityNode]
53
+ });
54
+ data[meta.id] = meta;
55
+ }
56
+ }
57
+ conn.ev.emit('groups.update', Object.values(data));
58
+ return data;
59
+ };
60
+ async function parseGroupResult(node) {
61
+ var _a;
62
+ (_a = conn.logger) === null || _a === void 0 ? void 0 : _a.info({ node }, 'parseGroupResult');
63
+ const groupNode = getBinaryNodeChild(node, 'group');
64
+ if (groupNode) {
65
+ try {
66
+ const metadata = await groupMetadata(`${groupNode.attrs.id}@g.us`);
67
+ return metadata || null;
68
+ }
69
+ catch (error) {
70
+ var _b;
71
+ (_b = conn.logger) === null || _b === void 0 ? void 0 : _b.warn({ error }, 'Error parsing group metadata');
72
+ return null;
73
+ }
74
+ }
75
+ return null;
76
+ }
77
+ conn.ws.on('CB:ib,,dirty', async (node) => {
78
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
79
+ if (attrs.type !== 'communities') {
80
+ return;
81
+ }
82
+ await communityFetchAllParticipating();
83
+ await cleanDirtyBits('groups');
84
+ });
85
+ return {
86
+ ...conn,
87
+ communityQuery,
88
+ communityMetadata,
89
+ communityCreate: async (subject, body) => {
90
+ const descriptionId = generateMessageID().substring(0, 12);
91
+ const result = await communityQuery('@g.us', 'set', [
92
+ {
93
+ tag: 'create',
94
+ attrs: { subject },
95
+ content: [
96
+ {
97
+ tag: 'description',
98
+ attrs: { id: descriptionId },
99
+ content: [
100
+ { tag: 'body', attrs: {}, content: Buffer.from(body || '', 'utf-8') }
101
+ ]
102
+ },
103
+ { tag: 'parent', attrs: { default_membership_approval_mode: 'request_required' } },
104
+ { tag: 'allow_non_admin_sub_group_creation', attrs: {} },
105
+ { tag: 'create_general_chat', attrs: {} }
106
+ ]
107
+ }
108
+ ]);
109
+ return await parseGroupResult(result);
110
+ },
111
+ communityCreateGroup: async (subject, participants, parentCommunityJid) => {
112
+ const key = generateMessageID();
113
+ const result = await communityQuery('@g.us', 'set', [
114
+ {
115
+ tag: 'create',
116
+ attrs: { subject, key },
117
+ content: [
118
+ ...participants.map((jid) => ({ tag: 'participant', attrs: { jid } })),
119
+ { tag: 'linked_parent', attrs: { jid: parentCommunityJid } }
120
+ ]
121
+ }
122
+ ]);
123
+ return await parseGroupResult(result);
124
+ },
125
+ communityLeave: async (id) => {
126
+ await communityQuery('@g.us', 'set', [
127
+ { tag: 'leave', attrs: {}, content: [{ tag: 'community', attrs: { id } }] }
128
+ ]);
129
+ },
130
+ communityUpdateSubject: async (jid, subject) => {
131
+ await communityQuery(jid, 'set', [
132
+ { tag: 'subject', attrs: {}, content: Buffer.from(subject, 'utf-8') }
133
+ ]);
134
+ },
135
+ communityLinkGroup: async (groupJid, parentCommunityJid) => {
136
+ await communityQuery(parentCommunityJid, 'set', [
137
+ {
138
+ tag: 'links',
139
+ attrs: {},
140
+ content: [
141
+ {
142
+ tag: 'link',
143
+ attrs: { link_type: 'sub_group' },
144
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
145
+ }
146
+ ]
147
+ }
148
+ ]);
149
+ },
150
+ communityUnlinkGroup: async (groupJid, parentCommunityJid) => {
151
+ await communityQuery(parentCommunityJid, 'set', [
152
+ {
153
+ tag: 'unlink',
154
+ attrs: { unlink_type: 'sub_group' },
155
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
156
+ }
157
+ ]);
158
+ },
159
+ communityFetchLinkedGroups: async (jid) => {
160
+ let communityJid = jid;
161
+ let isCommunity = false;
162
+ const metadata = await groupMetadata(jid);
163
+ if (metadata.linkedParent) {
164
+ communityJid = metadata.linkedParent;
165
+ }
166
+ else {
167
+ isCommunity = true;
168
+ }
169
+ const result = await communityQuery(communityJid, 'get', [
170
+ { tag: 'sub_groups', attrs: {} }
171
+ ]);
172
+ const linkedGroupsData = [];
173
+ const subGroupsNode = getBinaryNodeChild(result, 'sub_groups');
174
+ if (subGroupsNode) {
175
+ const groupNodes = getBinaryNodeChildren(subGroupsNode, 'group');
176
+ for (const groupNode of groupNodes) {
177
+ linkedGroupsData.push({
178
+ id: groupNode.attrs.id ? jidEncode(groupNode.attrs.id, 'g.us') : undefined,
179
+ subject: groupNode.attrs.subject || '',
180
+ creation: groupNode.attrs.creation ? Number(groupNode.attrs.creation) : undefined,
181
+ owner: groupNode.attrs.creator ? jidNormalizedUser(groupNode.attrs.creator) : undefined,
182
+ size: groupNode.attrs.size ? Number(groupNode.attrs.size) : undefined
183
+ });
184
+ }
185
+ }
186
+ return { communityJid, isCommunity, linkedGroups: linkedGroupsData };
187
+ },
188
+ communityRequestParticipantsList: async (jid) => {
189
+ const result = await communityQuery(jid, 'get', [
190
+ { tag: 'membership_approval_requests', attrs: {} }
191
+ ]);
192
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
193
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
194
+ return participants.map((v) => v.attrs);
195
+ },
196
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
197
+ const result = await communityQuery(jid, 'set', [
198
+ {
199
+ tag: 'membership_requests_action',
200
+ attrs: {},
201
+ content: [
202
+ {
203
+ tag: action,
204
+ attrs: {},
205
+ content: participants.map((jid) => ({ tag: 'participant', attrs: { jid } }))
206
+ }
207
+ ]
208
+ }
209
+ ]);
210
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
211
+ const nodeAction = getBinaryNodeChild(node, action);
212
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
213
+ return participantsAffected.map((p) => ({ status: p.attrs.error || '200', jid: p.attrs.jid }));
214
+ },
215
+ communityParticipantsUpdate: async (jid, participants, action) => {
216
+ const result = await communityQuery(jid, 'set', [
217
+ {
218
+ tag: action,
219
+ attrs: action === 'remove' ? { linked_groups: 'true' } : {},
220
+ content: participants.map((jid) => ({ tag: 'participant', attrs: { jid } }))
221
+ }
222
+ ]);
223
+ const node = getBinaryNodeChild(result, action);
224
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
225
+ return participantsAffected.map((p) => ({ status: p.attrs.error || '200', jid: p.attrs.jid, content: p }));
226
+ },
227
+ communityUpdateDescription: async (jid, description) => {
228
+ var _a;
229
+ const metadata = await communityMetadata(jid);
230
+ const prev = (_a = metadata.descId) !== null && _a !== void 0 ? _a : null;
231
+ await communityQuery(jid, 'set', [
232
+ {
233
+ tag: 'description',
234
+ attrs: {
235
+ ...(description ? { id: generateMessageID() } : { delete: 'true' }),
236
+ ...(prev ? { prev } : {})
237
+ },
238
+ content: description
239
+ ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }]
240
+ : undefined
241
+ }
242
+ ]);
243
+ },
244
+ communityInviteCode: async (jid) => {
245
+ var _a;
246
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
247
+ const inviteNode = getBinaryNodeChild(result, 'invite');
248
+ return (_a = inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs) === null || _a === void 0 ? void 0 : _a.code;
249
+ },
250
+ communityRevokeInvite: async (jid) => {
251
+ var _a;
252
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
253
+ const inviteNode = getBinaryNodeChild(result, 'invite');
254
+ return (_a = inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs) === null || _a === void 0 ? void 0 : _a.code;
255
+ },
256
+ communityAcceptInvite: async (code) => {
257
+ var _a;
258
+ const results = await communityQuery('@g.us', 'set', [
259
+ { tag: 'invite', attrs: { code } }
260
+ ]);
261
+ const result = getBinaryNodeChild(results, 'community');
262
+ return (_a = result === null || result === void 0 ? void 0 : result.attrs) === null || _a === void 0 ? void 0 : _a.jid;
263
+ },
264
+ communityRevokeInviteV4: async (communityJid, invitedJid) => {
265
+ const result = await communityQuery(communityJid, 'set', [
266
+ {
267
+ tag: 'revoke',
268
+ attrs: {},
269
+ content: [{ tag: 'participant', attrs: { jid: invitedJid } }]
270
+ }
271
+ ]);
272
+ return !!result;
273
+ },
274
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
275
+ var _a;
276
+ key = typeof key === 'string' ? { remoteJid: key } : key;
277
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [
278
+ {
279
+ tag: 'accept',
280
+ attrs: {
281
+ code: inviteMessage.inviteCode,
282
+ expiration: inviteMessage.inviteExpiration.toString(),
283
+ admin: key.remoteJid
284
+ }
285
+ }
286
+ ]);
287
+ if (key.id) {
288
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
289
+ inviteMessage.inviteExpiration = 0;
290
+ inviteMessage.inviteCode = '';
291
+ ev.emit('messages.update', [
292
+ { key, update: { message: { groupInviteMessage: inviteMessage } } }
293
+ ]);
294
+ }
295
+ await upsertMessage({
296
+ key: {
297
+ remoteJid: inviteMessage.groupJid,
298
+ id: generateMessageID((_a = conn.user) === null || _a === void 0 ? void 0 : _a.id),
299
+ fromMe: false,
300
+ participant: key.remoteJid
301
+ },
302
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
303
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
304
+ participant: key.remoteJid,
305
+ messageTimestamp: unixTimestampSeconds()
306
+ }, 'notify');
307
+ return results.attrs.from;
308
+ }),
309
+ communityGetInviteInfo: async (code) => {
310
+ const results = await communityQuery('@g.us', 'get', [
311
+ { tag: 'invite', attrs: { code } }
312
+ ]);
313
+ return extractCommunityMetadata(results);
314
+ },
315
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
316
+ const content = ephemeralExpiration
317
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
318
+ : { tag: 'not_ephemeral', attrs: {} };
319
+ await communityQuery(jid, 'set', [content]);
320
+ },
321
+ communitySettingUpdate: async (jid, setting) => {
322
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
323
+ },
324
+ communityMemberAddMode: async (jid, mode) => {
325
+ await communityQuery(jid, 'set', [
326
+ { tag: 'member_add_mode', attrs: {}, content: mode }
327
+ ]);
328
+ },
329
+ communityJoinApprovalMode: async (jid, mode) => {
330
+ await communityQuery(jid, 'set', [
331
+ {
332
+ tag: 'membership_approval_mode',
333
+ attrs: {},
334
+ content: [{ tag: 'community_join', attrs: { state: mode } }]
335
+ }
336
+ ]);
337
+ },
338
+ communityFetchAllParticipating
339
+ };
340
+ };
341
+ exports.makeCommunitiesSocket = makeCommunitiesSocket;
342
+ const extractCommunityMetadata = (result) => {
343
+ var _a, _b, _c, _d, _e;
344
+ const community = getBinaryNodeChild(result, 'group');
345
+ const descChild = getBinaryNodeChild(community, 'description');
346
+ let desc;
347
+ let descId;
348
+ if (descChild) {
349
+ desc = getBinaryNodeChildString(descChild, 'body');
350
+ descId = descChild.attrs.id;
351
+ }
352
+ const mode = community.attrs.addressing_mode;
353
+ const communityId = community.attrs.id.includes('@')
354
+ ? community.attrs.id
355
+ : jidEncode(community.attrs.id, 'g.us');
356
+ const eph = (_a = getBinaryNodeChild(community, 'ephemeral')) === null || _a === void 0 ? void 0 : _a.attrs.expiration;
357
+ const memberAddMode = getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add';
358
+ const metadata = {
359
+ id: communityId,
360
+ subject: community.attrs.subject,
361
+ subjectOwner: community.attrs.s_o,
362
+ subjectOwnerAlt: ((_b = community.attrs) === null || _b === void 0 ? void 0 : _b.s_o_pn) ? community.attrs.s_o_pn : community.attrs.s_o,
363
+ subjectTime: Number(community.attrs.s_t || 0),
364
+ size: Number((_c = community.attrs) === null || _c === void 0 ? void 0 : _c.size
365
+ ? community.attrs.size
366
+ : getBinaryNodeChildren(community, 'participant').length),
367
+ creation: Number(community.attrs.creation || 0),
368
+ owner: community.attrs.creator ? jidNormalizedUser(community.attrs.creator) : undefined,
369
+ ownerAlt: community.attrs.creator
370
+ ? jidNormalizedUser(((_d = community.attrs) === null || _d === void 0 ? void 0 : _d.creator_pn) ? community.attrs.creator_pn : community.attrs.creator)
371
+ : undefined,
372
+ ownerCountry: community.attrs.creator_country_code,
373
+ desc,
374
+ descId,
375
+ linkedParent: ((_e = getBinaryNodeChild(community, 'linked_parent')) === null || _e === void 0 ? void 0 : _e.attrs.jid) || undefined,
376
+ restrict: !!getBinaryNodeChild(community, 'locked'),
377
+ announce: !!getBinaryNodeChild(community, 'announcement'),
378
+ isCommunity: !!getBinaryNodeChild(community, 'parent'),
379
+ isCommunityAnnounce: !!getBinaryNodeChild(community, 'default_sub_group'),
380
+ joinApprovalMode: !!getBinaryNodeChild(community, 'membership_approval_mode'),
381
+ memberAddMode,
382
+ participants: getBinaryNodeChildren(community, 'participant').map(({ attrs }) => ({
383
+ id: mode === WAMessageAddressingMode.LID ? community.attrs.phone_number : attrs.jid,
384
+ lid: mode === WAMessageAddressingMode.LID ? community.attrs.jid : attrs.lid,
385
+ admin: attrs.type || null
386
+ })),
387
+ ephemeralDuration: eph ? Number(eph) : undefined,
388
+ addressingMode: mode
389
+ };
390
+ return metadata;
391
+ };
392
+ exports.extractCommunityMetadata = extractCommunityMetadata;