@vinzsocket/baileys 2.8.10 → 2.8.30

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