@prototypevip/baileys 0.0.1

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