@mrlegendbot/baileys 1.2.1 → 1.2.4

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 (212) hide show
  1. package/README.upstream.md +1312 -0
  2. package/WAProto/WAProto.json +1 -0
  3. package/WAProto/WAProto.proto +6275 -0
  4. package/WAProto/compiler.js +550 -0
  5. package/WAProto/index.js +2 -97691
  6. package/lib/Defaults/index.js +89 -92
  7. package/lib/Signal/Group/ciphertext-message.js +9 -10
  8. package/lib/Signal/Group/group-session-builder.js +21 -22
  9. package/lib/Signal/Group/group_cipher.js +75 -69
  10. package/lib/Signal/Group/index.js +0 -1
  11. package/lib/Signal/Group/keyhelper.js +9 -10
  12. package/lib/Signal/Group/sender-chain-key.js +21 -22
  13. package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
  14. package/lib/Signal/Group/sender-key-message.js +56 -62
  15. package/lib/Signal/Group/sender-key-name.js +38 -40
  16. package/lib/Signal/Group/sender-key-record.js +33 -34
  17. package/lib/Signal/Group/sender-key-state.js +72 -76
  18. package/lib/Signal/Group/sender-message-key.js +22 -23
  19. package/lib/Signal/libsignal.js +506 -405
  20. package/lib/Signal/lid-mapping.js +276 -258
  21. package/lib/Socket/Client/index.js +0 -1
  22. package/lib/Socket/Client/types.js +6 -7
  23. package/lib/Socket/Client/websocket.js +48 -49
  24. package/lib/Socket/business.js +349 -369
  25. package/lib/Socket/chats.js +1172 -1148
  26. package/lib/Socket/communities.js +473 -413
  27. package/lib/Socket/groups.js +387 -331
  28. package/lib/Socket/index.js +6 -8
  29. package/lib/Socket/messages-recv.js +1782 -1719
  30. package/lib/Socket/messages-send.js +1503 -2450
  31. package/lib/Socket/mex.js +40 -35
  32. package/lib/Socket/newsletter.js +239 -191
  33. package/lib/Socket/socket.js +992 -923
  34. package/lib/Store/index.js +8 -0
  35. package/lib/Store/keyed-db.js +108 -0
  36. package/lib/Store/make-cache-manager-store.js +70 -0
  37. package/lib/Store/make-in-memory-store.js +417 -0
  38. package/lib/Store/make-ordered-dictionary.js +69 -0
  39. package/lib/Store/object-repository.js +27 -0
  40. package/lib/Types/Auth.js +0 -1
  41. package/lib/Types/Bussines.js +0 -1
  42. package/lib/Types/Call.js +0 -1
  43. package/lib/Types/Chat.js +1 -8
  44. package/lib/Types/Contact.js +0 -1
  45. package/lib/Types/Events.js +1 -2
  46. package/lib/Types/GroupMetadata.js +0 -1
  47. package/lib/Types/Label.js +20 -22
  48. package/lib/Types/LabelAssociation.js +2 -4
  49. package/lib/Types/Message.js +3 -5
  50. package/lib/Types/Mex.js +26 -31
  51. package/lib/Types/Product.js +0 -1
  52. package/lib/Types/Signal.js +1 -2
  53. package/lib/Types/Socket.js +2 -3
  54. package/lib/Types/State.js +4 -52
  55. package/lib/Types/USync.js +0 -1
  56. package/lib/Types/index.js +10 -11
  57. package/lib/Utils/auth-utils.js +240 -277
  58. package/lib/Utils/browser-utils.js +20 -23
  59. package/lib/Utils/business.js +200 -204
  60. package/lib/Utils/chat-utils.js +836 -810
  61. package/lib/Utils/companion-reg-client-utils.js +23 -25
  62. package/lib/Utils/crypto.js +70 -86
  63. package/lib/Utils/decode-wa-message.js +277 -284
  64. package/lib/Utils/event-buffer.js +592 -595
  65. package/lib/Utils/generics.js +312 -335
  66. package/lib/Utils/history.js +123 -115
  67. package/lib/Utils/index.js +1 -2
  68. package/lib/Utils/link-preview.js +74 -73
  69. package/lib/Utils/logger.js +3 -2
  70. package/lib/Utils/lt-hash.js +0 -6
  71. package/lib/Utils/make-mutex.js +27 -27
  72. package/lib/Utils/message-retry-manager.js +213 -254
  73. package/lib/Utils/messages-media.js +741 -715
  74. package/lib/Utils/messages.js +791 -827
  75. package/lib/Utils/noise-handler.js +215 -187
  76. package/lib/Utils/pre-key-manager.js +69 -98
  77. package/lib/Utils/process-message.js +617 -586
  78. package/lib/Utils/reporting-utils.js +553 -234
  79. package/lib/Utils/signal.js +217 -170
  80. package/lib/Utils/stanza-ack.js +27 -35
  81. package/lib/Utils/sync-action-utils.js +37 -41
  82. package/lib/Utils/tc-token-utils.js +120 -141
  83. package/lib/Utils/use-multi-file-auth-state.js +87 -105
  84. package/lib/Utils/use-sqlite-auth-state.js +162 -0
  85. package/lib/Utils/validate-connection.js +190 -188
  86. package/lib/WABinary/constants.js +28 -1294
  87. package/lib/WABinary/decode.js +245 -249
  88. package/lib/WABinary/encode.js +201 -206
  89. package/lib/WABinary/generic-utils.js +86 -89
  90. package/lib/WABinary/index.js +0 -1
  91. package/lib/WABinary/jid-utils.js +68 -77
  92. package/lib/WABinary/types.js +0 -1
  93. package/lib/WAM/BinaryInfo.js +7 -8
  94. package/lib/WAM/constants.js +19892 -22853
  95. package/lib/WAM/encode.js +121 -132
  96. package/lib/WAM/index.js +0 -1
  97. package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
  98. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
  99. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
  100. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
  101. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
  102. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
  103. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  104. package/lib/WAUSync/Protocols/index.js +0 -1
  105. package/lib/WAUSync/USyncQuery.js +79 -88
  106. package/lib/WAUSync/USyncUser.js +28 -29
  107. package/lib/WAUSync/index.js +0 -1
  108. package/lib/index.js +2 -2
  109. package/package.json +22 -86
  110. package/WAProto/GenerateStatics.sh +0 -3
  111. package/WAProto/fix-imports.js +0 -85
  112. package/WAProto/index.d.ts +0 -14017
  113. package/lib/Defaults/index.d.ts +0 -75
  114. package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
  115. package/lib/Signal/Group/group-session-builder.d.ts +0 -15
  116. package/lib/Signal/Group/group_cipher.d.ts +0 -17
  117. package/lib/Signal/Group/index.d.ts +0 -12
  118. package/lib/Signal/Group/keyhelper.d.ts +0 -11
  119. package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
  120. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
  121. package/lib/Signal/Group/sender-key-message.d.ts +0 -19
  122. package/lib/Signal/Group/sender-key-name.d.ts +0 -18
  123. package/lib/Signal/Group/sender-key-record.d.ts +0 -31
  124. package/lib/Signal/Group/sender-key-state.d.ts +0 -39
  125. package/lib/Signal/Group/sender-message-key.d.ts +0 -12
  126. package/lib/Signal/libsignal.d.ts +0 -5
  127. package/lib/Signal/lid-mapping.d.ts +0 -23
  128. package/lib/Socket/Client/index.d.ts +0 -3
  129. package/lib/Socket/Client/types.d.ts +0 -16
  130. package/lib/Socket/Client/websocket.d.ts +0 -13
  131. package/lib/Socket/business.d.ts +0 -217
  132. package/lib/Socket/chats.d.ts +0 -125
  133. package/lib/Socket/communities.d.ts +0 -273
  134. package/lib/Socket/groups.d.ts +0 -161
  135. package/lib/Socket/index.d.ts +0 -260
  136. package/lib/Socket/messages-recv.d.ts +0 -203
  137. package/lib/Socket/messages-send.d.ts +0 -199
  138. package/lib/Socket/mex.d.ts +0 -3
  139. package/lib/Socket/newsletter.d.ts +0 -170
  140. package/lib/Socket/socket.d.ts +0 -59
  141. package/lib/Types/Auth.d.ts +0 -117
  142. package/lib/Types/Bussines.d.ts +0 -25
  143. package/lib/Types/Call.d.ts +0 -15
  144. package/lib/Types/Chat.d.ts +0 -124
  145. package/lib/Types/Contact.d.ts +0 -26
  146. package/lib/Types/Events.d.ts +0 -256
  147. package/lib/Types/GroupMetadata.d.ts +0 -71
  148. package/lib/Types/Label.d.ts +0 -47
  149. package/lib/Types/LabelAssociation.d.ts +0 -30
  150. package/lib/Types/Message.d.ts +0 -320
  151. package/lib/Types/Mex.d.ts +0 -141
  152. package/lib/Types/Product.d.ts +0 -79
  153. package/lib/Types/Signal.d.ts +0 -87
  154. package/lib/Types/Socket.d.ts +0 -136
  155. package/lib/Types/State.d.ts +0 -97
  156. package/lib/Types/USync.d.ts +0 -26
  157. package/lib/Types/index.d.ts +0 -66
  158. package/lib/Utils/auth-utils.d.ts +0 -24
  159. package/lib/Utils/browser-utils.d.ts +0 -4
  160. package/lib/Utils/business.d.ts +0 -23
  161. package/lib/Utils/chat-utils.d.ts +0 -100
  162. package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
  163. package/lib/Utils/crypto.d.ts +0 -37
  164. package/lib/Utils/decode-wa-message.d.ts +0 -66
  165. package/lib/Utils/event-buffer.d.ts +0 -36
  166. package/lib/Utils/generics.d.ts +0 -92
  167. package/lib/Utils/history.d.ts +0 -24
  168. package/lib/Utils/identity-change-handler.d.ts +0 -44
  169. package/lib/Utils/identity-change-handler.js +0 -50
  170. package/lib/Utils/index.d.ts +0 -22
  171. package/lib/Utils/link-preview.d.ts +0 -21
  172. package/lib/Utils/logger.d.ts +0 -12
  173. package/lib/Utils/lt-hash.d.ts +0 -8
  174. package/lib/Utils/make-mutex.d.ts +0 -9
  175. package/lib/Utils/message-retry-manager.d.ts +0 -115
  176. package/lib/Utils/messages-media.d.ts +0 -133
  177. package/lib/Utils/messages.d.ts +0 -91
  178. package/lib/Utils/noise-handler.d.ts +0 -20
  179. package/lib/Utils/offline-node-processor.d.ts +0 -17
  180. package/lib/Utils/offline-node-processor.js +0 -40
  181. package/lib/Utils/pre-key-manager.d.ts +0 -28
  182. package/lib/Utils/process-message.d.ts +0 -60
  183. package/lib/Utils/reporting-utils.d.ts +0 -11
  184. package/lib/Utils/signal.d.ts +0 -47
  185. package/lib/Utils/stanza-ack.d.ts +0 -11
  186. package/lib/Utils/sync-action-utils.d.ts +0 -19
  187. package/lib/Utils/tc-token-utils.d.ts +0 -37
  188. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
  189. package/lib/Utils/validate-connection.d.ts +0 -11
  190. package/lib/WABinary/constants.d.ts +0 -28
  191. package/lib/WABinary/decode.d.ts +0 -7
  192. package/lib/WABinary/encode.d.ts +0 -3
  193. package/lib/WABinary/generic-utils.d.ts +0 -15
  194. package/lib/WABinary/index.d.ts +0 -6
  195. package/lib/WABinary/jid-utils.d.ts +0 -48
  196. package/lib/WABinary/types.d.ts +0 -19
  197. package/lib/WAM/BinaryInfo.d.ts +0 -9
  198. package/lib/WAM/constants.d.ts +0 -40
  199. package/lib/WAM/encode.d.ts +0 -3
  200. package/lib/WAM/index.d.ts +0 -4
  201. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
  202. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
  203. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
  204. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
  205. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
  206. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
  208. package/lib/WAUSync/Protocols/index.d.ts +0 -6
  209. package/lib/WAUSync/USyncQuery.d.ts +0 -30
  210. package/lib/WAUSync/USyncUser.d.ts +0 -17
  211. package/lib/WAUSync/index.d.ts +0 -4
  212. package/lib/index.d.ts +0 -12
@@ -1,347 +1,403 @@
1
1
  import { Boom } from '@hapi/boom';
2
- import { proto } from '../../WAProto/index.js';
2
+ import { proto } from '../../WAProto/compiler.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 { authState, ev, query, upsertMessage } = sock;
10
- const groupQuery = async (jid, type, content) => query({
11
- tag: 'iq',
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',
12
86
  attrs: {
13
- type,
14
- xmlns: 'w:g2',
15
- to: jid
87
+ subject,
88
+ key
16
89
  },
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',
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',
26
136
  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
- ]
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;
137
+ jid
53
138
  }
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
54
216
  }
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;
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
63
244
  }
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
- ]);
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
232
255
  }
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);
251
- },
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]);
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
257
265
  },
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
- };
271
- };
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 });
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
281
278
  }
282
- throw new Boom('Invalid group metadata response: missing <group> node', { data: result });
283
- }
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;
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
+ };
321
+ };
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
+ });
301
333
  }
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;
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;
346
403
  };
347
- //# sourceMappingURL=groups.js.map