From e141a5df9765ffb60bb3b2f27e7a70b0fa4d29dc Mon Sep 17 00:00:00 2001 From: Marcelo Carvalho Date: Mon, 21 Dec 2020 23:48:19 -0300 Subject: [PATCH] feat: send media as document (#490) Co-authored-by: Pedro S. Lopez --- index.d.ts | 2 ++ src/Client.js | 2 ++ src/util/Injected.js | 13 ++++++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index bfa5e32..678357a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -553,6 +553,8 @@ declare namespace WAWebJS { linkPreview?: boolean /** Send audio as voice message */ sendAudioAsVoice?: boolean + /** Send media as document */ + sendMediaAsDocument?: boolean /** Automatically parse vCards and send them as contacts */ parseVCards?: boolean /** Image or videos caption */ diff --git a/src/Client.js b/src/Client.js index 8560094..c5357bb 100644 --- a/src/Client.js +++ b/src/Client.js @@ -420,6 +420,7 @@ class Client extends EventEmitter { * @typedef {Object} MessageSendOptions * @property {boolean} [linkPreview=true] - Show links preview * @property {boolean} [sendAudioAsVoice=false] - Send audio as voice message + * @property {boolean} [sendMediaAsDocument=false] = Send media as a document * @property {boolean} [parseVCards=true] - Automatically parse vCards and send them as contacts * @property {string} [caption] - Image or video caption * @property {string} [quotedMessageId] - Id of the message that is being quoted (or replied to) @@ -440,6 +441,7 @@ class Client extends EventEmitter { let internalOptions = { linkPreview: options.linkPreview === false ? undefined : true, sendAudioAsVoice: options.sendAudioAsVoice, + sendMediaAsDocument: options.sendMediaAsDocument, caption: options.caption, quotedMessageId: options.quotedMessageId, parseVCards: options.parseVCards === false ? false : true, diff --git a/src/util/Injected.js b/src/util/Injected.js index 0e15e90..654b361 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -56,7 +56,10 @@ exports.LoadUtils = () => { window.WWebJS.sendMessage = async (chat, content, options = {}) => { let attOptions = {}; if (options.attachment) { - attOptions = await window.WWebJS.processMediaData(options.attachment, options.sendAudioAsVoice); + attOptions = await window.WWebJS.processMediaData(options.attachment, { + forceVoice: options.sendAudioAsVoice, + forceDocument: options.sendMediaAsDocument + }); content = attOptions.preview; delete options.attachment; } @@ -157,10 +160,10 @@ exports.LoadUtils = () => { return window.Store.Msg.get(newMsgId._serialized); }; - window.WWebJS.processMediaData = async (mediaInfo, forceVoice) => { + window.WWebJS.processMediaData = async (mediaInfo, { forceVoice, forceDocument }) => { const file = window.WWebJS.mediaInfoToFile(mediaInfo); const mData = await window.Store.OpaqueData.createFromData(file, file.type); - const mediaPrep = window.Store.MediaPrep.prepRawMedia(mData, {}); + const mediaPrep = window.Store.MediaPrep.prepRawMedia(mData, { asDocument: forceDocument }); const mediaData = await mediaPrep.waitForPrep(); const mediaObject = window.Store.MediaObject.getOrCreateMediaObject(mediaData.filehash); @@ -173,6 +176,10 @@ exports.LoadUtils = () => { mediaData.type = 'ptt'; } + if (forceDocument) { + mediaData.type = 'document'; + } + if (!(mediaData.mediaBlob instanceof window.Store.OpaqueData)) { mediaData.mediaBlob = await window.Store.OpaqueData.createFromData(mediaData.mediaBlob, mediaData.mediaBlob.type); }