@crysnovax/baileys 2.6.6 → 2.6.8

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.
@@ -267,7 +267,13 @@ export const makeGroupsSocket = (config) => {
267
267
  { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
268
268
  ]);
269
269
  },
270
- groupFetchAllParticipating
270
+ groupFetchAllParticipating,
271
+ // crysnovax@GroupStatus --- Delete a group status by its message key.
272
+ // Pass the key object from the groupStatusMessageV2 you want to remove.
273
+ // Usage: await sock.deleteGroupStatus(jid, message.key)
274
+ deleteGroupStatus: async (jid, key) => {
275
+ await sock.sendMessage(jid, { delete: key });
276
+ }
271
277
  };
272
278
  };
273
279
  export const extractGroupMetadata = (result) => {
@@ -0,0 +1,347 @@
1
+ import { Boom } from '@hapi/boom';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { WAMessageAddressingMode, WAMessageStubType } from '../Types/index.js';
4
+ import { generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
5
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isLidUser, isPnUser, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
6
+ import { makeChatsSocket } from './chats.js';
7
+ export const makeGroupsSocket = (config) => {
8
+ const sock = makeChatsSocket(config);
9
+ const { authState, ev, query, upsertMessage } = sock;
10
+ const groupQuery = async (jid, type, content) => query({
11
+ tag: 'iq',
12
+ attrs: {
13
+ type,
14
+ xmlns: 'w:g2',
15
+ to: jid
16
+ },
17
+ content
18
+ });
19
+ const groupMetadata = async (jid) => {
20
+ const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
21
+ return extractGroupMetadata(result);
22
+ };
23
+ const groupFetchAllParticipating = async () => {
24
+ const result = await query({
25
+ tag: 'iq',
26
+ attrs: {
27
+ to: '@g.us',
28
+ xmlns: 'w:g2',
29
+ type: 'get'
30
+ },
31
+ content: [
32
+ {
33
+ tag: 'participating',
34
+ attrs: {},
35
+ content: [
36
+ { tag: 'participants', attrs: {} },
37
+ { tag: 'description', attrs: {} }
38
+ ]
39
+ }
40
+ ]
41
+ });
42
+ const data = {};
43
+ const groupsChild = getBinaryNodeChild(result, 'groups');
44
+ if (groupsChild) {
45
+ const groups = getBinaryNodeChildren(groupsChild, 'group');
46
+ for (const groupNode of groups) {
47
+ const meta = extractGroupMetadata({
48
+ tag: 'result',
49
+ attrs: {},
50
+ content: [groupNode]
51
+ });
52
+ data[meta.id] = meta;
53
+ }
54
+ }
55
+ // TODO: properly parse LID / PN DATA
56
+ sock.ev.emit('groups.update', Object.values(data));
57
+ return data;
58
+ };
59
+ sock.ws.on('CB:ib,,dirty', async (node) => {
60
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
61
+ if (attrs.type !== 'groups') {
62
+ return;
63
+ }
64
+ await groupFetchAllParticipating();
65
+ await sock.cleanDirtyBits('groups');
66
+ });
67
+ return {
68
+ ...sock,
69
+ groupQuery,
70
+ groupMetadata,
71
+ groupCreate: async (subject, participants) => {
72
+ const key = generateMessageIDV2();
73
+ const result = await groupQuery('@g.us', 'set', [
74
+ {
75
+ tag: 'create',
76
+ attrs: {
77
+ subject,
78
+ key
79
+ },
80
+ content: participants.map(jid => ({
81
+ tag: 'participant',
82
+ attrs: { jid }
83
+ }))
84
+ }
85
+ ]);
86
+ return extractGroupMetadata(result);
87
+ },
88
+ groupLeave: async (id) => {
89
+ await groupQuery('@g.us', 'set', [
90
+ {
91
+ tag: 'leave',
92
+ attrs: {},
93
+ content: [{ tag: 'group', attrs: { id } }]
94
+ }
95
+ ]);
96
+ },
97
+ groupUpdateSubject: async (jid, subject) => {
98
+ await groupQuery(jid, 'set', [
99
+ {
100
+ tag: 'subject',
101
+ attrs: {},
102
+ content: Buffer.from(subject, 'utf-8')
103
+ }
104
+ ]);
105
+ },
106
+ groupRequestParticipantsList: async (jid) => {
107
+ const result = await groupQuery(jid, 'get', [
108
+ {
109
+ tag: 'membership_approval_requests',
110
+ attrs: {}
111
+ }
112
+ ]);
113
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
114
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
115
+ return participants.map(v => v.attrs);
116
+ },
117
+ groupRequestParticipantsUpdate: async (jid, participants, action) => {
118
+ const result = await groupQuery(jid, 'set', [
119
+ {
120
+ tag: 'membership_requests_action',
121
+ attrs: {},
122
+ content: [
123
+ {
124
+ tag: action,
125
+ attrs: {},
126
+ content: participants.map(jid => ({
127
+ tag: 'participant',
128
+ attrs: { jid }
129
+ }))
130
+ }
131
+ ]
132
+ }
133
+ ]);
134
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
135
+ const nodeAction = getBinaryNodeChild(node, action);
136
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
137
+ return participantsAffected.map(p => {
138
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
139
+ });
140
+ },
141
+ groupParticipantsUpdate: async (jid, participants, action) => {
142
+ const result = await groupQuery(jid, 'set', [
143
+ {
144
+ tag: action,
145
+ attrs: {},
146
+ content: participants.map(jid => ({
147
+ tag: 'participant',
148
+ attrs: { jid }
149
+ }))
150
+ }
151
+ ]);
152
+ const node = getBinaryNodeChild(result, action);
153
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
154
+ return participantsAffected.map(p => {
155
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
156
+ });
157
+ },
158
+ groupUpdateDescription: async (jid, description) => {
159
+ const metadata = await groupMetadata(jid);
160
+ const prev = metadata.descId ?? null;
161
+ await groupQuery(jid, 'set', [
162
+ {
163
+ tag: 'description',
164
+ attrs: {
165
+ ...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
166
+ ...(prev ? { prev } : {})
167
+ },
168
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
169
+ }
170
+ ]);
171
+ },
172
+ groupInviteCode: async (jid) => {
173
+ const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
174
+ const inviteNode = getBinaryNodeChild(result, 'invite');
175
+ return inviteNode?.attrs.code;
176
+ },
177
+ groupRevokeInvite: async (jid) => {
178
+ const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
179
+ const inviteNode = getBinaryNodeChild(result, 'invite');
180
+ return inviteNode?.attrs.code;
181
+ },
182
+ groupAcceptInvite: async (code) => {
183
+ const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
184
+ const result = getBinaryNodeChild(results, 'group');
185
+ return result?.attrs.jid;
186
+ },
187
+ /**
188
+ * revoke a v4 invite for someone
189
+ * @param groupJid group jid
190
+ * @param invitedJid jid of person you invited
191
+ * @returns true if successful
192
+ */
193
+ groupRevokeInviteV4: async (groupJid, invitedJid) => {
194
+ const result = await groupQuery(groupJid, 'set', [
195
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
196
+ ]);
197
+ return !!result;
198
+ },
199
+ /**
200
+ * accept a GroupInviteMessage
201
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
202
+ * @param inviteMessage the message to accept
203
+ */
204
+ groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
205
+ key = typeof key === 'string' ? { remoteJid: key } : key;
206
+ const results = await groupQuery(inviteMessage.groupJid, 'set', [
207
+ {
208
+ tag: 'accept',
209
+ attrs: {
210
+ code: inviteMessage.inviteCode,
211
+ expiration: inviteMessage.inviteExpiration.toString(),
212
+ admin: key.remoteJid
213
+ }
214
+ }
215
+ ]);
216
+ // if we have the full message key
217
+ // update the invite message to be expired
218
+ if (key.id) {
219
+ // create new invite message that is expired
220
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
221
+ inviteMessage.inviteExpiration = 0;
222
+ inviteMessage.inviteCode = '';
223
+ ev.emit('messages.update', [
224
+ {
225
+ key,
226
+ update: {
227
+ message: {
228
+ groupInviteMessage: inviteMessage
229
+ }
230
+ }
231
+ }
232
+ ]);
233
+ }
234
+ // generate the group add message
235
+ await upsertMessage({
236
+ key: {
237
+ remoteJid: inviteMessage.groupJid,
238
+ id: generateMessageIDV2(sock.user?.id),
239
+ fromMe: false,
240
+ participant: key.remoteJid
241
+ },
242
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
243
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
244
+ participant: key.remoteJid,
245
+ messageTimestamp: unixTimestampSeconds()
246
+ }, 'notify');
247
+ return results.attrs.from;
248
+ }),
249
+ groupGetInviteInfo: async (code) => {
250
+ const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
251
+ return extractGroupMetadata(results);
252
+ },
253
+ groupToggleEphemeral: async (jid, ephemeralExpiration) => {
254
+ const content = ephemeralExpiration
255
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
256
+ : { tag: 'not_ephemeral', attrs: {} };
257
+ await groupQuery(jid, 'set', [content]);
258
+ },
259
+ groupSettingUpdate: async (jid, setting) => {
260
+ await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
261
+ },
262
+ groupMemberAddMode: async (jid, mode) => {
263
+ await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
264
+ },
265
+ groupJoinApprovalMode: async (jid, mode) => {
266
+ await groupQuery(jid, 'set', [
267
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
268
+ ]);
269
+ },
270
+ groupFetchAllParticipating
271
+ };
272
+ };
273
+ export const extractGroupMetadata = (result) => {
274
+ const group = getBinaryNodeChild(result, 'group');
275
+ if (!group) {
276
+ // Mirror WAWeb: surface server/client errors with their code+text instead of crashing.
277
+ const errorNode = getBinaryNodeChild(result, 'error');
278
+ if (errorNode) {
279
+ const code = errorNode.attrs.code ? +errorNode.attrs.code : 500;
280
+ const text = errorNode.attrs.text || 'group metadata query failed';
281
+ throw new Boom(text, { statusCode: code, data: errorNode });
282
+ }
283
+ throw new Boom('Invalid group metadata response: missing <group> node', { data: result });
284
+ }
285
+ if (!group.attrs.id) {
286
+ throw new Boom('Invalid group metadata response: missing group id', { data: group });
287
+ }
288
+ const descChild = getBinaryNodeChild(group, 'description');
289
+ let desc;
290
+ let descId;
291
+ let descOwner;
292
+ let descOwnerPn;
293
+ let descOwnerUsername;
294
+ let descTime;
295
+ if (descChild) {
296
+ desc = getBinaryNodeChildString(descChild, 'body');
297
+ descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
298
+ descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
299
+ descOwnerUsername = descChild.attrs.participant_username || undefined;
300
+ descTime = +descChild.attrs.t;
301
+ descId = descChild.attrs.id;
302
+ }
303
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
304
+ const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
305
+ const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
306
+ const metadata = {
307
+ id: groupId,
308
+ notify: group.attrs.notify,
309
+ addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
310
+ subject: group.attrs.subject,
311
+ subjectOwner: group.attrs.s_o,
312
+ subjectOwnerPn: group.attrs.s_o_pn,
313
+ subjectOwnerUsername: group.attrs.s_o_username,
314
+ subjectTime: +group.attrs.s_t,
315
+ size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
316
+ creation: +group.attrs.creation,
317
+ owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
318
+ ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
319
+ ownerUsername: group.attrs.creator_username || undefined,
320
+ owner_country_code: group.attrs.creator_country_code,
321
+ desc,
322
+ descId,
323
+ descOwner,
324
+ descOwnerPn,
325
+ descOwnerUsername,
326
+ descTime,
327
+ linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
328
+ restrict: !!getBinaryNodeChild(group, 'locked'),
329
+ announce: !!getBinaryNodeChild(group, 'announcement'),
330
+ isCommunity: !!getBinaryNodeChild(group, 'parent'),
331
+ isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
332
+ joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
333
+ memberAddMode,
334
+ participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
335
+ // TODO: Store LID MAPPINGS
336
+ return {
337
+ id: attrs.jid,
338
+ phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
339
+ lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
340
+ username: attrs.participant_username || attrs.username || undefined,
341
+ admin: (attrs.type || null)
342
+ };
343
+ }),
344
+ ephemeralDuration: eph ? +eph : undefined
345
+ };
346
+ return metadata;
347
+ };