@alannxd/baileys 6.0.3 → 6.0.5

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 (234) hide show
  1. package/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/WAProto.proto +5479 -0
  3. package/WAProto/fix-imports.js +85 -0
  4. package/WAProto/index.d.ts +14017 -0
  5. package/WAProto/index.js +201 -160
  6. package/engine-requirements.js +1 -1
  7. package/lib/Defaults/index.d.ts +37 -15
  8. package/lib/Defaults/index.js +119 -136
  9. package/lib/Signal/Group/ciphertext-message.d.ts +1 -0
  10. package/lib/Signal/Group/ciphertext-message.js +2 -5
  11. package/lib/Signal/Group/group-session-builder.d.ts +4 -3
  12. package/lib/Signal/Group/group-session-builder.js +7 -41
  13. package/lib/Signal/Group/group_cipher.d.ts +4 -4
  14. package/lib/Signal/Group/group_cipher.js +37 -51
  15. package/lib/Signal/Group/index.d.ts +12 -11
  16. package/lib/Signal/Group/index.js +12 -57
  17. package/lib/Signal/Group/keyhelper.d.ts +2 -1
  18. package/lib/Signal/Group/keyhelper.js +7 -44
  19. package/lib/Signal/Group/sender-chain-key.d.ts +3 -2
  20. package/lib/Signal/Group/sender-chain-key.js +7 -15
  21. package/lib/Signal/Group/sender-key-distribution-message.d.ts +2 -1
  22. package/lib/Signal/Group/sender-key-distribution-message.js +8 -11
  23. package/lib/Signal/Group/sender-key-message.d.ts +2 -1
  24. package/lib/Signal/Group/sender-key-message.js +9 -12
  25. package/lib/Signal/Group/sender-key-name.d.ts +1 -0
  26. package/lib/Signal/Group/sender-key-name.js +2 -5
  27. package/lib/Signal/Group/sender-key-record.d.ts +3 -2
  28. package/lib/Signal/Group/sender-key-record.js +9 -21
  29. package/lib/Signal/Group/sender-key-state.d.ts +7 -6
  30. package/lib/Signal/Group/sender-key-state.js +27 -42
  31. package/lib/Signal/Group/sender-message-key.d.ts +1 -0
  32. package/lib/Signal/Group/sender-message-key.js +4 -7
  33. package/lib/Signal/libsignal.d.ts +5 -3
  34. package/lib/Signal/libsignal.js +347 -90
  35. package/lib/Signal/lid-mapping.d.ts +23 -0
  36. package/lib/Signal/lid-mapping.js +277 -0
  37. package/lib/Socket/Client/index.d.ts +3 -3
  38. package/lib/Socket/Client/index.js +3 -19
  39. package/lib/Socket/Client/{abstract-socket-client.d.ts → types.d.ts} +4 -5
  40. package/lib/Socket/Client/types.js +11 -0
  41. package/lib/Socket/Client/{web-socket-client.d.ts → websocket.d.ts} +3 -2
  42. package/lib/Socket/Client/websocket.js +54 -0
  43. package/lib/Socket/business.d.ts +154 -108
  44. package/lib/Socket/business.js +162 -43
  45. package/lib/Socket/chats.d.ts +96 -239
  46. package/lib/Socket/chats.js +627 -427
  47. package/lib/Socket/communities.d.ts +239 -146
  48. package/lib/Socket/communities.js +90 -80
  49. package/lib/Socket/groups.d.ts +104 -57
  50. package/lib/Socket/groups.js +154 -161
  51. package/lib/Socket/index.d.ts +202 -115
  52. package/lib/Socket/index.js +11 -10
  53. package/lib/Socket/luxu.d.ts +22 -266
  54. package/lib/Socket/luxu.js +422 -465
  55. package/lib/Socket/messages-recv.d.ts +136 -84
  56. package/lib/Socket/messages-recv.js +1421 -615
  57. package/lib/Socket/messages-send.d.ts +142 -126
  58. package/lib/Socket/messages-send.js +878 -671
  59. package/lib/Socket/mex.d.ts +3 -0
  60. package/lib/Socket/mex.js +42 -0
  61. package/lib/Socket/newsletter.d.ts +121 -85
  62. package/lib/Socket/newsletter.js +147 -272
  63. package/lib/Socket/socket.d.ts +34 -19
  64. package/lib/Socket/socket.js +544 -313
  65. package/lib/Store/index.d.ts +10 -3
  66. package/lib/Store/index.js +10 -10
  67. package/lib/Store/keyed-db.d.ts +22 -0
  68. package/lib/Store/keyed-db.js +108 -0
  69. package/lib/Store/make-cache-manager-store.d.ts +17 -11
  70. package/lib/Store/make-cache-manager-store.js +43 -41
  71. package/lib/Store/make-in-memory-store.d.ts +39 -118
  72. package/lib/Store/make-in-memory-store.js +112 -341
  73. package/lib/Store/make-ordered-dictionary.d.ts +11 -10
  74. package/lib/Store/make-ordered-dictionary.js +14 -20
  75. package/lib/Store/object-repository.d.ts +10 -9
  76. package/lib/Store/object-repository.js +11 -6
  77. package/lib/Types/Auth.d.ts +19 -12
  78. package/lib/Types/Auth.js +2 -2
  79. package/lib/Types/Bussines.d.ts +25 -0
  80. package/lib/Types/Bussines.js +2 -0
  81. package/lib/Types/Call.d.ts +3 -1
  82. package/lib/Types/Call.js +2 -2
  83. package/lib/Types/Chat.d.ts +35 -13
  84. package/lib/Types/Chat.js +8 -4
  85. package/lib/Types/Contact.d.ts +8 -1
  86. package/lib/Types/Contact.js +2 -2
  87. package/lib/Types/Events.d.ts +116 -17
  88. package/lib/Types/Events.js +2 -2
  89. package/lib/Types/GroupMetadata.d.ts +21 -5
  90. package/lib/Types/GroupMetadata.js +2 -2
  91. package/lib/Types/Label.d.ts +12 -0
  92. package/lib/Types/Label.js +3 -5
  93. package/lib/Types/LabelAssociation.d.ts +1 -0
  94. package/lib/Types/LabelAssociation.js +3 -5
  95. package/lib/Types/Message.d.ts +105 -58
  96. package/lib/Types/Message.js +11 -9
  97. package/lib/Types/Mex.d.ts +141 -0
  98. package/lib/Types/Mex.js +37 -0
  99. package/lib/Types/Product.d.ts +2 -1
  100. package/lib/Types/Product.js +2 -2
  101. package/lib/Types/Signal.d.ts +32 -2
  102. package/lib/Types/Signal.js +2 -2
  103. package/lib/Types/Socket.d.ts +50 -25
  104. package/lib/Types/Socket.js +3 -2
  105. package/lib/Types/State.d.ts +72 -2
  106. package/lib/Types/State.js +56 -2
  107. package/lib/Types/USync.d.ts +3 -2
  108. package/lib/Types/USync.js +2 -2
  109. package/lib/Types/index.d.ts +22 -14
  110. package/lib/Types/index.js +15 -31
  111. package/lib/Utils/auth-utils.d.ts +12 -6
  112. package/lib/Utils/auth-utils.js +239 -143
  113. package/lib/Utils/browser-utils.d.ts +4 -0
  114. package/lib/Utils/browser-utils.js +28 -0
  115. package/lib/Utils/business.d.ts +3 -2
  116. package/lib/Utils/business.js +66 -69
  117. package/lib/Utils/chat-utils.d.ts +52 -23
  118. package/lib/Utils/chat-utils.js +396 -253
  119. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  120. package/lib/Utils/companion-reg-client-utils.js +35 -0
  121. package/lib/Utils/crypto.d.ts +18 -22
  122. package/lib/Utils/crypto.js +57 -90
  123. package/lib/Utils/decode-wa-message.d.ts +55 -8
  124. package/lib/Utils/decode-wa-message.js +203 -84
  125. package/lib/Utils/event-buffer.d.ts +9 -8
  126. package/lib/Utils/event-buffer.js +185 -77
  127. package/lib/Utils/generics.d.ts +28 -29
  128. package/lib/Utils/generics.js +180 -210
  129. package/lib/Utils/history.d.ts +18 -9
  130. package/lib/Utils/history.js +93 -55
  131. package/lib/Utils/identity-change-handler.d.ts +44 -0
  132. package/lib/Utils/identity-change-handler.js +50 -0
  133. package/lib/Utils/index.d.ts +22 -17
  134. package/lib/Utils/index.js +22 -33
  135. package/lib/Utils/link-preview.d.ts +5 -5
  136. package/lib/Utils/link-preview.js +16 -24
  137. package/lib/Utils/logger.d.ts +11 -3
  138. package/lib/Utils/logger.js +3 -7
  139. package/lib/Utils/lt-hash.d.ts +8 -12
  140. package/lib/Utils/lt-hash.js +3 -46
  141. package/lib/Utils/make-mutex.d.ts +4 -2
  142. package/lib/Utils/make-mutex.js +24 -34
  143. package/lib/Utils/message-retry-manager.d.ts +115 -0
  144. package/lib/Utils/message-retry-manager.js +265 -0
  145. package/lib/Utils/messages-media.d.ts +61 -44
  146. package/lib/Utils/messages-media.js +451 -482
  147. package/lib/Utils/messages.d.ts +32 -18
  148. package/lib/Utils/messages.js +458 -369
  149. package/lib/Utils/noise-handler.d.ts +13 -14
  150. package/lib/Utils/noise-handler.js +145 -99
  151. package/lib/Utils/offline-node-processor.d.ts +17 -0
  152. package/lib/Utils/offline-node-processor.js +40 -0
  153. package/lib/Utils/pre-key-manager.d.ts +28 -0
  154. package/lib/Utils/pre-key-manager.js +106 -0
  155. package/lib/Utils/process-message.d.ts +31 -12
  156. package/lib/Utils/process-message.js +459 -150
  157. package/lib/Utils/reporting-utils.d.ts +11 -0
  158. package/lib/Utils/reporting-utils.js +258 -0
  159. package/lib/Utils/signal.d.ts +20 -5
  160. package/lib/Utils/signal.js +120 -72
  161. package/lib/Utils/stanza-ack.d.ts +11 -0
  162. package/lib/Utils/stanza-ack.js +38 -0
  163. package/lib/Utils/sync-action-utils.d.ts +19 -0
  164. package/lib/Utils/sync-action-utils.js +49 -0
  165. package/lib/Utils/tc-token-utils.d.ts +37 -0
  166. package/lib/Utils/tc-token-utils.js +163 -0
  167. package/lib/Utils/use-multi-file-auth-state.d.ts +2 -2
  168. package/lib/Utils/use-multi-file-auth-state.js +29 -27
  169. package/lib/Utils/validate-connection.d.ts +7 -7
  170. package/lib/Utils/validate-connection.js +73 -99
  171. package/lib/WABinary/constants.d.ts +25 -27
  172. package/lib/WABinary/constants.js +1281 -20
  173. package/lib/WABinary/decode.d.ts +5 -5
  174. package/lib/WABinary/decode.js +52 -42
  175. package/lib/WABinary/encode.d.ts +3 -3
  176. package/lib/WABinary/encode.js +110 -155
  177. package/lib/WABinary/generic-utils.d.ts +8 -7
  178. package/lib/WABinary/generic-utils.js +48 -49
  179. package/lib/WABinary/index.d.ts +6 -5
  180. package/lib/WABinary/index.js +6 -21
  181. package/lib/WABinary/jid-utils.d.ts +25 -8
  182. package/lib/WABinary/jid-utils.js +74 -40
  183. package/lib/WABinary/types.d.ts +2 -1
  184. package/lib/WABinary/types.js +2 -2
  185. package/lib/WAM/BinaryInfo.d.ts +3 -11
  186. package/lib/WAM/BinaryInfo.js +2 -5
  187. package/lib/WAM/constants.d.ts +5 -3
  188. package/lib/WAM/constants.js +19071 -11568
  189. package/lib/WAM/encode.d.ts +3 -3
  190. package/lib/WAM/encode.js +17 -22
  191. package/lib/WAM/index.d.ts +4 -3
  192. package/lib/WAM/index.js +4 -19
  193. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +4 -3
  194. package/lib/WAUSync/Protocols/USyncContactProtocol.js +33 -13
  195. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +3 -2
  196. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
  197. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +3 -2
  198. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
  199. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +3 -2
  200. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
  201. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  202. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  203. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +4 -3
  204. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
  205. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +5 -3
  206. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -8
  207. package/lib/WAUSync/Protocols/index.d.ts +6 -4
  208. package/lib/WAUSync/Protocols/index.js +6 -20
  209. package/lib/WAUSync/USyncQuery.d.ts +6 -4
  210. package/lib/WAUSync/USyncQuery.js +44 -35
  211. package/lib/WAUSync/USyncUser.d.ts +10 -5
  212. package/lib/WAUSync/USyncUser.js +10 -5
  213. package/lib/WAUSync/index.d.ts +4 -0
  214. package/lib/WAUSync/index.js +4 -19
  215. package/lib/index.d.ts +10 -9
  216. package/lib/index.js +12 -34
  217. package/package.json +84 -53
  218. package/WAProto/fix-import.js +0 -29
  219. package/lib/Defaults/baileys-version.json +0 -3
  220. package/lib/Defaults/phonenumber-mcc.json +0 -223
  221. package/lib/Signal/Group/queue-job.d.ts +0 -1
  222. package/lib/Signal/Group/queue-job.js +0 -57
  223. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  224. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
  225. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  226. package/lib/Socket/Client/web-socket-client.js +0 -62
  227. package/lib/Socket/registration.d.ts +0 -267
  228. package/lib/Socket/registration.js +0 -166
  229. package/lib/Socket/usync.d.ts +0 -36
  230. package/lib/Socket/usync.js +0 -70
  231. package/lib/Types/Newsletter.d.ts +0 -103
  232. package/lib/Types/Newsletter.js +0 -38
  233. package/lib/Utils/baileys-event-stream.d.ts +0 -16
  234. package/lib/Utils/baileys-event-stream.js +0 -63
@@ -1,91 +1,24 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extractGroupMetadata = exports.makeGroupsSocket = void 0;
4
- const WAProto_1 = require("../../WAProto");
5
- const Types_1 = require("../Types");
6
- const Utils_1 = require("../Utils");
7
- const WABinary_1 = require("../WABinary");
8
- const chats_1 = require("./chats");
9
- const WAUSync_1 = require("../WAUSync");
10
- const makeGroupsSocket = (config) => {
11
- const sock = (0, chats_1.makeChatsSocket)(config);
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';
7
+ export const makeGroupsSocket = (config) => {
8
+ const sock = makeChatsSocket(config);
12
9
  const { authState, ev, query, upsertMessage } = sock;
13
- const groupQuery = async (jid, type, content) => (query({
10
+ const groupQuery = async (jid, type, content, xlmns) => query({
14
11
  tag: 'iq',
15
12
  attrs: {
16
13
  type,
17
- xmlns: 'w:g2',
18
- to: jid,
14
+ xmlns: xlmns ? xlmns : 'w:g2',
15
+ to: jid
19
16
  },
20
17
  content
21
- }));
18
+ });
22
19
  const groupMetadata = async (jid) => {
23
20
  const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
24
- return (0, exports.extractGroupMetadata)(result);
25
- };
26
- const sendMessageMembers = async (jid, message, { additionalNodes, additionalAttributes, batchSize = 250, getMetadata = true, participants: participantsInput = [] } = {}) => {
27
- let participants = [];
28
- if (getMetadata) {
29
- const groupInfo = await groupMetadata(jid);
30
- participants = groupInfo.participants.filter(p => p && (p.admin === null || typeof p.admin === 'undefined' || p.admin === false)).map(p => p.phoneNumber || p.id).filter(id => id && typeof id === 'string' && !id.includes('@lid'));
31
- } else {
32
- participants = participantsInput.filter(id => id && typeof id === 'string' && !id.includes('@lid'));
33
- }
34
- if (!participants.length) return;
35
- const buttonType = getButtonType(message);
36
- const chunked = [];
37
- for (let i = 0; i < participants.length; i += batchSize) {
38
- chunked.push(participants.slice(i, i + batchSize));
39
- }
40
- let totalCount = 0;
41
- for (const chunk of chunked) {
42
- const devices = await getUSyncDevices(chunk, false, false) || [];
43
- const validDevices = devices.filter(j => {
44
- const raw = j.device;
45
- const d = typeof raw === 'string' ? parseInt(raw, 10) : raw;
46
- return j.user && (d == null || (Number.isInteger(d) && d >= 0 && d < 99));
47
- });
48
- const mappedDevices = validDevices.map(j => {
49
- const raw = j.device;
50
- const d = typeof raw === 'string' ? parseInt(raw, 10) : raw;
51
- return (d == null || d === 0) ? `${j.user}@s.whatsapp.net` : `${j.user}:${d}@s.whatsapp.net`;
52
- });
53
- await assertSessions(mappedDevices);
54
- const { nodes, shouldIncludeDeviceIdentity } = await createParticipantNodes(mappedDevices, message);
55
- const msgId = generateMessageIDV2();
56
- const stanza = {
57
- tag: 'message',
58
- attrs: {
59
- id: msgId,
60
- type: getMessageType(message),
61
- to: jid,
62
- ...(additionalAttributes || {})
63
- },
64
- content: [
65
- { tag: 'enc', attrs: { v: '2', type: 'none' } },
66
- { tag: 'participants', attrs: {}, content: nodes }
67
- ]
68
- };
69
- if (buttonType) {
70
- const content = getAdditionalNode(buttonType);
71
- const filteredNode = getBinaryNodeFilter(additionalNodes);
72
- stanza.content.push(...(filteredNode ? additionalNodes : content));
73
- }
74
- if (additionalNodes?.length) {
75
- stanza.content.push(...additionalNodes);
76
- }
77
- if (shouldIncludeDeviceIdentity) {
78
- stanza.content.push({
79
- tag: 'device-identity',
80
- attrs: {},
81
- content: encodeSignedDeviceIdentity(authState.creds.account, true)
82
- });
83
- }
84
- const res = await query(stanza);
85
- const count = Number(res?.attrs?.count || 0);
86
- totalCount += count;
87
- }
88
- return totalCount
21
+ return extractGroupMetadata(result);
89
22
  };
90
23
  const groupFetchAllParticipating = async () => {
91
24
  const result = await query({
@@ -93,7 +26,7 @@ const makeGroupsSocket = (config) => {
93
26
  attrs: {
94
27
  to: '@g.us',
95
28
  xmlns: 'w:g2',
96
- type: 'get',
29
+ type: 'get'
97
30
  },
98
31
  content: [
99
32
  {
@@ -107,11 +40,11 @@ const makeGroupsSocket = (config) => {
107
40
  ]
108
41
  });
109
42
  const data = {};
110
- const groupsChild = (0, WABinary_1.getBinaryNodeChild)(result, 'groups');
43
+ const groupsChild = getBinaryNodeChild(result, 'groups');
111
44
  if (groupsChild) {
112
- const groups = (0, WABinary_1.getBinaryNodeChildren)(groupsChild, 'group');
45
+ const groups = getBinaryNodeChildren(groupsChild, 'group');
113
46
  for (const groupNode of groups) {
114
- const meta = (0, exports.extractGroupMetadata)({
47
+ const meta = extractGroupMetadata({
115
48
  tag: 'result',
116
49
  attrs: {},
117
50
  content: [groupNode]
@@ -119,11 +52,12 @@ const makeGroupsSocket = (config) => {
119
52
  data[meta.id] = meta;
120
53
  }
121
54
  }
55
+ // TODO: properly parse LID / PN DATA
122
56
  sock.ev.emit('groups.update', Object.values(data));
123
57
  return data;
124
58
  };
125
59
  sock.ws.on('CB:ib,,dirty', async (node) => {
126
- const { attrs } = (0, WABinary_1.getBinaryNodeChild)(node, 'dirty');
60
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
127
61
  if (attrs.type !== 'groups') {
128
62
  return;
129
63
  }
@@ -134,7 +68,7 @@ const makeGroupsSocket = (config) => {
134
68
  ...sock,
135
69
  groupMetadata,
136
70
  groupCreate: async (subject, participants) => {
137
- const key = (0, Utils_1.generateMessageID)();
71
+ const key = generateMessageIDV2();
138
72
  const result = await groupQuery('@g.us', 'set', [
139
73
  {
140
74
  tag: 'create',
@@ -148,16 +82,14 @@ const makeGroupsSocket = (config) => {
148
82
  }))
149
83
  }
150
84
  ]);
151
- return (0, exports.extractGroupMetadata)(result);
85
+ return extractGroupMetadata(result);
152
86
  },
153
87
  groupLeave: async (id) => {
154
88
  await groupQuery('@g.us', 'set', [
155
89
  {
156
90
  tag: 'leave',
157
91
  attrs: {},
158
- content: [
159
- { tag: 'group', attrs: { id } }
160
- ]
92
+ content: [{ tag: 'group', attrs: { id } }]
161
93
  }
162
94
  ]);
163
95
  },
@@ -177,12 +109,13 @@ const makeGroupsSocket = (config) => {
177
109
  attrs: {}
178
110
  }
179
111
  ]);
180
- const node = (0, WABinary_1.getBinaryNodeChild)(result, 'membership_approval_requests');
181
- const participants = (0, WABinary_1.getBinaryNodeChildren)(node, 'membership_approval_request');
112
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
113
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
182
114
  return participants.map(v => v.attrs);
183
115
  },
184
116
  groupRequestParticipantsUpdate: async (jid, participants, action) => {
185
- const result = await groupQuery(jid, 'set', [{
117
+ const result = await groupQuery(jid, 'set', [
118
+ {
186
119
  tag: 'membership_requests_action',
187
120
  attrs: {},
188
121
  content: [
@@ -195,10 +128,11 @@ const makeGroupsSocket = (config) => {
195
128
  }))
196
129
  }
197
130
  ]
198
- }]);
199
- const node = (0, WABinary_1.getBinaryNodeChild)(result, 'membership_requests_action');
200
- const nodeAction = (0, WABinary_1.getBinaryNodeChild)(node, action);
201
- const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(nodeAction, 'participant');
131
+ }
132
+ ]);
133
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
134
+ const nodeAction = getBinaryNodeChild(node, action);
135
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
202
136
  return participantsAffected.map(p => {
203
137
  return { status: p.attrs.error || '200', jid: p.attrs.jid };
204
138
  });
@@ -214,43 +148,52 @@ const makeGroupsSocket = (config) => {
214
148
  }))
215
149
  }
216
150
  ]);
217
- const node = (0, WABinary_1.getBinaryNodeChild)(result, action);
218
- const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(node, 'participant');
151
+ const node = getBinaryNodeChild(result, action);
152
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
219
153
  return participantsAffected.map(p => {
220
154
  return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
221
155
  });
222
156
  },
223
157
  groupUpdateDescription: async (jid, description) => {
224
- var _a;
225
158
  const metadata = await groupMetadata(jid);
226
- const prev = (_a = metadata.descId) !== null && _a !== void 0 ? _a : null;
159
+ const prev = metadata.descId ?? null;
227
160
  await groupQuery(jid, 'set', [
228
161
  {
229
162
  tag: 'description',
230
163
  attrs: {
231
- ...(description ? { id: (0, Utils_1.generateMessageID)() } : { delete: 'true' }),
164
+ ...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
232
165
  ...(prev ? { prev } : {})
233
166
  },
234
- content: description ? [
235
- { tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }
236
- ] : undefined
167
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
237
168
  }
238
169
  ]);
239
170
  },
240
171
  groupInviteCode: async (jid) => {
241
172
  const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
242
- const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
243
- return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
173
+ const inviteNode = getBinaryNodeChild(result, 'invite');
174
+ return inviteNode?.attrs.code;
244
175
  },
245
176
  groupRevokeInvite: async (jid) => {
246
177
  const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
247
- const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
248
- return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
178
+ const inviteNode = getBinaryNodeChild(result, 'invite');
179
+ return inviteNode?.attrs.code;
249
180
  },
250
181
  groupAcceptInvite: async (code) => {
251
182
  const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
252
- const result = (0, WABinary_1.getBinaryNodeChild)(results, 'group');
253
- return result === null || result === void 0 ? void 0 : result.attrs.jid;
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;
254
197
  },
255
198
  /**
256
199
  * accept a GroupInviteMessage
@@ -259,19 +202,21 @@ const makeGroupsSocket = (config) => {
259
202
  */
260
203
  groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
261
204
  key = typeof key === 'string' ? { remoteJid: key } : key;
262
- const results = await groupQuery(inviteMessage.groupJid, 'set', [{
205
+ const results = await groupQuery(inviteMessage.groupJid, 'set', [
206
+ {
263
207
  tag: 'accept',
264
208
  attrs: {
265
209
  code: inviteMessage.inviteCode,
266
210
  expiration: inviteMessage.inviteExpiration.toString(),
267
211
  admin: key.remoteJid
268
212
  }
269
- }]);
213
+ }
214
+ ]);
270
215
  // if we have the full message key
271
216
  // update the invite message to be expired
272
217
  if (key.id) {
273
218
  // create new invite message that is expired
274
- inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);
219
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
275
220
  inviteMessage.inviteExpiration = 0;
276
221
  inviteMessage.inviteCode = '';
277
222
  ev.emit('messages.update', [
@@ -289,27 +234,25 @@ const makeGroupsSocket = (config) => {
289
234
  await upsertMessage({
290
235
  key: {
291
236
  remoteJid: inviteMessage.groupJid,
292
- id: (0, Utils_1.generateMessageID)(),
237
+ id: generateMessageIDV2(sock.user?.id),
293
238
  fromMe: false,
294
- participant: key.remoteJid,
239
+ participant: key.remoteJid
295
240
  },
296
- messageStubType: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,
297
- messageStubParameters: [
298
- authState.creds.me.id
299
- ],
241
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
242
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
300
243
  participant: key.remoteJid,
301
- messageTimestamp: (0, Utils_1.unixTimestampSeconds)()
244
+ messageTimestamp: unixTimestampSeconds()
302
245
  }, 'notify');
303
246
  return results.attrs.from;
304
247
  }),
305
248
  groupGetInviteInfo: async (code) => {
306
249
  const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
307
- return (0, exports.extractGroupMetadata)(results);
250
+ return extractGroupMetadata(results);
308
251
  },
309
252
  groupToggleEphemeral: async (jid, ephemeralExpiration) => {
310
- const content = ephemeralExpiration ?
311
- { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } :
312
- { tag: 'not_ephemeral', attrs: {} };
253
+ const content = ephemeralExpiration
254
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
255
+ : { tag: 'not_ephemeral', attrs: {} };
313
256
  await groupQuery(jid, 'set', [content]);
314
257
  },
315
258
  groupSettingUpdate: async (jid, setting) => {
@@ -319,63 +262,113 @@ const makeGroupsSocket = (config) => {
319
262
  await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
320
263
  },
321
264
  groupJoinApprovalMode: async (jid, mode) => {
322
- await groupQuery(jid, 'set', [{ tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }]);
265
+ await groupQuery(jid, 'set', [
266
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
267
+ ]);
268
+ },
269
+ groupToggleHistory: async (jid, enable = true) => {
270
+ const result = await query({
271
+ tag: "iq",
272
+ attrs: {
273
+ type: "get",
274
+ to: "s.whatsapp.net",
275
+ xmlns: "w:mex"
276
+ },
277
+ content: [
278
+ {
279
+ tag: "query",
280
+ attrs: {
281
+ query_id: "24688994337458819"
282
+ },
283
+ content: Buffer.from(JSON.stringify({
284
+ variables: {
285
+ group_id: jid,
286
+ input: {
287
+ member_share_group_history_mode: enable ? "ALL_MEMBER_SHARE" : "ADMIN_SHARE"
288
+ }
289
+ }
290
+ }), "utf-8")
291
+ }
292
+ ]
293
+ });
294
+ return result;
323
295
  },
324
296
  groupFetchAllParticipating
325
297
  };
326
298
  };
327
- exports.makeGroupsSocket = makeGroupsSocket;
328
- const extractGroupMetadata = (result) => {
329
- var _a, _b;
330
- const group = WABinary_1.getBinaryNodeChild(result, 'group');
331
- const descChild = WABinary_1.getBinaryNodeChild(group, 'description');
299
+ export const extractGroupMetadata = (result) => {
300
+ const group = getBinaryNodeChild(result, 'group');
301
+ if (!group) {
302
+ // Mirror WAWeb: surface server/client errors with their code+text instead of crashing.
303
+ const errorNode = getBinaryNodeChild(result, 'error');
304
+ if (errorNode) {
305
+ const code = errorNode.attrs.code ? +errorNode.attrs.code : 500;
306
+ const text = errorNode.attrs.text || 'group metadata query failed';
307
+ throw new Boom(text, { statusCode: code, data: errorNode });
308
+ }
309
+ throw new Boom('Invalid group metadata response: missing <group> node', { data: result });
310
+ }
311
+ if (!group.attrs.id) {
312
+ throw new Boom('Invalid group metadata response: missing group id', { data: group });
313
+ }
314
+ const descChild = getBinaryNodeChild(group, 'description');
332
315
  let desc;
333
316
  let descId;
334
317
  let descOwner;
335
- let descOwnerLid;
318
+ let descOwnerPn;
319
+ let descOwnerUsername;
336
320
  let descTime;
337
321
  if (descChild) {
338
- desc = WABinary_1.getBinaryNodeChildString(descChild, 'body');
339
- descOwner = WABinary_1.jidNormalizedUser(descChild.attrs.participant_pn || descChild.attrs.participant);
340
- if (group.attrs.addressing_mode === 'lid') {
341
- descOwnerLid = WABinary_1.jidNormalizedUser(descChild.attrs.participant);
342
- }
322
+ desc = getBinaryNodeChildString(descChild, 'body');
323
+ descOwner = descChild.attrs.participant ? jidNormalizedUser(descChild.attrs.participant) : undefined;
324
+ descOwnerPn = descChild.attrs.participant_pn ? jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
325
+ descOwnerUsername = descChild.attrs.participant_username || undefined;
326
+ descTime = +descChild.attrs.t;
343
327
  descId = descChild.attrs.id;
344
- descTime = descChild.attrs.t ? +descChild.attrs.t : undefined;
345
328
  }
346
- const groupSize = group.attrs.size ? Number(group.attrs.size) : undefined;
347
- const groupId = group.attrs.id.includes('@') ? group.attrs.id : (0, WABinary_1.jidEncode)(group.attrs.id, 'g.us');
348
- const eph = WABinary_1.getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration
349
- const memberAddMode = WABinary_1.getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
329
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : jidEncode(group.attrs.id, 'g.us');
330
+ const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration;
331
+ const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
350
332
  const metadata = {
351
333
  id: groupId,
352
- addressingMode: group.attrs.addressing_mode,
334
+ notify: group.attrs.notify,
335
+ addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN,
353
336
  subject: group.attrs.subject,
354
- subjectOwner: WABinary_1.jidNormalizedUser(group.attrs.s_o_pn || group.attrs.s_o),
355
- ...(group.attrs.addressing_mode === 'lid' ? { subjectOwnerLid: WABinary_1.jidNormalizedUser(group.attrs.s_o) } : {}),
356
- subjectTime: group.attrs.s_t ? +group.attrs.s_t : undefined,
357
- size: groupSize || WABinary_1.getBinaryNodeChildren(group, 'participant').length,
358
- creation: group.attrs.creation ? +group.attrs.creation : undefined,
359
- owner: WABinary_1.jidNormalizedUser(group.attrs.creator_pn || group.attrs.creator),
360
- ...(group.attrs.addressing_mode === 'lid' ? { ownerLid: WABinary_1.jidNormalizedUser(group.attrs.creator) } : {}),
337
+ subjectOwner: group.attrs.s_o,
338
+ subjectOwnerPn: group.attrs.s_o_pn,
339
+ subjectOwnerUsername: group.attrs.s_o_username,
340
+ subjectTime: +group.attrs.s_t,
341
+ size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length,
342
+ creation: +group.attrs.creation,
343
+ owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined,
344
+ ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined,
345
+ ownerUsername: group.attrs.creator_username || undefined,
346
+ owner_country_code: group.attrs.creator_country_code,
347
+ desc,
348
+ descId,
361
349
  descOwner,
362
- descOwnerLid,
363
- restrict: !!WABinary_1.getBinaryNodeChild(group, 'locked'),
364
- announce: !!WABinary_1.getBinaryNodeChild(group, 'announcement'),
365
- isCommunity: !!WABinary_1.getBinaryNodeChild(group, 'parent'),
366
- isCommunityAnnounce: !!WABinary_1.getBinaryNodeChild(group, 'default_sub_group'),
367
- joinApprovalMode: !!WABinary_1.getBinaryNodeChild(group, 'membership_approval_mode'),
350
+ descOwnerPn,
351
+ descOwnerUsername,
352
+ descTime,
353
+ linkedParent: getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
354
+ restrict: !!getBinaryNodeChild(group, 'locked'),
355
+ announce: !!getBinaryNodeChild(group, 'announcement'),
356
+ isCommunity: !!getBinaryNodeChild(group, 'parent'),
357
+ isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
358
+ joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
368
359
  memberAddMode,
369
- participants: WABinary_1.getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
360
+ participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
361
+ // TODO: Store LID MAPPINGS
370
362
  return {
371
363
  id: attrs.jid,
372
- jid: attrs.phone_number || attrs.jid,
373
- admin: (attrs.type || null),
364
+ phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined,
365
+ lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined,
366
+ username: attrs.participant_username || attrs.username || undefined,
367
+ admin: (attrs.type || null)
374
368
  };
375
369
  }),
376
370
  ephemeralDuration: eph ? +eph : undefined
377
371
  };
378
372
  return metadata;
379
373
  };
380
- exports.extractGroupMetadata = extractGroupMetadata;
381
-
374
+ //# sourceMappingURL=groups.js.map