Compare commits

..

2 Commits

Author SHA1 Message Date
Pedro Lopez
8afe71d959 add constants to version updater 2022-11-26 18:17:44 -04:00
Pedro Lopez
afb0b5d7d3 feat: use specific whatsapp web version 2022-11-26 18:17:19 -04:00
9 changed files with 90 additions and 89 deletions

7
index.d.ts vendored
View File

@@ -1108,11 +1108,11 @@ declare namespace WAWebJS {
/** Promotes or demotes participants by IDs to regular users or admins */
export type ChangeParticipantsPermissions =
(participantIds: Array<string>, sleep?: number) => Promise<{ status: number }>
(participantIds: Array<string>) => Promise<{ status: number }>
/** Adds or removes a list of participants by ID to the group */
export type ChangeGroupParticipants =
(participantIds: Array<string>, sleep?: number) => Promise<{
(participantIds: Array<string>) => Promise<{
status: number;
participants: Array<{
[key: string]: {
@@ -1326,9 +1326,6 @@ declare namespace WAWebJS {
webClientShouldHandle: boolean,
/** Object with participants */
participants: object
/** Reject the call */
reject: () => Promise<void>
}
/** Message type List */

View File

@@ -6,6 +6,7 @@ const moduleRaid = require('@pedroslopez/moduleraid/moduleraid');
const Util = require('./util/Util');
const InterfaceController = require('./util/InterfaceController');
const { getIndexForVersion } = require('./util/VersionResolver');
const { WhatsWebURL, DefaultOptions, Events, WAState } = require('./util/Constants');
const { ExposeStore, LoadUtils } = require('./util/Injected');
const ChatFactory = require('./factories/ChatFactory');
@@ -108,6 +109,7 @@ class Client extends EventEmitter {
this.pupPage = page;
await this.authStrategy.afterBrowserInitialized();
await this.initVersionOverride();
await page.goto(WhatsWebURL, {
waitUntil: 'load',
@@ -562,6 +564,22 @@ class Client extends EventEmitter {
});
}
async initVersionOverride() {
const version = this.options.webVersion;
await this.pupPage.setRequestInterception(true);
this.pupPage.on('request', async (req) => {
if(req.url() === WhatsWebURL) {
req.respond({
status: 200,
contentType: 'text/html',
body: await getIndexForVersion(version)
});
} else {
req.continue();
}
});
}
/**
* Closes the client
*/

View File

@@ -62,15 +62,7 @@ class Call extends Base {
return super._patch(data);
}
/**
* Reject the call
*/
async reject() {
return this.client.pupPage.evaluate((peerJid, id) => {
return window.WWebJS.rejectCall(peerJid, id);
}, this.from, this.id);
}
}
module.exports = Call;

View File

@@ -1,7 +1,6 @@
'use strict';
const Chat = require('./Chat');
const Util = require('../util/Util');
/**
* Group participant information
@@ -53,65 +52,57 @@ class GroupChat extends Chat {
get participants() {
return this.groupMetadata.participants;
}
/**
* Internal function to change a number of participant's state..
* @param {string} type (promote|demote|add|remove)
*/
async _changeParticipants(participantIds, type, sleep = null) {
return await this.client.pupPage.evaluate(async (chatId, participantIds, type, sleep) => {
if (type != 'add' && type != 'remove' && type != 'promote' && type != 'demote') return null;
const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
const status = [];
for (const participantWid of participantWids) {
status.push(await window.Store.GroupParticipants['send'+type.charAt(0).toUpperCase() + type.slice(1)+'Participants'](chatWid, [participantWid]));
if (sleep) {
await Util.sleep(sleep);
}
}
return status;
}, this.id._serialized, participantIds, type, sleep);
}
/**
* Adds a list of participants by ID to the group
* @param {Array<string>} participantIds
* @param {?number} default 100ms, amount to sleep in milliseconds before adding the next participant
* @returns {Promise<Array<Object>>}
* @param {Array<string>} participantIds
* @returns {Promise<Object>}
*/
async addParticipants(participantIds, sleep = 100) {
return this._changeParticipants(participantIds, 'add', sleep);
async addParticipants(participantIds) {
return await this.client.pupPage.evaluate((chatId, participantIds) => {
const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
return window.Store.GroupParticipants.sendAddParticipants(chatWid, participantWids);
}, this.id._serialized, participantIds);
}
/**
* Removes a list of participants by ID to the group
* @param {Array<string>} participantIds
* @param {?number} default 100ms, amount to sleep in milliseconds before removing the next participant
* @returns {Promise<Array<Object>>}
* @param {Array<string>} participantIds
* @returns {Promise<Object>}
*/
async removeParticipants(participantIds, sleep = 100) {
return this._changeParticipants(participantIds, 'remove', sleep);
async removeParticipants(participantIds) {
return await this.client.pupPage.evaluate((chatId, participantIds) => {
const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
return window.Store.GroupParticipants.sendRemoveParticipants(chatWid, participantWids);
}, this.id._serialized, participantIds);
}
/**
* Promote participants to admins by IDs
* @param {Array<string>} participantIds
* @param {?number} default 100ms, amount to sleep in milliseconds before promoting the next participant
* @returns {Promise<Array<Object>>}
* Promotes participants by IDs to admins
* @param {Array<string>} participantIds
* @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
*/
async promoteParticipants(participantIds, sleep = 100) {
return this._changeParticipants(participantIds, 'promote', sleep);
async promoteParticipants(participantIds) {
return await this.client.pupPage.evaluate((chatId, participantIds) => {
const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
return window.Store.GroupParticipants.sendPromoteParticipants(chatWid, participantWids);
}, this.id._serialized, participantIds);
}
/**
* Demotes admins to regular participants by IDs
* @param {Array<string>} participantIds
* @param {?number} default 100ms, amount to sleep in milliseconds before demoting the next participant
* @returns {Promise<Array<Object>>}
* Demotes participants by IDs to regular users
* @param {Array<string>} participantIds
* @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
*/
async demoteParticipants(participantIds, sleep = 100) {
return this._changeParticipants(participantIds, 'demote', sleep);
async demoteParticipants(participantIds) {
return await this.client.pupPage.evaluate((chatId, participantIds) => {
const chatWid = window.Store.WidFactory.createWid(chatId);
const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p));
return window.Store.GroupParticipants.sendDemoteParticipants(chatWid, participantWids);
}, this.id._serialized, participantIds);
}
/**
@@ -240,4 +231,4 @@ class GroupChat extends Chat {
}
module.exports = GroupChat;
module.exports = GroupChat;

View File

@@ -7,6 +7,7 @@ exports.DefaultOptions = {
headless: true,
defaultViewport: null
},
webVersion: '2.2244.6',
authTimeoutMs: 0,
qrMaxRetries: 0,
takeoverOnConflict: false,

View File

@@ -53,8 +53,6 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.ReplyUtils = window.mR.findModule('canReplyMsg').length > 0 && window.mR.findModule('canReplyMsg')[0];
window.Store.MsgActionChecks = window.mR.findModule('canSenderRevokeMsg')[0];
window.Store.QuotedMsg = window.mR.findModule('getQuotedMsgObj')[0];
window.Store.Socket = window.mR.findModule('deprecatedSendIq')[0];
window.Store.SocketWap = window.mR.findModule('wap')[0];
window.Store.StickerTools = {
...window.mR.findModule('toWebpSticker')[0],
...window.mR.findModule('addWebpMetadata')[0]
@@ -604,21 +602,4 @@ exports.LoadUtils = () => {
return undefined;
};
window.WWebJS.rejectCall = async (peerJid, id) => {
peerJid = peerJid.split('@')[0] + '@s.whatsapp.net';
let userId = window.Store.User.getMaybeMeUser().user + '@s.whatsapp.net';
const stanza = window.Store.SocketWap.wap('call', {
id: window.Store.SocketWap.generateId(),
from: window.Store.SocketWap.USER_JID(userId),
to: window.Store.SocketWap.USER_JID(peerJid),
}, [
window.Store.SocketWap.wap('reject', {
'call-id': id,
'call-creator': window.Store.SocketWap.USER_JID(peerJid),
count: '0',
})
]);
await window.Store.Socket.deprecatedCastStanza(stanza);
};
};

View File

@@ -181,10 +181,6 @@ class Util {
static setFfmpegPath(path) {
ffmpeg.setFfmpegPath(path);
}
static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
module.exports = Util;

View File

@@ -0,0 +1,16 @@
const fetch = require('node-fetch');
const VERSION_ARCHIVE_URL = 'https://archive.wwebjs.dev/web-index';
class VersionResolveError extends Error { }
const getIndexForVersion = async (version) => {
const cachedRes = await fetch(`${VERSION_ARCHIVE_URL}/${version}`);
if(!cachedRes.ok) throw new VersionResolveError(`Couldn't load app for version ${version} from the archive`);
return cachedRes.text();
};
module.exports = {
VersionResolveError,
getIndexForVersion
};

View File

@@ -18,15 +18,24 @@ const getCurrentVersion = () => {
}
};
const updateVersion = async (oldVersion, newVersion) => {
const readmePath = '../../README.md';
const readme = fs.readFileSync(readmePath);
const newReadme = readme.toString().replaceAll(oldVersion, newVersion);
const updateInFile = (filePath, oldVersion, newVersion) => {
const originalFile = fs.readFileSync(filePath);
const newFile = originalFile.toString().replaceAll(oldVersion, newVersion);
fs.writeFileSync(readmePath, newReadme);
fs.writeFileSync('./.version', newVersion);
fs.writeFileSync(filePath, newFile);
};
const updateVersion = async (oldVersion, newVersion) => {
const filesToUpdate = [
'../../src/util/Constants.js',
'../../README.md',
];
for (const file of filesToUpdate) {
updateInFile(file, oldVersion, newVersion);
}
fs.writeFileSync('./.version', newVersion);
};
(async () => {