From e6d666b5b07aced5299836f064deff9f2ed0db1c Mon Sep 17 00:00:00 2001 From: devlikepro Date: Tue, 4 Feb 2025 13:52:06 +0700 Subject: [PATCH] [core] WEBJS Channels Search, Messages Preview --- .../{WebjsClient.ts => WebjsClientCore.ts} | 12 ++++-- src/core/engines/webjs/session.webjs.core.ts | 43 ++++++++++++++++--- 2 files changed, 46 insertions(+), 9 deletions(-) rename src/core/engines/webjs/{WebjsClient.ts => WebjsClientCore.ts} (95%) diff --git a/src/core/engines/webjs/WebjsClient.ts b/src/core/engines/webjs/WebjsClientCore.ts similarity index 95% rename from src/core/engines/webjs/WebjsClient.ts rename to src/core/engines/webjs/WebjsClientCore.ts index d15bb474..e456086d 100644 --- a/src/core/engines/webjs/WebjsClient.ts +++ b/src/core/engines/webjs/WebjsClientCore.ts @@ -18,17 +18,21 @@ const { LoadPaginator } = require('./_Paginator.js'); // eslint-disable-next-line @typescript-eslint/no-var-requires const ChatFactory = require('whatsapp-web.js/src/factories/ChatFactory'); -export class WebjsClient extends Client { +export class WebjsClientCore extends Client { constructor(options) { super(options); // Wait until it's READY and inject more utils this.on(Events.READY, async () => { - await this.pupPage.evaluate(LoadLodash); - await this.pupPage.evaluate(LoadPaginator); - await this.pupPage.evaluate(LoadWAHA); + await this.injectWaha(); }); } + async injectWaha() { + await this.pupPage.evaluate(LoadLodash); + await this.pupPage.evaluate(LoadPaginator); + await this.pupPage.evaluate(LoadWAHA); + } + async unpair() { await this.pupPage.evaluate(async () => { if ( diff --git a/src/core/engines/webjs/session.webjs.core.ts b/src/core/engines/webjs/session.webjs.core.ts index 0be2c1db..cbb23190 100644 --- a/src/core/engines/webjs/session.webjs.core.ts +++ b/src/core/engines/webjs/session.webjs.core.ts @@ -4,7 +4,7 @@ import { WhatsappSession, } from '@waha/core/abc/session.abc'; import { LocalAuth } from '@waha/core/engines/webjs/LocalAuth'; -import { WebjsClient } from '@waha/core/engines/webjs/WebjsClient'; +import { WebjsClientCore } from '@waha/core/engines/webjs/WebjsClientCore'; import { AvailableInPlusVersion, NotImplementedByEngineError, @@ -15,9 +15,14 @@ import { splitAt } from '@waha/helpers'; import { PairingCodeResponse } from '@waha/structures/auth.dto'; import { Channel, + ChannelListResult, + ChannelMessage, ChannelRole, + ChannelSearchByText, + ChannelSearchByView, CreateChannelRequest, ListChannelsQuery, + PreviewChannelMessages, } from '@waha/structures/channels.dto'; import { ChatSortField, @@ -106,7 +111,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { private engineStateCheckDelayedJob: SingleDelayedJobRunner; private shouldRestart: boolean; - whatsapp: WebjsClient; + whatsapp: WebjsClientCore; protected qr: QR; public constructor(config) { @@ -169,7 +174,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { rmMaxRetries: undefined, }); this.addProxyConfig(clientOptions); - return new WebjsClient(clientOptions); + return new WebjsClientCore(clientOptions); } private restartClient() { @@ -922,6 +927,25 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { /** * Channels methods */ + public searchChannelsByView( + query: ChannelSearchByView, + ): Promise { + throw new AvailableInPlusVersion(); + } + + public searchChannelsByText( + query: ChannelSearchByText, + ): Promise { + throw new AvailableInPlusVersion(); + } + + public async previewChannelMessages( + inviteCode: string, + query: PreviewChannelMessages, + ): Promise { + throw new AvailableInPlusVersion(); + } + protected ChatToChannel(chat: WEBJSChannel): Channel { // @ts-ignore const metadata = chat.channelMetadata; @@ -938,6 +962,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { picture: null, verified: metadata.verified, role: role, + subscribersCount: null, }; } @@ -955,6 +980,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { picture: metadata.pictureUrl, verified: metadata.isVerified, role: role, + subscribersCount: metadata.subscribersCount, }; } @@ -966,6 +992,10 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { channels = channels.filter((channel) => channel.role === query.role); } + // Exclude GUEST, browser saves the data + // when we search channels or getting messages + channels = channels.filter((channel) => channel.role === 'GUEST'); + const promises = channels.map(async (channel) => this.whatsapp.getProfilePicUrl(channel.id), ); @@ -1211,7 +1241,10 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { this.events2.get(WAHAEvents.CALL_RECEIVED).switch(calls$); } - private async processIncomingMessage(message: Message, downloadMedia = true) { + protected async processIncomingMessage( + message: Message, + downloadMedia = true, + ) { if (downloadMedia) { try { message = await this.downloadMedia(message); @@ -1267,7 +1300,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { protected extractReplyTo(message: Message): ReplyToMessage | null { // @ts-ignore - const quotedMsg = message.rawData.quotedMsg; + const quotedMsg = message.rawData?.quotedMsg; if (!quotedMsg) { return; }