@x4r1s3/baileys 0.0.0-stage → 1.0.0

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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1262 -2
  3. package/SECURITY.md +47 -0
  4. package/WAProto/V +1 -0
  5. package/WAProto/WAProto.proto +8892 -0
  6. package/WAProto/index.d.ts +104128 -0
  7. package/WAProto/index.js +334920 -0
  8. package/assets/rolling-circle.svg +133 -0
  9. package/assets/x4r1s3-banner.svg +32 -0
  10. package/engine-requirements.js +27 -0
  11. package/lib/Builders/A2UI.js +245 -0
  12. package/lib/Builders/AIRich.js +179 -0
  13. package/lib/Builders/Button.js +108 -0
  14. package/lib/Builders/CFC.js +284 -0
  15. package/lib/Builders/Carousel.js +81 -0
  16. package/lib/Call/THIRD_PARTY_LICENSE.txt +21 -0
  17. package/lib/Call/audio-feeder.mjs +179 -0
  18. package/lib/Call/audio-recorder.mjs +250 -0
  19. package/lib/Call/fetch-wasm-resources.mjs +104 -0
  20. package/lib/Call/index.mjs +680 -0
  21. package/lib/Call/relay-transport.mjs +563 -0
  22. package/lib/Call/signaling.mjs +641 -0
  23. package/lib/Call/types.mjs +17 -0
  24. package/lib/Call/video-feeder.mjs +94 -0
  25. package/lib/Call/wasm-engine.mjs +1372 -0
  26. package/lib/Call/worker-bootstrap.mjs +1042 -0
  27. package/lib/Defaults/ban-checker.js +56 -0
  28. package/lib/Defaults/index.js +170 -0
  29. package/lib/Signal/Group/ciphertext-message.js +23 -0
  30. package/lib/Signal/Group/group-session-builder.js +41 -0
  31. package/lib/Signal/Group/group_cipher.js +93 -0
  32. package/lib/Signal/Group/index.js +23 -0
  33. package/lib/Signal/Group/keyhelper.js +29 -0
  34. package/lib/Signal/Group/sender-chain-key.js +37 -0
  35. package/lib/Signal/Group/sender-key-distribution-message.js +74 -0
  36. package/lib/Signal/Group/sender-key-message.js +77 -0
  37. package/lib/Signal/Group/sender-key-name.js +59 -0
  38. package/lib/Signal/Group/sender-key-record.js +52 -0
  39. package/lib/Signal/Group/sender-key-state.js +95 -0
  40. package/lib/Signal/Group/sender-message-key.js +37 -0
  41. package/lib/Signal/libsignal.js +440 -0
  42. package/lib/Signal/lid-mapping.js +292 -0
  43. package/lib/Socket/Client/index.js +14 -0
  44. package/lib/Socket/Client/types.js +22 -0
  45. package/lib/Socket/Client/websocket.js +65 -0
  46. package/lib/Socket/aigroups.js +327 -0
  47. package/lib/Socket/ban-checker.js +165 -0
  48. package/lib/Socket/business.js +419 -0
  49. package/lib/Socket/chats.js +1921 -0
  50. package/lib/Socket/communities.js +443 -0
  51. package/lib/Socket/graphql.js +911 -0
  52. package/lib/Socket/groups.js +561 -0
  53. package/lib/Socket/index.js +49 -0
  54. package/lib/Socket/interactive-handler.js +612 -0
  55. package/lib/Socket/interop.js +575 -0
  56. package/lib/Socket/managed-account.js +220 -0
  57. package/lib/Socket/messages-recv.js +2191 -0
  58. package/lib/Socket/messages-send.js +4104 -0
  59. package/lib/Socket/mex.js +53 -0
  60. package/lib/Socket/newsletter.js +291 -0
  61. package/lib/Socket/privacy.js +458 -0
  62. package/lib/Socket/registration.js +440 -0
  63. package/lib/Socket/socket.js +1029 -0
  64. package/lib/Socket/status-actions.js +287 -0
  65. package/lib/Socket/username.js +241 -0
  66. package/lib/Store/index.js +16 -0
  67. package/lib/Store/make-cache-manager-store.js +101 -0
  68. package/lib/Store/make-in-memory-store.js +432 -0
  69. package/lib/Store/make-ordered-dictionary.js +90 -0
  70. package/lib/Store/object-repository.js +35 -0
  71. package/lib/Types/Auth.js +13 -0
  72. package/lib/Types/Bussines.js +13 -0
  73. package/lib/Types/Call.js +13 -0
  74. package/lib/Types/Chat.js +19 -0
  75. package/lib/Types/Contact.js +13 -0
  76. package/lib/Types/Events.js +13 -0
  77. package/lib/Types/GroupMetadata.js +13 -0
  78. package/lib/Types/Label.js +36 -0
  79. package/lib/Types/LabelAssociation.js +18 -0
  80. package/lib/Types/Message.js +30 -0
  81. package/lib/Types/Mex.js +53 -0
  82. package/lib/Types/Product.js +13 -0
  83. package/lib/Types/RichType.js +34 -0
  84. package/lib/Types/Signal.js +13 -0
  85. package/lib/Types/Socket.js +14 -0
  86. package/lib/Types/State.js +60 -0
  87. package/lib/Types/USync.js +13 -0
  88. package/lib/Types/index.js +37 -0
  89. package/lib/Utils/auth-utils.js +313 -0
  90. package/lib/Utils/ban-checker.js +48 -0
  91. package/lib/Utils/bot-capabilities.js +96 -0
  92. package/lib/Utils/bot-planning-replay.js +177 -0
  93. package/lib/Utils/browser-utils.js +40 -0
  94. package/lib/Utils/business.js +242 -0
  95. package/lib/Utils/c2582.js +147 -0
  96. package/lib/Utils/chat-utils.js +905 -0
  97. package/lib/Utils/companion-reg-client-utils.js +44 -0
  98. package/lib/Utils/consumer-application.js +114 -0
  99. package/lib/Utils/crypto.js +129 -0
  100. package/lib/Utils/decode-wa-message.js +496 -0
  101. package/lib/Utils/direct-chat-theme.js +164 -0
  102. package/lib/Utils/event-buffer.js +601 -0
  103. package/lib/Utils/experimental-proto.js +206 -0
  104. package/lib/Utils/games/miniapp.js +17 -0
  105. package/lib/Utils/games/slot-machine.js +270 -0
  106. package/lib/Utils/games/website-preview.js +306 -0
  107. package/lib/Utils/generics.js +402 -0
  108. package/lib/Utils/group-history.js +68 -0
  109. package/lib/Utils/group-wallpaper.js +168 -0
  110. package/lib/Utils/history.js +162 -0
  111. package/lib/Utils/identity-change-handler.js +60 -0
  112. package/lib/Utils/index.js +61 -0
  113. package/lib/Utils/jid-display-normalization.js +221 -0
  114. package/lib/Utils/link-preview.js +96 -0
  115. package/lib/Utils/logger.js +14 -0
  116. package/lib/Utils/lt-hash.js +19 -0
  117. package/lib/Utils/make-mutex.js +44 -0
  118. package/lib/Utils/message-composer.js +709 -0
  119. package/lib/Utils/message-inspect.js +79 -0
  120. package/lib/Utils/message-retry-manager.js +253 -0
  121. package/lib/Utils/message-security.js +167 -0
  122. package/lib/Utils/messages-media.js +1027 -0
  123. package/lib/Utils/messages.js +2467 -0
  124. package/lib/Utils/meta-ai-msmsg.js +215 -0
  125. package/lib/Utils/meta-compositing.js +239 -0
  126. package/lib/Utils/native-flow.js +158 -0
  127. package/lib/Utils/newsletter-live-update-utils.js +74 -0
  128. package/lib/Utils/newsletter-status-utils.js +152 -0
  129. package/lib/Utils/noise-handler.js +212 -0
  130. package/lib/Utils/offline-node-processor.js +51 -0
  131. package/lib/Utils/poll-manager.js +206 -0
  132. package/lib/Utils/portable-compat.js +12 -0
  133. package/lib/Utils/pre-key-manager.js +117 -0
  134. package/lib/Utils/process-message.js +610 -0
  135. package/lib/Utils/reporting-utils.js +269 -0
  136. package/lib/Utils/rich-message-utils.js +2841 -0
  137. package/lib/Utils/scheduled-message.js +107 -0
  138. package/lib/Utils/signal.js +212 -0
  139. package/lib/Utils/source-grep.js +57 -0
  140. package/lib/Utils/stanza-ack.js +49 -0
  141. package/lib/Utils/sticker-annotation.js +87 -0
  142. package/lib/Utils/sync-action-utils.js +59 -0
  143. package/lib/Utils/tc-token-utils.js +174 -0
  144. package/lib/Utils/use-multi-file-auth-state.js +132 -0
  145. package/lib/Utils/use-single-file-auth-state.js +108 -0
  146. package/lib/Utils/use-sqlite-auth-state.js +120 -0
  147. package/lib/Utils/validate-connection.js +220 -0
  148. package/lib/Utils/view-once-utils.js +218 -0
  149. package/lib/Utils/voip-rekey.js +32 -0
  150. package/lib/Utils/website-preview.js +27 -0
  151. package/lib/Utils/welcome-flow.js +228 -0
  152. package/lib/Utils/x4r1s3.js +144 -0
  153. package/lib/WABinary/constants.js +1475 -0
  154. package/lib/WABinary/decode.js +273 -0
  155. package/lib/WABinary/encode.js +231 -0
  156. package/lib/WABinary/generic-utils.js +242 -0
  157. package/lib/WABinary/index.js +17 -0
  158. package/lib/WABinary/jid-utils.js +107 -0
  159. package/lib/WABinary/types.js +13 -0
  160. package/lib/WAM/BinaryInfo.js +21 -0
  161. package/lib/WAM/constants.js +22864 -0
  162. package/lib/WAM/encode.js +161 -0
  163. package/lib/WAM/index.js +15 -0
  164. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +108 -0
  165. package/lib/WAUSync/Protocols/USyncContactProtocol.js +68 -0
  166. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
  167. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +38 -0
  168. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +82 -0
  169. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +40 -0
  170. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +37 -0
  171. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
  172. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +46 -0
  173. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +36 -0
  174. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -0
  175. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +40 -0
  176. package/lib/WAUSync/Protocols/index.js +24 -0
  177. package/lib/WAUSync/USyncQuery.js +213 -0
  178. package/lib/WAUSync/USyncUser.js +66 -0
  179. package/lib/WAUSync/index.js +15 -0
  180. package/lib/antiban.cjs +4726 -0
  181. package/lib/index.d.ts +1285 -0
  182. package/lib/index.js +30 -0
  183. package/package.json +125 -4
@@ -0,0 +1,443 @@
1
+ /**
2
+ * @X4R1S3
3
+ *
4
+ * File: lib/Socket/communities.js
5
+ * About: Communities module for the X4R1S3 WhatsApp protocol client.
6
+ * Important exports: makeCommunitiesSocket, extractCommunityMetadata
7
+ * Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
8
+ *
9
+ * Copyright and attribution notice.
10
+ * Do not copy, reproduce, redistribute, modify, or create derivative works
11
+ * from this source without explicit written permission.
12
+ */
13
+ import { proto } from '../../WAProto/index.js';
14
+ import { WAMessageStubType } from '../Types/index.js';
15
+ import { generateMessageID, generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
16
+ import logger from '../Utils/logger.js';
17
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
18
+ import { makeBusinessSocket } from './business.js';
19
+ export const makeCommunitiesSocket = (config) => {
20
+ const sock = makeBusinessSocket(config);
21
+ const { authState, ev, query, upsertMessage } = sock;
22
+ const communityQuery = async (jid, type, content) => query({
23
+ tag: 'iq',
24
+ attrs: {
25
+ type,
26
+ xmlns: 'w:g2',
27
+ to: jid
28
+ },
29
+ content
30
+ });
31
+ const communityMetadata = async (jid) => {
32
+ const result = await communityQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
33
+ return extractCommunityMetadata(result);
34
+ };
35
+ const communityFetchAllParticipating = async () => {
36
+ const result = await query({
37
+ tag: 'iq',
38
+ attrs: {
39
+ to: '@g.us',
40
+ xmlns: 'w:g2',
41
+ type: 'get'
42
+ },
43
+ content: [
44
+ {
45
+ tag: 'participating',
46
+ attrs: {},
47
+ content: [
48
+ { tag: 'participants', attrs: {} },
49
+ { tag: 'description', attrs: {} }
50
+ ]
51
+ }
52
+ ]
53
+ });
54
+ const data = {};
55
+ const communitiesChild = getBinaryNodeChild(result, 'communities');
56
+ if (communitiesChild) {
57
+ const communities = getBinaryNodeChildren(communitiesChild, 'community');
58
+ for (const communityNode of communities) {
59
+ const meta = extractCommunityMetadata({
60
+ tag: 'result',
61
+ attrs: {},
62
+ content: [communityNode]
63
+ });
64
+ data[meta.id] = meta;
65
+ }
66
+ }
67
+ sock.ev.emit('groups.update', Object.values(data));
68
+ return data;
69
+ };
70
+ async function parseGroupResult(node) {
71
+ logger.info({ node }, 'parseGroupResult');
72
+ const groupNode = getBinaryNodeChild(node, 'group');
73
+ if (groupNode) {
74
+ try {
75
+ logger.info({ groupNode }, 'groupNode');
76
+ const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`);
77
+ return metadata ? metadata : Optional.empty();
78
+ }
79
+ catch (error) {
80
+ console.error('Error parsing group metadata:', error);
81
+ return Optional.empty();
82
+ }
83
+ }
84
+ return Optional.empty();
85
+ }
86
+ const Optional = {
87
+ empty: () => null,
88
+ of: (value) => (value !== null ? { value } : null)
89
+ };
90
+ sock.ws.on('CB:ib,,dirty', async (node) => {
91
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
92
+ if (attrs.type !== 'communities') {
93
+ return;
94
+ }
95
+ await communityFetchAllParticipating();
96
+ await sock.cleanDirtyBits('groups');
97
+ });
98
+ return {
99
+ ...sock,
100
+ communityQuery,
101
+ communityMetadata,
102
+ communityCreate: async (subject, body) => {
103
+ const descriptionId = generateMessageID().substring(0, 12);
104
+ const result = await communityQuery('@g.us', 'set', [
105
+ {
106
+ tag: 'create',
107
+ attrs: { subject },
108
+ content: [
109
+ {
110
+ tag: 'description',
111
+ attrs: { id: descriptionId },
112
+ content: [
113
+ {
114
+ tag: 'body',
115
+ attrs: {},
116
+ content: Buffer.from(body || '', 'utf-8')
117
+ }
118
+ ]
119
+ },
120
+ {
121
+ tag: 'parent',
122
+ attrs: { default_membership_approval_mode: 'request_required' }
123
+ },
124
+ {
125
+ tag: 'allow_non_admin_sub_group_creation',
126
+ attrs: {}
127
+ },
128
+ {
129
+ tag: 'create_general_chat',
130
+ attrs: {}
131
+ }
132
+ ]
133
+ }
134
+ ]);
135
+ return await parseGroupResult(result);
136
+ },
137
+ communityCreateGroup: async (subject, participants, parentCommunityJid) => {
138
+ const key = generateMessageIDV2();
139
+ const result = await communityQuery('@g.us', 'set', [
140
+ {
141
+ tag: 'create',
142
+ attrs: {
143
+ subject,
144
+ key
145
+ },
146
+ content: [
147
+ ...participants.map(jid => ({
148
+ tag: 'participant',
149
+ attrs: { jid }
150
+ })),
151
+ { tag: 'linked_parent', attrs: { jid: parentCommunityJid } }
152
+ ]
153
+ }
154
+ ]);
155
+ return await parseGroupResult(result);
156
+ },
157
+ communityLeave: async (id) => {
158
+ await communityQuery('@g.us', 'set', [
159
+ {
160
+ tag: 'leave',
161
+ attrs: {},
162
+ content: [{ tag: 'community', attrs: { id } }]
163
+ }
164
+ ]);
165
+ },
166
+ communityUpdateSubject: async (jid, subject) => {
167
+ await communityQuery(jid, 'set', [
168
+ {
169
+ tag: 'subject',
170
+ attrs: {},
171
+ content: Buffer.from(subject, 'utf-8')
172
+ }
173
+ ]);
174
+ },
175
+ communityLinkGroup: async (groupJid, parentCommunityJid) => {
176
+ await communityQuery(parentCommunityJid, 'set', [
177
+ {
178
+ tag: 'links',
179
+ attrs: {},
180
+ content: [
181
+ {
182
+ tag: 'link',
183
+ attrs: { link_type: 'sub_group' },
184
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
185
+ }
186
+ ]
187
+ }
188
+ ]);
189
+ },
190
+ communityUnlinkGroup: async (groupJid, parentCommunityJid) => {
191
+ await communityQuery(parentCommunityJid, 'set', [
192
+ {
193
+ tag: 'unlink',
194
+ attrs: { unlink_type: 'sub_group' },
195
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
196
+ }
197
+ ]);
198
+ },
199
+ communityFetchLinkedGroups: async (jid) => {
200
+ let communityJid = jid;
201
+ let isCommunity = false;
202
+ // Try to determine if it is a subgroup or a community
203
+ const metadata = await sock.groupMetadata(jid);
204
+ if (metadata.linkedParent) {
205
+ // It is a subgroup, get the community jid
206
+ communityJid = metadata.linkedParent;
207
+ }
208
+ else {
209
+ // It is a community
210
+ isCommunity = true;
211
+ }
212
+ // Fetch all subgroups of the community
213
+ const result = await communityQuery(communityJid, 'get', [{ tag: 'sub_groups', attrs: {} }]);
214
+ const linkedGroupsData = [];
215
+ const subGroupsNode = getBinaryNodeChild(result, 'sub_groups');
216
+ if (subGroupsNode) {
217
+ const groupNodes = getBinaryNodeChildren(subGroupsNode, 'group');
218
+ for (const groupNode of groupNodes) {
219
+ linkedGroupsData.push({
220
+ id: groupNode.attrs.id ? jidEncode(groupNode.attrs.id, 'g.us') : undefined,
221
+ subject: groupNode.attrs.subject || '',
222
+ creation: groupNode.attrs.creation ? Number(groupNode.attrs.creation) : undefined,
223
+ owner: groupNode.attrs.creator ? jidNormalizedUser(groupNode.attrs.creator) : undefined,
224
+ size: groupNode.attrs.size ? Number(groupNode.attrs.size) : undefined
225
+ });
226
+ }
227
+ }
228
+ return {
229
+ communityJid,
230
+ isCommunity,
231
+ linkedGroups: linkedGroupsData
232
+ };
233
+ },
234
+ communityRequestParticipantsList: async (jid) => {
235
+ const result = await communityQuery(jid, 'get', [
236
+ {
237
+ tag: 'membership_approval_requests',
238
+ attrs: {}
239
+ }
240
+ ]);
241
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
242
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
243
+ return participants.map(v => v.attrs);
244
+ },
245
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
246
+ const result = await communityQuery(jid, 'set', [
247
+ {
248
+ tag: 'membership_requests_action',
249
+ attrs: {},
250
+ content: [
251
+ {
252
+ tag: action,
253
+ attrs: {},
254
+ content: participants.map(jid => ({
255
+ tag: 'participant',
256
+ attrs: { jid }
257
+ }))
258
+ }
259
+ ]
260
+ }
261
+ ]);
262
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
263
+ const nodeAction = getBinaryNodeChild(node, action);
264
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
265
+ return participantsAffected.map(p => {
266
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
267
+ });
268
+ },
269
+ communityParticipantsUpdate: async (jid, participants, action) => {
270
+ const result = await communityQuery(jid, 'set', [
271
+ {
272
+ tag: action,
273
+ attrs: action === 'remove' ? { linked_groups: 'true' } : {},
274
+ content: participants.map(jid => ({
275
+ tag: 'participant',
276
+ attrs: { jid }
277
+ }))
278
+ }
279
+ ]);
280
+ const node = getBinaryNodeChild(result, action);
281
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
282
+ return participantsAffected.map(p => {
283
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
284
+ });
285
+ },
286
+ communityUpdateDescription: async (jid, description) => {
287
+ const metadata = await communityMetadata(jid);
288
+ const prev = metadata.descId ?? null;
289
+ await communityQuery(jid, 'set', [
290
+ {
291
+ tag: 'description',
292
+ attrs: {
293
+ ...(description ? { id: generateMessageID() } : { delete: 'true' }),
294
+ ...(prev ? { prev } : {})
295
+ },
296
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
297
+ }
298
+ ]);
299
+ },
300
+ communityInviteCode: async (jid) => {
301
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
302
+ const inviteNode = getBinaryNodeChild(result, 'invite');
303
+ return inviteNode?.attrs.code;
304
+ },
305
+ communityRevokeInvite: async (jid) => {
306
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
307
+ const inviteNode = getBinaryNodeChild(result, 'invite');
308
+ return inviteNode?.attrs.code;
309
+ },
310
+ communityAcceptInvite: async (code) => {
311
+ const results = await communityQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
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', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
324
+ ]);
325
+ return !!result;
326
+ },
327
+ /**
328
+ * accept a CommunityInviteMessage
329
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
330
+ * @param inviteMessage the message to accept
331
+ */
332
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
333
+ key = typeof key === 'string' ? { remoteJid: key } : key;
334
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [
335
+ {
336
+ tag: 'accept',
337
+ attrs: {
338
+ code: inviteMessage.inviteCode,
339
+ expiration: inviteMessage.inviteExpiration.toString(),
340
+ admin: key.remoteJid
341
+ }
342
+ }
343
+ ]);
344
+ // if we have the full message key
345
+ // update the invite message to be expired
346
+ if (key.id) {
347
+ // create new invite message that is expired
348
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
349
+ inviteMessage.inviteExpiration = 0;
350
+ inviteMessage.inviteCode = '';
351
+ ev.emit('messages.update', [
352
+ {
353
+ key,
354
+ update: {
355
+ message: {
356
+ groupInviteMessage: inviteMessage
357
+ }
358
+ }
359
+ }
360
+ ]);
361
+ }
362
+ // generate the community add message
363
+ await upsertMessage({
364
+ key: {
365
+ remoteJid: inviteMessage.groupJid,
366
+ id: generateMessageIDV2(sock.user?.id),
367
+ fromMe: false,
368
+ participant: key.remoteJid // TODO: investigate if this makes any sense at all
369
+ },
370
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
371
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
372
+ participant: key.remoteJid,
373
+ messageTimestamp: unixTimestampSeconds()
374
+ }, 'notify');
375
+ return results.attrs.from;
376
+ }),
377
+ communityGetInviteInfo: async (code) => {
378
+ const results = await communityQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
379
+ return extractCommunityMetadata(results);
380
+ },
381
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
382
+ const content = ephemeralExpiration
383
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
384
+ : { tag: 'not_ephemeral', attrs: {} };
385
+ await communityQuery(jid, 'set', [content]);
386
+ },
387
+ communitySettingUpdate: async (jid, setting) => {
388
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
389
+ },
390
+ communityMemberAddMode: async (jid, mode) => {
391
+ await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
392
+ },
393
+ communityJoinApprovalMode: async (jid, mode) => {
394
+ await communityQuery(jid, 'set', [
395
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'community_join', attrs: { state: mode } }] }
396
+ ]);
397
+ },
398
+ communityFetchAllParticipating
399
+ };
400
+ };
401
+ export const extractCommunityMetadata = (result) => {
402
+ const community = getBinaryNodeChild(result, 'community');
403
+ const descChild = getBinaryNodeChild(community, 'description');
404
+ let desc;
405
+ let descId;
406
+ if (descChild) {
407
+ desc = getBinaryNodeChildString(descChild, 'body');
408
+ descId = descChild.attrs.id;
409
+ }
410
+ const communityId = community.attrs.id?.includes('@')
411
+ ? community.attrs.id
412
+ : jidEncode(community.attrs.id || '', 'g.us');
413
+ const eph = getBinaryNodeChild(community, 'ephemeral')?.attrs.expiration;
414
+ const memberAddMode = getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add';
415
+ const metadata = {
416
+ id: communityId,
417
+ subject: community.attrs.subject || '',
418
+ subjectOwner: community.attrs.s_o,
419
+ subjectTime: Number(community.attrs.s_t || 0),
420
+ size: getBinaryNodeChildren(community, 'participant').length,
421
+ creation: Number(community.attrs.creation || 0),
422
+ owner: community.attrs.creator ? jidNormalizedUser(community.attrs.creator) : undefined,
423
+ desc,
424
+ descId,
425
+ linkedParent: getBinaryNodeChild(community, 'linked_parent')?.attrs.jid || undefined,
426
+ restrict: !!getBinaryNodeChild(community, 'locked'),
427
+ announce: !!getBinaryNodeChild(community, 'announcement'),
428
+ isCommunity: !!getBinaryNodeChild(community, 'parent'),
429
+ isCommunityAnnounce: !!getBinaryNodeChild(community, 'default_sub_community'),
430
+ joinApprovalMode: !!getBinaryNodeChild(community, 'membership_approval_mode'),
431
+ memberAddMode,
432
+ participants: getBinaryNodeChildren(community, 'participant').map(({ attrs }) => {
433
+ return {
434
+ // TODO: IMPLEMENT THE PN/LID FIELDS HERE!!
435
+ id: attrs.jid,
436
+ admin: (attrs.type || null)
437
+ };
438
+ }),
439
+ ephemeralDuration: eph ? +eph : undefined,
440
+ addressingMode: getBinaryNodeChildString(community, 'addressing_mode')
441
+ };
442
+ return metadata;
443
+ };