@wenzyx1/bails 1.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 (218) hide show
  1. package/README.md +231 -0
  2. package/WAProto/fix-imports.js +85 -0
  3. package/WAProto/index.d.ts +79257 -0
  4. package/WAProto/index.js +242946 -0
  5. package/engine-requirements.js +10 -0
  6. package/lib/Defaults/baileys-version.json +3 -0
  7. package/lib/Defaults/index.d.ts +53 -0
  8. package/lib/Defaults/index.js +147 -0
  9. package/lib/Defaults/phonenumber-mcc.json +223 -0
  10. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  11. package/lib/Signal/Group/ciphertext-message.js +15 -0
  12. package/lib/Signal/Group/group-session-builder.d.ts +14 -0
  13. package/lib/Signal/Group/group-session-builder.js +64 -0
  14. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  15. package/lib/Signal/Group/group_cipher.js +93 -0
  16. package/lib/Signal/Group/index.d.ts +11 -0
  17. package/lib/Signal/Group/index.js +57 -0
  18. package/lib/Signal/Group/keyhelper.d.ts +10 -0
  19. package/lib/Signal/Group/keyhelper.js +55 -0
  20. package/lib/Signal/Group/queue-job.d.ts +1 -0
  21. package/lib/Signal/Group/queue-job.js +53 -0
  22. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  23. package/lib/Signal/Group/sender-chain-key.js +34 -0
  24. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  25. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  26. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  27. package/lib/Signal/Group/sender-key-message.js +69 -0
  28. package/lib/Signal/Group/sender-key-name.d.ts +17 -0
  29. package/lib/Signal/Group/sender-key-name.js +51 -0
  30. package/lib/Signal/Group/sender-key-record.d.ts +30 -0
  31. package/lib/Signal/Group/sender-key-record.js +53 -0
  32. package/lib/Signal/Group/sender-key-state.d.ts +38 -0
  33. package/lib/Signal/Group/sender-key-state.js +99 -0
  34. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  35. package/lib/Signal/Group/sender-message-key.js +29 -0
  36. package/lib/Signal/libsignal.d.ts +3 -0
  37. package/lib/Signal/libsignal.js +454 -0
  38. package/lib/Signal/lid-mapping.js +280 -0
  39. package/lib/Socket/Client/abstract-socket-client.d.ts +17 -0
  40. package/lib/Socket/Client/abstract-socket-client.js +13 -0
  41. package/lib/Socket/Client/index.d.ts +3 -0
  42. package/lib/Socket/Client/index.js +19 -0
  43. package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
  44. package/lib/Socket/Client/mobile-socket-client.js +65 -0
  45. package/lib/Socket/Client/web-socket-client.d.ts +12 -0
  46. package/lib/Socket/Client/web-socket-client.js +62 -0
  47. package/lib/Socket/business.d.ts +171 -0
  48. package/lib/Socket/business.js +342 -0
  49. package/lib/Socket/chats.d.ts +267 -0
  50. package/lib/Socket/chats.js +978 -0
  51. package/lib/Socket/communities.mjs +428 -0
  52. package/lib/Socket/dugong.d.ts +254 -0
  53. package/lib/Socket/dugong.js +558 -0
  54. package/lib/Socket/groups.d.ts +115 -0
  55. package/lib/Socket/groups.js +380 -0
  56. package/lib/Socket/index.d.ts +173 -0
  57. package/lib/Socket/index.js +32 -0
  58. package/lib/Socket/luxu.js +545 -0
  59. package/lib/Socket/messages-recv.d.ts +161 -0
  60. package/lib/Socket/messages-recv.js +1124 -0
  61. package/lib/Socket/messages-send.d.ts +149 -0
  62. package/lib/Socket/messages-send.js +1476 -0
  63. package/lib/Socket/mex.mjs +50 -0
  64. package/lib/Socket/newsletter.d.ts +134 -0
  65. package/lib/Socket/newsletter.js +351 -0
  66. package/lib/Socket/registration.d.ts +267 -0
  67. package/lib/Socket/registration.js +166 -0
  68. package/lib/Socket/socket.d.ts +43 -0
  69. package/lib/Socket/socket.js +904 -0
  70. package/lib/Socket/username.mjs +132 -0
  71. package/lib/Socket/usync.d.ts +36 -0
  72. package/lib/Socket/usync.js +70 -0
  73. package/lib/Store/index.d.ts +3 -0
  74. package/lib/Store/index.js +10 -0
  75. package/lib/Store/keyed-db.d.ts +30 -0
  76. package/lib/Store/keyed-db.js +233 -0
  77. package/lib/Store/make-cache-manager-store.d.ts +13 -0
  78. package/lib/Store/make-cache-manager-store.js +83 -0
  79. package/lib/Store/make-in-memory-store.d.ts +118 -0
  80. package/lib/Store/make-in-memory-store.js +427 -0
  81. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  82. package/lib/Store/make-ordered-dictionary.js +81 -0
  83. package/lib/Store/object-repository.d.ts +10 -0
  84. package/lib/Store/object-repository.js +27 -0
  85. package/lib/Types/Auth.d.ts +110 -0
  86. package/lib/Types/Auth.js +2 -0
  87. package/lib/Types/Call.d.ts +13 -0
  88. package/lib/Types/Call.js +2 -0
  89. package/lib/Types/Chat.d.ts +102 -0
  90. package/lib/Types/Chat.js +4 -0
  91. package/lib/Types/Contact.d.ts +19 -0
  92. package/lib/Types/Contact.js +2 -0
  93. package/lib/Types/Events.d.ts +157 -0
  94. package/lib/Types/Events.js +2 -0
  95. package/lib/Types/GroupMetadata.d.ts +55 -0
  96. package/lib/Types/GroupMetadata.js +2 -0
  97. package/lib/Types/Label.d.ts +35 -0
  98. package/lib/Types/Label.js +27 -0
  99. package/lib/Types/LabelAssociation.d.ts +29 -0
  100. package/lib/Types/LabelAssociation.js +9 -0
  101. package/lib/Types/Message.d.ts +273 -0
  102. package/lib/Types/Message.js +9 -0
  103. package/lib/Types/Mex.js +39 -0
  104. package/lib/Types/Newsletter.d.ts +103 -0
  105. package/lib/Types/Newsletter.js +38 -0
  106. package/lib/Types/Product.d.ts +78 -0
  107. package/lib/Types/Product.js +2 -0
  108. package/lib/Types/Signal.d.ts +57 -0
  109. package/lib/Types/Signal.js +2 -0
  110. package/lib/Types/Socket.d.ts +111 -0
  111. package/lib/Types/Socket.js +2 -0
  112. package/lib/Types/State.d.ts +27 -0
  113. package/lib/Types/State.js +2 -0
  114. package/lib/Types/USync.d.ts +25 -0
  115. package/lib/Types/USync.js +2 -0
  116. package/lib/Types/index.d.ts +57 -0
  117. package/lib/Types/index.js +43 -0
  118. package/lib/Utils/Mex.js +39 -0
  119. package/lib/Utils/auth-utils.d.ts +18 -0
  120. package/lib/Utils/auth-utils.js +206 -0
  121. package/lib/Utils/baileys-event-stream.d.ts +16 -0
  122. package/lib/Utils/baileys-event-stream.js +57 -0
  123. package/lib/Utils/browser-presets.js +64 -0
  124. package/lib/Utils/business.d.ts +22 -0
  125. package/lib/Utils/business.js +234 -0
  126. package/lib/Utils/chat-utils.d.ts +71 -0
  127. package/lib/Utils/chat-utils.js +726 -0
  128. package/lib/Utils/companion-reg-client-utils.js +40 -0
  129. package/lib/Utils/crypto.d.ts +41 -0
  130. package/lib/Utils/crypto.js +151 -0
  131. package/lib/Utils/decode-wa-message.d.ts +19 -0
  132. package/lib/Utils/decode-wa-message.js +198 -0
  133. package/lib/Utils/event-buffer.d.ts +35 -0
  134. package/lib/Utils/event-buffer.js +514 -0
  135. package/lib/Utils/extended-messages.js +231 -0
  136. package/lib/Utils/generics.d.ts +92 -0
  137. package/lib/Utils/generics.js +425 -0
  138. package/lib/Utils/history.d.ts +15 -0
  139. package/lib/Utils/history.js +96 -0
  140. package/lib/Utils/identity-change-handler.js +52 -0
  141. package/lib/Utils/index.d.ts +17 -0
  142. package/lib/Utils/index.js +40 -0
  143. package/lib/Utils/link-preview.d.ts +21 -0
  144. package/lib/Utils/link-preview.js +93 -0
  145. package/lib/Utils/logger.d.ts +4 -0
  146. package/lib/Utils/logger.js +7 -0
  147. package/lib/Utils/lt-hash.d.ts +12 -0
  148. package/lib/Utils/lt-hash.js +51 -0
  149. package/lib/Utils/make-mutex.d.ts +7 -0
  150. package/lib/Utils/make-mutex.js +43 -0
  151. package/lib/Utils/message-composer.js +293 -0
  152. package/lib/Utils/message-retry-manager.js +226 -0
  153. package/lib/Utils/messages-media.d.ts +116 -0
  154. package/lib/Utils/messages-media.js +815 -0
  155. package/lib/Utils/messages.d.ts +77 -0
  156. package/lib/Utils/messages.js +888 -0
  157. package/lib/Utils/noise-handler.d.ts +21 -0
  158. package/lib/Utils/noise-handler.js +155 -0
  159. package/lib/Utils/offline-node-processor.js +46 -0
  160. package/lib/Utils/pre-key-manager.js +95 -0
  161. package/lib/Utils/process-message.d.ts +41 -0
  162. package/lib/Utils/process-message.js +321 -0
  163. package/lib/Utils/reporting-utils.js +272 -0
  164. package/lib/Utils/signal.d.ts +32 -0
  165. package/lib/Utils/signal.js +160 -0
  166. package/lib/Utils/stanza-ack.js +38 -0
  167. package/lib/Utils/sync-action-utils.js +50 -0
  168. package/lib/Utils/tc-token-utils.js +169 -0
  169. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  170. package/lib/Utils/use-multi-file-auth-state.js +119 -0
  171. package/lib/Utils/validate-connection.d.ts +11 -0
  172. package/lib/Utils/validate-connection.js +229 -0
  173. package/lib/WABinary/constants.d.ts +30 -0
  174. package/lib/WABinary/constants.js +40 -0
  175. package/lib/WABinary/decode.d.ts +7 -0
  176. package/lib/WABinary/decode.js +252 -0
  177. package/lib/WABinary/encode.d.ts +3 -0
  178. package/lib/WABinary/encode.js +265 -0
  179. package/lib/WABinary/generic-utils.d.ts +17 -0
  180. package/lib/WABinary/generic-utils.js +204 -0
  181. package/lib/WABinary/index.d.ts +5 -0
  182. package/lib/WABinary/index.js +21 -0
  183. package/lib/WABinary/jid-utils.d.ts +31 -0
  184. package/lib/WABinary/jid-utils.js +132 -0
  185. package/lib/WABinary/types.d.ts +18 -0
  186. package/lib/WABinary/types.js +2 -0
  187. package/lib/WAM/BinaryInfo.d.ts +17 -0
  188. package/lib/WAM/BinaryInfo.js +13 -0
  189. package/lib/WAM/constants.d.ts +38 -0
  190. package/lib/WAM/constants.js +15350 -0
  191. package/lib/WAM/encode.d.ts +3 -0
  192. package/lib/WAM/encode.js +155 -0
  193. package/lib/WAM/index.d.ts +3 -0
  194. package/lib/WAM/index.js +19 -0
  195. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
  196. package/lib/WAUSync/Protocols/USyncContactProtocol.js +55 -0
  197. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  198. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  199. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  200. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  201. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  202. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  203. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +27 -0
  204. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
  205. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  206. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  208. package/lib/WAUSync/Protocols/index.d.ts +4 -0
  209. package/lib/WAUSync/Protocols/index.js +21 -0
  210. package/lib/WAUSync/USyncQuery.d.ts +28 -0
  211. package/lib/WAUSync/USyncQuery.js +93 -0
  212. package/lib/WAUSync/USyncUser.d.ts +12 -0
  213. package/lib/WAUSync/USyncUser.js +34 -0
  214. package/lib/WAUSync/index.d.ts +3 -0
  215. package/lib/WAUSync/index.js +19 -0
  216. package/lib/index.d.ts +12 -0
  217. package/lib/index.js +52 -0
  218. package/package.json +112 -0
@@ -0,0 +1,428 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import TypesDefault from '../Types/index.js';
3
+ import UtilsDefault from '../Utils/index.js';
4
+ import loggerDefault from '../Utils/logger.js';
5
+ import WABinaryDefault from '../WABinary/index.js';
6
+
7
+ const { WAMessageStubType } = TypesDefault;
8
+ const { generateMessageID, generateMessageIDV2, unixTimestampSeconds } = UtilsDefault;
9
+ const logger = loggerDefault?.default ?? loggerDefault;
10
+ const { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, jidEncode, jidNormalizedUser } = WABinaryDefault;
11
+
12
+ /**
13
+ * Ported from xata-bail's lib/Socket/communities.js.
14
+ * Adapted for edgar-bail: takes an already-built `sock` (the output of
15
+ * edgar's existing, unmodified synchronous chain) instead of building its
16
+ * own chain via makeBusinessSocket(config).
17
+ */
18
+ export const makeCommunitiesSocketLayer = (sock) => {
19
+ const { authState, ev, query, upsertMessage } = sock;
20
+
21
+ const communityQuery = async (jid, type, content) => query({
22
+ tag: 'iq',
23
+ attrs: {
24
+ type,
25
+ xmlns: 'w:g2',
26
+ to: jid
27
+ },
28
+ content
29
+ });
30
+
31
+ const communityMetadata = async (jid) => {
32
+ const result = await communityQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
33
+ return extractCommunityMetadata(result);
34
+ };
35
+
36
+ const communityFetchAllParticipating = async () => {
37
+ const result = await query({
38
+ tag: 'iq',
39
+ attrs: {
40
+ to: '@g.us',
41
+ xmlns: 'w:g2',
42
+ type: 'get'
43
+ },
44
+ content: [
45
+ {
46
+ tag: 'participating',
47
+ attrs: {},
48
+ content: [
49
+ { tag: 'participants', attrs: {} },
50
+ { tag: 'description', attrs: {} }
51
+ ]
52
+ }
53
+ ]
54
+ });
55
+ const data = {};
56
+ const communitiesChild = getBinaryNodeChild(result, 'communities');
57
+ if (communitiesChild) {
58
+ const communities = getBinaryNodeChildren(communitiesChild, 'community');
59
+ for (const communityNode of communities) {
60
+ const meta = extractCommunityMetadata({
61
+ tag: 'result',
62
+ attrs: {},
63
+ content: [communityNode]
64
+ });
65
+ data[meta.id] = meta;
66
+ }
67
+ }
68
+ sock.ev.emit('groups.update', Object.values(data));
69
+ return data;
70
+ };
71
+
72
+ async function parseGroupResult(node) {
73
+ logger.info({ node }, 'parseGroupResult');
74
+ const groupNode = getBinaryNodeChild(node, 'group');
75
+ if (groupNode) {
76
+ try {
77
+ logger.info({ groupNode }, 'groupNode');
78
+ const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`);
79
+ return metadata ? metadata : Optional.empty();
80
+ }
81
+ catch (error) {
82
+ console.error('Error parsing group metadata:', error);
83
+ return Optional.empty();
84
+ }
85
+ }
86
+ return Optional.empty();
87
+ }
88
+
89
+ const Optional = {
90
+ empty: () => null,
91
+ of: (value) => (value !== null ? { value } : null)
92
+ };
93
+
94
+ sock.ws.on('CB:ib,,dirty', async (node) => {
95
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
96
+ if (attrs.type !== 'communities') {
97
+ return;
98
+ }
99
+ await communityFetchAllParticipating();
100
+ await sock.cleanDirtyBits('groups');
101
+ });
102
+
103
+ return {
104
+ ...sock,
105
+ communityMetadata,
106
+ communityCreate: async (subject, body) => {
107
+ const descriptionId = generateMessageID().substring(0, 12);
108
+ const result = await communityQuery('@g.us', 'set', [
109
+ {
110
+ tag: 'create',
111
+ attrs: { subject },
112
+ content: [
113
+ {
114
+ tag: 'description',
115
+ attrs: { id: descriptionId },
116
+ content: [
117
+ {
118
+ tag: 'body',
119
+ attrs: {},
120
+ content: Buffer.from(body || '', 'utf-8')
121
+ }
122
+ ]
123
+ },
124
+ {
125
+ tag: 'parent',
126
+ attrs: { default_membership_approval_mode: 'request_required' }
127
+ },
128
+ {
129
+ tag: 'allow_non_admin_sub_group_creation',
130
+ attrs: {}
131
+ },
132
+ {
133
+ tag: 'create_general_chat',
134
+ attrs: {}
135
+ }
136
+ ]
137
+ }
138
+ ]);
139
+ return await parseGroupResult(result);
140
+ },
141
+ communityCreateGroup: async (subject, participants, parentCommunityJid) => {
142
+ const key = generateMessageIDV2();
143
+ const result = await communityQuery('@g.us', 'set', [
144
+ {
145
+ tag: 'create',
146
+ attrs: {
147
+ subject,
148
+ key
149
+ },
150
+ content: [
151
+ ...participants.map(jid => ({
152
+ tag: 'participant',
153
+ attrs: { jid }
154
+ })),
155
+ { tag: 'linked_parent', attrs: { jid: parentCommunityJid } }
156
+ ]
157
+ }
158
+ ]);
159
+ return await parseGroupResult(result);
160
+ },
161
+ communityLeave: async (id) => {
162
+ await communityQuery('@g.us', 'set', [
163
+ {
164
+ tag: 'leave',
165
+ attrs: {},
166
+ content: [{ tag: 'community', attrs: { id } }]
167
+ }
168
+ ]);
169
+ },
170
+ communityUpdateSubject: async (jid, subject) => {
171
+ await communityQuery(jid, 'set', [
172
+ {
173
+ tag: 'subject',
174
+ attrs: {},
175
+ content: Buffer.from(subject, 'utf-8')
176
+ }
177
+ ]);
178
+ },
179
+ communityLinkGroup: async (groupJid, parentCommunityJid) => {
180
+ await communityQuery(parentCommunityJid, 'set', [
181
+ {
182
+ tag: 'links',
183
+ attrs: {},
184
+ content: [
185
+ {
186
+ tag: 'link',
187
+ attrs: { link_type: 'sub_group' },
188
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
189
+ }
190
+ ]
191
+ }
192
+ ]);
193
+ },
194
+ communityUnlinkGroup: async (groupJid, parentCommunityJid) => {
195
+ await communityQuery(parentCommunityJid, 'set', [
196
+ {
197
+ tag: 'unlink',
198
+ attrs: { unlink_type: 'sub_group' },
199
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
200
+ }
201
+ ]);
202
+ },
203
+ communityFetchLinkedGroups: async (jid) => {
204
+ let communityJid = jid;
205
+ let isCommunity = false;
206
+ const metadata = await sock.groupMetadata(jid);
207
+ if (metadata.linkedParent) {
208
+ communityJid = metadata.linkedParent;
209
+ }
210
+ else {
211
+ isCommunity = true;
212
+ }
213
+ const result = await communityQuery(communityJid, 'get', [{ tag: 'sub_groups', attrs: {} }]);
214
+ const linkedGroupsData = [];
215
+ const subGroupsNode = getBinaryNodeChild(result, 'sub_groups');
216
+ if (subGroupsNode) {
217
+ const groupNodes = getBinaryNodeChildren(subGroupsNode, 'group');
218
+ for (const groupNode of groupNodes) {
219
+ linkedGroupsData.push({
220
+ id: groupNode.attrs.id ? jidEncode(groupNode.attrs.id, 'g.us') : undefined,
221
+ subject: groupNode.attrs.subject || '',
222
+ creation: groupNode.attrs.creation ? Number(groupNode.attrs.creation) : undefined,
223
+ owner: groupNode.attrs.creator ? jidNormalizedUser(groupNode.attrs.creator) : undefined,
224
+ size: groupNode.attrs.size ? Number(groupNode.attrs.size) : undefined
225
+ });
226
+ }
227
+ }
228
+ return {
229
+ communityJid,
230
+ isCommunity,
231
+ linkedGroups: linkedGroupsData
232
+ };
233
+ },
234
+ communityRequestParticipantsList: async (jid) => {
235
+ const result = await communityQuery(jid, 'get', [
236
+ {
237
+ tag: 'membership_approval_requests',
238
+ attrs: {}
239
+ }
240
+ ]);
241
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
242
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
243
+ return participants.map(v => v.attrs);
244
+ },
245
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
246
+ const result = await communityQuery(jid, 'set', [
247
+ {
248
+ tag: 'membership_requests_action',
249
+ attrs: {},
250
+ content: [
251
+ {
252
+ tag: action,
253
+ attrs: {},
254
+ content: participants.map(jid => ({
255
+ tag: 'participant',
256
+ attrs: { jid }
257
+ }))
258
+ }
259
+ ]
260
+ }
261
+ ]);
262
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
263
+ const nodeAction = getBinaryNodeChild(node, action);
264
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
265
+ return participantsAffected.map(p => {
266
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
267
+ });
268
+ },
269
+ communityParticipantsUpdate: async (jid, participants, action) => {
270
+ const result = await communityQuery(jid, 'set', [
271
+ {
272
+ tag: action,
273
+ attrs: action === 'remove' ? { linked_groups: 'true' } : {},
274
+ content: participants.map(jid => ({
275
+ tag: 'participant',
276
+ attrs: { jid }
277
+ }))
278
+ }
279
+ ]);
280
+ const node = getBinaryNodeChild(result, action);
281
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
282
+ return participantsAffected.map(p => {
283
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
284
+ });
285
+ },
286
+ communityUpdateDescription: async (jid, description) => {
287
+ const metadata = await communityMetadata(jid);
288
+ const prev = metadata.descId ?? null;
289
+ await communityQuery(jid, 'set', [
290
+ {
291
+ tag: 'description',
292
+ attrs: {
293
+ ...(description ? { id: generateMessageID() } : { delete: 'true' }),
294
+ ...(prev ? { prev } : {})
295
+ },
296
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
297
+ }
298
+ ]);
299
+ },
300
+ communityInviteCode: async (jid) => {
301
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
302
+ const inviteNode = getBinaryNodeChild(result, 'invite');
303
+ return inviteNode?.attrs.code;
304
+ },
305
+ communityRevokeInvite: async (jid) => {
306
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
307
+ const inviteNode = getBinaryNodeChild(result, 'invite');
308
+ return inviteNode?.attrs.code;
309
+ },
310
+ communityAcceptInvite: async (code) => {
311
+ const results = await communityQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
312
+ const result = getBinaryNodeChild(results, 'community');
313
+ return result?.attrs.jid;
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
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
322
+ key = typeof key === 'string' ? { remoteJid: key } : key;
323
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [
324
+ {
325
+ tag: 'accept',
326
+ attrs: {
327
+ code: inviteMessage.inviteCode,
328
+ expiration: inviteMessage.inviteExpiration.toString(),
329
+ admin: key.remoteJid
330
+ }
331
+ }
332
+ ]);
333
+ if (key.id) {
334
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
335
+ inviteMessage.inviteExpiration = 0;
336
+ inviteMessage.inviteCode = '';
337
+ ev.emit('messages.update', [
338
+ {
339
+ key,
340
+ update: {
341
+ message: {
342
+ groupInviteMessage: inviteMessage
343
+ }
344
+ }
345
+ }
346
+ ]);
347
+ }
348
+ await upsertMessage({
349
+ key: {
350
+ remoteJid: inviteMessage.groupJid,
351
+ id: generateMessageIDV2(sock.user?.id),
352
+ fromMe: false,
353
+ participant: key.remoteJid
354
+ },
355
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
356
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
357
+ participant: key.remoteJid,
358
+ messageTimestamp: unixTimestampSeconds()
359
+ }, 'notify');
360
+ return results.attrs.from;
361
+ }),
362
+ communityGetInviteInfo: async (code) => {
363
+ const results = await communityQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
364
+ return extractCommunityMetadata(results);
365
+ },
366
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
367
+ const content = ephemeralExpiration
368
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
369
+ : { tag: 'not_ephemeral', attrs: {} };
370
+ await communityQuery(jid, 'set', [content]);
371
+ },
372
+ communitySettingUpdate: async (jid, setting) => {
373
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
374
+ },
375
+ communityMemberAddMode: async (jid, mode) => {
376
+ await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
377
+ },
378
+ communityJoinApprovalMode: async (jid, mode) => {
379
+ await communityQuery(jid, 'set', [
380
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'community_join', attrs: { state: mode } }] }
381
+ ]);
382
+ },
383
+ communityFetchAllParticipating
384
+ };
385
+ };
386
+
387
+ export const extractCommunityMetadata = (result) => {
388
+ const community = getBinaryNodeChild(result, 'community');
389
+ const descChild = getBinaryNodeChild(community, 'description');
390
+ let desc;
391
+ let descId;
392
+ if (descChild) {
393
+ desc = getBinaryNodeChildString(descChild, 'body');
394
+ descId = descChild.attrs.id;
395
+ }
396
+ const communityId = community.attrs.id?.includes('@')
397
+ ? community.attrs.id
398
+ : jidEncode(community.attrs.id || '', 'g.us');
399
+ const eph = getBinaryNodeChild(community, 'ephemeral')?.attrs.expiration;
400
+ const memberAddMode = getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add';
401
+ const metadata = {
402
+ id: communityId,
403
+ subject: community.attrs.subject || '',
404
+ subjectOwner: community.attrs.s_o,
405
+ subjectTime: Number(community.attrs.s_t || 0),
406
+ size: getBinaryNodeChildren(community, 'participant').length,
407
+ creation: Number(community.attrs.creation || 0),
408
+ owner: community.attrs.creator ? jidNormalizedUser(community.attrs.creator) : undefined,
409
+ desc,
410
+ descId,
411
+ linkedParent: getBinaryNodeChild(community, 'linked_parent')?.attrs.jid || undefined,
412
+ restrict: !!getBinaryNodeChild(community, 'locked'),
413
+ announce: !!getBinaryNodeChild(community, 'announcement'),
414
+ isCommunity: !!getBinaryNodeChild(community, 'parent'),
415
+ isCommunityAnnounce: !!getBinaryNodeChild(community, 'default_sub_community'),
416
+ joinApprovalMode: !!getBinaryNodeChild(community, 'membership_approval_mode'),
417
+ memberAddMode,
418
+ participants: getBinaryNodeChildren(community, 'participant').map(({ attrs }) => {
419
+ return {
420
+ id: attrs.jid,
421
+ admin: (attrs.type || null)
422
+ };
423
+ }),
424
+ ephemeralDuration: eph ? +eph : undefined,
425
+ addressingMode: getBinaryNodeChildString(community, 'addressing_mode')
426
+ };
427
+ return metadata;
428
+ };
@@ -0,0 +1,254 @@
1
+ // dugong.d.ts
2
+ import { proto } from '../../WAProto';
3
+
4
+ declare namespace kikyy {
5
+ interface MediaUploadOptions {
6
+ fileEncSha256?: Buffer;
7
+ mediaType?: string;
8
+ newsletter?: boolean;
9
+ }
10
+
11
+ type WAMediaUploadFunction = (
12
+ stream: Buffer | NodeJS.ReadableStream,
13
+ options?: MediaUploadOptions
14
+ ) => Promise<{ url: string; directPath: string }>;
15
+
16
+ interface WAMessageContentGenerationOptions {
17
+ upload?: WAMediaUploadFunction;
18
+ mediaCache?: any;
19
+ options?: any;
20
+ logger?: any;
21
+ }
22
+
23
+ interface StickerMessage {
24
+ url: string;
25
+ fileSha256: Buffer | string;
26
+ fileEncSha256: Buffer | string;
27
+ mediaKey: Buffer | string;
28
+ mimetype: string;
29
+ directPath: string;
30
+ fileLength: number | string;
31
+ mediaKeyTimestamp: number | string;
32
+ isAnimated?: boolean;
33
+ stickerSentTs?: number | string;
34
+ isAvatar?: boolean;
35
+ isAiSticker?: boolean;
36
+ isLottie?: boolean;
37
+ }
38
+
39
+ interface PaymentMessage {
40
+ amount: number;
41
+ currency?: string;
42
+ from?: string;
43
+ expiry?: number;
44
+ sticker?: { stickerMessage: StickerMessage };
45
+ note?: string;
46
+ background?: {
47
+ id?: string;
48
+ fileLength?: string;
49
+ width?: number;
50
+ height?: number;
51
+ mimetype?: string;
52
+ placeholderArgb?: number;
53
+ textArgb?: number;
54
+ subtextArgb?: number;
55
+ };
56
+ }
57
+
58
+ interface ProductMessage {
59
+ title: string;
60
+ description: string;
61
+ thumbnail: Buffer | { url: string };
62
+ productId: string;
63
+ retailerId: string;
64
+ url: string;
65
+ body?: string;
66
+ footer?: string;
67
+ buttons?: proto.Message.InteractiveMessage.INativeFlowButton[];
68
+ priceAmount1000?: number | null;
69
+ currencyCode?: string;
70
+ }
71
+
72
+ interface InteractiveMessage {
73
+ header?: string;
74
+ title: string;
75
+ footer?: string;
76
+ thumbnail?: string;
77
+ image?: string | Buffer | { url: string };
78
+ video?: string | Buffer | { url: string };
79
+ document?: string | Buffer | { url: string };
80
+ mimetype?: string;
81
+ fileName?: string;
82
+ jpegThumbnail?: string | Buffer | { url: string };
83
+ contextInfo?: {
84
+ mentionedJid?: string[];
85
+ forwardingScore?: number;
86
+ isForwarded?: boolean;
87
+ externalAdReply?: {
88
+ title?: string;
89
+ body?: string;
90
+ mediaType?: number;
91
+ thumbnailUrl?: string;
92
+ mediaUrl?: string;
93
+ sourceUrl?: string;
94
+ showAdAttribution?: boolean;
95
+ renderLargerThumbnail?: boolean;
96
+ [key: string]: any;
97
+ };
98
+ [key: string]: any;
99
+ };
100
+ externalAdReply?: {
101
+ title?: string;
102
+ body?: string;
103
+ mediaType?: number;
104
+ thumbnailUrl?: string;
105
+ mediaUrl?: string;
106
+ sourceUrl?: string;
107
+ showAdAttribution?: boolean;
108
+ renderLargerThumbnail?: boolean;
109
+ [key: string]: any;
110
+ };
111
+ buttons?: proto.Message.InteractiveMessage.INativeFlowButton[];
112
+ nativeFlowMessage?: {
113
+ messageParamsJson?: string;
114
+ buttons?: proto.Message.InteractiveMessage.INativeFlowButton[];
115
+ [key: string]: any;
116
+ };
117
+ }
118
+
119
+ interface AlbumItem {
120
+ image?: string | Buffer | { url: string; caption?: string };
121
+ video?: string | Buffer | { url: string; caption?: string };
122
+ }
123
+
124
+ interface EventMessageLocation {
125
+ degreesLatitude: number;
126
+ degreesLongitude: number;
127
+ name: string;
128
+ }
129
+
130
+ interface EventMessage {
131
+ isCanceled?: boolean;
132
+ name: string;
133
+ description: string;
134
+ location?: EventMessageLocation;
135
+ joinLink?: string;
136
+ startTime?: string | number;
137
+ endTime?: string | number;
138
+ extraGuestsAllowed?: boolean;
139
+ }
140
+
141
+ interface PollVote {
142
+ optionName: string;
143
+ optionVoteCount: string | number;
144
+ }
145
+
146
+ interface PollResultMessage {
147
+ name: string;
148
+ pollVotes: PollVote[];
149
+ }
150
+
151
+ interface GroupStatusMessage {
152
+ message?: any;
153
+ image?: string | Buffer | { url: string };
154
+ video?: string | Buffer | { url: string };
155
+ text?: string;
156
+ caption?: string;
157
+ document?: string | Buffer | { url: string };
158
+ [key: string]: any;
159
+ }
160
+
161
+ interface MessageContent {
162
+ requestPaymentMessage?: PaymentMessage;
163
+ productMessage?: ProductMessage;
164
+ interactiveMessage?: InteractiveMessage;
165
+ albumMessage?: AlbumItem[];
166
+ eventMessage?: EventMessage;
167
+ pollResultMessage?: PollResultMessage;
168
+ groupStatusMessage?: GroupStatusMessage;
169
+ sender?: string;
170
+ }
171
+
172
+ interface MessageOptions {
173
+ quoted?: proto.IWebMessageInfo;
174
+ filter?: boolean;
175
+ }
176
+
177
+ interface Utils {
178
+ prepareWAMessageMedia: (media: any, options: WAMessageContentGenerationOptions) => Promise<any>;
179
+ generateWAMessageContent: (content: any, options: WAMessageContentGenerationOptions) => Promise<any>;
180
+ generateWAMessageFromContent: (jid: string, content: any, options?: any) => Promise<any>;
181
+ generateWAMessage: (jid: string, content: any, options?: any) => Promise<any>;
182
+ generateMessageID: () => string;
183
+ prepareMessageContent?: (content: any, options?: any) => Promise<any>;
184
+ }
185
+
186
+ interface BailUtils {
187
+ generateWAMessageContent?: (content: any, options: WAMessageContentGenerationOptions) => Promise<any>;
188
+ generateMessageID: () => string;
189
+ getContentType: (msg: any) => string;
190
+ }
191
+ }
192
+
193
+ declare class kikyy {
194
+ constructor(
195
+ utils: kikyy.Utils,
196
+ waUploadToServer: kikyy.WAMediaUploadFunction,
197
+ relayMessageFn?: (jid: string, content: any, options?: any) => Promise<any>
198
+ );
199
+
200
+ detectType(content: kikyy.MessageContent): 'PAYMENT' | 'PRODUCT' | 'INTERACTIVE' | 'ALBUM' | 'EVENT' | 'POLL_RESULT' | 'GROUP_STORY' | null;
201
+
202
+ handlePayment(
203
+ content: { requestPaymentMessage: kikyy.PaymentMessage },
204
+ quoted?: proto.IWebMessageInfo
205
+ ): Promise<{ requestPaymentMessage: proto.Message.RequestPaymentMessage }>;
206
+
207
+ handleProduct(
208
+ content: { productMessage: kikyy.ProductMessage },
209
+ jid: string,
210
+ quoted?: proto.IWebMessageInfo
211
+ ): Promise<{ viewOnceMessage: proto.Message.ViewOnceMessage }>;
212
+
213
+ handleInteractive(
214
+ content: { interactiveMessage: kikyy.InteractiveMessage },
215
+ jid: string,
216
+ quoted?: proto.IWebMessageInfo
217
+ ): Promise<{ interactiveMessage: proto.Message.InteractiveMessage }>;
218
+
219
+ handleAlbum(
220
+ content: { albumMessage: kikyy.AlbumItem[] },
221
+ jid: string,
222
+ quoted?: proto.IWebMessageInfo
223
+ ): Promise<any>;
224
+
225
+ handleEvent(
226
+ content: { eventMessage: kikyy.EventMessage },
227
+ jid: string,
228
+ quoted?: proto.IWebMessageInfo
229
+ ): Promise<any>;
230
+
231
+ handlePollResult(
232
+ content: { pollResultMessage: kikyy.PollResultMessage },
233
+ jid: string,
234
+ quoted?: proto.IWebMessageInfo
235
+ ): Promise<any>;
236
+
237
+ handleGroupStory(
238
+ content: { groupStatusMessage: kikyy.GroupStatusMessage },
239
+ jid: string,
240
+ quoted?: proto.IWebMessageInfo
241
+ ): Promise<any>;
242
+
243
+ buildMessageContent(
244
+ content: any,
245
+ opts?: kikyy.WAMessageContentGenerationOptions
246
+ ): Promise<any>;
247
+
248
+ utils: kikyy.Utils;
249
+ relayMessage: (jid: string, content: any, options?: any) => Promise<any>;
250
+ waUploadToServer: kikyy.WAMediaUploadFunction;
251
+ bail: kikyy.BailUtils;
252
+ }
253
+
254
+ export = kikyy;