diff --git a/index.d.ts b/index.d.ts index a99c508..edf4684 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1062,6 +1062,10 @@ declare namespace WAWebJS { * Set this to Infinity to load all messages. */ limit?: number + /** + * Return only messages from the bot number or vise versa. To get all messages, leave the option undefined. + */ + fromMe?: boolean } /** diff --git a/src/authStrategies/RemoteAuth.js b/src/authStrategies/RemoteAuth.js index 1645ccd..5ae85cc 100644 --- a/src/authStrategies/RemoteAuth.js +++ b/src/authStrategies/RemoteAuth.js @@ -6,7 +6,9 @@ try { var unzipper = require('unzipper'); var archiver = require('archiver'); } catch { - throw new Error('Optional Dependencies [fs-extra, unzipper, archiver] are required to use RemoteAuth. Make sure to run npm install correctly and remove the --no-optional flag'); + fs = undefined; + unzipper = undefined; + archiver = undefined; } const path = require('path'); @@ -23,6 +25,7 @@ const BaseAuthStrategy = require('./BaseAuthStrategy'); */ class RemoteAuth extends BaseAuthStrategy { constructor({ clientId, dataPath, store, backupSyncIntervalMs } = {}) { + if (!fs && !unzipper && !archiver) throw new Error('Optional Dependencies [fs-extra, unzipper, archiver] are required to use RemoteAuth. Make sure to run npm install correctly and remove the --no-optional flag'); super(); const idRegex = /^[-_\w]+$/i; @@ -198,4 +201,4 @@ class RemoteAuth extends BaseAuthStrategy { } } -module.exports = RemoteAuth; \ No newline at end of file +module.exports = RemoteAuth; diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 6f0f0e2..241d297 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -170,13 +170,22 @@ class Chat extends Base { /** * Loads chat messages, sorted from earliest to latest. - * @param {Object} searchOptions Options for searching messages. Right now only limit is supported. + * @param {Object} searchOptions Options for searching messages. Right now only limit and fromMe is supported. * @param {Number} [searchOptions.limit] The amount of messages to return. If no limit is specified, the available messages will be returned. Note that the actual number of returned messages may be smaller if there aren't enough messages in the conversation. Set this to Infinity to load all messages. + * @param {Boolean} [searchOptions.fromMe] Return only messages from the bot number or vise versa. To get all messages, leave the option undefined. * @returns {Promise>} */ async fetchMessages(searchOptions) { let messages = await this.client.pupPage.evaluate(async (chatId, searchOptions) => { - const msgFilter = m => !m.isNotification; // dont include notification messages + const msgFilter = (m) => { + if (m.isNotification) { + return false; // dont include notification messages + } + if (searchOptions && searchOptions.fromMe && m.id.fromMe !== searchOptions.fromMe) { + return false; + } + return true; + }; const chat = window.Store.Chat.get(chatId); let msgs = chat.msgs.getModelsArray().filter(msgFilter); diff --git a/src/structures/Message.js b/src/structures/Message.js index 7c1d3d0..dc4acaf 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -410,7 +410,7 @@ class Message extends Base { signal: (new AbortController).signal }); - const data = window.WWebJS.arrayBufferToBase64(decryptedMedia); + const data = await window.WWebJS.arrayBufferToBase64Async(decryptedMedia); return { data, diff --git a/src/util/Injected.js b/src/util/Injected.js index e2e4525..b354557 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -756,6 +756,20 @@ exports.LoadUtils = () => { return window.btoa(binary); }; + window.WWebJS.arrayBufferToBase64Async = (arrayBuffer) => + new Promise((resolve, reject) => { + const blob = new Blob([arrayBuffer], { + type: 'application/octet-stream', + }); + const fileReader = new FileReader(); + fileReader.onload = () => { + const [, data] = fileReader.result.split(','); + resolve(data); + }; + fileReader.onerror = (e) => reject(e); + fileReader.readAsDataURL(blob); + }); + window.WWebJS.getFileHash = async (data) => { let buffer = await data.arrayBuffer(); const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);