astrabail 1.0.6

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 (224) hide show
  1. package/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/WAProto.proto +5479 -0
  3. package/WAProto/fix-imports.js +73 -0
  4. package/WAProto/index.d.ts +14017 -0
  5. package/WAProto/index.js +89351 -0
  6. package/WAProto/p.html +1 -0
  7. package/engine-requirements.js +7 -0
  8. package/lib/Defaults/astrabail-version.json +8 -0
  9. package/lib/Defaults/index.d.ts +74 -0
  10. package/lib/Defaults/index.js +166 -0
  11. package/lib/Defaults/phonenumber-mcc.json +223 -0
  12. package/lib/Defaults/update-wa-version.js +75 -0
  13. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  14. package/lib/Signal/Group/ciphertext-message.js +15 -0
  15. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  16. package/lib/Signal/Group/group-session-builder.js +66 -0
  17. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  18. package/lib/Signal/Group/group_cipher.js +85 -0
  19. package/lib/Signal/Group/index.d.ts +12 -0
  20. package/lib/Signal/Group/index.js +57 -0
  21. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  22. package/lib/Signal/Group/keyhelper.js +55 -0
  23. package/lib/Signal/Group/queue-job.js +57 -0
  24. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  25. package/lib/Signal/Group/sender-chain-key.js +29 -0
  26. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  27. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  28. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  29. package/lib/Signal/Group/sender-key-message.js +69 -0
  30. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  31. package/lib/Signal/Group/sender-key-name.js +51 -0
  32. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  33. package/lib/Signal/Group/sender-key-record.js +44 -0
  34. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  35. package/lib/Signal/Group/sender-key-state.js +87 -0
  36. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  37. package/lib/Signal/Group/sender-message-key.js +29 -0
  38. package/lib/Signal/libsignal.d.ts +5 -0
  39. package/lib/Signal/libsignal.js +478 -0
  40. package/lib/Signal/lid-mapping.d.ts +19 -0
  41. package/lib/Signal/lid-mapping.js +274 -0
  42. package/lib/Socket/Client/index.d.ts +3 -0
  43. package/lib/Socket/Client/index.js +18 -0
  44. package/lib/Socket/Client/types.d.ts +16 -0
  45. package/lib/Socket/Client/types.js +14 -0
  46. package/lib/Socket/Client/websocket.d.ts +13 -0
  47. package/lib/Socket/Client/websocket.js +60 -0
  48. package/lib/Socket/business.d.ts +202 -0
  49. package/lib/Socket/business.js +382 -0
  50. package/lib/Socket/chats.d.ts +111 -0
  51. package/lib/Socket/chats.js +1155 -0
  52. package/lib/Socket/communities.d.ts +258 -0
  53. package/lib/Socket/communities.js +438 -0
  54. package/lib/Socket/community.js +333 -0
  55. package/lib/Socket/groups.d.ts +150 -0
  56. package/lib/Socket/groups.js +470 -0
  57. package/lib/Socket/index.d.ts +245 -0
  58. package/lib/Socket/index.js +13 -0
  59. package/lib/Socket/messages-recv.d.ts +187 -0
  60. package/lib/Socket/messages-recv.js +1708 -0
  61. package/lib/Socket/messages-send.d.ts +183 -0
  62. package/lib/Socket/messages-send.js +1692 -0
  63. package/lib/Socket/mex.d.ts +3 -0
  64. package/lib/Socket/mex.js +45 -0
  65. package/lib/Socket/newsletter.d.ts +160 -0
  66. package/lib/Socket/newsletter.js +293 -0
  67. package/lib/Socket/socket.d.ts +55 -0
  68. package/lib/Socket/socket.js +947 -0
  69. package/lib/Socket/usync.js +70 -0
  70. package/lib/Store/index.js +20 -0
  71. package/lib/Store/make-cache-manager-store.js +83 -0
  72. package/lib/Store/make-in-memory-store.js +398 -0
  73. package/lib/Store/make-ordered-dictionary.js +81 -0
  74. package/lib/Store/object-repository.js +27 -0
  75. package/lib/Types/Auth.d.ts +116 -0
  76. package/lib/Types/Auth.js +2 -0
  77. package/lib/Types/Bussines.d.ts +25 -0
  78. package/lib/Types/Bussines.js +2 -0
  79. package/lib/Types/Call.d.ts +15 -0
  80. package/lib/Types/Call.js +2 -0
  81. package/lib/Types/Chat.d.ts +123 -0
  82. package/lib/Types/Chat.js +10 -0
  83. package/lib/Types/Contact.d.ts +24 -0
  84. package/lib/Types/Contact.js +2 -0
  85. package/lib/Types/Events.d.ts +237 -0
  86. package/lib/Types/Events.js +3 -0
  87. package/lib/Types/GroupMetadata.d.ts +67 -0
  88. package/lib/Types/GroupMetadata.js +2 -0
  89. package/lib/Types/Label.d.ts +47 -0
  90. package/lib/Types/Label.js +25 -0
  91. package/lib/Types/LabelAssociation.d.ts +30 -0
  92. package/lib/Types/LabelAssociation.js +7 -0
  93. package/lib/Types/Message.d.ts +305 -0
  94. package/lib/Types/Message.js +11 -0
  95. package/lib/Types/MexUpdates.js +11 -0
  96. package/lib/Types/Newsletter.d.ts +135 -0
  97. package/lib/Types/Newsletter.js +43 -0
  98. package/lib/Types/Product.d.ts +79 -0
  99. package/lib/Types/Product.js +2 -0
  100. package/lib/Types/Signal.d.ts +76 -0
  101. package/lib/Types/Signal.js +3 -0
  102. package/lib/Types/Socket.d.ts +133 -0
  103. package/lib/Types/Socket.js +3 -0
  104. package/lib/Types/State.d.ts +39 -0
  105. package/lib/Types/State.js +14 -0
  106. package/lib/Types/USync.d.ts +26 -0
  107. package/lib/Types/USync.js +3 -0
  108. package/lib/Types/index.d.ts +65 -0
  109. package/lib/Types/index.js +42 -0
  110. package/lib/Utils/astrabail-event-stream.js +3 -0
  111. package/lib/Utils/audioToBuffer.js +31 -0
  112. package/lib/Utils/auth-utils.d.ts +19 -0
  113. package/lib/Utils/auth-utils.js +298 -0
  114. package/lib/Utils/baileys-event-stream.js +60 -0
  115. package/lib/Utils/bridge-runtime.d.ts +1 -0
  116. package/lib/Utils/bridge-runtime.js +14 -0
  117. package/lib/Utils/browser-utils.d.ts +4 -0
  118. package/lib/Utils/browser-utils.js +44 -0
  119. package/lib/Utils/business.d.ts +23 -0
  120. package/lib/Utils/business.js +240 -0
  121. package/lib/Utils/chat-utils.d.ts +70 -0
  122. package/lib/Utils/chat-utils.js +825 -0
  123. package/lib/Utils/crypto.d.ts +37 -0
  124. package/lib/Utils/crypto.js +168 -0
  125. package/lib/Utils/decode-wa-message.d.ts +48 -0
  126. package/lib/Utils/decode-wa-message.js +287 -0
  127. package/lib/Utils/event-buffer.d.ts +34 -0
  128. package/lib/Utils/event-buffer.js +580 -0
  129. package/lib/Utils/generics.d.ts +91 -0
  130. package/lib/Utils/generics.js +483 -0
  131. package/lib/Utils/history.d.ts +22 -0
  132. package/lib/Utils/history.js +137 -0
  133. package/lib/Utils/identity-change-handler.d.ts +37 -0
  134. package/lib/Utils/identity-change-handler.js +54 -0
  135. package/lib/Utils/index.d.ts +22 -0
  136. package/lib/Utils/index.js +48 -0
  137. package/lib/Utils/link-preview.d.ts +21 -0
  138. package/lib/Utils/link-preview.js +121 -0
  139. package/lib/Utils/logger.d.ts +13 -0
  140. package/lib/Utils/logger.js +7 -0
  141. package/lib/Utils/lt-hash.d.ts +8 -0
  142. package/lib/Utils/lt-hash.js +10 -0
  143. package/lib/Utils/make-mutex.d.ts +9 -0
  144. package/lib/Utils/make-mutex.js +37 -0
  145. package/lib/Utils/message-retry-manager.d.ts +110 -0
  146. package/lib/Utils/message-retry-manager.js +226 -0
  147. package/lib/Utils/messages-media.d.ts +130 -0
  148. package/lib/Utils/messages-media.js +887 -0
  149. package/lib/Utils/messages-newsletter.d.ts +84 -0
  150. package/lib/Utils/messages-newsletter.js +295 -0
  151. package/lib/Utils/messages.d.ts +92 -0
  152. package/lib/Utils/messages.js +1752 -0
  153. package/lib/Utils/noise-handler.d.ts +20 -0
  154. package/lib/Utils/noise-handler.js +204 -0
  155. package/lib/Utils/pre-key-manager.d.ts +28 -0
  156. package/lib/Utils/pre-key-manager.js +112 -0
  157. package/lib/Utils/process-message.d.ts +60 -0
  158. package/lib/Utils/process-message.js +536 -0
  159. package/lib/Utils/reporting-utils.d.ts +11 -0
  160. package/lib/Utils/reporting-utils.js +262 -0
  161. package/lib/Utils/resolve-jid.d.ts +43 -0
  162. package/lib/Utils/resolve-jid.js +95 -0
  163. package/lib/Utils/rust-bridge-shim.d.ts +22 -0
  164. package/lib/Utils/rust-bridge-shim.js +70 -0
  165. package/lib/Utils/serial-task-queue.js +29 -0
  166. package/lib/Utils/signal.d.ts +34 -0
  167. package/lib/Utils/signal.js +170 -0
  168. package/lib/Utils/streamToBuffer.js +17 -0
  169. package/lib/Utils/sync-action-utils.d.ts +19 -0
  170. package/lib/Utils/sync-action-utils.js +52 -0
  171. package/lib/Utils/tc-token-utils.d.ts +12 -0
  172. package/lib/Utils/tc-token-utils.js +20 -0
  173. package/lib/Utils/use-mongo-file-auth-state.js +71 -0
  174. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  175. package/lib/Utils/use-multi-file-auth-state.js +124 -0
  176. package/lib/Utils/use-single-file-auth-state.js +73 -0
  177. package/lib/Utils/validate-connection.d.ts +11 -0
  178. package/lib/Utils/validate-connection.js +206 -0
  179. package/lib/WABinary/constants.d.ts +28 -0
  180. package/lib/WABinary/constants.js +1303 -0
  181. package/lib/WABinary/decode.d.ts +7 -0
  182. package/lib/WABinary/decode.js +300 -0
  183. package/lib/WABinary/encode.d.ts +3 -0
  184. package/lib/WABinary/encode.js +256 -0
  185. package/lib/WABinary/generic-utils.d.ts +15 -0
  186. package/lib/WABinary/generic-utils.js +138 -0
  187. package/lib/WABinary/index.d.ts +6 -0
  188. package/lib/WABinary/index.js +25 -0
  189. package/lib/WABinary/jid-utils.d.ts +48 -0
  190. package/lib/WABinary/jid-utils.js +115 -0
  191. package/lib/WABinary/types.d.ts +19 -0
  192. package/lib/WABinary/types.js +36 -0
  193. package/lib/WAM/BinaryInfo.d.ts +9 -0
  194. package/lib/WAM/BinaryInfo.js +13 -0
  195. package/lib/WAM/constants.d.ts +40 -0
  196. package/lib/WAM/constants.js +22855 -0
  197. package/lib/WAM/encode.d.ts +3 -0
  198. package/lib/WAM/encode.js +153 -0
  199. package/lib/WAM/index.d.ts +4 -0
  200. package/lib/WAM/index.js +19 -0
  201. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  202. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  203. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  204. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  205. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  206. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  207. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  208. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +41 -0
  209. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  210. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +54 -0
  211. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  212. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +32 -0
  213. package/lib/WAUSync/Protocols/index.d.ts +5 -0
  214. package/lib/WAUSync/Protocols/index.js +22 -0
  215. package/lib/WAUSync/USyncQuery.d.ts +29 -0
  216. package/lib/WAUSync/USyncQuery.js +97 -0
  217. package/lib/WAUSync/USyncUser.d.ts +13 -0
  218. package/lib/WAUSync/USyncUser.js +26 -0
  219. package/lib/WAUSync/index.d.ts +4 -0
  220. package/lib/WAUSync/index.js +19 -0
  221. package/lib/index.d.ts +12 -0
  222. package/lib/index.js +124 -0
  223. package/package.json +120 -0
  224. package/readme.md +1956 -0
@@ -0,0 +1,470 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractGroupMetadata = exports.makeGroupsSocket = void 0;
4
+ const index_js_1 = require("../../WAProto/index.js");
5
+ const index_js_2 = require("../Types/index.js");
6
+ const index_js_3 = require("../Utils/index.js");
7
+ const index_js_4 = require("../WABinary/index.js");
8
+ const chats_js_1 = require("./chats.js");
9
+ const makeGroupsSocket = (config) => {
10
+ const sock = (0, chats_js_1.makeChatsSocket)(config);
11
+ const { authState, ev, query, upsertMessage } = sock;
12
+ const { cachedGroupMetadata } = config;
13
+ // ── Built-in group metadata cache ─────────────────────────────────────────
14
+ const groupMetadataCache = new Map();
15
+ const GROUP_CACHE_TTL = (config.groupCacheTTL || 5) * 60 * 1000; // default 5 menit
16
+ const getCachedGroupMetadata = async (jid) => {
17
+ // 1. Cek user-provided cachedGroupMetadata (dari config makeWASocket)
18
+ if (cachedGroupMetadata) {
19
+ const cached = await cachedGroupMetadata(jid);
20
+ if (cached && Array.isArray(cached.participants))
21
+ return cached;
22
+ }
23
+ // 2. Cek internal Map cache
24
+ const entry = groupMetadataCache.get(jid);
25
+ if (entry && Date.now() - entry.ts < GROUP_CACHE_TTL) {
26
+ return entry.data;
27
+ }
28
+ return undefined;
29
+ };
30
+ const setCachedGroupMetadata = (jid, data) => {
31
+ groupMetadataCache.set(jid, { data, ts: Date.now() });
32
+ };
33
+ // Update cache saat groups.update event
34
+ ev.on('groups.update', (updates) => {
35
+ for (const update of updates) {
36
+ const entry = groupMetadataCache.get(update.id);
37
+ if (entry) {
38
+ // Merge update ke cache yang ada
39
+ groupMetadataCache.set(update.id, {
40
+ data: { ...entry.data, ...update },
41
+ ts: entry.ts
42
+ });
43
+ }
44
+ }
45
+ });
46
+ // Update cache saat participant berubah
47
+ // Debounce map to prevent duplicate refresh calls for same group
48
+ const _refreshDebounce = new Map();
49
+ const _refreshGroupMetadata = async (jid) => {
50
+ // Debounce: skip jika refresh sudah dijadwalkan dalam 2 detik terakhir
51
+ if (_refreshDebounce.has(jid))
52
+ return;
53
+ _refreshDebounce.set(jid, true);
54
+ setTimeout(() => _refreshDebounce.delete(jid), 2000);
55
+ try {
56
+ const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
57
+ const meta = (0, exports.extractGroupMetadata)(result);
58
+ setCachedGroupMetadata(jid, meta);
59
+ // Emit groups.update agar subscriber luar (makeInMemoryStore dll) ikut terupdate
60
+ ev.emit('groups.update', [meta]);
61
+ }
62
+ catch (e) {
63
+ // Ignore jika gagal (bot mungkin sudah keluar dari grup)
64
+ }
65
+ };
66
+ ev.on('group-participants.update', ({ id, participants, action }) => {
67
+ const entry = groupMetadataCache.get(id);
68
+ if (entry && Array.isArray(entry.data?.participants)) {
69
+ // Fast-path: update cache lokal secara optimistis tanpa tunggu network
70
+ const meta = { ...entry.data, participants: [...entry.data.participants] };
71
+ if (action === 'add') {
72
+ const existing = new Set(meta.participants.map(p => p.id));
73
+ for (const jid of participants) {
74
+ if (!existing.has(jid))
75
+ meta.participants.push({ id: jid, admin: null });
76
+ }
77
+ }
78
+ else if (action === 'remove') {
79
+ meta.participants = meta.participants.filter(p => !participants.includes(p.id));
80
+ }
81
+ else if (action === 'promote') {
82
+ meta.participants = meta.participants.map(p => participants.includes(p.id) ? { ...p, admin: 'admin' } : p);
83
+ }
84
+ else if (action === 'demote') {
85
+ meta.participants = meta.participants.map(p => participants.includes(p.id) ? { ...p, admin: null } : p);
86
+ }
87
+ groupMetadataCache.set(id, { data: meta, ts: entry.ts });
88
+ }
89
+ // Auto-refresh dari network untuk semua aksi participant
90
+ // Ini memastikan data selalu akurat dari server WA
91
+ _refreshGroupMetadata(id);
92
+ });
93
+ // ── End group metadata cache ───────────────────────────────────────────────
94
+ const groupQuery = async (jid, type, content) => query({
95
+ tag: 'iq',
96
+ attrs: {
97
+ type,
98
+ xmlns: 'w:g2',
99
+ to: jid
100
+ },
101
+ content
102
+ });
103
+ const groupMetadata = async (jid) => {
104
+ // Cek cache dulu sebelum hit network
105
+ const cached = await getCachedGroupMetadata(jid);
106
+ if (cached)
107
+ return cached;
108
+ // Fetch dari WA
109
+ const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
110
+ const meta = (0, exports.extractGroupMetadata)(result);
111
+ // Simpan ke cache
112
+ setCachedGroupMetadata(jid, meta);
113
+ return meta;
114
+ };
115
+ const groupFetchAllParticipating = async () => {
116
+ const result = await query({
117
+ tag: 'iq',
118
+ attrs: {
119
+ to: '@g.us',
120
+ xmlns: 'w:g2',
121
+ type: 'get'
122
+ },
123
+ content: [
124
+ {
125
+ tag: 'participating',
126
+ attrs: {},
127
+ content: [
128
+ { tag: 'participants', attrs: {} },
129
+ { tag: 'description', attrs: {} }
130
+ ]
131
+ }
132
+ ]
133
+ });
134
+ const data = {};
135
+ const groupsChild = (0, index_js_4.getBinaryNodeChild)(result, 'groups');
136
+ if (groupsChild) {
137
+ const groups = (0, index_js_4.getBinaryNodeChildren)(groupsChild, 'group');
138
+ for (const groupNode of groups) {
139
+ const meta = (0, exports.extractGroupMetadata)({
140
+ tag: 'result',
141
+ attrs: {},
142
+ content: [groupNode]
143
+ });
144
+ data[meta.id] = meta;
145
+ }
146
+ }
147
+ // TODO: properly parse LID / PN DATA
148
+ sock.ev.emit('groups.update', Object.values(data));
149
+ return data;
150
+ };
151
+ sock.ws.on('CB:ib,,dirty', async (node) => {
152
+ const { attrs } = (0, index_js_4.getBinaryNodeChild)(node, 'dirty');
153
+ if (attrs.type !== 'groups') {
154
+ return;
155
+ }
156
+ await groupFetchAllParticipating();
157
+ await sock.cleanDirtyBits('groups');
158
+ });
159
+ return {
160
+ ...sock,
161
+ groupMetadata,
162
+ groupCreate: async (subject, participants) => {
163
+ const key = (0, index_js_3.generateMessageIDV2)();
164
+ const result = await groupQuery('@g.us', 'set', [
165
+ {
166
+ tag: 'create',
167
+ attrs: {
168
+ subject,
169
+ key
170
+ },
171
+ content: participants.map(jid => ({
172
+ tag: 'participant',
173
+ attrs: { jid }
174
+ }))
175
+ }
176
+ ]);
177
+ return (0, exports.extractGroupMetadata)(result);
178
+ },
179
+ groupLeave: async (id) => {
180
+ await groupQuery('@g.us', 'set', [
181
+ {
182
+ tag: 'leave',
183
+ attrs: {},
184
+ content: [{ tag: 'group', attrs: { id } }]
185
+ }
186
+ ]);
187
+ },
188
+ groupUpdateSubject: async (jid, subject) => {
189
+ await groupQuery(jid, 'set', [
190
+ {
191
+ tag: 'subject',
192
+ attrs: {},
193
+ content: Buffer.from(subject, 'utf-8')
194
+ }
195
+ ]);
196
+ },
197
+ groupRequestParticipantsList: async (jid) => {
198
+ const result = await groupQuery(jid, 'get', [
199
+ {
200
+ tag: 'membership_approval_requests',
201
+ attrs: {}
202
+ }
203
+ ]);
204
+ const node = (0, index_js_4.getBinaryNodeChild)(result, 'membership_approval_requests');
205
+ const participants = (0, index_js_4.getBinaryNodeChildren)(node, 'membership_approval_request');
206
+ return participants.map(v => v.attrs);
207
+ },
208
+ groupRequestParticipantsUpdate: async (jid, participants, action) => {
209
+ const result = await groupQuery(jid, 'set', [
210
+ {
211
+ tag: 'membership_requests_action',
212
+ attrs: {},
213
+ content: [
214
+ {
215
+ tag: action,
216
+ attrs: {},
217
+ content: participants.map(jid => ({
218
+ tag: 'participant',
219
+ attrs: { jid }
220
+ }))
221
+ }
222
+ ]
223
+ }
224
+ ]);
225
+ const node = (0, index_js_4.getBinaryNodeChild)(result, 'membership_requests_action');
226
+ const nodeAction = (0, index_js_4.getBinaryNodeChild)(node, action);
227
+ const participantsAffected = (0, index_js_4.getBinaryNodeChildren)(nodeAction, 'participant');
228
+ return participantsAffected.map(p => {
229
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
230
+ });
231
+ },
232
+ groupParticipantsUpdate: async (jid, participants, action) => {
233
+ const result = await groupQuery(jid, 'set', [
234
+ {
235
+ tag: action,
236
+ attrs: {},
237
+ content: participants.map(jid => ({
238
+ tag: 'participant',
239
+ attrs: { jid }
240
+ }))
241
+ }
242
+ ]);
243
+ const node = (0, index_js_4.getBinaryNodeChild)(result, action);
244
+ const participantsAffected = (0, index_js_4.getBinaryNodeChildren)(node, 'participant');
245
+ return participantsAffected.map(p => {
246
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
247
+ });
248
+ },
249
+ groupUpdateDescription: async (jid, description) => {
250
+ const metadata = await groupMetadata(jid);
251
+ const prev = metadata.descId ?? null;
252
+ await groupQuery(jid, 'set', [
253
+ {
254
+ tag: 'description',
255
+ attrs: {
256
+ ...(description ? { id: (0, index_js_3.generateMessageIDV2)() } : { delete: 'true' }),
257
+ ...(prev ? { prev } : {})
258
+ },
259
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
260
+ }
261
+ ]);
262
+ },
263
+ groupInviteCode: async (jid) => {
264
+ const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
265
+ const inviteNode = (0, index_js_4.getBinaryNodeChild)(result, 'invite');
266
+ return inviteNode?.attrs.code;
267
+ },
268
+ groupRevokeInvite: async (jid) => {
269
+ const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
270
+ const inviteNode = (0, index_js_4.getBinaryNodeChild)(result, 'invite');
271
+ return inviteNode?.attrs.code;
272
+ },
273
+ groupAcceptInvite: async (code) => {
274
+ const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
275
+ const result = (0, index_js_4.getBinaryNodeChild)(results, 'group');
276
+ return result?.attrs.jid;
277
+ },
278
+ /**
279
+ * revoke a v4 invite for someone
280
+ * @param groupJid group jid
281
+ * @param invitedJid jid of person you invited
282
+ * @returns true if successful
283
+ */
284
+ groupRevokeInviteV4: async (groupJid, invitedJid) => {
285
+ const result = await groupQuery(groupJid, 'set', [
286
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
287
+ ]);
288
+ return !!result;
289
+ },
290
+ /**
291
+ * accept a GroupInviteMessage
292
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
293
+ * @param inviteMessage the message to accept
294
+ */
295
+ groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
296
+ key = typeof key === 'string' ? { remoteJid: key } : key;
297
+ const results = await groupQuery(inviteMessage.groupJid, 'set', [
298
+ {
299
+ tag: 'accept',
300
+ attrs: {
301
+ code: inviteMessage.inviteCode,
302
+ expiration: inviteMessage.inviteExpiration.toString(),
303
+ admin: key.remoteJid
304
+ }
305
+ }
306
+ ]);
307
+ // if we have the full message key
308
+ // update the invite message to be expired
309
+ if (key.id) {
310
+ // create new invite message that is expired
311
+ inviteMessage = index_js_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);
312
+ inviteMessage.inviteExpiration = 0;
313
+ inviteMessage.inviteCode = '';
314
+ ev.emit('messages.update', [
315
+ {
316
+ key,
317
+ update: {
318
+ message: {
319
+ groupInviteMessage: inviteMessage
320
+ }
321
+ }
322
+ }
323
+ ]);
324
+ }
325
+ // generate the group add message
326
+ await upsertMessage({
327
+ key: {
328
+ remoteJid: inviteMessage.groupJid,
329
+ id: (0, index_js_3.generateMessageIDV2)(sock.user?.id),
330
+ fromMe: false,
331
+ participant: key.remoteJid
332
+ },
333
+ messageStubType: index_js_2.WAMessageStubType.GROUP_PARTICIPANT_ADD,
334
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
335
+ participant: key.remoteJid,
336
+ messageTimestamp: (0, index_js_3.unixTimestampSeconds)()
337
+ }, 'notify');
338
+ return results.attrs.from;
339
+ }),
340
+ groupGetInviteInfo: async (code) => {
341
+ const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
342
+ return (0, exports.extractGroupMetadata)(results);
343
+ },
344
+ groupToggleEphemeral: async (jid, ephemeralExpiration) => {
345
+ const content = ephemeralExpiration
346
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
347
+ : { tag: 'not_ephemeral', attrs: {} };
348
+ await groupQuery(jid, 'set', [content]);
349
+ },
350
+ groupSettingUpdate: async (jid, setting) => {
351
+ await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
352
+ },
353
+ groupMemberAddMode: async (jid, mode) => {
354
+ await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
355
+ },
356
+ groupJoinApprovalMode: async (jid, mode) => {
357
+ await groupQuery(jid, 'set', [
358
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
359
+ ]);
360
+ },
361
+ groupFetchAllParticipating,
362
+ /**
363
+ * Auto detect isAdmin & isBotAdmin dari groupMetadata
364
+ * @param {string} groupJid - JID grup
365
+ * @param {string} senderJid - JID pengirim pesan
366
+ * @returns {Promise<{isAdmin: boolean, isBotAdmin: boolean}>}
367
+ */
368
+ getAdminStatus: async (groupJid, senderJid) => {
369
+ const normalizeJid = (jid) => {
370
+ if (!jid)
371
+ return null;
372
+ try {
373
+ return (0, index_js_4.jidNormalizedUser)(jid).split('@')[0];
374
+ }
375
+ catch {
376
+ return String(jid).split('@')[0];
377
+ }
378
+ };
379
+ const botJid = sock.authState?.creds?.me?.id;
380
+ const meta = await sock.groupMetadata(groupJid).catch(() => null);
381
+ if (!meta || !Array.isArray(meta.participants)) {
382
+ return { isAdmin: false, isBotAdmin: false };
383
+ }
384
+ const senderNorm = normalizeJid(senderJid);
385
+ const botNorm = normalizeJid(botJid);
386
+ const isAdmin = meta.participants.some(p => {
387
+ const pid = normalizeJid(p.jid || p.id || p.lid);
388
+ return pid === senderNorm && (p.admin === 'admin' || p.admin === 'superadmin');
389
+ });
390
+ // Cek isBotAdmin: via participants
391
+ let isBotAdmin = meta.participants.some(p => {
392
+ const pid = normalizeJid(p.jid || p.id || p.lid);
393
+ return pid === botNorm && (p.admin === 'admin' || p.admin === 'superadmin');
394
+ });
395
+ // Fallback: cek via owner/subjectOwner
396
+ if (!isBotAdmin) {
397
+ const owners = [meta.owner, meta.subjectOwner, meta.ownerPn]
398
+ .filter(Boolean)
399
+ .map(normalizeJid);
400
+ if (owners.includes(botNorm))
401
+ isBotAdmin = true;
402
+ }
403
+ return { isAdmin, isBotAdmin };
404
+ }
405
+ };
406
+ };
407
+ exports.makeGroupsSocket = makeGroupsSocket;
408
+ const extractGroupMetadata = (result) => {
409
+ const group = (0, index_js_4.getBinaryNodeChild)(result, 'group');
410
+ const descChild = (0, index_js_4.getBinaryNodeChild)(group, 'description');
411
+ let desc;
412
+ let descId;
413
+ let descOwner;
414
+ let descOwnerPn;
415
+ let descTime;
416
+ if (descChild) {
417
+ desc = (0, index_js_4.getBinaryNodeChildString)(descChild, 'body');
418
+ descOwner = descChild.attrs.participant ? (0, index_js_4.jidNormalizedUser)(descChild.attrs.participant) : undefined;
419
+ descOwnerPn = descChild.attrs.participant_pn ? (0, index_js_4.jidNormalizedUser)(descChild.attrs.participant_pn) : undefined;
420
+ descTime = +descChild.attrs.t;
421
+ descId = descChild.attrs.id;
422
+ }
423
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : (0, index_js_4.jidEncode)(group.attrs.id, 'g.us');
424
+ const eph = (0, index_js_4.getBinaryNodeChild)(group, 'ephemeral')?.attrs.expiration;
425
+ const memberAddMode = (0, index_js_4.getBinaryNodeChildString)(group, 'member_add_mode') === 'all_member_add';
426
+ const metadata = {
427
+ id: groupId,
428
+ notify: group.attrs.notify,
429
+ addressingMode: group.attrs.addressing_mode === 'lid' ? index_js_2.WAMessageAddressingMode.LID : index_js_2.WAMessageAddressingMode.PN,
430
+ subject: group.attrs.subject,
431
+ subjectOwner: group.attrs.s_o,
432
+ subjectOwnerPn: group.attrs.s_o_pn,
433
+ subjectTime: +group.attrs.s_t,
434
+ size: group.attrs.size ? +group.attrs.size : (0, index_js_4.getBinaryNodeChildren)(group, 'participant').length,
435
+ creation: +group.attrs.creation,
436
+ owner: group.attrs.creator ? (0, index_js_4.jidNormalizedUser)(group.attrs.creator) : undefined,
437
+ ownerPn: group.attrs.creator_pn ? (0, index_js_4.jidNormalizedUser)(group.attrs.creator_pn) : undefined,
438
+ owner_country_code: group.attrs.creator_country_code,
439
+ desc,
440
+ descId,
441
+ descOwner,
442
+ descOwnerPn,
443
+ descTime,
444
+ linkedParent: (0, index_js_4.getBinaryNodeChild)(group, 'linked_parent')?.attrs.jid || undefined,
445
+ restrict: !!(0, index_js_4.getBinaryNodeChild)(group, 'locked'),
446
+ announce: !!(0, index_js_4.getBinaryNodeChild)(group, 'announcement'),
447
+ isCommunity: !!(0, index_js_4.getBinaryNodeChild)(group, 'parent'),
448
+ isCommunityAnnounce: !!(0, index_js_4.getBinaryNodeChild)(group, 'default_sub_group'),
449
+ joinApprovalMode: !!(0, index_js_4.getBinaryNodeChild)(group, 'membership_approval_mode'),
450
+ memberAddMode,
451
+ participants: (0, index_js_4.getBinaryNodeChildren)(group, 'participant').map(({ attrs }) => {
452
+ const isLid = (0, index_js_4.isLidUser)(attrs.jid);
453
+ const pn = attrs.phone_number;
454
+ const hasPn = (0, index_js_4.isPnUser)(pn);
455
+ // Jika grup pakai LID addressing:
456
+ // - id → pakai phoneNumber (PN) agar bisa dicompare dengan m.sender
457
+ // - lid → simpan LID asli
458
+ // Jika grup pakai PN addressing: id tetap PN
459
+ return {
460
+ id: isLid && hasPn ? pn : attrs.jid,
461
+ phoneNumber: isLid && hasPn ? pn : undefined,
462
+ lid: isLid ? attrs.jid : ((0, index_js_4.isPnUser)(attrs.jid) && (0, index_js_4.isLidUser)(attrs.lid) ? attrs.lid : undefined),
463
+ admin: (attrs.type || null)
464
+ };
465
+ }),
466
+ ephemeralDuration: eph ? +eph : undefined
467
+ };
468
+ return metadata;
469
+ };
470
+ exports.extractGroupMetadata = extractGroupMetadata;