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