whatsapp-web.js 1.33.2 → 1.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whatsapp-web.js",
3
- "version": "1.33.2",
3
+ "version": "1.34.0",
4
4
  "description": "Library for interacting with the WhatsApp Web API ",
5
5
  "main": "./index.js",
6
6
  "typings": "./index.d.ts",
package/src/Client.js CHANGED
@@ -2343,19 +2343,17 @@ class Client extends EventEmitter {
2343
2343
  * @returns {Promise<Array<{ lid: string, pn: string }>>}
2344
2344
  */
2345
2345
  async getContactLidAndPhone(userIds) {
2346
- return await this.pupPage.evaluate((userIds) => {
2347
- !Array.isArray(userIds) && (userIds = [userIds]);
2348
- return userIds.map(userId => {
2349
- const wid = window.Store.WidFactory.createWid(userId);
2350
- const isLid = wid.server === 'lid';
2351
- const lid = isLid ? wid : window.Store.LidUtils.getCurrentLid(wid);
2352
- const phone = isLid ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
2346
+ return await this.pupPage.evaluate(async (userIds) => {
2347
+ if (!Array.isArray(userIds)) userIds = [userIds];
2348
+
2349
+ return await Promise.all(userIds.map(async (userId) => {
2350
+ const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(userId);
2353
2351
 
2354
2352
  return {
2355
- lid: lid._serialized,
2356
- pn: phone._serialized
2353
+ lid: lid?._serialized,
2354
+ pn: phone?._serialized
2357
2355
  };
2358
- });
2356
+ }));
2359
2357
  }, userIds);
2360
2358
  }
2361
2359
  }
@@ -195,12 +195,12 @@ class GroupChat extends Chat {
195
195
  async removeParticipants(participantIds) {
196
196
  return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
197
197
  const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
198
- const participants = participantIds.map(p => {
199
- const wid = window.Store.WidFactory.createWid(p);
200
- const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
201
- const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
202
- return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
203
- }).filter(p => Boolean(p));
198
+ const participants = (await Promise.all(participantIds.map(async p => {
199
+ const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(p);
200
+
201
+ return chat.groupMetadata.participants.get(lid?._serialized) ||
202
+ chat.groupMetadata.participants.get(phone?._serialized);
203
+ }))).filter(Boolean);
204
204
  await window.Store.GroupParticipants.removeParticipants(chat, participants);
205
205
  return { status: 200 };
206
206
  }, this.id._serialized, participantIds);
@@ -214,12 +214,12 @@ class GroupChat extends Chat {
214
214
  async promoteParticipants(participantIds) {
215
215
  return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
216
216
  const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
217
- const participants = participantIds.map(p => {
218
- const wid = window.Store.WidFactory.createWid(p);
219
- const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
220
- const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
221
- return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
222
- }).filter(p => Boolean(p));
217
+ const participants = (await Promise.all(participantIds.map(async p => {
218
+ const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(p);
219
+
220
+ return chat.groupMetadata.participants.get(lid?._serialized) ||
221
+ chat.groupMetadata.participants.get(phone?._serialized);
222
+ }))).filter(Boolean);
223
223
  await window.Store.GroupParticipants.promoteParticipants(chat, participants);
224
224
  return { status: 200 };
225
225
  }, this.id._serialized, participantIds);
@@ -233,12 +233,12 @@ class GroupChat extends Chat {
233
233
  async demoteParticipants(participantIds) {
234
234
  return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
235
235
  const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
236
- const participants = participantIds.map(p => {
237
- const wid = window.Store.WidFactory.createWid(p);
238
- const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
239
- const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
240
- return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
241
- }).filter(p => Boolean(p));
236
+ const participants = (await Promise.all(participantIds.map(async p => {
237
+ const { lid, phone } = await window.WWebJS.enforceLidAndPnRetrieval(p);
238
+
239
+ return chat.groupMetadata.participants.get(lid?._serialized) ||
240
+ chat.groupMetadata.participants.get(phone?._serialized);
241
+ }))).filter(Boolean);
242
242
  await window.Store.GroupParticipants.demoteParticipants(chat, participants);
243
243
  return { status: 200 };
244
244
  }, this.id._serialized, participantIds);
@@ -102,6 +102,7 @@ exports.ExposeStore = () => {
102
102
  window.Store.PinnedMsgUtils = window.require('WAWebSendPinMessageAction');
103
103
  window.Store.UploadUtils = window.require('WAWebUploadManager');
104
104
  window.Store.WAWebStreamModel = window.require('WAWebStreamModel');
105
+ window.Store.FindOrCreateChat = window.require('WAWebFindChatAction');
105
106
 
106
107
  window.Store.Settings = {
107
108
  ...window.require('WAWebUserPrefsGeneral'),
@@ -210,14 +211,21 @@ exports.ExposeStore = () => {
210
211
  * @param {Function} callback Modified function
211
212
  */
212
213
  window.injectToFunction = (target, callback) => {
213
- const module = window.require(target.module);
214
- const originalFunction = module[target.function];
215
- const modifiedFunction = (...args) => callback(originalFunction, ...args);
216
- module[target.function] = modifiedFunction;
214
+ let module = window.require(target.module);
215
+
216
+ const path = target.function.split('.');
217
+ const funcName = path.pop();
218
+ for (const key of path) {
219
+ module = module[key];
220
+ }
221
+
222
+ const originalFunction = module[funcName];
223
+ module[funcName] = (...args) => callback(originalFunction, ...args);
217
224
  };
218
225
 
219
226
  window.injectToFunction({ module: 'WAWebBackendJobsCommon', function: 'mediaTypeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage ? null : func(...args); });
220
227
 
221
228
  window.injectToFunction({ module: 'WAWebE2EProtoUtils', function: 'typeAttributeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage || proto.groupInviteMessage ? 'text' : func(...args); });
222
229
 
230
+ window.injectToFunction({ module: 'WAWebLid1X1MigrationGating', function: 'Lid1X1MigrationUtils.isLidMigrated' }, () => false);
223
231
  };
@@ -249,7 +249,7 @@ exports.LoadUtils = () => {
249
249
 
250
250
  if (typeof chat.id?.isGroup === 'function' && chat.id.isGroup()) {
251
251
  from = chat.groupMetadata && chat.groupMetadata.isLidAddressingMode ? lidUser : meUser;
252
- participant = window.Store.WidFactory.toUserWidOrThrow(from);
252
+ participant = window.Store.WidFactory.asUserWidOrThrow(from);
253
253
  }
254
254
 
255
255
  const newMsgKey = new window.Store.MsgKey({
@@ -539,7 +539,7 @@ exports.LoadUtils = () => {
539
539
  chat = null;
540
540
  }
541
541
  } else {
542
- chat = window.Store.Chat.get(chatWid) || (await window.Store.Chat.find(chatWid));
542
+ chat = window.Store.Chat.get(chatWid) || (await window.Store.FindOrCreateChat.findOrCreateLatestChat(chatWid))?.chat;
543
543
  }
544
544
 
545
545
  return getAsModel && chat
@@ -1144,4 +1144,26 @@ exports.LoadUtils = () => {
1144
1144
  const statuses = window.Store.Status.getModelsArray();
1145
1145
  return statuses.map(status => window.WWebJS.getStatusModel(status));
1146
1146
  };
1147
+
1148
+ window.WWebJS.enforceLidAndPnRetrieval = async (userId) => {
1149
+ const wid = window.Store.WidFactory.createWid(userId);
1150
+ const isLid = wid.server === 'lid';
1151
+
1152
+ let lid = isLid ? wid : window.Store.LidUtils.getCurrentLid(wid);
1153
+ let phone = isLid ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
1154
+
1155
+ if (!isLid && !lid) {
1156
+ const queryResult = await window.Store.QueryExist(wid);
1157
+ if (!queryResult?.wid) return {};
1158
+ lid = window.Store.LidUtils.getCurrentLid(wid);
1159
+ }
1160
+
1161
+ if (isLid && !phone) {
1162
+ const queryResult = await window.Store.QueryExist(wid);
1163
+ if (!queryResult?.wid) return {};
1164
+ phone = window.Store.LidUtils.getPhoneNumber(wid);
1165
+ }
1166
+
1167
+ return { lid, phone };
1168
+ };
1147
1169
  };