@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,312 +1,347 @@
1
- //=======================================================//
2
- import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isLidUser, isPnUser, jidEncode, jidNormalizedUser } from "../WABinary/index.js";
3
- import { WAMessageAddressingMode, WAMessageStubType } from "../Types/index.js";
4
- import { generateMessageIDV2, unixTimestampSeconds } from "../Utils/index.js";
5
- import { proto } from "../../WAProto/index.js";
6
- import { makeChatsSocket } from "./chats.js";
7
- //=======================================================//
1
+ import { Boom } from '@hapi/boom';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { WAMessageAddressingMode, WAMessageStubType } from '../Types/index.js';
4
+ import { generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
5
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isLidUser, isPnUser, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
6
+ import { makeChatsSocket } from './chats.js';
8
7
  export const makeGroupsSocket = (config) => {
9
- const sock = makeChatsSocket(config);
10
- const { authState, ev, query, upsertMessage } = sock;
11
- const groupQuery = async (jid, type, content) => query({
12
- tag: "iq",
13
- attrs: {
14
- type,
15
- xmlns: "w:g2",
16
- to: jid
17
- },
18
- content
19
- });
20
- const groupMetadata = async (jid) => {
21
- const result = await groupQuery(jid, "get", [{ tag: "query", attrs: { request: "interactive" } }]);
22
- return extractGroupMetadata(result);
23
- };
24
- const groupFetchAllParticipating = async () => {
25
- const result = await query({
26
- tag: "iq",
27
- attrs: {
28
- to: "@g.us",
29
- xmlns: "w:g2",
30
- type: "get"
31
- },
32
- content: [
33
- {
34
- tag: "participating",
35
- attrs: {},
36
- content: [
37
- { tag: "participants", attrs: {} },
38
- { tag: "description", attrs: {} }
39
- ]
40
- }
41
- ]
8
+ const sock = makeChatsSocket(config);
9
+ const { authState, ev, query, upsertMessage } = sock;
10
+ const groupQuery = async (jid, type, content) => query({
11
+ tag: 'iq',
12
+ attrs: {
13
+ type,
14
+ xmlns: 'w:g2',
15
+ to: jid
16
+ },
17
+ content
42
18
  });
43
- const data = {};
44
- const groupsChild = getBinaryNodeChild(result, "groups");
45
- if (groupsChild) {
46
- const groups = getBinaryNodeChildren(groupsChild, "group");
47
- for (const groupNode of groups) {
48
- const meta = extractGroupMetadata({
49
- tag: "result",
50
- attrs: {},
51
- content: [groupNode]
19
+ const groupMetadata = async (jid) => {
20
+ const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
21
+ return extractGroupMetadata(result);
22
+ };
23
+ const groupFetchAllParticipating = 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: [
32
+ {
33
+ tag: 'participating',
34
+ attrs: {},
35
+ content: [
36
+ { tag: 'participants', attrs: {} },
37
+ { tag: 'description', attrs: {} }
38
+ ]
39
+ }
40
+ ]
52
41
  });
53
- data[meta.id] = meta;
54
- }
55
- }
56
- sock.ev.emit("groups.update", Object.values(data));
57
- return data;
58
- };
59
- sock.ws.on("CB:ib,,dirty", async (node) => {
60
- const { attrs } = getBinaryNodeChild(node, "dirty");
61
- if (attrs.type !== "groups") {
62
- return;
63
- }
64
- await groupFetchAllParticipating();
65
- await sock.cleanDirtyBits("groups");
66
- });
67
- return {
68
- ...sock,
69
- groupMetadata,
70
- groupCreate: async (subject, participants) => {
71
- const key = generateMessageIDV2();
72
- const result = await groupQuery("@g.us", "set", [
73
- {
74
- tag: "create",
75
- attrs: {
76
- subject,
77
- key
78
- },
79
- content: participants.map(jid => ({
80
- tag: "participant",
81
- attrs: { jid }
82
- }))
83
- }
84
- ]);
85
- return extractGroupMetadata(result);
86
- },
87
- groupLeave: async (id) => {
88
- await groupQuery("@g.us", "set", [
89
- {
90
- tag: "leave",
91
- attrs: {},
92
- content: [{ tag: "group", attrs: { id } }]
93
- }
94
- ]);
95
- },
96
- groupUpdateSubject: async (jid, subject) => {
97
- await groupQuery(jid, "set", [
98
- {
99
- tag: "subject",
100
- attrs: {},
101
- content: Buffer.from(subject, "utf-8")
102
- }
103
- ]);
104
- },
105
- groupRequestParticipantsList: async (jid) => {
106
- const result = await groupQuery(jid, "get", [
107
- {
108
- tag: "membership_approval_requests",
109
- attrs: {}
110
- }
111
- ]);
112
- const node = getBinaryNodeChild(result, "membership_approval_requests");
113
- const participants = getBinaryNodeChildren(node, "membership_approval_request");
114
- return participants.map(v => v.attrs);
115
- },
116
- groupRequestParticipantsUpdate: async (jid, participants, action) => {
117
- const result = await groupQuery(jid, "set", [
118
- {
119
- tag: "membership_requests_action",
120
- attrs: {},
121
- content: [
122
- {
123
- tag: action,
124
- attrs: {},
125
- content: participants.map(jid => ({
126
- tag: "participant",
127
- attrs: { jid }
128
- }))
42
+ const data = {};
43
+ const groupsChild = getBinaryNodeChild(result, 'groups');
44
+ if (groupsChild) {
45
+ const groups = getBinaryNodeChildren(groupsChild, 'group');
46
+ for (const groupNode of groups) {
47
+ const meta = extractGroupMetadata({
48
+ tag: 'result',
49
+ attrs: {},
50
+ content: [groupNode]
51
+ });
52
+ data[meta.id] = meta;
129
53
  }
130
- ]
131
54
  }
132
- ]);
133
- const node = getBinaryNodeChild(result, "membership_requests_action");
134
- const nodeAction = getBinaryNodeChild(node, action);
135
- const participantsAffected = getBinaryNodeChildren(nodeAction, "participant");
136
- return participantsAffected.map(p => {
137
- return { status: p.attrs.error || "200", jid: p.attrs.jid };
138
- });
139
- },
140
- groupParticipantsUpdate: async (jid, participants, action) => {
141
- const result = await groupQuery(jid, "set", [
142
- {
143
- tag: action,
144
- attrs: {},
145
- content: participants.map(jid => ({
146
- tag: "participant",
147
- attrs: { jid }
148
- }))
55
+ // TODO: properly parse LID / PN DATA
56
+ sock.ev.emit('groups.update', Object.values(data));
57
+ return data;
58
+ };
59
+ sock.ws.on('CB:ib,,dirty', async (node) => {
60
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
61
+ if (attrs.type !== 'groups') {
62
+ return;
149
63
  }
150
- ]);
151
- const node = getBinaryNodeChild(result, action);
152
- const participantsAffected = getBinaryNodeChildren(node, "participant");
153
- return participantsAffected.map(p => {
154
- return { status: p.attrs.error || "200", jid: p.attrs.jid, content: p };
155
- });
156
- },
157
- groupUpdateDescription: async (jid, description) => {
158
- const metadata = await groupMetadata(jid);
159
- const prev = metadata.descId ?? null;
160
- await groupQuery(jid, "set", [
161
- {
162
- tag: "description",
163
- attrs: {
164
- ...(description ? { id: generateMessageIDV2() } : { delete: "true" }),
165
- ...(prev ? { prev } : {})
166
- },
167
- content: description ? [{ tag: "body", attrs: {}, content: Buffer.from(description, "utf-8") }] : undefined
168
- }
169
- ]);
170
- },
171
- groupInviteCode: async (jid) => {
172
- const result = await groupQuery(jid, "get", [{ tag: "invite", attrs: {} }]);
173
- const inviteNode = getBinaryNodeChild(result, "invite");
174
- return inviteNode?.attrs.code;
175
- },
176
- groupRevokeInvite: async (jid) => {
177
- const result = await groupQuery(jid, "set", [{ tag: "invite", attrs: {} }]);
178
- const inviteNode = getBinaryNodeChild(result, "invite");
179
- return inviteNode?.attrs.code;
180
- },
181
- groupAcceptInvite: async (code) => {
182
- const results = await groupQuery("@g.us", "set", [{ tag: "invite", attrs: { code } }]);
183
- const result = getBinaryNodeChild(results, "group");
184
- return result?.attrs.jid;
185
- },
186
- groupRevokeInviteV4: async (groupJid, invitedJid) => {
187
- const result = await groupQuery(groupJid, "set", [
188
- { tag: "revoke", attrs: {}, content: [{ tag: "participant", attrs: { jid: invitedJid } }] }
189
- ]);
190
- return !!result;
191
- },
192
- groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
193
- key = typeof key === "string" ? { remoteJid: key } : key;
194
- const results = await groupQuery(inviteMessage.groupJid, "set", [
195
- {
196
- tag: "accept",
197
- attrs: {
198
- code: inviteMessage.inviteCode,
199
- expiration: inviteMessage.inviteExpiration.toString(),
200
- admin: key.remoteJid
201
- }
202
- }
203
- ]);
204
- if (key.id) {
205
- inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
206
- inviteMessage.inviteExpiration = 0;
207
- inviteMessage.inviteCode = "";
208
- ev.emit("messages.update", [
209
- {
210
- key,
211
- update: {
212
- message: {
213
- groupInviteMessage: inviteMessage
214
- }
64
+ await groupFetchAllParticipating();
65
+ await sock.cleanDirtyBits('groups');
66
+ });
67
+ return {
68
+ ...sock,
69
+ groupQuery,
70
+ groupMetadata,
71
+ groupCreate: async (subject, participants) => {
72
+ const key = generateMessageIDV2();
73
+ const result = await groupQuery('@g.us', 'set', [
74
+ {
75
+ tag: 'create',
76
+ attrs: {
77
+ subject,
78
+ key
79
+ },
80
+ content: participants.map(jid => ({
81
+ tag: 'participant',
82
+ attrs: { jid }
83
+ }))
84
+ }
85
+ ]);
86
+ return extractGroupMetadata(result);
87
+ },
88
+ groupLeave: async (id) => {
89
+ await groupQuery('@g.us', 'set', [
90
+ {
91
+ tag: 'leave',
92
+ attrs: {},
93
+ content: [{ tag: 'group', attrs: { id } }]
94
+ }
95
+ ]);
96
+ },
97
+ groupUpdateSubject: async (jid, subject) => {
98
+ await groupQuery(jid, 'set', [
99
+ {
100
+ tag: 'subject',
101
+ attrs: {},
102
+ content: Buffer.from(subject, 'utf-8')
103
+ }
104
+ ]);
105
+ },
106
+ groupRequestParticipantsList: async (jid) => {
107
+ const result = await groupQuery(jid, 'get', [
108
+ {
109
+ tag: 'membership_approval_requests',
110
+ attrs: {}
111
+ }
112
+ ]);
113
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
114
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
115
+ return participants.map(v => v.attrs);
116
+ },
117
+ groupRequestParticipantsUpdate: async (jid, participants, action) => {
118
+ const result = await groupQuery(jid, 'set', [
119
+ {
120
+ tag: 'membership_requests_action',
121
+ attrs: {},
122
+ content: [
123
+ {
124
+ tag: action,
125
+ attrs: {},
126
+ content: participants.map(jid => ({
127
+ tag: 'participant',
128
+ attrs: { jid }
129
+ }))
130
+ }
131
+ ]
132
+ }
133
+ ]);
134
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
135
+ const nodeAction = getBinaryNodeChild(node, action);
136
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
137
+ return participantsAffected.map(p => {
138
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
139
+ });
140
+ },
141
+ groupParticipantsUpdate: async (jid, participants, action) => {
142
+ const result = await groupQuery(jid, 'set', [
143
+ {
144
+ tag: action,
145
+ attrs: {},
146
+ content: participants.map(jid => ({
147
+ tag: 'participant',
148
+ attrs: { jid }
149
+ }))
150
+ }
151
+ ]);
152
+ const node = getBinaryNodeChild(result, action);
153
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
154
+ return participantsAffected.map(p => {
155
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
156
+ });
157
+ },
158
+ groupUpdateDescription: async (jid, description) => {
159
+ const metadata = await groupMetadata(jid);
160
+ const prev = metadata.descId ?? null;
161
+ await groupQuery(jid, 'set', [
162
+ {
163
+ tag: 'description',
164
+ attrs: {
165
+ ...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
166
+ ...(prev ? { prev } : {})
167
+ },
168
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
169
+ }
170
+ ]);
171
+ },
172
+ groupInviteCode: async (jid) => {
173
+ const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
174
+ const inviteNode = getBinaryNodeChild(result, 'invite');
175
+ return inviteNode?.attrs.code;
176
+ },
177
+ groupRevokeInvite: async (jid) => {
178
+ const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
179
+ const inviteNode = getBinaryNodeChild(result, 'invite');
180
+ return inviteNode?.attrs.code;
181
+ },
182
+ groupAcceptInvite: async (code) => {
183
+ const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
184
+ const result = getBinaryNodeChild(results, 'group');
185
+ return result?.attrs.jid;
186
+ },
187
+ /**
188
+ * revoke a v4 invite for someone
189
+ * @param groupJid group jid
190
+ * @param invitedJid jid of person you invited
191
+ * @returns true if successful
192
+ */
193
+ groupRevokeInviteV4: async (groupJid, invitedJid) => {
194
+ const result = await groupQuery(groupJid, 'set', [
195
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
196
+ ]);
197
+ return !!result;
198
+ },
199
+ /**
200
+ * accept a GroupInviteMessage
201
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
202
+ * @param inviteMessage the message to accept
203
+ */
204
+ groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
205
+ key = typeof key === 'string' ? { remoteJid: key } : key;
206
+ const results = await groupQuery(inviteMessage.groupJid, 'set', [
207
+ {
208
+ tag: 'accept',
209
+ attrs: {
210
+ code: inviteMessage.inviteCode,
211
+ expiration: inviteMessage.inviteExpiration.toString(),
212
+ admin: key.remoteJid
213
+ }
214
+ }
215
+ ]);
216
+ // if we have the full message key
217
+ // update the invite message to be expired
218
+ if (key.id) {
219
+ // create new invite message that is expired
220
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
221
+ inviteMessage.inviteExpiration = 0;
222
+ inviteMessage.inviteCode = '';
223
+ ev.emit('messages.update', [
224
+ {
225
+ key,
226
+ update: {
227
+ message: {
228
+ groupInviteMessage: inviteMessage
229
+ }
230
+ }
231
+ }
232
+ ]);
215
233
  }
216
- }
217
- ]);
218
- }
219
- await upsertMessage({
220
- key: {
221
- remoteJid: inviteMessage.groupJid,
222
- id: generateMessageIDV2(sock.user?.id),
223
- fromMe: false,
224
- participant: key.remoteJid
234
+ // generate the group add message
235
+ await upsertMessage({
236
+ key: {
237
+ remoteJid: inviteMessage.groupJid,
238
+ id: generateMessageIDV2(sock.user?.id),
239
+ fromMe: false,
240
+ participant: key.remoteJid
241
+ },
242
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
243
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
244
+ participant: key.remoteJid,
245
+ messageTimestamp: unixTimestampSeconds()
246
+ }, 'notify');
247
+ return results.attrs.from;
248
+ }),
249
+ groupGetInviteInfo: async (code) => {
250
+ const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
251
+ return extractGroupMetadata(results);
252
+ },
253
+ groupToggleEphemeral: async (jid, ephemeralExpiration) => {
254
+ const content = ephemeralExpiration
255
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
256
+ : { tag: 'not_ephemeral', attrs: {} };
257
+ await groupQuery(jid, 'set', [content]);
225
258
  },
226
- messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
227
- messageStubParameters: [JSON.stringify(authState.creds.me)],
228
- participant: key.remoteJid,
229
- messageTimestamp: unixTimestampSeconds()
230
- }, "notify");
231
- return results.attrs.from;
232
- }),
233
- groupGetInviteInfo: async (code) => {
234
- const results = await groupQuery("@g.us", "get", [{ tag: "invite", attrs: { code } }]);
235
- return extractGroupMetadata(results);
236
- },
237
- groupToggleEphemeral: async (jid, ephemeralExpiration) => {
238
- const content = ephemeralExpiration
239
- ? { tag: "ephemeral", attrs: { expiration: ephemeralExpiration.toString() } }
240
- : { tag: "not_ephemeral", attrs: {} };
241
- await groupQuery(jid, "set", [content]);
242
- },
243
- groupSettingUpdate: async (jid, setting) => {
244
- await groupQuery(jid, "set", [{ tag: setting, attrs: {} }]);
245
- },
246
- groupMemberAddMode: async (jid, mode) => {
247
- await groupQuery(jid, "set", [{ tag: "member_add_mode", attrs: {}, content: mode }]);
248
- },
249
- groupJoinApprovalMode: async (jid, mode) => {
250
- await groupQuery(jid, "set", [
251
- { tag: "membership_approval_mode", attrs: {}, content: [{ tag: "group_join", attrs: { state: mode } }] }
252
- ]);
253
- },
254
- groupFetchAllParticipating
255
- };
259
+ groupSettingUpdate: async (jid, setting) => {
260
+ await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
261
+ },
262
+ groupMemberAddMode: async (jid, mode) => {
263
+ await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
264
+ },
265
+ groupJoinApprovalMode: async (jid, mode) => {
266
+ await groupQuery(jid, 'set', [
267
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
268
+ ]);
269
+ },
270
+ groupFetchAllParticipating
271
+ };
256
272
  };
257
273
  export const extractGroupMetadata = (result) => {
258
- const group = getBinaryNodeChild(result, "group");
259
- const descChild = getBinaryNodeChild(group, "description");
260
- let desc;
261
- let descId;
262
- let descOwner;
263
- let descOwnerPn;
264
- let descTime;
265
- if (descChild) {
266
- desc = getBinaryNodeChildString(descChild, "body");
267
- descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
268
- descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
269
- descTime = +descChild.attrs.t;
270
- descId = descChild.attrs.id;
271
- }
272
- const groupId = group.attrs.id.includes("@") ? group.attrs.id : jidEncode(group.attrs.id, "g.us");
273
- const eph = getBinaryNodeChild(group, "ephemeral")?.attrs.expiration;
274
- const memberAddMode = getBinaryNodeChildString(group, "member_add_mode") === "all_member_add";
275
- const metadata = {
276
- id: groupId,
277
- notify: group.attrs.notify,
278
- addressingMode: group.attrs.addressing_mode === "lid" ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
279
- subject: group.attrs.subject,
280
- subjectOwner: group.attrs.s_o,
281
- subjectOwnerPn: group.attrs.s_o_pn,
282
- subjectTime: +group.attrs.s_t,
283
- size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, "participant").length,
284
- creation: +group.attrs.creation,
285
- owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
286
- ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
287
- owner_country_code: group.attrs.creator_country_code,
288
- desc,
289
- descId,
290
- descOwner,
291
- descOwnerPn,
292
- descTime,
293
- linkedParent: getBinaryNodeChild(group, "linked_parent")?.attrs.jid || undefined,
294
- restrict: !!getBinaryNodeChild(group, "locked"),
295
- announce: !!getBinaryNodeChild(group, "announcement"),
296
- isCommunity: !!getBinaryNodeChild(group, "parent"),
297
- isCommunityAnnounce: !!getBinaryNodeChild(group, "default_sub_group"),
298
- joinApprovalMode: !!getBinaryNodeChild(group, "membership_approval_mode"),
299
- memberAddMode,
300
- participants: getBinaryNodeChildren(group, "participant").map(({ attrs }) => {
301
- return {
302
- id: attrs.jid,
303
- jid: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
304
- lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
305
- admin: (attrs.type || null)
306
- };
307
- }),
308
- ephemeralDuration: eph ? +eph : undefined
309
- };
310
- return metadata;
274
+ const group = getBinaryNodeChild(result, 'group');
275
+ if (!group) {
276
+ // Mirror WAWeb: surface server/client errors with their code+text instead of crashing.
277
+ const errorNode = getBinaryNodeChild(result, 'error');
278
+ if (errorNode) {
279
+ const code = errorNode.attrs.code ? +errorNode.attrs.code : 500;
280
+ const text = errorNode.attrs.text || 'group metadata query failed';
281
+ throw new Boom(text, { statusCode: code, data: errorNode });
282
+ }
283
+ throw new Boom('Invalid group metadata response: missing <group> node', { data: result });
284
+ }
285
+ if (!group.attrs.id) {
286
+ throw new Boom('Invalid group metadata response: missing group id', { data: group });
287
+ }
288
+ const descChild = getBinaryNodeChild(group, 'description');
289
+ let desc;
290
+ let descId;
291
+ let descOwner;
292
+ let descOwnerPn;
293
+ let descOwnerUsername;
294
+ let descTime;
295
+ if (descChild) {
296
+ desc = getBinaryNodeChildString(descChild, 'body');
297
+ descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
298
+ descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
299
+ descOwnerUsername = descChild.attrs.participant_username || undefined;
300
+ descTime = +descChild.attrs.t;
301
+ descId = descChild.attrs.id;
302
+ }
303
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
304
+ const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
305
+ const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
306
+ const metadata = {
307
+ id: groupId,
308
+ notify: group.attrs.notify,
309
+ addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
310
+ subject: group.attrs.subject,
311
+ subjectOwner: group.attrs.s_o,
312
+ subjectOwnerPn: group.attrs.s_o_pn,
313
+ subjectOwnerUsername: group.attrs.s_o_username,
314
+ subjectTime: +group.attrs.s_t,
315
+ size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
316
+ creation: +group.attrs.creation,
317
+ owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
318
+ ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
319
+ ownerUsername: group.attrs.creator_username || undefined,
320
+ owner_country_code: group.attrs.creator_country_code,
321
+ desc,
322
+ descId,
323
+ descOwner,
324
+ descOwnerPn,
325
+ descOwnerUsername,
326
+ descTime,
327
+ linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
328
+ restrict: !!getBinaryNodeChild(group, 'locked'),
329
+ announce: !!getBinaryNodeChild(group, 'announcement'),
330
+ isCommunity: !!getBinaryNodeChild(group, 'parent'),
331
+ isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
332
+ joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
333
+ memberAddMode,
334
+ participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
335
+ // TODO: Store LID MAPPINGS
336
+ return {
337
+ id: attrs.jid,
338
+ phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
339
+ lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
340
+ username: attrs.participant_username || attrs.username || undefined,
341
+ admin: (attrs.type || null)
342
+ };
343
+ }),
344
+ ephemeralDuration: eph ? +eph : undefined
345
+ };
346
+ return metadata;
311
347
  };
312
- //=======================================================//