@mrlegendbot/baileys 1.2.4 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/fix-imports.js +85 -0
  3. package/WAProto/index.d.ts +14017 -0
  4. package/WAProto/index.js +97691 -2
  5. package/lib/Defaults/index.d.ts +75 -0
  6. package/lib/Defaults/index.js +92 -89
  7. package/lib/Menu/index.d.ts +42 -0
  8. package/lib/Menu/index.js +283 -0
  9. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  10. package/lib/Signal/Group/ciphertext-message.js +10 -9
  11. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  12. package/lib/Signal/Group/group-session-builder.js +22 -21
  13. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  14. package/lib/Signal/Group/group_cipher.js +69 -75
  15. package/lib/Signal/Group/index.d.ts +12 -0
  16. package/lib/Signal/Group/index.js +1 -0
  17. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  18. package/lib/Signal/Group/keyhelper.js +10 -9
  19. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  20. package/lib/Signal/Group/sender-chain-key.js +22 -21
  21. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  22. package/lib/Signal/Group/sender-key-distribution-message.js +60 -51
  23. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  24. package/lib/Signal/Group/sender-key-message.js +62 -56
  25. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  26. package/lib/Signal/Group/sender-key-name.js +40 -38
  27. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  28. package/lib/Signal/Group/sender-key-record.js +34 -33
  29. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  30. package/lib/Signal/Group/sender-key-state.js +76 -72
  31. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  32. package/lib/Signal/Group/sender-message-key.js +23 -22
  33. package/lib/Signal/libsignal.d.ts +5 -0
  34. package/lib/Signal/libsignal.js +405 -506
  35. package/lib/Signal/lid-mapping.d.ts +23 -0
  36. package/lib/Signal/lid-mapping.js +258 -276
  37. package/lib/Socket/Client/index.d.ts +3 -0
  38. package/lib/Socket/Client/index.js +1 -0
  39. package/lib/Socket/Client/types.d.ts +16 -0
  40. package/lib/Socket/Client/types.js +7 -6
  41. package/lib/Socket/Client/websocket.d.ts +13 -0
  42. package/lib/Socket/Client/websocket.js +49 -48
  43. package/lib/Socket/business.d.ts +217 -0
  44. package/lib/Socket/business.js +369 -349
  45. package/lib/Socket/chats.d.ts +125 -0
  46. package/lib/Socket/chats.js +1148 -1172
  47. package/lib/Socket/communities.d.ts +273 -0
  48. package/lib/Socket/communities.js +413 -473
  49. package/lib/Socket/groups.d.ts +161 -0
  50. package/lib/Socket/groups.js +331 -387
  51. package/lib/Socket/index.d.ts +267 -0
  52. package/lib/Socket/index.js +19 -6
  53. package/lib/Socket/messages-recv.d.ts +203 -0
  54. package/lib/Socket/messages-recv.js +1719 -1782
  55. package/lib/Socket/messages-send.d.ts +199 -0
  56. package/lib/Socket/messages-send.js +2451 -1504
  57. package/lib/Socket/mex.d.ts +3 -0
  58. package/lib/Socket/mex.js +35 -40
  59. package/lib/Socket/newsletter.d.ts +170 -0
  60. package/lib/Socket/newsletter.js +191 -239
  61. package/lib/Socket/socket.d.ts +59 -0
  62. package/lib/Socket/socket.js +923 -992
  63. package/lib/Types/Auth.d.ts +117 -0
  64. package/lib/Types/Auth.js +1 -0
  65. package/lib/Types/Bussines.d.ts +25 -0
  66. package/lib/Types/Bussines.js +1 -0
  67. package/lib/Types/Call.d.ts +15 -0
  68. package/lib/Types/Call.js +1 -0
  69. package/lib/Types/Chat.d.ts +124 -0
  70. package/lib/Types/Chat.js +8 -1
  71. package/lib/Types/Contact.d.ts +26 -0
  72. package/lib/Types/Contact.js +1 -0
  73. package/lib/Types/Events.d.ts +256 -0
  74. package/lib/Types/Events.js +2 -1
  75. package/lib/Types/GroupMetadata.d.ts +71 -0
  76. package/lib/Types/GroupMetadata.js +1 -0
  77. package/lib/Types/Label.d.ts +47 -0
  78. package/lib/Types/Label.js +22 -20
  79. package/lib/Types/LabelAssociation.d.ts +30 -0
  80. package/lib/Types/LabelAssociation.js +4 -2
  81. package/lib/Types/Message.d.ts +320 -0
  82. package/lib/Types/Message.js +5 -3
  83. package/lib/Types/Mex.d.ts +141 -0
  84. package/lib/Types/Mex.js +31 -26
  85. package/lib/Types/Product.d.ts +79 -0
  86. package/lib/Types/Product.js +1 -0
  87. package/lib/Types/Signal.d.ts +87 -0
  88. package/lib/Types/Signal.js +2 -1
  89. package/lib/Types/Socket.d.ts +136 -0
  90. package/lib/Types/Socket.js +3 -2
  91. package/lib/Types/State.d.ts +97 -0
  92. package/lib/Types/State.js +52 -4
  93. package/lib/Types/USync.d.ts +26 -0
  94. package/lib/Types/USync.js +1 -0
  95. package/lib/Types/index.d.ts +66 -0
  96. package/lib/Types/index.js +11 -10
  97. package/lib/Utils/auth-utils.d.ts +24 -0
  98. package/lib/Utils/auth-utils.js +277 -240
  99. package/lib/Utils/browser-utils.d.ts +4 -0
  100. package/lib/Utils/browser-utils.js +23 -20
  101. package/lib/Utils/business.d.ts +23 -0
  102. package/lib/Utils/business.js +204 -200
  103. package/lib/Utils/chat-utils.d.ts +100 -0
  104. package/lib/Utils/chat-utils.js +810 -836
  105. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  106. package/lib/Utils/companion-reg-client-utils.js +25 -23
  107. package/lib/Utils/crypto.d.ts +37 -0
  108. package/lib/Utils/crypto.js +86 -70
  109. package/lib/Utils/decode-wa-message.d.ts +66 -0
  110. package/lib/Utils/decode-wa-message.js +284 -277
  111. package/lib/Utils/event-buffer.d.ts +36 -0
  112. package/lib/Utils/event-buffer.js +595 -592
  113. package/lib/Utils/generics.d.ts +92 -0
  114. package/lib/Utils/generics.js +335 -312
  115. package/lib/Utils/history.d.ts +24 -0
  116. package/lib/Utils/history.js +115 -123
  117. package/lib/Utils/identity-change-handler.d.ts +44 -0
  118. package/lib/Utils/identity-change-handler.js +50 -0
  119. package/lib/Utils/index.d.ts +22 -0
  120. package/lib/Utils/index.js +2 -1
  121. package/lib/Utils/link-preview.d.ts +21 -0
  122. package/lib/Utils/link-preview.js +73 -74
  123. package/lib/Utils/logger.d.ts +12 -0
  124. package/lib/Utils/logger.js +2 -3
  125. package/lib/Utils/lt-hash.d.ts +8 -0
  126. package/lib/Utils/lt-hash.js +6 -0
  127. package/lib/Utils/make-mutex.d.ts +9 -0
  128. package/lib/Utils/make-mutex.js +27 -27
  129. package/lib/Utils/message-retry-manager.d.ts +115 -0
  130. package/lib/Utils/message-retry-manager.js +254 -213
  131. package/lib/Utils/messages-media.d.ts +133 -0
  132. package/lib/Utils/messages-media.js +715 -741
  133. package/lib/Utils/messages.d.ts +91 -0
  134. package/lib/Utils/messages.js +827 -791
  135. package/lib/Utils/noise-handler.d.ts +20 -0
  136. package/lib/Utils/noise-handler.js +187 -215
  137. package/lib/Utils/offline-node-processor.d.ts +17 -0
  138. package/lib/Utils/offline-node-processor.js +40 -0
  139. package/lib/Utils/pre-key-manager.d.ts +28 -0
  140. package/lib/Utils/pre-key-manager.js +98 -69
  141. package/lib/Utils/process-message.d.ts +60 -0
  142. package/lib/Utils/process-message.js +586 -617
  143. package/lib/Utils/reporting-utils.d.ts +11 -0
  144. package/lib/Utils/reporting-utils.js +234 -553
  145. package/lib/Utils/signal.d.ts +47 -0
  146. package/lib/Utils/signal.js +170 -217
  147. package/lib/Utils/stanza-ack.d.ts +11 -0
  148. package/lib/Utils/stanza-ack.js +35 -27
  149. package/lib/Utils/sync-action-utils.d.ts +19 -0
  150. package/lib/Utils/sync-action-utils.js +41 -37
  151. package/lib/Utils/tc-token-utils.d.ts +37 -0
  152. package/lib/Utils/tc-token-utils.js +141 -120
  153. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  154. package/lib/Utils/use-multi-file-auth-state.js +105 -87
  155. package/lib/Utils/validate-connection.d.ts +11 -0
  156. package/lib/Utils/validate-connection.js +188 -190
  157. package/lib/WABinary/constants.d.ts +28 -0
  158. package/lib/WABinary/constants.js +1294 -28
  159. package/lib/WABinary/decode.d.ts +7 -0
  160. package/lib/WABinary/decode.js +249 -245
  161. package/lib/WABinary/encode.d.ts +3 -0
  162. package/lib/WABinary/encode.js +206 -201
  163. package/lib/WABinary/generic-utils.d.ts +15 -0
  164. package/lib/WABinary/generic-utils.js +89 -86
  165. package/lib/WABinary/index.d.ts +6 -0
  166. package/lib/WABinary/index.js +1 -0
  167. package/lib/WABinary/jid-utils.d.ts +48 -0
  168. package/lib/WABinary/jid-utils.js +77 -68
  169. package/lib/WABinary/types.d.ts +19 -0
  170. package/lib/WABinary/types.js +1 -0
  171. package/lib/WAM/BinaryInfo.d.ts +9 -0
  172. package/lib/WAM/BinaryInfo.js +8 -7
  173. package/lib/WAM/constants.d.ts +40 -0
  174. package/lib/WAM/constants.js +22853 -19892
  175. package/lib/WAM/encode.d.ts +3 -0
  176. package/lib/WAM/encode.js +132 -121
  177. package/lib/WAM/index.d.ts +4 -0
  178. package/lib/WAM/index.js +1 -0
  179. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  180. package/lib/WAUSync/Protocols/USyncContactProtocol.js +43 -46
  181. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  182. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -47
  183. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  184. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -22
  185. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  186. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -30
  187. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  188. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +20 -19
  189. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  190. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +44 -53
  191. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  192. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  193. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  194. package/lib/WAUSync/Protocols/index.js +1 -0
  195. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  196. package/lib/WAUSync/USyncQuery.js +88 -79
  197. package/lib/WAUSync/USyncUser.d.ts +17 -0
  198. package/lib/WAUSync/USyncUser.js +29 -28
  199. package/lib/WAUSync/index.d.ts +4 -0
  200. package/lib/WAUSync/index.js +1 -0
  201. package/lib/index.d.ts +12 -0
  202. package/lib/index.js +2 -2
  203. package/package.json +86 -22
  204. package/README.upstream.md +0 -1312
  205. package/WAProto/WAProto.json +0 -1
  206. package/WAProto/WAProto.proto +0 -6275
  207. package/WAProto/compiler.js +0 -550
  208. package/lib/Store/index.js +0 -8
  209. package/lib/Store/keyed-db.js +0 -108
  210. package/lib/Store/make-cache-manager-store.js +0 -70
  211. package/lib/Store/make-in-memory-store.js +0 -417
  212. package/lib/Store/make-ordered-dictionary.js +0 -69
  213. package/lib/Store/object-repository.js +0 -27
  214. package/lib/Utils/use-sqlite-auth-state.js +0 -162
@@ -1,403 +1,347 @@
1
1
  import { Boom } from '@hapi/boom';
2
- import { proto } from '../../WAProto/compiler.js';
2
+ import { proto } from '../../WAProto/index.js';
3
3
  import { WAMessageAddressingMode, WAMessageStubType } from '../Types/index.js';
4
4
  import { generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
5
5
  import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, isLidUser, isPnUser, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
6
6
  import { makeChatsSocket } from './chats.js';
7
- export const makeGroupsSocket = config => {
8
- const sock = makeChatsSocket(config);
9
- const {
10
- authState,
11
- ev,
12
- query,
13
- upsertMessage
14
- } = sock;
15
- const groupQuery = async (jid, type, content) => query({
16
- tag: 'iq',
17
- attrs: {
18
- type,
19
- xmlns: 'w:g2',
20
- to: jid
21
- },
22
- content
23
- });
24
- const groupMetadata = async jid => {
25
- const result = await groupQuery(jid, 'get', [{
26
- tag: 'query',
27
- attrs: {
28
- request: 'interactive'
29
- }
30
- }]);
31
- return extractGroupMetadata(result);
32
- };
33
- const groupFetchAllParticipating = async () => {
34
- const result = await query({
35
- tag: 'iq',
36
- attrs: {
37
- to: '@g.us',
38
- xmlns: 'w:g2',
39
- type: 'get'
40
- },
41
- content: [{
42
- tag: 'participating',
43
- attrs: {},
44
- content: [{
45
- tag: 'participants',
46
- attrs: {}
47
- }, {
48
- tag: 'description',
49
- attrs: {}
50
- }]
51
- }]
52
- });
53
- const data = {};
54
- const groupsChild = getBinaryNodeChild(result, 'groups');
55
- if (groupsChild) {
56
- const groups = getBinaryNodeChildren(groupsChild, 'group');
57
- for (const groupNode of groups) {
58
- const meta = extractGroupMetadata({
59
- tag: 'result',
60
- attrs: {},
61
- content: [groupNode]
62
- });
63
- data[meta.id] = meta;
64
- }
65
- }
66
- sock.ev.emit('groups.update', Object.values(data));
67
- return data;
68
- };
69
- sock.ws.on('CB:ib,,dirty', async node => {
70
- const {
71
- attrs
72
- } = getBinaryNodeChild(node, 'dirty');
73
- if (attrs.type !== 'groups') {
74
- return;
75
- }
76
- await groupFetchAllParticipating();
77
- await sock.cleanDirtyBits('groups');
78
- });
79
- return {
80
- ...sock,
81
- groupMetadata,
82
- groupCreate: async (subject, participants) => {
83
- const key = generateMessageIDV2();
84
- const result = await groupQuery('@g.us', 'set', [{
85
- tag: 'create',
7
+ export const makeGroupsSocket = (config) => {
8
+ const sock = makeChatsSocket(config);
9
+ const { authState, ev, query, upsertMessage } = sock;
10
+ const groupQuery = async (jid, type, content) => query({
11
+ tag: 'iq',
86
12
  attrs: {
87
- subject,
88
- key
13
+ type,
14
+ xmlns: 'w:g2',
15
+ to: jid
89
16
  },
90
- content: participants.map(jid => ({
91
- tag: 'participant',
92
- attrs: {
93
- jid
94
- }
95
- }))
96
- }]);
97
- return extractGroupMetadata(result);
98
- },
99
- groupLeave: async id => {
100
- await groupQuery('@g.us', 'set', [{
101
- tag: 'leave',
102
- attrs: {},
103
- content: [{
104
- tag: 'group',
105
- attrs: {
106
- id
107
- }
108
- }]
109
- }]);
110
- },
111
- groupUpdateSubject: async (jid, subject) => {
112
- await groupQuery(jid, 'set', [{
113
- tag: 'subject',
114
- attrs: {},
115
- content: Buffer.from(subject, 'utf-8')
116
- }]);
117
- },
118
- groupRequestParticipantsList: async jid => {
119
- const result = await groupQuery(jid, 'get', [{
120
- tag: 'membership_approval_requests',
121
- attrs: {}
122
- }]);
123
- const node = getBinaryNodeChild(result, 'membership_approval_requests');
124
- const participants = getBinaryNodeChildren(node, 'membership_approval_request');
125
- return participants.map(v => v.attrs);
126
- },
127
- groupRequestParticipantsUpdate: async (jid, participants, action) => {
128
- const result = await groupQuery(jid, 'set', [{
129
- tag: 'membership_requests_action',
130
- attrs: {},
131
- content: [{
132
- tag: action,
133
- attrs: {},
134
- content: participants.map(jid => ({
135
- tag: 'participant',
17
+ content
18
+ });
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',
136
26
  attrs: {
137
- jid
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
+ ]
41
+ });
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;
138
53
  }
139
- }))
140
- }]
141
- }]);
142
- const node = getBinaryNodeChild(result, 'membership_requests_action');
143
- const nodeAction = getBinaryNodeChild(node, action);
144
- const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
145
- return participantsAffected.map(p => {
146
- return {
147
- status: p.attrs.error || '200',
148
- jid: p.attrs.jid
149
- };
150
- });
151
- },
152
- groupParticipantsUpdate: async (jid, participants, action) => {
153
- const result = await groupQuery(jid, 'set', [{
154
- tag: action,
155
- attrs: {},
156
- content: participants.map(jid => ({
157
- tag: 'participant',
158
- attrs: {
159
- jid
160
- }
161
- }))
162
- }]);
163
- const node = getBinaryNodeChild(result, action);
164
- const participantsAffected = getBinaryNodeChildren(node, 'participant');
165
- return participantsAffected.map(p => {
166
- return {
167
- status: p.attrs.error || '200',
168
- jid: p.attrs.jid,
169
- content: p
170
- };
171
- });
172
- },
173
- groupUpdateDescription: async (jid, description) => {
174
- const metadata = await groupMetadata(jid);
175
- const prev = metadata.descId ?? null;
176
- await groupQuery(jid, 'set', [{
177
- tag: 'description',
178
- attrs: {
179
- ...(description ? {
180
- id: generateMessageIDV2()
181
- } : {
182
- delete: 'true'
183
- }),
184
- ...(prev ? {
185
- prev
186
- } : {})
187
- },
188
- content: description ? [{
189
- tag: 'body',
190
- attrs: {},
191
- content: Buffer.from(description, 'utf-8')
192
- }] : undefined
193
- }]);
194
- },
195
- groupInviteCode: async jid => {
196
- const result = await groupQuery(jid, 'get', [{
197
- tag: 'invite',
198
- attrs: {}
199
- }]);
200
- const inviteNode = getBinaryNodeChild(result, 'invite');
201
- return inviteNode?.attrs.code;
202
- },
203
- groupRevokeInvite: async jid => {
204
- const result = await groupQuery(jid, 'set', [{
205
- tag: 'invite',
206
- attrs: {}
207
- }]);
208
- const inviteNode = getBinaryNodeChild(result, 'invite');
209
- return inviteNode?.attrs.code;
210
- },
211
- groupAcceptInvite: async code => {
212
- const results = await groupQuery('@g.us', 'set', [{
213
- tag: 'invite',
214
- attrs: {
215
- code
216
54
  }
217
- }]);
218
- const result = getBinaryNodeChild(results, 'group');
219
- return result?.attrs.jid;
220
- },
221
- groupRevokeInviteV4: async (groupJid, invitedJid) => {
222
- const result = await groupQuery(groupJid, 'set', [{
223
- tag: 'revoke',
224
- attrs: {},
225
- content: [{
226
- tag: 'participant',
227
- attrs: {
228
- jid: invitedJid
229
- }
230
- }]
231
- }]);
232
- return !!result;
233
- },
234
- groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
235
- key = typeof key === 'string' ? {
236
- remoteJid: key
237
- } : key;
238
- const results = await groupQuery(inviteMessage.groupJid, 'set', [{
239
- tag: 'accept',
240
- attrs: {
241
- code: inviteMessage.inviteCode,
242
- expiration: inviteMessage.inviteExpiration.toString(),
243
- admin: key.remoteJid
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;
244
63
  }
245
- }]);
246
- if (key.id) {
247
- inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
248
- inviteMessage.inviteExpiration = 0;
249
- inviteMessage.inviteCode = '';
250
- ev.emit('messages.update', [{
251
- key,
252
- update: {
253
- message: {
254
- groupInviteMessage: inviteMessage
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
+ }))
129
+ }
130
+ ]
131
+ }
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
+ }))
149
+ }
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
+ /**
187
+ * revoke a v4 invite for someone
188
+ * @param groupJid group jid
189
+ * @param invitedJid jid of person you invited
190
+ * @returns true if successful
191
+ */
192
+ groupRevokeInviteV4: async (groupJid, invitedJid) => {
193
+ const result = await groupQuery(groupJid, 'set', [
194
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
195
+ ]);
196
+ return !!result;
197
+ },
198
+ /**
199
+ * accept a GroupInviteMessage
200
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
201
+ * @param inviteMessage the message to accept
202
+ */
203
+ groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
204
+ key = typeof key === 'string' ? { remoteJid: key } : key;
205
+ const results = await groupQuery(inviteMessage.groupJid, 'set', [
206
+ {
207
+ tag: 'accept',
208
+ attrs: {
209
+ code: inviteMessage.inviteCode,
210
+ expiration: inviteMessage.inviteExpiration.toString(),
211
+ admin: key.remoteJid
212
+ }
213
+ }
214
+ ]);
215
+ // if we have the full message key
216
+ // update the invite message to be expired
217
+ if (key.id) {
218
+ // create new invite message that is expired
219
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
220
+ inviteMessage.inviteExpiration = 0;
221
+ inviteMessage.inviteCode = '';
222
+ ev.emit('messages.update', [
223
+ {
224
+ key,
225
+ update: {
226
+ message: {
227
+ groupInviteMessage: inviteMessage
228
+ }
229
+ }
230
+ }
231
+ ]);
255
232
  }
256
- }
257
- }]);
258
- }
259
- await upsertMessage({
260
- key: {
261
- remoteJid: inviteMessage.groupJid,
262
- id: generateMessageIDV2(sock.user?.id),
263
- fromMe: false,
264
- participant: key.remoteJid
233
+ // generate the group add message
234
+ await upsertMessage({
235
+ key: {
236
+ remoteJid: inviteMessage.groupJid,
237
+ id: generateMessageIDV2(sock.user?.id),
238
+ fromMe: false,
239
+ participant: key.remoteJid
240
+ },
241
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
242
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
243
+ participant: key.remoteJid,
244
+ messageTimestamp: unixTimestampSeconds()
245
+ }, 'notify');
246
+ return results.attrs.from;
247
+ }),
248
+ groupGetInviteInfo: async (code) => {
249
+ const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
250
+ return extractGroupMetadata(results);
265
251
  },
266
- messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
267
- messageStubParameters: [JSON.stringify(authState.creds.me)],
268
- participant: key.remoteJid,
269
- messageTimestamp: unixTimestampSeconds()
270
- }, 'notify');
271
- return results.attrs.from;
272
- }),
273
- groupGetInviteInfo: async code => {
274
- const results = await groupQuery('@g.us', 'get', [{
275
- tag: 'invite',
276
- attrs: {
277
- code
278
- }
279
- }]);
280
- return extractGroupMetadata(results);
281
- },
282
- groupToggleEphemeral: async (jid, ephemeralExpiration) => {
283
- const content = ephemeralExpiration ? {
284
- tag: 'ephemeral',
285
- attrs: {
286
- expiration: ephemeralExpiration.toString()
287
- }
288
- } : {
289
- tag: 'not_ephemeral',
290
- attrs: {}
291
- };
292
- await groupQuery(jid, 'set', [content]);
293
- },
294
- groupSettingUpdate: async (jid, setting) => {
295
- await groupQuery(jid, 'set', [{
296
- tag: setting,
297
- attrs: {}
298
- }]);
299
- },
300
- groupMemberAddMode: async (jid, mode) => {
301
- await groupQuery(jid, 'set', [{
302
- tag: 'member_add_mode',
303
- attrs: {},
304
- content: mode
305
- }]);
306
- },
307
- groupJoinApprovalMode: async (jid, mode) => {
308
- await groupQuery(jid, 'set', [{
309
- tag: 'membership_approval_mode',
310
- attrs: {},
311
- content: [{
312
- tag: 'group_join',
313
- attrs: {
314
- state: mode
315
- }
316
- }]
317
- }]);
318
- },
319
- groupFetchAllParticipating
320
- };
252
+ groupToggleEphemeral: async (jid, ephemeralExpiration) => {
253
+ const content = ephemeralExpiration
254
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
255
+ : { tag: 'not_ephemeral', attrs: {} };
256
+ await groupQuery(jid, 'set', [content]);
257
+ },
258
+ groupSettingUpdate: async (jid, setting) => {
259
+ await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
260
+ },
261
+ groupMemberAddMode: async (jid, mode) => {
262
+ await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
263
+ },
264
+ groupJoinApprovalMode: async (jid, mode) => {
265
+ await groupQuery(jid, 'set', [
266
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
267
+ ]);
268
+ },
269
+ groupFetchAllParticipating
270
+ };
321
271
  };
322
- export const extractGroupMetadata = result => {
323
- const group = getBinaryNodeChild(result, 'group');
324
- if (!group) {
325
- const errorNode = getBinaryNodeChild(result, 'error');
326
- if (errorNode) {
327
- const code = errorNode.attrs.code ? +errorNode.attrs.code : 500;
328
- const text = errorNode.attrs.text || 'group metadata query failed';
329
- throw new Boom(text, {
330
- statusCode: code,
331
- data: errorNode
332
- });
272
+ export const extractGroupMetadata = (result) => {
273
+ const group = getBinaryNodeChild(result, 'group');
274
+ if (!group) {
275
+ // Mirror WAWeb: surface server/client errors with their code+text instead of crashing.
276
+ const errorNode = getBinaryNodeChild(result, 'error');
277
+ if (errorNode) {
278
+ const code = errorNode.attrs.code ? +errorNode.attrs.code : 500;
279
+ const text = errorNode.attrs.text || 'group metadata query failed';
280
+ throw new Boom(text, { statusCode: code, data: errorNode });
281
+ }
282
+ throw new Boom('Invalid group metadata response: missing <group> node', { data: result });
333
283
  }
334
- throw new Boom('Invalid group metadata response: missing <group> node', {
335
- data: result
336
- });
337
- }
338
- if (!group.attrs.id) {
339
- throw new Boom('Invalid group metadata response: missing group id', {
340
- data: group
341
- });
342
- }
343
- const descChild = getBinaryNodeChild(group, 'description');
344
- let desc;
345
- let descId;
346
- let descOwner;
347
- let descOwnerPn;
348
- let descOwnerUsername;
349
- let descTime;
350
- if (descChild) {
351
- desc = getBinaryNodeChildString(descChild, 'body');
352
- descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
353
- descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
354
- descOwnerUsername = descChild.attrs.participant_username || undefined;
355
- descTime = +descChild.attrs.t;
356
- descId = descChild.attrs.id;
357
- }
358
- const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
359
- const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
360
- const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
361
- const metadata = {
362
- id: groupId,
363
- notify: group.attrs.notify,
364
- addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
365
- subject: group.attrs.subject,
366
- subjectOwner: group.attrs.s_o,
367
- subjectOwnerPn: group.attrs.s_o_pn,
368
- subjectOwnerUsername: group.attrs.s_o_username,
369
- subjectTime: +group.attrs.s_t,
370
- size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
371
- creation: +group.attrs.creation,
372
- owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
373
- ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
374
- ownerUsername: group.attrs.creator_username || undefined,
375
- owner_country_code: group.attrs.creator_country_code,
376
- desc,
377
- descId,
378
- descOwner,
379
- descOwnerPn,
380
- descOwnerUsername,
381
- descTime,
382
- linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
383
- restrict: !!getBinaryNodeChild(group, 'locked'),
384
- announce: !!getBinaryNodeChild(group, 'announcement'),
385
- isCommunity: !!getBinaryNodeChild(group, 'parent'),
386
- isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
387
- joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
388
- memberAddMode,
389
- participants: getBinaryNodeChildren(group, 'participant').map(({
390
- attrs
391
- }) => {
392
- return {
393
- id: attrs.jid,
394
- phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
395
- lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
396
- username: attrs.participant_username || attrs.username || undefined,
397
- admin: attrs.type || null
398
- };
399
- }),
400
- ephemeralDuration: eph ? +eph : undefined
401
- };
402
- return metadata;
284
+ if (!group.attrs.id) {
285
+ throw new Boom('Invalid group metadata response: missing group id', { data: group });
286
+ }
287
+ const descChild = getBinaryNodeChild(group, 'description');
288
+ let desc;
289
+ let descId;
290
+ let descOwner;
291
+ let descOwnerPn;
292
+ let descOwnerUsername;
293
+ let descTime;
294
+ if (descChild) {
295
+ desc = getBinaryNodeChildString(descChild, 'body');
296
+ descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
297
+ descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
298
+ descOwnerUsername = descChild.attrs.participant_username || undefined;
299
+ descTime = +descChild.attrs.t;
300
+ descId = descChild.attrs.id;
301
+ }
302
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
303
+ const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
304
+ const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
305
+ const metadata = {
306
+ id: groupId,
307
+ notify: group.attrs.notify,
308
+ addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
309
+ subject: group.attrs.subject,
310
+ subjectOwner: group.attrs.s_o,
311
+ subjectOwnerPn: group.attrs.s_o_pn,
312
+ subjectOwnerUsername: group.attrs.s_o_username,
313
+ subjectTime: +group.attrs.s_t,
314
+ size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
315
+ creation: +group.attrs.creation,
316
+ owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
317
+ ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
318
+ ownerUsername: group.attrs.creator_username || undefined,
319
+ owner_country_code: group.attrs.creator_country_code,
320
+ desc,
321
+ descId,
322
+ descOwner,
323
+ descOwnerPn,
324
+ descOwnerUsername,
325
+ descTime,
326
+ linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
327
+ restrict: !!getBinaryNodeChild(group, 'locked'),
328
+ announce: !!getBinaryNodeChild(group, 'announcement'),
329
+ isCommunity: !!getBinaryNodeChild(group, 'parent'),
330
+ isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
331
+ joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
332
+ memberAddMode,
333
+ participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
334
+ // TODO: Store LID MAPPINGS
335
+ return {
336
+ id: attrs.jid,
337
+ phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
338
+ lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
339
+ username: attrs.participant_username || attrs.username || undefined,
340
+ admin: (attrs.type || null)
341
+ };
342
+ }),
343
+ ephemeralDuration: eph ? +eph : undefined
344
+ };
345
+ return metadata;
403
346
  };
347
+ //# sourceMappingURL=groups.js.map