@@ -292,7 +292,7 @@ class Client extends EventEmitter {
292292 await this . authStrategy . beforeBrowserInitialized ( ) ;
293293
294294 const puppeteerOpts = this . options . puppeteer ;
295- if ( puppeteerOpts && puppeteerOpts . browserWSEndpoint ) {
295+ if ( puppeteerOpts && ( puppeteerOpts . browserWSEndpoint || puppeteerOpts . browserURL ) ) {
296296 browser = await puppeteer . connect ( puppeteerOpts ) ;
297297 page = await browser . newPage ( ) ;
298298 } else {
@@ -1207,25 +1207,38 @@ class Client extends EventEmitter {
12071207 /**
12081208 * Mutes this chat forever, unless a date is specified
12091209 * @param {string } chatId ID of the chat that will be muted
1210- * @param {?Date } unmuteDate Date when the chat will be unmuted, leave as is to mute forever
1210+ * @param {?Date } unmuteDate Date when the chat will be unmuted, don't provide a value to mute forever
1211+ * @returns {Promise<{isMuted: boolean, muteExpiration: number}> }
12111212 */
12121213 async muteChat ( chatId , unmuteDate ) {
1213- unmuteDate = unmuteDate ? unmuteDate . getTime ( ) / 1000 : - 1 ;
1214- await this . pupPage . evaluate ( async ( chatId , timestamp ) => {
1215- let chat = await window . Store . Chat . get ( chatId ) ;
1216- await chat . mute . mute ( { expiration : timestamp , sendDevice :! 0 } ) ;
1217- } , chatId , unmuteDate || - 1 ) ;
1214+ unmuteDate = unmuteDate ? Math . floor ( unmuteDate . getTime ( ) / 1000 ) : - 1 ;
1215+ return this . _muteUnmuteChat ( chatId , 'MUTE' , unmuteDate ) ;
12181216 }
12191217
12201218 /**
12211219 * Unmutes the Chat
12221220 * @param {string } chatId ID of the chat that will be unmuted
1221+ * @returns {Promise<{isMuted: boolean, muteExpiration: number}> }
12231222 */
12241223 async unmuteChat ( chatId ) {
1225- await this . pupPage . evaluate ( async chatId => {
1226- let chat = await window . Store . Chat . get ( chatId ) ;
1227- await window . Store . Cmd . muteChat ( chat , false ) ;
1228- } , chatId ) ;
1224+ return this . _muteUnmuteChat ( chatId , 'UNMUTE' ) ;
1225+ }
1226+
1227+ /**
1228+ * Internal method to mute or unmute the chat
1229+ * @param {string } chatId ID of the chat that will be muted/unmuted
1230+ * @param {string } action The action: 'MUTE' or 'UNMUTE'
1231+ * @param {number } unmuteDateTs Timestamp at which the chat will be unmuted
1232+ * @returns {Promise<{isMuted: boolean, muteExpiration: number}> }
1233+ */
1234+ async _muteUnmuteChat ( chatId , action , unmuteDateTs ) {
1235+ return this . pupPage . evaluate ( async ( chatId , action , unmuteDateTs ) => {
1236+ const chat = window . Store . Chat . get ( chatId ) ?? await window . Store . Chat . find ( chatId ) ;
1237+ action === 'MUTE'
1238+ ? await chat . mute . mute ( { expiration : unmuteDateTs , sendDevice : true } )
1239+ : await chat . mute . unmute ( { sendDevice : true } ) ;
1240+ return { isMuted : chat . mute . expiration !== 0 , muteExpiration : chat . mute . expiration } ;
1241+ } , chatId , action , unmuteDateTs || - 1 ) ;
12291242 }
12301243
12311244 /**
@@ -1295,7 +1308,7 @@ class Client extends EventEmitter {
12951308 */
12961309 async resetState ( ) {
12971310 await this . pupPage . evaluate ( ( ) => {
1298- window . Store . AppState . phoneWatchdog . shiftTimer . forceRunNow ( ) ;
1311+ window . Store . AppState . reconnect ( ) ;
12991312 } ) ;
13001313 }
13011314
@@ -1735,6 +1748,23 @@ class Client extends EventEmitter {
17351748 } , flag ) ;
17361749 }
17371750
1751+ /**
1752+ * Setting background synchronization.
1753+ * NOTE: this action will take effect after you restart the client.
1754+ * @param {boolean } flag true/false
1755+ * @returns {Promise<boolean> }
1756+ */
1757+ async setBackgroundSync ( flag ) {
1758+ return await this . pupPage . evaluate ( async flag => {
1759+ const backSync = window . Store . Settings . getGlobalOfflineNotifications ( ) ;
1760+ if ( backSync === flag ) {
1761+ return flag ;
1762+ }
1763+ await window . Store . Settings . setGlobalOfflineNotifications ( flag ) ;
1764+ return flag ;
1765+ } , flag ) ;
1766+ }
1767+
17381768 /**
17391769 * Get user device count by ID
17401770 * Each WaWeb Connection counts as one device, and the phone (if exists) counts as one
@@ -1759,8 +1789,9 @@ class Client extends EventEmitter {
17591789 */
17601790 async syncHistory ( chatId ) {
17611791 return await this . pupPage . evaluate ( async ( chatId ) => {
1762- const chat = await window . WWebJS . getChat ( chatId ) ;
1763- if ( chat . endOfHistoryTransferType === 0 ) {
1792+ const chatWid = window . Store . WidFactory . createWid ( chatId ) ;
1793+ const chat = window . Store . Chat . get ( chatWid ) ?? ( await window . Store . Chat . find ( chatWid ) ) ;
1794+ if ( chat ?. endOfHistoryTransferType === 0 ) {
17641795 await window . Store . HistorySync . sendPeerDataOperationRequest ( 3 , {
17651796 chatId : chat . id
17661797 } ) ;
@@ -1769,6 +1800,39 @@ class Client extends EventEmitter {
17691800 return false ;
17701801 } , chatId ) ;
17711802 }
1803+
1804+ /**
1805+ * Save new contact to user's addressbook or edit the existing one
1806+ * @param {string } phoneNumber The contact's phone number in a format "17182222222", where "1" is a country code
1807+ * @param {string } firstName
1808+ * @param {string } lastName
1809+ * @param {boolean } [syncToAddressbook = false] If set to true, the contact will also be saved to the user's address book on their phone. False by default
1810+ * @returns {Promise<import('..').ChatId> } Object in a wid format
1811+ */
1812+ async saveOrEditAddressbookContact ( phoneNumber , firstName , lastName , syncToAddressbook = false )
1813+ {
1814+ return await this . pupPage . evaluate ( async ( phoneNumber , firstName , lastName , syncToAddressbook ) => {
1815+ return await window . Store . AddressbookContactUtils . saveContactAction (
1816+ phoneNumber ,
1817+ null ,
1818+ firstName ,
1819+ lastName ,
1820+ syncToAddressbook
1821+ ) ;
1822+ } , phoneNumber , firstName , lastName , syncToAddressbook ) ;
1823+ }
1824+
1825+ /**
1826+ * Deletes the contact from user's addressbook
1827+ * @param {string } phoneNumber The contact's phone number in a format "17182222222", where "1" is a country code
1828+ * @returns {Promise<void> }
1829+ */
1830+ async deleteAddressbookContact ( phoneNumber )
1831+ {
1832+ return await this . pupPage . evaluate ( async ( phoneNumber ) => {
1833+ return await window . Store . AddressbookContactUtils . deleteContactAction ( phoneNumber ) ;
1834+ } , phoneNumber ) ;
1835+ }
17721836}
17731837
17741838module . exports = Client ;
0 commit comments