@gqb333/based 2.7.71

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 (201) hide show
  1. package/LICENSE +58 -0
  2. package/README.MD +611 -0
  3. package/WAProto/GenerateStatics.sh +4 -0
  4. package/WAProto/WAProto.proto +5604 -0
  5. package/WAProto/index.d.ts +63156 -0
  6. package/WAProto/index.js +195638 -0
  7. package/WAProto/p.html +1 -0
  8. package/engine-requirements.js +10 -0
  9. package/lib/Defaults/baileys-version.json +3 -0
  10. package/lib/Defaults/index.d.ts +53 -0
  11. package/lib/Defaults/index.js +108 -0
  12. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  13. package/lib/Signal/Group/ciphertext-message.js +15 -0
  14. package/lib/Signal/Group/group-session-builder.d.ts +14 -0
  15. package/lib/Signal/Group/group-session-builder.js +64 -0
  16. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  17. package/lib/Signal/Group/group_cipher.js +96 -0
  18. package/lib/Signal/Group/index.d.ts +11 -0
  19. package/lib/Signal/Group/index.js +57 -0
  20. package/lib/Signal/Group/keyhelper.d.ts +10 -0
  21. package/lib/Signal/Group/keyhelper.js +55 -0
  22. package/lib/Signal/Group/queue-job.d.ts +1 -0
  23. package/lib/Signal/Group/queue-job.js +57 -0
  24. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  25. package/lib/Signal/Group/sender-chain-key.js +34 -0
  26. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  27. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  28. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  29. package/lib/Signal/Group/sender-key-message.js +69 -0
  30. package/lib/Signal/Group/sender-key-name.d.ts +17 -0
  31. package/lib/Signal/Group/sender-key-name.js +51 -0
  32. package/lib/Signal/Group/sender-key-record.d.ts +30 -0
  33. package/lib/Signal/Group/sender-key-record.js +53 -0
  34. package/lib/Signal/Group/sender-key-state.d.ts +38 -0
  35. package/lib/Signal/Group/sender-key-state.js +99 -0
  36. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  37. package/lib/Signal/Group/sender-message-key.js +29 -0
  38. package/lib/Signal/libsignal.d.ts +3 -0
  39. package/lib/Signal/libsignal.js +174 -0
  40. package/lib/Socket/Client/index.d.ts +2 -0
  41. package/lib/Socket/Client/index.js +18 -0
  42. package/lib/Socket/Client/types.d.ts +16 -0
  43. package/lib/Socket/Client/types.js +13 -0
  44. package/lib/Socket/Client/websocket.d.ts +13 -0
  45. package/lib/Socket/Client/websocket.js +111 -0
  46. package/lib/Socket/business.d.ts +172 -0
  47. package/lib/Socket/business.js +260 -0
  48. package/lib/Socket/chats.d.ts +85 -0
  49. package/lib/Socket/chats.js +1094 -0
  50. package/lib/Socket/groups.d.ts +124 -0
  51. package/lib/Socket/groups.js +423 -0
  52. package/lib/Socket/index.d.ts +172 -0
  53. package/lib/Socket/index.js +32 -0
  54. package/lib/Socket/messages-interactive.js +259 -0
  55. package/lib/Socket/messages-recv.d.ts +161 -0
  56. package/lib/Socket/messages-recv.js +1474 -0
  57. package/lib/Socket/messages-send.d.ts +151 -0
  58. package/lib/Socket/messages-send.js +1085 -0
  59. package/lib/Socket/newsletter.d.ts +136 -0
  60. package/lib/Socket/newsletter.js +250 -0
  61. package/lib/Socket/socket.d.ts +43 -0
  62. package/lib/Socket/socket.js +1241 -0
  63. package/lib/Socket/usync.d.ts +36 -0
  64. package/lib/Socket/usync.js +123 -0
  65. package/lib/Store/index.d.ts +2 -0
  66. package/lib/Store/index.js +8 -0
  67. package/lib/Store/make-in-memory-store.d.ts +118 -0
  68. package/lib/Store/make-in-memory-store.js +452 -0
  69. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  70. package/lib/Store/make-ordered-dictionary.js +81 -0
  71. package/lib/Store/object-repository.d.ts +10 -0
  72. package/lib/Store/object-repository.js +27 -0
  73. package/lib/Types/Auth.d.ts +114 -0
  74. package/lib/Types/Auth.js +2 -0
  75. package/lib/Types/Call.d.ts +13 -0
  76. package/lib/Types/Call.js +2 -0
  77. package/lib/Types/Chat.d.ts +109 -0
  78. package/lib/Types/Chat.js +4 -0
  79. package/lib/Types/Contact.d.ts +29 -0
  80. package/lib/Types/Contact.js +2 -0
  81. package/lib/Types/Events.d.ts +199 -0
  82. package/lib/Types/Events.js +2 -0
  83. package/lib/Types/GroupMetadata.d.ts +64 -0
  84. package/lib/Types/GroupMetadata.js +2 -0
  85. package/lib/Types/Label.d.ts +35 -0
  86. package/lib/Types/Label.js +27 -0
  87. package/lib/Types/LabelAssociation.d.ts +29 -0
  88. package/lib/Types/LabelAssociation.js +9 -0
  89. package/lib/Types/Message.d.ts +740 -0
  90. package/lib/Types/Message.js +7 -0
  91. package/lib/Types/Newsletter.d.ts +22 -0
  92. package/lib/Types/Newsletter.js +18 -0
  93. package/lib/Types/Product.d.ts +78 -0
  94. package/lib/Types/Product.js +2 -0
  95. package/lib/Types/Signal.d.ts +63 -0
  96. package/lib/Types/Signal.js +2 -0
  97. package/lib/Types/Socket.d.ts +134 -0
  98. package/lib/Types/Socket.js +2 -0
  99. package/lib/Types/State.d.ts +27 -0
  100. package/lib/Types/State.js +2 -0
  101. package/lib/Types/USync.d.ts +25 -0
  102. package/lib/Types/USync.js +2 -0
  103. package/lib/Types/index.d.ts +65 -0
  104. package/lib/Types/index.js +43 -0
  105. package/lib/Utils/auth-utils.d.ts +18 -0
  106. package/lib/Utils/auth-utils.js +209 -0
  107. package/lib/Utils/baileys-event-stream.d.ts +16 -0
  108. package/lib/Utils/baileys-event-stream.js +63 -0
  109. package/lib/Utils/business.d.ts +22 -0
  110. package/lib/Utils/business.js +234 -0
  111. package/lib/Utils/cache-manager.d.ts +16 -0
  112. package/lib/Utils/cache-manager.js +114 -0
  113. package/lib/Utils/chat-utils.d.ts +70 -0
  114. package/lib/Utils/chat-utils.js +734 -0
  115. package/lib/Utils/crypto.d.ts +40 -0
  116. package/lib/Utils/crypto.js +193 -0
  117. package/lib/Utils/decode-wa-message.d.ts +35 -0
  118. package/lib/Utils/decode-wa-message.js +207 -0
  119. package/lib/Utils/event-buffer.d.ts +35 -0
  120. package/lib/Utils/event-buffer.js +619 -0
  121. package/lib/Utils/generics.d.ts +89 -0
  122. package/lib/Utils/generics.js +440 -0
  123. package/lib/Utils/history.d.ts +19 -0
  124. package/lib/Utils/history.js +94 -0
  125. package/lib/Utils/index.d.ts +22 -0
  126. package/lib/Utils/index.js +38 -0
  127. package/lib/Utils/jid-validation.d.ts +2 -0
  128. package/lib/Utils/jid-validation.js +186 -0
  129. package/lib/Utils/link-preview.d.ts +21 -0
  130. package/lib/Utils/link-preview.js +152 -0
  131. package/lib/Utils/logger.d.ts +11 -0
  132. package/lib/Utils/logger.js +59 -0
  133. package/lib/Utils/lt-hash.d.ts +12 -0
  134. package/lib/Utils/lt-hash.js +51 -0
  135. package/lib/Utils/make-mutex.d.ts +7 -0
  136. package/lib/Utils/make-mutex.js +43 -0
  137. package/lib/Utils/messages-media.d.ts +120 -0
  138. package/lib/Utils/messages-media.js +848 -0
  139. package/lib/Utils/messages.d.ts +131 -0
  140. package/lib/Utils/messages.js +1793 -0
  141. package/lib/Utils/newsletter-utils.d.ts +2 -0
  142. package/lib/Utils/newsletter-utils.js +48 -0
  143. package/lib/Utils/noise-handler.d.ts +19 -0
  144. package/lib/Utils/noise-handler.js +150 -0
  145. package/lib/Utils/performance-config.d.ts +70 -0
  146. package/lib/Utils/performance-config.js +183 -0
  147. package/lib/Utils/process-message.d.ts +42 -0
  148. package/lib/Utils/process-message.js +498 -0
  149. package/lib/Utils/rate-limiter.js +90 -0
  150. package/lib/Utils/retry.js +66 -0
  151. package/lib/Utils/signal.d.ts +33 -0
  152. package/lib/Utils/signal.js +153 -0
  153. package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
  154. package/lib/Utils/use-multi-file-auth-state.js +129 -0
  155. package/lib/Utils/validate-connection.d.ts +10 -0
  156. package/lib/Utils/validate-connection.js +233 -0
  157. package/lib/WABinary/constants.d.ts +27 -0
  158. package/lib/WABinary/constants.js +1303 -0
  159. package/lib/WABinary/decode.d.ts +6 -0
  160. package/lib/WABinary/decode.js +279 -0
  161. package/lib/WABinary/encode.d.ts +2 -0
  162. package/lib/WABinary/encode.js +264 -0
  163. package/lib/WABinary/generic-utils.d.ts +14 -0
  164. package/lib/WABinary/generic-utils.js +114 -0
  165. package/lib/WABinary/index.d.ts +5 -0
  166. package/lib/WABinary/index.js +21 -0
  167. package/lib/WABinary/jid-utils.d.ts +38 -0
  168. package/lib/WABinary/jid-utils.js +485 -0
  169. package/lib/WABinary/types.d.ts +18 -0
  170. package/lib/WABinary/types.js +2 -0
  171. package/lib/WAM/BinaryInfo.d.ts +8 -0
  172. package/lib/WAM/BinaryInfo.js +13 -0
  173. package/lib/WAM/constants.d.ts +38 -0
  174. package/lib/WAM/constants.js +15350 -0
  175. package/lib/WAM/encode.d.ts +2 -0
  176. package/lib/WAM/encode.js +155 -0
  177. package/lib/WAM/index.d.ts +3 -0
  178. package/lib/WAM/index.js +19 -0
  179. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
  180. package/lib/WAUSync/Protocols/USyncContactProtocol.js +102 -0
  181. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  182. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +110 -0
  183. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  184. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  185. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  186. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  187. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
  188. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  189. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
  190. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  191. package/lib/WAUSync/Protocols/index.d.ts +4 -0
  192. package/lib/WAUSync/Protocols/index.js +20 -0
  193. package/lib/WAUSync/USyncQuery.d.ts +28 -0
  194. package/lib/WAUSync/USyncQuery.js +147 -0
  195. package/lib/WAUSync/USyncUser.d.ts +12 -0
  196. package/lib/WAUSync/USyncUser.js +26 -0
  197. package/lib/WAUSync/index.d.ts +3 -0
  198. package/lib/WAUSync/index.js +19 -0
  199. package/lib/index.d.ts +17 -0
  200. package/lib/index.js +53 -0
  201. package/package.json +104 -0
@@ -0,0 +1,124 @@
1
+ import { proto } from '../../WAProto';
2
+ import { GroupMetadata, ParticipantAction, SocketConfig, WAMessageKey } from '../Types';
3
+ import { BinaryNode } from '../WABinary';
4
+ export declare const makeGroupsSocket: (config: SocketConfig) => {
5
+ groupQuery: (jid: string, type: "get" | "set", content: BinaryNode[]) => Promise<any>;
6
+ groupMetadata: (jid: string) => Promise<GroupMetadata>;
7
+ groupCreate: (subject: string, participants: string[]) => Promise<GroupMetadata>;
8
+ groupLeave: (id: string) => Promise<void>;
9
+ groupUpdateSubject: (jid: string, subject: string) => Promise<void>;
10
+ groupRequestParticipantsList: (jid: string) => Promise<{
11
+ [key: string]: string;
12
+ }[]>;
13
+ groupRequestParticipantsUpdate: (jid: string, participants: string[], action: "approve" | "reject") => Promise<{
14
+ status: string;
15
+ jid: string;
16
+ }[]>;
17
+ groupParticipantsUpdate: (jid: string, participants: string[], action: ParticipantAction) => Promise<{
18
+ status: string;
19
+ jid: string;
20
+ content: BinaryNode;
21
+ }[]>;
22
+ groupUpdateDescription: (jid: string, description?: string) => Promise<void>;
23
+ groupInviteCode: (jid: string) => Promise<string | undefined>;
24
+ groupRevokeInvite: (jid: string) => Promise<string | undefined>;
25
+ groupAcceptInvite: (code: string) => Promise<string | undefined>;
26
+ /**
27
+ * revoke a v4 invite for someone
28
+ * @param groupJid group jid
29
+ * @param invitedJid jid of person you invited
30
+ * @returns true if successful
31
+ */
32
+ groupRevokeInviteV4: (groupJid: string, invitedJid: string) => Promise<boolean>;
33
+ /**
34
+ * accept a GroupInviteMessage
35
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
36
+ * @param inviteMessage the message to accept
37
+ */
38
+ groupAcceptInviteV4: (key: string | WAMessageKey, inviteMessage: proto.Message.IGroupInviteMessage) => Promise<any>;
39
+ groupGetInviteInfo: (code: string) => Promise<GroupMetadata>;
40
+ groupToggleEphemeral: (jid: string, ephemeralExpiration: number) => Promise<void>;
41
+ groupSettingUpdate: (jid: string, setting: "announcement" | "not_announcement" | "locked" | "unlocked") => Promise<void>;
42
+ groupMemberAddMode: (jid: string, mode: "admin_add" | "all_member_add") => Promise<void>;
43
+ groupJoinApprovalMode: (jid: string, mode: "on" | "off") => Promise<void>;
44
+ groupFetchAllParticipating: () => Promise<{
45
+ [_: string]: GroupMetadata;
46
+ }>;
47
+ getBotListV2: () => Promise<import("../Types").BotListInfo[]>;
48
+ processingMutex: {
49
+ mutex<T>(code: () => Promise<T> | T): Promise<T>;
50
+ };
51
+ fetchPrivacySettings: (force?: boolean) => Promise<{
52
+ [_: string]: string;
53
+ }>;
54
+ upsertMessage: (msg: import("../Types").WAMessage, type: import("../Types").MessageUpsertType) => Promise<void>;
55
+ appPatch: (patchCreate: import("../Types").WAPatchCreate) => Promise<void>;
56
+ sendPresenceUpdate: (type: import("../Types").WAPresence, toJid?: string) => Promise<void>;
57
+ presenceSubscribe: (toJid: string, tcToken?: Buffer) => Promise<void>;
58
+ profilePictureUrl: (jid: string, type?: "preview" | "image", timeoutMs?: number) => Promise<string | undefined>;
59
+ onWhatsApp: (...jids: string[]) => Promise<{
60
+ jid: string;
61
+ exists: unknown;
62
+ lid: unknown;
63
+ }[] | undefined>;
64
+ fetchBlocklist: () => Promise<string[]>;
65
+ fetchDisappearingDuration: (...jids: string[]) => Promise<import("..").USyncQueryResultList[] | undefined>;
66
+ fetchStatus: (...jids: string[]) => Promise<import("..").USyncQueryResultList[] | undefined>;
67
+ updateProfilePicture: (jid: string, content: import("../Types").WAMediaUpload) => Promise<void>;
68
+ removeProfilePicture: (jid: string) => Promise<void>;
69
+ updateProfileStatus: (status: string) => Promise<void>;
70
+ updateProfileName: (name: string) => Promise<void>;
71
+ updateBlockStatus: (jid: string, action: "block" | "unblock") => Promise<void>;
72
+ updateCallPrivacy: (value: import("../Types").WAPrivacyCallValue) => Promise<void>;
73
+ updateMessagesPrivacy: (value: import("../Types").WAPrivacyMessagesValue) => Promise<void>;
74
+ updateLastSeenPrivacy: (value: import("../Types").WAPrivacyValue) => Promise<void>;
75
+ updateOnlinePrivacy: (value: import("../Types").WAPrivacyOnlineValue) => Promise<void>;
76
+ updateProfilePicturePrivacy: (value: import("../Types").WAPrivacyValue) => Promise<void>;
77
+ updateStatusPrivacy: (value: import("../Types").WAPrivacyValue) => Promise<void>;
78
+ updateReadReceiptsPrivacy: (value: import("../Types").WAReadReceiptsValue) => Promise<void>;
79
+ updateGroupsAddPrivacy: (value: import("../Types").WAPrivacyGroupAddValue) => Promise<void>;
80
+ updateDefaultDisappearingMode: (duration: number) => Promise<void>;
81
+ getBusinessProfile: (jid: string) => Promise<import("../Types").WABusinessProfile | void>;
82
+ resyncAppState: (collections: readonly ("critical_unblock_low" | "regular_high" | "regular_low" | "critical_block" | "regular")[], isInitialSync: boolean) => Promise<void>;
83
+ chatModify: (mod: import("../Types").ChatModification, jid: string) => Promise<void>;
84
+ cleanDirtyBits: (type: "account_sync" | "groups", fromTimestamp?: number | string) => Promise<void>;
85
+ addChatLabel: (jid: string, labelId: string) => Promise<void>;
86
+ removeChatLabel: (jid: string, labelId: string) => Promise<void>;
87
+ addMessageLabel: (jid: string, messageId: string, labelId: string) => Promise<void>;
88
+ removeMessageLabel: (jid: string, messageId: string, labelId: string) => Promise<void>;
89
+ star: (jid: string, messages: {
90
+ id: string;
91
+ fromMe?: boolean;
92
+ }[], star: boolean) => Promise<void>;
93
+ executeUSyncQuery: (usyncQuery: import("..").USyncQuery) => Promise<import("..").USyncQueryResult | undefined>;
94
+ type: "md";
95
+ ws: import("./Client").WebSocketClient;
96
+ ev: import("../Types").BaileysEventEmitter & {
97
+ process(handler: (events: Partial<import("../Types").BaileysEventMap>) => void | Promise<void>): (() => void);
98
+ buffer(): void;
99
+ createBufferedFunction<A extends any[], T>(work: (...args: A) => Promise<T>): ((...args: A) => Promise<T>);
100
+ flush(force?: boolean): boolean;
101
+ isBuffering(): boolean;
102
+ };
103
+ authState: {
104
+ creds: import("../Types").AuthenticationCreds;
105
+ keys: import("../Types").SignalKeyStoreWithTransaction;
106
+ };
107
+ signalRepository: import("../Types").SignalRepository;
108
+ user: import("../Types").Contact | undefined;
109
+ generateMessageTag: () => string;
110
+ query: (node: BinaryNode, timeoutMs?: number) => Promise<any>;
111
+ waitForMessage: <T>(msgId: string, timeoutMs?: number | undefined) => Promise<any>;
112
+ waitForSocketOpen: () => Promise<void>;
113
+ sendRawMessage: (data: Uint8Array | Buffer) => Promise<void>;
114
+ sendNode: (frame: BinaryNode) => Promise<void>;
115
+ logout: (msg?: string) => Promise<void>;
116
+ end: (error: Error | undefined) => void;
117
+ onUnexpectedError: (err: Error | import("@hapi/boom").Boom, msg: string) => void;
118
+ uploadPreKeys: (count?: number) => Promise<void>;
119
+ uploadPreKeysToServerIfRequired: () => Promise<void>;
120
+ requestPairingCode: (phoneNumber: any, pairKey?: string) => Promise<string>;
121
+ waitForConnectionUpdate: (check: (u: Partial<import("../Types").ConnectionState>) => Promise<boolean | undefined>, timeoutMs?: number) => Promise<void>;
122
+ sendWAMBuffer: (wamBuffer: Buffer) => Promise<any>;
123
+ };
124
+ export declare const extractGroupMetadata: (result: BinaryNode) => GroupMetadata;
@@ -0,0 +1,423 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractGroupMetadata = exports.makeGroupsSocket = void 0;
4
+ const WAProto_1 = require("../../WAProto");
5
+ const Types_1 = require("../Types");
6
+ const Utils_1 = require("../Utils");
7
+ const WABinary_1 = require("../WABinary");
8
+ const chats_1 = require("./chats");
9
+ const { getPerformanceConfig } = require('../Utils/performance-config');
10
+ const { CacheManager } = require('../Utils/cache-manager');
11
+ const makeGroupsSocket = (config) => {
12
+ const sock = (0, chats_1.makeChatsSocket)(config);
13
+ const { authState, ev, query, upsertMessage } = sock;
14
+ const inFlightGroupMetadata = new Map();
15
+ const forbiddenGroupMetadataUntil = new Map();
16
+ const resolveJid = WABinary_1.resolveJid;
17
+ const groupQuery = 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 groupMetadata = async (jid) => {
27
+ const config = getPerformanceConfig();
28
+
29
+ const forbiddenUntil = forbiddenGroupMetadataUntil.get(jid);
30
+ if (forbiddenUntil && forbiddenUntil > Date.now()) {
31
+ const err = new Error('forbidden');
32
+ err.data = 403;
33
+ throw err;
34
+ }
35
+ else if (forbiddenUntil) {
36
+ forbiddenGroupMetadataUntil.delete(jid);
37
+ }
38
+
39
+ // Check cache first
40
+ const cached = CacheManager.get('groupMetadataCache', jid);
41
+ if (cached) {
42
+ config.logger?.trace({ jid }, 'using cached group metadata');
43
+ return cached;
44
+ }
45
+
46
+ // If a fetch is already in progress for this group, reuse it
47
+ const inFlight = inFlightGroupMetadata.get(jid);
48
+ if (inFlight) {
49
+ return inFlight;
50
+ }
51
+
52
+ const fetchPromise = (async () => {
53
+ // Fetch from server with rate limiting and retry logic
54
+ let retryCount = 0;
55
+ const maxRetries = 3;
56
+ const baseDelay = 1000; // 1 second
57
+
58
+ while (retryCount <= maxRetries) {
59
+ try {
60
+ const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
61
+ const metadata = (0, exports.extractGroupMetadata)(result);
62
+
63
+ // Cache the result
64
+ CacheManager.set('groupMetadataCache', jid, metadata, config.cache.groupMetadataCache.ttl);
65
+
66
+ return metadata;
67
+ } catch (error) {
68
+ const statusCode = error?.output?.statusCode || error?.data;
69
+ if (statusCode === 403 || error?.message === 'forbidden') {
70
+ const cooldownMs = 5 * 60 * 1000;
71
+ forbiddenGroupMetadataUntil.set(jid, Date.now() + cooldownMs);
72
+ throw error;
73
+ }
74
+ if (error.message === 'rate-overlimit' && retryCount < maxRetries) {
75
+ const delay = baseDelay * Math.pow(2, retryCount); // Exponential backoff
76
+ config.logger?.warn({ jid, retryCount, delay }, 'Rate limit hit, retrying group metadata fetch');
77
+ await new Promise(resolve => setTimeout(resolve, delay));
78
+ retryCount++;
79
+ } else {
80
+ // Re-throw non-rate-limit errors or if max retries exceeded
81
+ throw error;
82
+ }
83
+ }
84
+ }
85
+ })();
86
+
87
+ inFlightGroupMetadata.set(jid, fetchPromise);
88
+ try {
89
+ return await fetchPromise;
90
+ } finally {
91
+ inFlightGroupMetadata.delete(jid);
92
+ }
93
+ };
94
+ const groupFetchAllParticipating = async () => {
95
+ const result = await query({
96
+ tag: 'iq',
97
+ attrs: {
98
+ to: '@g.us',
99
+ xmlns: 'w:g2',
100
+ type: 'get',
101
+ },
102
+ content: [
103
+ {
104
+ tag: 'participating',
105
+ attrs: {},
106
+ content: [
107
+ { tag: 'participants', attrs: {} },
108
+ { tag: 'description', attrs: {} }
109
+ ]
110
+ }
111
+ ]
112
+ });
113
+ const data = {};
114
+ const groupsChild = (0, WABinary_1.getBinaryNodeChild)(result, 'groups');
115
+ if (groupsChild) {
116
+ const groups = (0, WABinary_1.getBinaryNodeChildren)(groupsChild, 'group');
117
+ for (const groupNode of groups) {
118
+ const meta = (0, exports.extractGroupMetadata)({
119
+ tag: 'result',
120
+ attrs: {},
121
+ content: [groupNode]
122
+ });
123
+ data[meta.id] = meta;
124
+ }
125
+ }
126
+ sock.ev.emit('groups.update', Object.values(data));
127
+ return data;
128
+ };
129
+ sock.ws.on('CB:ib,,dirty', async (node) => {
130
+ const { attrs } = (0, WABinary_1.getBinaryNodeChild)(node, 'dirty');
131
+ if (attrs.type !== 'groups') {
132
+ return;
133
+ }
134
+ await groupFetchAllParticipating();
135
+ await sock.cleanDirtyBits('groups');
136
+ });
137
+
138
+ // Cache invalidation on group events
139
+ ev.on('groups.update', async (groups) => {
140
+ for (const group of groups) {
141
+ CacheManager.del('groupMetadataCache', group.id);
142
+ }
143
+ });
144
+
145
+ ev.on('group-participants.update', async (update) => {
146
+ CacheManager.del('groupMetadataCache', update.id);
147
+ });
148
+
149
+ ev.on('groups.upsert', async (groups) => {
150
+ for (const group of groups) {
151
+ CacheManager.del('groupMetadataCache', group.id);
152
+ }
153
+ });
154
+ return {
155
+ ...sock,
156
+ groupQuery,
157
+ groupMetadata,
158
+ groupCreate: async (subject, participants) => {
159
+ participants = participants.map(resolveJid);
160
+ const key = (0, Utils_1.generateMessageIDV2)();
161
+ const result = await groupQuery('@g.us', 'set', [
162
+ {
163
+ tag: 'create',
164
+ attrs: {
165
+ subject,
166
+ key
167
+ },
168
+ content: participants.map(jid => ({
169
+ tag: 'participant',
170
+ attrs: { jid }
171
+ }))
172
+ }
173
+ ]);
174
+ return (0, exports.extractGroupMetadata)(result);
175
+ },
176
+ groupLeave: async (id) => {
177
+ await groupQuery('@g.us', 'set', [
178
+ {
179
+ tag: 'leave',
180
+ attrs: {},
181
+ content: [
182
+ { tag: 'group', attrs: { id } }
183
+ ]
184
+ }
185
+ ]);
186
+ },
187
+ groupUpdateSubject: async (jid, subject) => {
188
+ await groupQuery(jid, 'set', [
189
+ {
190
+ tag: 'subject',
191
+ attrs: {},
192
+ content: Buffer.from(subject, 'utf-8')
193
+ }
194
+ ]);
195
+ },
196
+ groupRequestParticipantsList: async (jid) => {
197
+ const result = await groupQuery(jid, 'get', [
198
+ {
199
+ tag: 'membership_approval_requests',
200
+ attrs: {}
201
+ }
202
+ ]);
203
+ const node = (0, WABinary_1.getBinaryNodeChild)(result, 'membership_approval_requests');
204
+ const participants = (0, WABinary_1.getBinaryNodeChildren)(node, 'membership_approval_request');
205
+ return participants.map(v => v.attrs);
206
+ },
207
+ groupRequestParticipantsUpdate: async (jid, participants, action) => {
208
+ participants = participants.map(resolveJid);
209
+ const result = await groupQuery(jid, 'set', [{
210
+ tag: 'membership_requests_action',
211
+ attrs: {},
212
+ content: [
213
+ {
214
+ tag: action,
215
+ attrs: {},
216
+ content: participants.map(jid => ({
217
+ tag: 'participant',
218
+ attrs: { jid }
219
+ }))
220
+ }
221
+ ]
222
+ }]);
223
+ const node = (0, WABinary_1.getBinaryNodeChild)(result, 'membership_requests_action');
224
+ const nodeAction = (0, WABinary_1.getBinaryNodeChild)(node, action);
225
+ const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(nodeAction, 'participant');
226
+ return participantsAffected.map(p => {
227
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
228
+ });
229
+ },
230
+ groupParticipantsUpdate: async (jid, participants, action) => {
231
+ participants = participants.map(resolveJid);
232
+ const result = await groupQuery(jid, 'set', [
233
+ {
234
+ tag: action,
235
+ attrs: {},
236
+ content: participants.map(jid => ({
237
+ tag: 'participant',
238
+ attrs: { jid }
239
+ }))
240
+ }
241
+ ]);
242
+ const node = (0, WABinary_1.getBinaryNodeChild)(result, action);
243
+ const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(node, 'participant');
244
+ return participantsAffected.map(p => {
245
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
246
+ });
247
+ },
248
+ groupUpdateDescription: async (jid, description) => {
249
+ var _a;
250
+ const metadata = await groupMetadata(jid);
251
+ const prev = (_a = metadata.descId) !== null && _a !== void 0 ? _a : null;
252
+ await groupQuery(jid, 'set', [
253
+ {
254
+ tag: 'description',
255
+ attrs: {
256
+ ...(description ? { id: (0, Utils_1.generateMessageIDV2)() } : { delete: 'true' }),
257
+ ...(prev ? { prev } : {})
258
+ },
259
+ content: description ? [
260
+ { tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }
261
+ ] : undefined
262
+ }
263
+ ]);
264
+ },
265
+ groupInviteCode: async (jid) => {
266
+ const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
267
+ const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
268
+ return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
269
+ },
270
+ groupRevokeInvite: async (jid) => {
271
+ const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
272
+ const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
273
+ return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
274
+ },
275
+ groupAcceptInvite: async (code) => {
276
+ const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
277
+ const result = (0, WABinary_1.getBinaryNodeChild)(results, 'group');
278
+ return result === null || result === void 0 ? void 0 : result.attrs.jid;
279
+ },
280
+ /**
281
+ * revoke a v4 invite for someone
282
+ * @param groupJid group jid
283
+ * @param invitedJid jid of person you invited
284
+ * @returns true if successful
285
+ */
286
+ groupRevokeInviteV4: async (groupJid, invitedJid) => {
287
+ invitedJid = resolveJid(invitedJid);
288
+ const result = await groupQuery(groupJid, 'set', [{ tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }]);
289
+ return !!result;
290
+ },
291
+ /**
292
+ * accept a GroupInviteMessage
293
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
294
+ * @param inviteMessage the message to accept
295
+ */
296
+ groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
297
+ var _a;
298
+ key = typeof key === 'string' ? { remoteJid: key } : key;
299
+ key.remoteJid = resolveJid(key.remoteJid);
300
+ const results = await groupQuery(inviteMessage.groupJid, 'set', [{
301
+ tag: 'accept',
302
+ attrs: {
303
+ code: inviteMessage.inviteCode,
304
+ expiration: inviteMessage.inviteExpiration.toString(),
305
+ admin: key.remoteJid
306
+ }
307
+ }]);
308
+ // if we have the full message key
309
+ // update the invite message to be expired
310
+ if (key.id) {
311
+ // create new invite message that is expired
312
+ inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);
313
+ inviteMessage.inviteExpiration = 0;
314
+ inviteMessage.inviteCode = '';
315
+ ev.emit('messages.update', [
316
+ {
317
+ key,
318
+ update: {
319
+ message: {
320
+ groupInviteMessage: inviteMessage
321
+ }
322
+ }
323
+ }
324
+ ]);
325
+ }
326
+ // generate the group add message
327
+ await upsertMessage({
328
+ key: {
329
+ remoteJid: inviteMessage.groupJid,
330
+ id: (0, Utils_1.generateMessageIDV2)((_a = sock.user) === null || _a === void 0 ? void 0 : _a.id),
331
+ fromMe: false,
332
+ participant: key.remoteJid,
333
+ },
334
+ messageStubType: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,
335
+ messageStubParameters: [
336
+ authState.creds.me.id
337
+ ],
338
+ participant: key.remoteJid,
339
+ messageTimestamp: (0, Utils_1.unixTimestampSeconds)()
340
+ }, 'notify');
341
+ return results.attrs.from;
342
+ }),
343
+ groupGetInviteInfo: async (code) => {
344
+ const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
345
+ return (0, exports.extractGroupMetadata)(results);
346
+ },
347
+ groupToggleEphemeral: async (jid, ephemeralExpiration) => {
348
+ const content = ephemeralExpiration ?
349
+ { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } :
350
+ { tag: 'not_ephemeral', attrs: {} };
351
+ await groupQuery(jid, 'set', [content]);
352
+ },
353
+ groupSettingUpdate: async (jid, setting) => {
354
+ await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
355
+ },
356
+ groupMemberAddMode: async (jid, mode) => {
357
+ await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
358
+ },
359
+ groupJoinApprovalMode: async (jid, mode) => {
360
+ await groupQuery(jid, 'set', [{ tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }]);
361
+ },
362
+ groupFetchAllParticipating
363
+ };
364
+ };
365
+ exports.makeGroupsSocket = makeGroupsSocket;
366
+ const extractGroupMetadata = (result) => {
367
+ var _a, _b;
368
+ const group = (0, WABinary_1.getBinaryNodeChild)(result, 'group');
369
+ const descChild = (0, WABinary_1.getBinaryNodeChild)(group, 'description');
370
+ let desc;
371
+ let descId;
372
+ let descOwner;
373
+ let descOwnerLid;
374
+ let descTime;
375
+ if (descChild) {
376
+ desc = (0, WABinary_1.getBinaryNodeChildString)(descChild, 'body');
377
+ descOwner = (0, WABinary_1.jidNormalizedUser)(descChild.attrs.participant_pn || descChild.attrs.participant);
378
+ if (group.attrs.addressing_mode === 'lid') {
379
+ descOwnerLid = (0, WABinary_1.jidNormalizedUser)(descChild.attrs.participant);
380
+ }
381
+ descId = descChild.attrs.id;
382
+ descTime = descChild.attrs.t ? +descChild.attrs.t : undefined;
383
+ }
384
+ const groupSize = group.attrs.size ? Number(group.attrs.size) : undefined;
385
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : (0, WABinary_1.jidEncode)(group.attrs.id, 'g.us');
386
+ const eph = (_a = (0, WABinary_1.getBinaryNodeChild)(group, 'ephemeral')) === null || _a === void 0 ? void 0 : _a.attrs.expiration;
387
+ const memberAddMode = (0, WABinary_1.getBinaryNodeChildString)(group, 'member_add_mode') === 'all_member_add';
388
+ const metadata = {
389
+ id: groupId,
390
+ addressingMode: group.attrs.addressing_mode,
391
+ subject: group.attrs.subject,
392
+ subjectOwner: (0, WABinary_1.jidNormalizedUser)(group.attrs.s_o_pn || group.attrs.s_o),
393
+ ...(group.attrs.addressing_mode === 'lid' ? { subjectOwnerLid: (0, WABinary_1.jidNormalizedUser)(group.attrs.s_o) } : {}),
394
+ subjectTime: group.attrs.s_t ? +group.attrs.s_t : undefined,
395
+ size: groupSize || (0, WABinary_1.getBinaryNodeChildren)(group, 'participant').length,
396
+ creation: group.attrs.creation ? +group.attrs.creation : undefined,
397
+ owner: (0, WABinary_1.jidNormalizedUser)(group.attrs.creator_pn || group.attrs.creator),
398
+ ...(group.attrs.addressing_mode === 'lid' ? { ownerLid: (0, WABinary_1.jidNormalizedUser)(group.attrs.creator) } : {}),
399
+ desc,
400
+ descId,
401
+ descOwner,
402
+ descOwnerLid,
403
+ descTime,
404
+ linkedParent: ((_b = (0, WABinary_1.getBinaryNodeChild)(group, 'linked_parent')) === null || _b === void 0 ? void 0 : _b.attrs.jid) || undefined,
405
+ restrict: !!(0, WABinary_1.getBinaryNodeChild)(group, 'locked'),
406
+ announce: !!(0, WABinary_1.getBinaryNodeChild)(group, 'announcement'),
407
+ isCommunity: !!(0, WABinary_1.getBinaryNodeChild)(group, 'parent'),
408
+ isCommunityAnnounce: !!(0, WABinary_1.getBinaryNodeChild)(group, 'default_sub_group'),
409
+ joinApprovalMode: !!(0, WABinary_1.getBinaryNodeChild)(group, 'membership_approval_mode'),
410
+ memberAddMode,
411
+ participants: (0, WABinary_1.getBinaryNodeChildren)(group, 'participant').map(({ attrs }) => {
412
+ return {
413
+ id: attrs.jid,
414
+ jid: attrs.phone_number || (0, WABinary_1.lidToJid)(attrs.jid),
415
+ lid: attrs.lid || ((0, WABinary_1.isLid)(attrs.jid) ? attrs.jid : undefined),
416
+ admin: (attrs.type || null),
417
+ };
418
+ }),
419
+ ephemeralDuration: eph ? +eph : undefined
420
+ };
421
+ return metadata;
422
+ };
423
+ exports.extractGroupMetadata = extractGroupMetadata;