11const axios = require ( 'axios' )
22const { globalApiKey, disabledCallbacks, enableWebHook } = require ( './config' )
33const { logger } = require ( './logger' )
4+ const ChatFactory = require ( 'whatsapp-web.js/src/factories/ChatFactory' )
5+ const Client = require ( 'whatsapp-web.js' ) . Client
6+ const { Chat, Message } = require ( 'whatsapp-web.js/src/structures' )
47
58// Trigger webhook endpoint
69const triggerWebhook = ( webhookURL , sessionId , dataType , data ) => {
@@ -73,6 +76,103 @@ const exposeFunctionIfAbsent = async (page, name, fn) => {
7376 await page . exposeFunction ( name , fn )
7477}
7578
79+ const patchWWebLibrary = async ( client ) => {
80+ // MUST be run after the 'ready' event fired
81+ Client . prototype . getChats = async function ( searchOptions = { } ) {
82+ const chats = await this . pupPage . evaluate ( async ( searchOptions ) => {
83+ return await window . WWebJS . getChats ( { ...searchOptions } )
84+ } , searchOptions )
85+
86+ return chats . map ( chat => ChatFactory . create ( this , chat ) )
87+ }
88+
89+ Chat . prototype . fetchMessages = async function ( searchOptions ) {
90+ const messages = await this . client . pupPage . evaluate ( async ( chatId , searchOptions ) => {
91+ const msgFilter = ( m ) => {
92+ if ( m . isNotification ) {
93+ return false
94+ }
95+ if ( searchOptions && searchOptions . fromMe !== undefined && m . id . fromMe !== searchOptions . fromMe ) {
96+ return false
97+ }
98+ if ( searchOptions && searchOptions . since !== undefined && Number . isFinite ( searchOptions . since ) && m . t < searchOptions . since ) {
99+ return false
100+ }
101+ return true
102+ }
103+
104+ const chat = await window . WWebJS . getChat ( chatId , { getAsModel : false } )
105+ let msgs = chat . msgs . getModelsArray ( ) . filter ( msgFilter )
106+
107+ if ( searchOptions && searchOptions . limit > 0 ) {
108+ while ( msgs . length < searchOptions . limit ) {
109+ const loadedMessages = await window . Store . ConversationMsgs . loadEarlierMsgs ( chat )
110+ if ( ! loadedMessages || ! loadedMessages . length ) break
111+ msgs = [ ...loadedMessages . filter ( msgFilter ) , ...msgs ]
112+ }
113+
114+ if ( msgs . length > searchOptions . limit ) {
115+ msgs . sort ( ( a , b ) => ( a . t > b . t ) ? 1 : - 1 )
116+ msgs = msgs . splice ( msgs . length - searchOptions . limit )
117+ }
118+ }
119+
120+ return msgs . map ( m => window . WWebJS . getMessageModel ( m ) )
121+ } , this . id . _serialized , searchOptions )
122+
123+ return messages . map ( m => new Message ( this . client , m ) )
124+ }
125+
126+ await client . pupPage . evaluate ( ( ) => {
127+ // hotfix for https://github.com/pedroslopez/whatsapp-web.js/pull/3643
128+ window . WWebJS . getChats = async ( searchOptions = { } ) => {
129+ const chatFilter = ( c ) => {
130+ if ( searchOptions && searchOptions . unread === true && c . unreadCount === 0 ) {
131+ return false
132+ }
133+ if ( searchOptions && searchOptions . since !== undefined && Number . isFinite ( searchOptions . since ) && c . t < searchOptions . since ) {
134+ return false
135+ }
136+ return true
137+ }
138+
139+ const allChats = window . Store . Chat . getModelsArray ( )
140+
141+ const filteredChats = allChats . filter ( chatFilter )
142+
143+ return await Promise . all (
144+ filteredChats . map ( chat => window . WWebJS . getChatModel ( chat ) )
145+ )
146+ }
147+
148+ // hotfix for https://github.com/pedroslopez/whatsapp-web.js/pull/3703
149+ window . Store . FindOrCreateChat = window . require ( 'WAWebFindChatAction' )
150+ window . WWebJS . getChat = async ( chatId , { getAsModel = true } = { } ) => {
151+ const isChannel = / @ \w * n e w s l e t t e r \b / . test ( chatId )
152+ const chatWid = window . Store . WidFactory . createWid ( chatId )
153+ let chat
154+
155+ if ( isChannel ) {
156+ try {
157+ chat = window . Store . NewsletterCollection . get ( chatId )
158+ if ( ! chat ) {
159+ await window . Store . ChannelUtils . loadNewsletterPreviewChat ( chatId )
160+ chat = await window . Store . NewsletterCollection . find ( chatWid )
161+ }
162+ } catch ( err ) {
163+ chat = null
164+ }
165+ } else {
166+ chat = window . Store . Chat . get ( chatWid ) || ( await window . Store . FindOrCreateChat . findOrCreateLatestChat ( chatWid ) ) ?. chat
167+ }
168+
169+ return getAsModel && chat
170+ ? await window . WWebJS . getChatModel ( chat , { isChannel } )
171+ : chat
172+ }
173+ } )
174+ }
175+
76176module . exports = {
77177 triggerWebhook,
78178 sendErrorResponse,
@@ -81,5 +181,6 @@ module.exports = {
81181 sendMessageSeenStatus,
82182 decodeBase64,
83183 sleep,
84- exposeFunctionIfAbsent
184+ exposeFunctionIfAbsent,
185+ patchWWebLibrary
85186}
0 commit comments