jagproject 26.3.22 → 26.3.25

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