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