Compare commits

..

21 Commits

Author SHA1 Message Date
Rajeh Taher
48a3a03cdc Merge branch 'main' into patch-participants 2022-12-23 09:54:14 +02:00
Rajeh Taher
288a572af6 feat: Implement Call Rejection + Introduction of Socket API (#1882)
* Initial stage

* ESLint

* eslint is annoying

* fix: distinguish Wap from Socket's Wap
2022-12-22 16:02:49 -03:00
Rajeh Taher
9b659a26fa Merge branch 'main' into patch-participants 2022-11-08 18:00:33 +02:00
Rajeh Taher
f0c7a0b0c2 Merge branch 'main' into patch-participants 2022-10-22 22:36:50 +03:00
Rajeh Taher
53b8c1d967 Merge branch 'main' into patch-participants 2022-10-10 01:55:56 +03:00
Rajeh Taher
56ec18a67c Update GroupChat.js 2022-10-10 01:55:32 +03:00
Rajeh Taher
3775fd7c3e Merge branch 'main' into patch-participants 2022-09-29 20:20:17 +03:00
Rajeh Taher
4247257850 ERROR/ESLINT 2022-08-10 19:55:06 +03:00
Rajeh Taher
374a6c505f Merge branch 'main' into patch-participants 2022-08-10 19:53:50 +03:00
Rajeh Taher
1f80a86962 Update GroupChat.js 2022-08-09 13:50:56 +03:00
Rajeh Taher
a9bbc0ed99 Update GroupChat.js 2022-08-09 13:42:03 +03:00
Rajeh Taher
b99aa61dcc Merge branch 'main' into patch-participants 2022-08-09 13:22:23 +03:00
Rajeh Taher
4ffd83015e eslint!! 2022-07-07 18:57:34 +03:00
Rajeh Taher
510d44dc3e a 2022-07-07 18:53:20 +03:00
Rajeh Taher
02ad3e0fd0 add sleep 2022-07-07 18:49:19 +03:00
Rajeh Taher
6ae8c42816 fixing ESLint errors 2022-07-07 18:30:26 +03:00
Rajeh Taher
7fc72fe149 Merge branch 'main' into patch-participants 2022-07-07 18:23:18 +03:00
Rajeh Taher
e980bf9b07 Merge branch 'main' into patch-participants 2022-02-09 10:57:41 +02:00
Rajeh Taher
8e6314212d Merge branch 'main' into patch-participants 2022-02-06 21:35:24 +02:00
Rajeh Taher
eb0d000159 fixes 🎉 2022-01-29 01:58:14 +02:00
Rajeh Taher
cf5c5e4ec1 Update GroupChat.js 2022-01-29 01:54:30 +02:00
9 changed files with 88 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>) => Promise<{ status: number }>
(participantIds: Array<string>, sleep?: number) => Promise<{ status: number }>
/** Adds or removes a list of participants by ID to the group */
export type ChangeGroupParticipants =
(participantIds: Array<string>) => Promise<{
(participantIds: Array<string>, sleep?: number) => Promise<{
status: number;
participants: Array<{
[key: string]: {
@@ -1326,6 +1326,9 @@ declare namespace WAWebJS {
webClientShouldHandle: boolean,
/** Object with participants */
participants: object
/** Reject the call */
reject: () => Promise<void>
}
/** Message type List */

View File

@@ -6,7 +6,6 @@ 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');
@@ -109,7 +108,6 @@ class Client extends EventEmitter {
this.pupPage = page;
await this.authStrategy.afterBrowserInitialized();
await this.initVersionOverride();
await page.goto(WhatsWebURL, {
waitUntil: 'load',
@@ -564,22 +562,6 @@ 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,7 +62,15 @@ 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,6 +1,7 @@
'use strict';
const Chat = require('./Chat');
const Util = require('../util/Util');
/**
* Group participant information
@@ -52,57 +53,65 @@ 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
* @returns {Promise<Object>}
* @param {Array<string>} participantIds
* @param {?number} default 100ms, amount to sleep in milliseconds before adding the next participant
* @returns {Promise<Array<Object>>}
*/
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);
async addParticipants(participantIds, sleep = 100) {
return this._changeParticipants(participantIds, 'add', sleep);
}
/**
* Removes a list of participants by ID to the group
* @param {Array<string>} participantIds
* @returns {Promise<Object>}
* @param {Array<string>} participantIds
* @param {?number} default 100ms, amount to sleep in milliseconds before removing the next participant
* @returns {Promise<Array<Object>>}
*/
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);
async removeParticipants(participantIds, sleep = 100) {
return this._changeParticipants(participantIds, 'remove', sleep);
}
/**
* Promotes participants by IDs to admins
* @param {Array<string>} participantIds
* @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
* 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>>}
*/
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);
async promoteParticipants(participantIds, sleep = 100) {
return this._changeParticipants(participantIds, 'promote', sleep);
}
/**
* 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
* 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>>}
*/
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);
async demoteParticipants(participantIds, sleep = 100) {
return this._changeParticipants(participantIds, 'demote', sleep);
}
/**
@@ -231,4 +240,4 @@ class GroupChat extends Chat {
}
module.exports = GroupChat;
module.exports = GroupChat;

View File

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

View File

@@ -53,6 +53,8 @@ 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]
@@ -602,4 +604,21 @@ 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,6 +181,10 @@ class Util {
static setFfmpegPath(path) {
ffmpeg.setFfmpegPath(path);
}
static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
module.exports = Util;

View File

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