[core] WEBJS - remove all mentions of window.WAHA
This commit is contained in:
1 parent
638555297c
commit
55dddd5991
4 files changed
+86
-168
No files matched your search
@@ -14,18 +14,12 @@ import { Message as MessageInstance } from 'whatsapp-web.js/src/structures';
|
||||
|
||||
import { CallErrorEvent, PAGE_CALL_ERROR_EVENT, WPage } from './WPage';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const { LoadWAHA } = require('./_WAHA.js');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const { LoadLodash } = require('./_lodash.js');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const { LoadPaginator } = require('./_Paginator.js');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const { LoadWAHACore } = require('./_WAHACore.js');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const ChatFactory = require('whatsapp-web.js/src/factories/ChatFactory');
|
||||
|
||||
@@ -130,8 +124,6 @@ export class WebjsClientCore extends Client {
|
||||
async injectWaha() {
|
||||
await this.pupPage.evaluate(LoadLodash);
|
||||
await this.pupPage.evaluate(LoadPaginator);
|
||||
await this.pupPage.evaluate(LoadWAHA);
|
||||
await this.pupPage.evaluate(LoadWAHACore);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,10 +168,10 @@ export class WebjsClientCore extends Client {
|
||||
}
|
||||
|
||||
const tags = ['receipt', 'presence', 'chatstate'];
|
||||
const WAWap = window.require('WAWap');
|
||||
// @ts-ignore
|
||||
window.decodeStanzaBack = window.Store.SocketWap.decodeStanza;
|
||||
// @ts-ignore
|
||||
window.Store.SocketWap.decodeStanza = async (...args) => {
|
||||
window.decodeStanzaBack = WAWap.decodeStanza;
|
||||
WAWap.decodeStanza = async (...args) => {
|
||||
// @ts-ignore
|
||||
const result = await window.decodeStanzaBack(...args);
|
||||
if (tags.includes(result?.tag)) {
|
||||
@@ -200,9 +192,9 @@ export class WebjsClientCore extends Client {
|
||||
async setPushName(name: string) {
|
||||
await this.ensureWahaInjected();
|
||||
await this.pupPage.evaluate(async (pushName) => {
|
||||
return await window['WAHA'].WAWebSetPushnameConnAction.setPushname(
|
||||
pushName,
|
||||
);
|
||||
return await window
|
||||
.require('WAWebSetPushnameConnAction')
|
||||
.setPushname(pushName);
|
||||
}, name);
|
||||
if (this.info) {
|
||||
this.info.pushname = name;
|
||||
@@ -211,16 +203,9 @@ export class WebjsClientCore extends Client {
|
||||
|
||||
async unpair() {
|
||||
await this.pupPage.evaluate(async () => {
|
||||
if (
|
||||
// @ts-ignore
|
||||
window.Store &&
|
||||
// @ts-ignore
|
||||
window.Store.AppState &&
|
||||
// @ts-ignore
|
||||
typeof window.Store.AppState.logout === 'function'
|
||||
) {
|
||||
// @ts-ignore
|
||||
await window.Store.AppState.logout();
|
||||
const Socket = window.require('WAWebSocketModel')?.Socket;
|
||||
if (Socket && typeof Socket.logout === 'function') {
|
||||
await Socket.logout();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -229,11 +214,9 @@ export class WebjsClientCore extends Client {
|
||||
await this.ensureWahaInjected();
|
||||
const labelId: number = (await this.pupPage.evaluate(
|
||||
async (name, color) => {
|
||||
// @ts-ignore
|
||||
return await window.WAHA.WAWebBizLabelEditingAction.labelAddAction(
|
||||
name,
|
||||
color,
|
||||
);
|
||||
return await window
|
||||
.require('WAWebBizLabelEditingAction')
|
||||
.labelAddAction(name, color);
|
||||
},
|
||||
name,
|
||||
color,
|
||||
@@ -244,20 +227,16 @@ export class WebjsClientCore extends Client {
|
||||
async deleteLabel(label: Label) {
|
||||
await this.ensureWahaInjected();
|
||||
return await this.pupPage.evaluate(async (label) => {
|
||||
// @ts-ignore
|
||||
return await window.WAHA.WAWebBizLabelEditingAction.labelDeleteAction(
|
||||
label.id,
|
||||
label.name,
|
||||
label.color,
|
||||
);
|
||||
return await window
|
||||
.require('WAWebBizLabelEditingAction')
|
||||
.labelDeleteAction(label.id, label.name, label.color);
|
||||
}, label);
|
||||
}
|
||||
|
||||
async updateLabel(label: Label) {
|
||||
await this.ensureWahaInjected();
|
||||
return await this.pupPage.evaluate(async (label) => {
|
||||
// @ts-ignore
|
||||
return await window.WAHA.WAWebBizLabelEditingAction.labelEditAction(
|
||||
return await window.require('WAWebBizLabelEditingAction').labelEditAction(
|
||||
label.id,
|
||||
label.name,
|
||||
undefined, // predefinedId
|
||||
@@ -279,8 +258,26 @@ export class WebjsClientCore extends Client {
|
||||
|
||||
const chats = await this.pupPage.evaluate(
|
||||
async (pagination, filter) => {
|
||||
let chats = window
|
||||
.require('WAWebCollections')
|
||||
.Chat.getModelsArray()
|
||||
.slice();
|
||||
|
||||
// Filter chats by IDs if filter is provided
|
||||
if (filter && filter.ids && filter.ids.length > 0) {
|
||||
chats = chats.filter((chat) =>
|
||||
filter.ids.includes(chat.id._serialized),
|
||||
);
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
return await window.WAHA.getChats(pagination, filter);
|
||||
const paginator = new window.Paginator(pagination);
|
||||
chats = paginator.apply(chats);
|
||||
const chatPromises = chats.map((chat) =>
|
||||
// @ts-ignore
|
||||
window.WWebJS.getChatModel(chat),
|
||||
);
|
||||
return await Promise.all(chatPromises);
|
||||
},
|
||||
pagination,
|
||||
filter,
|
||||
@@ -292,7 +289,7 @@ export class WebjsClientCore extends Client {
|
||||
protected async ensureWahaInjected() {
|
||||
const hasWaha = await this.pupPage.evaluate(() => {
|
||||
// @ts-ignore
|
||||
return Boolean(window.WAHA && window.WAHA.getChats);
|
||||
return Boolean(window.Paginator);
|
||||
});
|
||||
if (!hasWaha) {
|
||||
await this.injectWaha();
|
||||
@@ -310,14 +307,14 @@ export class WebjsClientCore extends Client {
|
||||
font: status.font,
|
||||
};
|
||||
const sentMsg = await this.pupPage.evaluate(async (status) => {
|
||||
// @ts-ignore
|
||||
await window.Store.SendStatus.sendStatusTextMsgAction(status);
|
||||
// @ts-ignore
|
||||
const meUser = window.Store.User.getMaybeMePnUser();
|
||||
// @ts-ignore
|
||||
const myStatus = window.Store.Status.getModelsArray().findLast(
|
||||
(x) => x.id == meUser,
|
||||
);
|
||||
await window
|
||||
.require('WAWebSendStatusMsgAction')
|
||||
.sendStatusTextMsgAction(status);
|
||||
const meUser = window.require('WAWebUserPrefsMeUser').getMaybeMePnUser();
|
||||
const myStatus = window
|
||||
.require('WAWebCollections')
|
||||
.Status.getModelsArray()
|
||||
.findLast((x) => x.id == meUser);
|
||||
if (!myStatus) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -387,15 +384,15 @@ export class WebjsClientCore extends Client {
|
||||
// object has the exact shape msgFindByDirection expects.
|
||||
const lastReceivedSerialized = chat.lastReceivedKey?._serialized;
|
||||
if (!lastReceivedSerialized) return [];
|
||||
// @ts-ignore
|
||||
let currentAnchorKey = window.Store.MsgKey.fromString(
|
||||
lastReceivedSerialized,
|
||||
);
|
||||
let currentAnchorKey = window
|
||||
.require('WAWebMsgKey')
|
||||
.fromString(lastReceivedSerialized);
|
||||
|
||||
// msgFindByDirection is exclusive of the anchor; include the anchor
|
||||
// message itself (the most recent message in the chat) upfront
|
||||
// @ts-ignore
|
||||
const anchorMsg = window.Store.Msg.get(lastReceivedSerialized);
|
||||
const anchorMsg = window
|
||||
.require('WAWebCollections')
|
||||
.Msg.get(lastReceivedSerialized);
|
||||
if (anchorMsg) {
|
||||
msgs.push(anchorMsg);
|
||||
}
|
||||
@@ -410,13 +407,12 @@ export class WebjsClientCore extends Client {
|
||||
const toModel = (m) => {
|
||||
if (m && typeof m.serialize === 'function') return m;
|
||||
const serializedId = m?.id?._serialized;
|
||||
const Msg = window.require('WAWebCollections').Msg;
|
||||
if (serializedId) {
|
||||
// @ts-ignore
|
||||
const stored = window.Store.Msg.get(serializedId);
|
||||
const stored = Msg.get(serializedId);
|
||||
if (stored) return stored;
|
||||
}
|
||||
// @ts-ignore
|
||||
return new window.Store.Msg.modelClass(m);
|
||||
return new Msg.modelClass(m);
|
||||
};
|
||||
|
||||
while (true) {
|
||||
@@ -469,19 +465,17 @@ export class WebjsClientCore extends Client {
|
||||
const oldestInBatch = batchModels[batchModels.length - 1];
|
||||
const oldestSerialized = oldestInBatch?.id?._serialized;
|
||||
if (!oldestSerialized) break;
|
||||
// @ts-ignore
|
||||
currentAnchorKey = window.Store.MsgKey.fromString(oldestSerialized);
|
||||
currentAnchorKey = window
|
||||
.require('WAWebMsgKey')
|
||||
.fromString(oldestSerialized);
|
||||
}
|
||||
} else {
|
||||
// Legacy fallback: loadEarlierMsgs loop
|
||||
msgs = chat.msgs.getModelsArray();
|
||||
while (msgs.length < pagination.limit + pagination.offset) {
|
||||
const loadedMessages =
|
||||
// @ts-ignore
|
||||
await window.Store.ConversationMsgs.loadEarlierMsgs(
|
||||
chat,
|
||||
chat.msgs,
|
||||
);
|
||||
const loadedMessages = await window
|
||||
.require('WAWebChatLoadMessages')
|
||||
.loadEarlierMsgs(chat, chat.msgs);
|
||||
if (!loadedMessages || loadedMessages.length == 0) break;
|
||||
|
||||
msgs = [...loadedMessages, ...msgs];
|
||||
@@ -530,8 +524,7 @@ export class WebjsClientCore extends Client {
|
||||
pagination.offset ||= 0;
|
||||
pagination.sortBy ||= 'lid';
|
||||
|
||||
// @ts-ignore
|
||||
const WAWebApiContact = window.Store.LidUtils;
|
||||
const WAWebApiContact = window.require('WAWebApiContact');
|
||||
|
||||
await WAWebApiContact.warmUpAllLidPnMappings();
|
||||
const lidMap = WAWebApiContact.lidPnCache['$1'];
|
||||
@@ -556,8 +549,7 @@ export class WebjsClientCore extends Client {
|
||||
|
||||
public async getLidsCount(): Promise<number> {
|
||||
const count: number = (await this.pupPage.evaluate(async () => {
|
||||
// @ts-ignore
|
||||
const WAWebApiContact = window.Store.LidUtils;
|
||||
const WAWebApiContact = window.require('WAWebApiContact');
|
||||
|
||||
await WAWebApiContact.warmUpAllLidPnMappings();
|
||||
const lidMap = WAWebApiContact.lidPnCache['$1'];
|
||||
@@ -568,10 +560,8 @@ export class WebjsClientCore extends Client {
|
||||
|
||||
public async findPNByLid(lid: string): Promise<string> {
|
||||
const pn = await this.pupPage.evaluate(async (lid) => {
|
||||
// @ts-ignore
|
||||
const WAWebApiContact = window.Store.LidUtils;
|
||||
// @ts-ignore
|
||||
const WAWebWidFactory = window.Store.WidFactory;
|
||||
const WAWebApiContact = window.require('WAWebApiContact');
|
||||
const WAWebWidFactory = window.require('WAWebWidFactory');
|
||||
|
||||
const wid = WAWebWidFactory.createWid(lid);
|
||||
const result = WAWebApiContact.getPhoneNumber(wid);
|
||||
@@ -582,10 +572,8 @@ export class WebjsClientCore extends Client {
|
||||
|
||||
public async findLIDByPhoneNumber(phoneNumber: string): Promise<string> {
|
||||
const lid: string = (await this.pupPage.evaluate(async (pn) => {
|
||||
// @ts-ignore
|
||||
const WAWebApiContact = window.Store.LidUtils;
|
||||
// @ts-ignore
|
||||
const WAWebWidFactory = window.Store.WidFactory;
|
||||
const WAWebApiContact = window.require('WAWebApiContact');
|
||||
const WAWebWidFactory = window.require('WAWebWidFactory');
|
||||
|
||||
const wid = WAWebWidFactory.createWid(pn);
|
||||
const result = WAWebApiContact.getCurrentLid(wid);
|
||||
@@ -652,16 +640,18 @@ export class WebjsClientCore extends Client {
|
||||
inviteCode: string,
|
||||
limit: number,
|
||||
): Promise<WebjsChannelMessage[]> {
|
||||
await this.ensureWahaExtraInjected();
|
||||
const response: _GetNewsletterPreviewDataResponse =
|
||||
await this.pupPage.evaluate(
|
||||
async (code, limit) => {
|
||||
// Overwrite limit
|
||||
window['WAHA'].WAWebNewsletterGatingUtils = () => limit;
|
||||
// Overwrite the server-side message count so the preview fetches
|
||||
// exactly `limit` messages
|
||||
window.require(
|
||||
'WAWebNewsletterGatingUtils',
|
||||
).getMaxMsgCountFromServer = () => limit;
|
||||
|
||||
const result = await window[
|
||||
'WAHA'
|
||||
].WAWebNewsletterPreviewJob.getNewsletterPreviewData(code, 'guest');
|
||||
const result = await window
|
||||
.require('WAWebNewsletterPreviewJob')
|
||||
.getNewsletterPreviewData(code, 'guest');
|
||||
for (const newsletterReaction of result.newsletterReactions) {
|
||||
// puppeter doesn't support Map,
|
||||
// so we need to convert it to object
|
||||
@@ -671,11 +661,9 @@ export class WebjsClientCore extends Client {
|
||||
}
|
||||
|
||||
// Fetch one more time to save in database so we can fetch media later
|
||||
await window[
|
||||
'WAHA'
|
||||
].WAWebLoadNewsletterPreviewChatAction.loadNewsletterPreviewChat(
|
||||
code,
|
||||
);
|
||||
await window
|
||||
.require('WAWebLoadNewsletterPreviewChatAction')
|
||||
.loadNewsletterPreviewChat(code);
|
||||
return result;
|
||||
},
|
||||
inviteCode,
|
||||
@@ -704,40 +692,20 @@ export class WebjsClientCore extends Client {
|
||||
* Channels Search methods
|
||||
*/
|
||||
async searchChannelsView(params: any): Promise<any> {
|
||||
await this.ensureWahaExtraInjected();
|
||||
const newsletters: any = await this.pupPage.evaluate(async (params) => {
|
||||
return await window[
|
||||
'WAHA'
|
||||
].WAWebNewsletterDirectorySearchJob.getNewsletterDirectoryList(params);
|
||||
return await window
|
||||
.require('WAWebNewsletterDirectorySearchJob')
|
||||
.getNewsletterDirectoryList(params);
|
||||
}, params);
|
||||
return newsletters;
|
||||
}
|
||||
|
||||
async searchChannelsText(params: any): Promise<any> {
|
||||
await this.ensureWahaExtraInjected();
|
||||
const newsletters: any = await this.pupPage.evaluate(async (params) => {
|
||||
return await window[
|
||||
'WAHA'
|
||||
].WAWebNewsletterDirectorySearchJob.getNewsletterDirectorySearchResults(
|
||||
params,
|
||||
);
|
||||
return await window
|
||||
.require('WAWebNewsletterDirectorySearchJob')
|
||||
.getNewsletterDirectorySearchResults(params);
|
||||
}, params);
|
||||
return newsletters;
|
||||
}
|
||||
|
||||
protected async ensureWahaExtraInjected() {
|
||||
const hasWahaExtra = await this.pupPage.evaluate(() => {
|
||||
return Boolean(
|
||||
// @ts-ignore
|
||||
window.WAHA &&
|
||||
// @ts-ignore
|
||||
window.WAHA.WAWebNewsletterDirectorySearchJob &&
|
||||
// @ts-ignore
|
||||
window.WAHA.WAWebNewsletterPreviewJob,
|
||||
);
|
||||
});
|
||||
if (!hasWahaExtra) {
|
||||
await this.injectWaha();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
exports.LoadWAHA = () => {
|
||||
window.WAHA = {};
|
||||
window.WAHA.WAWebBizLabelEditingAction = window.require(
|
||||
'WAWebBizLabelEditingAction',
|
||||
);
|
||||
|
||||
window.WAHA.getChats = async (pagination, filter) => {
|
||||
let chats = window.Store.Chat.getModelsArray().slice();
|
||||
|
||||
// Filter chats by IDs if filter is provided
|
||||
if (filter && filter.ids && filter.ids.length > 0) {
|
||||
chats = chats.filter((chat) => filter.ids.includes(chat.id._serialized));
|
||||
}
|
||||
|
||||
const paginator = new window.Paginator(pagination);
|
||||
chats = paginator.apply(chats);
|
||||
const chatPromises = chats.map((chat) => window.WWebJS.getChatModel(chat));
|
||||
return await Promise.all(chatPromises);
|
||||
};
|
||||
|
||||
// Set push name
|
||||
window.WAHA.WAWebSetPushnameConnAction = window.require(
|
||||
'WAWebSetPushnameConnAction',
|
||||
);
|
||||
|
||||
// Get my status
|
||||
window.WAHA.WAWebTextStatusCollection = window.require(
|
||||
'WAWebTextStatusCollection',
|
||||
);
|
||||
};
|
||||
@@ -1,18 +0,0 @@
|
||||
exports.LoadWAHACore = () => {
|
||||
// Channels Search
|
||||
window.WAHA.WAWebNewsletterDirectorySearchJob = window.require(
|
||||
'WAWebNewsletterDirectorySearchJob',
|
||||
);
|
||||
// Channels load "preview" - fetch messages by invite code
|
||||
window.WAHA.WAWebNewsletterPreviewJob = window.require(
|
||||
'WAWebNewsletterPreviewJob',
|
||||
);
|
||||
window.WAHA.WAWebLoadNewsletterPreviewChatAction = window.require(
|
||||
'WAWebLoadNewsletterPreviewChatAction',
|
||||
);
|
||||
|
||||
// Rewrite newsletter count to different number
|
||||
window.WAHA.WAWebNewsletterGatingUtils = window.require(
|
||||
'WAWebNewsletterGatingUtils',
|
||||
);
|
||||
};
|
||||
@@ -664,14 +664,12 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
|
||||
|
||||
private async loadClientInfo() {
|
||||
const data = await this.whatsapp.pupPage.evaluate(() => {
|
||||
const WAWebUserPrefsMeUser = window.require('WAWebUserPrefsMeUser');
|
||||
return {
|
||||
// @ts-ignore
|
||||
...window.Store.Conn.serialize(),
|
||||
...window.require('WAWebConnModel').Conn.serialize(),
|
||||
wid:
|
||||
// @ts-ignore
|
||||
window.Store.User.getMaybeMePnUser() ||
|
||||
// @ts-ignore
|
||||
window.Store.User.getMaybeMeLidUser(),
|
||||
WAWebUserPrefsMeUser.getMaybeMePnUser() ||
|
||||
WAWebUserPrefsMeUser.getMaybeMeLidUser(),
|
||||
};
|
||||
});
|
||||
this.whatsapp.info = data as any;
|
||||
|
||||
Reference in new issue
Block a user