From 3b4632201d54146b7923a69dbc0f0f931029f206 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 6 Feb 2022 16:26:25 -0400 Subject: [PATCH 01/14] fix: add missing message types --- index.d.ts | 22 +++++++++++++++++++++- src/util/Constants.js | 20 +++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 05e0285..d9d5ea6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -443,7 +443,27 @@ declare namespace WAWebJS { PAYMENT = 'payment', UNKNOWN = 'unknown', GROUP_INVITE = 'groups_v4_invite', - BUTTONS_RESPONSE = 'buttons_response' + LIST = 'list', + LIST_RESPONSE = 'list_response', + BUTTONS_RESPONSE = 'buttons_response', + PAYMENT = 'payment', + BROADCAST_NOTIFICATION = 'broadcast_notification', + CALL_LOG = 'call_log', + CIPHERTEXT = 'ciphertext', + DEBUG = 'debug', + E2E_NOTIFICATION = 'e2e_notification', + GP2 = 'gp2', + GROUP_NOTIFICATION = 'group_notification', + HSM = 'hsm', + INTERACTIVE = 'interactive', + NATIVE_FLOW = 'native_flow', + NOTIFICATION = 'notification', + NOTIFICATION_TEMPLATE = 'notification_template', + OVERSIZED = 'oversized', + PROTOCOL = 'protocol', + PTT = 'ptt', + REACTION = 'reaction', + TEMPLATE_BUTTON_REPLY = 'template_button_reply', } /** Client status */ diff --git a/src/util/Constants.js b/src/util/Constants.js index 2ff4a6f..1e02e04 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -77,8 +77,26 @@ exports.MessageTypes = { UNKNOWN: 'unknown', GROUP_INVITE: 'groups_v4_invite', LIST: 'list', + LIST_RESPONSE: 'list_response', BUTTONS_RESPONSE: 'buttons_response', - PAYMENT: 'payment' + PAYMENT: 'payment', + BROADCAST_NOTIFICATION: 'broadcast_notification', + CALL_LOG: 'call_log', + CIPHERTEXT: 'ciphertext', + DEBUG: 'debug', + E2E_NOTIFICATION: 'e2e_notification', + GP2: 'gp2', + GROUP_NOTIFICATION: 'group_notification', + HSM: 'hsm', + INTERACTIVE: 'interactive', + NATIVE_FLOW: 'native_flow', + NOTIFICATION: 'notification', + NOTIFICATION_TEMPLATE: 'notification_template', + OVERSIZED: 'oversized', + PROTOCOL: 'protocol', + PTT: 'ptt', + REACTION: 'reaction', + TEMPLATE_BUTTON_REPLY: 'template_button_reply', }; /** From a9b2a1aab8df69b73fff29f259281ac93fe28262 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 6 Feb 2022 16:30:44 -0400 Subject: [PATCH 02/14] remove duplicated message type --- index.d.ts | 1 - src/util/Constants.js | 1 - 2 files changed, 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index d9d5ea6..42b32bd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -461,7 +461,6 @@ declare namespace WAWebJS { NOTIFICATION_TEMPLATE = 'notification_template', OVERSIZED = 'oversized', PROTOCOL = 'protocol', - PTT = 'ptt', REACTION = 'reaction', TEMPLATE_BUTTON_REPLY = 'template_button_reply', } diff --git a/src/util/Constants.js b/src/util/Constants.js index 1e02e04..b95ccd1 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -94,7 +94,6 @@ exports.MessageTypes = { NOTIFICATION_TEMPLATE: 'notification_template', OVERSIZED: 'oversized', PROTOCOL: 'protocol', - PTT: 'ptt', REACTION: 'reaction', TEMPLATE_BUTTON_REPLY: 'template_button_reply', }; From d7478de68adce1b454667f5e59d7a9650aa973b3 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 6 Feb 2022 18:54:10 -0400 Subject: [PATCH 03/14] fix: emit message events after ciphertext is decrypted fixes #924 fixes #1041 --- src/Client.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Client.js b/src/Client.js index 6b761fb..3d8de39 100644 --- a/src/Client.js +++ b/src/Client.js @@ -239,8 +239,6 @@ class Client extends EventEmitter { // Register events await page.exposeFunction('onAddMessageEvent', msg => { - if (!msg.isNewMsg) return; - if (msg.type === 'gp2') { const notification = new GroupNotification(this, msg); if (msg.subtype === 'add' || msg.subtype === 'invite') { @@ -425,7 +423,6 @@ class Client extends EventEmitter { }); await page.evaluate(() => { - window.Store.Msg.on('add', (msg) => { if (msg.isNewMsg) window.onAddMessageEvent(window.WWebJS.getMessageModel(msg)); }); window.Store.Msg.on('change', (msg) => { window.onChangeMessageEvent(window.WWebJS.getMessageModel(msg)); }); window.Store.Msg.on('change:type', (msg) => { window.onChangeMessageTypeEvent(window.WWebJS.getMessageModel(msg)); }); window.Store.Msg.on('change:ack', (msg,ack) => { window.onMessageAckEvent(window.WWebJS.getMessageModel(msg), ack); }); @@ -434,6 +431,16 @@ class Client extends EventEmitter { window.Store.AppState.on('change:state', (_AppState, state) => { window.onAppStateChangedEvent(state); }); window.Store.Conn.on('change:battery', (state) => { window.onBatteryStateChangedEvent(state); }); window.Store.Call.on('add', (call) => { window.onIncomingCall(call); }); + window.Store.Msg.on('add', (msg) => { + if (msg.isNewMsg) { + if(msg.type === 'ciphertext') { + // defer message event until ciphertext is resolved (type changed) + msg.once('change:type', (_msg) => window.onAddMessageEvent(window.WWebJS.getMessageModel(_msg))); + } else { + window.onAddMessageEvent(window.WWebJS.getMessageModel(msg)); + } + } + }); }); /** From 7631400de13caf872ba4e23ddcbe8af817730b2b Mon Sep 17 00:00:00 2001 From: "Pedro S. Lopez" Date: Sun, 6 Feb 2022 19:16:57 -0400 Subject: [PATCH 04/14] add back the `pull_request` trigger on lint --- .github/workflows/lint.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f0228fc..82036b5 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,6 +2,7 @@ name: Lint on: push: + pull_request: jobs: eslint: From aa9e19a19e1ee07286c10bb3a0335f98a8483d6e Mon Sep 17 00:00:00 2001 From: Azeem Haider Date: Mon, 7 Feb 2022 04:27:38 +0500 Subject: [PATCH 05/14] fetchMessages gets currently available messages by default (#1015) * get chat default messages fix #1014 * fixing format * Update src/structures/Chat.js Co-authored-by: Pedro S. Lopez Co-authored-by: Pedro S. Lopez --- src/structures/Chat.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/structures/Chat.js b/src/structures/Chat.js index b8aacb1..e4f59d1 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -171,30 +171,30 @@ 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 {Number} [searchOptions.limit=50] The amount of messages to return. 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 {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. * @returns {Promise>} */ async fetchMessages(searchOptions) { - if (!searchOptions || !searchOptions.limit) { - searchOptions = { limit: 50 }; - } - let messages = await this.client.pupPage.evaluate(async (chatId, limit) => { + let messages = await this.client.pupPage.evaluate(async (chatId, searchOptions) => { const msgFilter = m => !m.isNotification; // dont include notification messages const chat = window.Store.Chat.get(chatId); let msgs = chat.msgs.models.filter(msgFilter); - while (msgs.length < limit) { - const loadedMessages = await chat.loadEarlierMsgs(); - if (!loadedMessages) break; - msgs = [...loadedMessages.filter(msgFilter), ...msgs]; + if (searchOptions && searchOptions.limit) { + while (msgs.length < searchOptions.limit) { + const loadedMessages = await chat.loadEarlierMsgs(); + if (!loadedMessages) break; + msgs = [...loadedMessages.filter(msgFilter), ...msgs]; + } + if (msgs.length > searchOptions.limit) + msgs = msgs.splice(msgs.length - searchOptions.limit); } msgs.sort((a, b) => (a.t > b.t) ? 1 : -1); - if (msgs.length > limit) msgs = msgs.splice(msgs.length - limit); return msgs.map(m => window.WWebJS.getMessageModel(m)); - }, this.id._serialized, searchOptions.limit); + }, this.id._serialized, searchOptions); return messages.map(m => new Message(this.client, m)); } From 64803c2ba61bf4c5d97503b99a9c2d1f44d41639 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 6 Feb 2022 19:30:13 -0400 Subject: [PATCH 06/14] update typings for `fetchMessages` changes --- index.d.ts | 3 +-- src/structures/Chat.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 42b32bd..65b6ea9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -930,10 +930,9 @@ declare namespace WAWebJS { export interface MessageSearchOptions { /** - * The amount of messages to return. + * 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. - * @default 50 */ limit?: number } diff --git a/src/structures/Chat.js b/src/structures/Chat.js index e4f59d1..01eec83 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -181,7 +181,7 @@ class Chat extends Base { const chat = window.Store.Chat.get(chatId); let msgs = chat.msgs.models.filter(msgFilter); - if (searchOptions && searchOptions.limit) { + if (searchOptions && searchOptions.limit > 0) { while (msgs.length < searchOptions.limit) { const loadedMessages = await chat.loadEarlierMsgs(); if (!loadedMessages) break; From 4aae8b0f32fe276aa5cf0ef890b94984ef0db371 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 6 Feb 2022 20:03:59 -0400 Subject: [PATCH 07/14] fix: sort before splicing when fetching messages --- src/structures/Chat.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 01eec83..20aa7dc 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -187,11 +187,13 @@ class Chat extends Base { if (!loadedMessages) break; msgs = [...loadedMessages.filter(msgFilter), ...msgs]; } - if (msgs.length > searchOptions.limit) + + if (msgs.length > searchOptions.limit) { + msgs.sort((a, b) => (a.t > b.t) ? 1 : -1); msgs = msgs.splice(msgs.length - searchOptions.limit); + } } - msgs.sort((a, b) => (a.t > b.t) ? 1 : -1); return msgs.map(m => window.WWebJS.getMessageModel(m)); }, this.id._serialized, searchOptions); From 303ea3ab6b5c1c1ca9a0f471090246cfc9d1afad Mon Sep 17 00:00:00 2001 From: Noam Zaks Date: Mon, 7 Feb 2022 02:56:57 +0200 Subject: [PATCH 08/14] fix types and docs for Buttons class (#1070) * fix: typing of buttons class * fix remaining types Co-authored-by: Pedro Lopez --- index.d.ts | 4 ++-- src/structures/Buttons.js | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 65b6ea9..f596284 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1211,11 +1211,11 @@ declare namespace WAWebJS { /** Message type buttons */ export class Buttons { body: string | MessageMedia - buttons: Array> + buttons: Array<{ buttonId: string; buttonText: {displayText: string}; type: number }> title?: string | null footer?: string | null - constructor(body: string, buttons: Array>, title?: string | null, footer?: string | null) + constructor(body: string, buttons: Array<{ id?: string; body: string }>, title?: string | null, footer?: string | null) } } diff --git a/src/structures/Buttons.js b/src/structures/Buttons.js index a67b3fa..66b7cdf 100644 --- a/src/structures/Buttons.js +++ b/src/structures/Buttons.js @@ -3,13 +3,27 @@ const MessageMedia = require('./MessageMedia'); const Util = require('../util/Util'); +/** + * Button spec used in Buttons constructor + * @typedef {Object} ButtonSpec + * @property {string=} id - Custom ID to set on the button. A random one will be generated if one is not passed. + * @property {string} body - The text to show on the button. + */ + +/** + * @typedef {Object} FormattedButtonSpec + * @property {string} buttonId + * @property {number} type + * @property {Object} buttonText + */ + /** * Message type buttons */ class Buttons { /** * @param {string|MessageMedia} body - * @param {Array>} buttons + * @param {ButtonSpec[]} buttons - See {@link ButtonSpec} * @param {string?} title * @param {string?} footer */ @@ -41,7 +55,7 @@ class Buttons { /** * buttons of message - * @type {Array>} + * @type {FormattedButtonSpec[]} */ this.buttons = this._format(buttons); if(!this.buttons.length){ throw '[BT01] No buttons';} @@ -50,8 +64,8 @@ class Buttons { /** * Creates button array from simple array - * @param {Array>} buttons - * @returns {Array>} + * @param {ButtonSpec[]} buttons + * @returns {FormattedButtonSpec[]} * @example * Input: [{id:'customId',body:'button1'},{body:'button2'},{body:'button3'},{body:'button4'}] * Returns: [{ buttonId:'customId',buttonText:{'displayText':'button1'},type: 1 },{buttonId:'n3XKsL',buttonText:{'displayText':'button2'},type:1},{buttonId:'NDJk0a',buttonText:{'displayText':'button3'},type:1}] From 7e36cceaf0eea684bad3cc3bd1d706bf347d0de5 Mon Sep 17 00:00:00 2001 From: Shir Serlui <70711723+shirser121@users.noreply.github.com> Date: Mon, 7 Feb 2022 03:00:10 +0200 Subject: [PATCH 09/14] `extra` options can override other options when sending message (#1103) * Fix the option to use special options When we want to use some extra function we need to pass it into the options object, but because it inserted in the first line, the auto options reset some options (like 'type'). This is minor change that can help to add functionallity * Update Injected.js Put the special options in the end * Update Injected.js Fix , mistake * only `extra` options can override everything else Co-authored-by: Pedro Lopez --- src/Client.js | 2 +- src/util/Injected.js | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Client.js b/src/Client.js index 3d8de39..b09c2f3 100644 --- a/src/Client.js +++ b/src/Client.js @@ -540,7 +540,7 @@ class Client extends EventEmitter { quotedMessageId: options.quotedMessageId, parseVCards: options.parseVCards === false ? false : true, mentionedJidList: Array.isArray(options.mentions) ? options.mentions.map(contact => contact.id._serialized) : [], - ...options.extra + extraOptions: options.extra }; const sendSeen = typeof options.sendSeen === 'undefined' ? true : options.sendSeen; diff --git a/src/util/Injected.js b/src/util/Injected.js index 06b7049..fe0f9c8 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -153,7 +153,7 @@ exports.LoadUtils = () => { } } - let extraOptions = {}; + let buttonOptions = {}; if(options.buttons){ let caption; if(options.buttons.type === 'chat') { @@ -162,7 +162,7 @@ exports.LoadUtils = () => { }else{ caption = options.caption ? options.caption : ' '; //Caption can't be empty } - extraOptions = { + buttonOptions = { productHeaderImageRejected: false, isFromTemplate: false, isDynamicReplyButtonsMsg: true, @@ -175,12 +175,12 @@ exports.LoadUtils = () => { delete options.buttons; } + let listOptions = {}; if(options.list){ if(window.Store.Conn.platform === 'smba' || window.Store.Conn.platform === 'smbi'){ throw '[LT01] Whatsapp business can\'t send this yet'; } - extraOptions = { - ...extraOptions, + listOptions = { type: 'list', footer: options.list.footer, list: { @@ -190,7 +190,7 @@ exports.LoadUtils = () => { body: options.list.description }; delete options.list; - delete extraOptions.list.footer; + delete listOptions.list.footer; } const newMsgId = new window.Store.MsgKey({ @@ -199,6 +199,9 @@ exports.LoadUtils = () => { id: window.Store.genId(), }); + const extraOptions = options.extraOptions || {}; + delete options.extraOptions; + const message = { ...options, id: newMsgId, @@ -215,6 +218,8 @@ exports.LoadUtils = () => { ...attOptions, ...quotedMsgOptions, ...vcardOptions, + ...buttonOptions, + ...listOptions, ...extraOptions }; From 70ca073f3851ab72f3185c6056ccab8a5ab671d4 Mon Sep 17 00:00:00 2001 From: "Pedro S. Lopez" Date: Sun, 6 Feb 2022 21:48:55 -0400 Subject: [PATCH 10/14] fix: send messages properly when disappearing mode is on (#1174) * fix: send messages properly when dissappearing mode is on * add `isEphemeral` to message model --- index.d.ts | 2 ++ src/structures/Message.js | 6 ++++++ src/util/Injected.js | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/index.d.ts b/index.d.ts index f596284..6e62c54 100644 --- a/index.d.ts +++ b/index.d.ts @@ -547,6 +547,8 @@ declare namespace WAWebJS { isStatus: boolean, /** Indicates if the message is a Gif */ isGif: boolean, + /** Indicates if the message will disappear after it expires */ + isEphemeral: boolean, /** ID for the Chat that this message was sent to, except if the message was sent by the current user */ from: string, /** Indicates if the message was sent by the current user */ diff --git a/src/structures/Message.js b/src/structures/Message.js index 0c582cd..d1e0e87 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -185,6 +185,12 @@ class Message extends Base { */ this.isGif = Boolean(data.isGif); + /** + * Indicates if the message will disappear after it expires + * @type {boolean} + */ + this.isEphemeral = data.isEphemeral; + /** Title */ if (data.title) { this.title = data.title; diff --git a/src/util/Injected.js b/src/util/Injected.js index fe0f9c8..f125f83 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -202,6 +202,12 @@ exports.LoadUtils = () => { const extraOptions = options.extraOptions || {}; delete options.extraOptions; + const ephemeralSettings = { + ephemeralDuration: chat.isEphemeralSettingOn() ? chat.getEphemeralSetting() : undefined, + ephemeralSettingTimestamp: chat.getEphemeralSettingTimestamp() || undefined, + disappearingModeInitiator: chat.getDisappearingModeInitiator() || undefined, + }; + const message = { ...options, id: newMsgId, @@ -214,6 +220,7 @@ exports.LoadUtils = () => { t: parseInt(new Date().getTime() / 1000), isNewMsg: true, type: 'chat', + ...ephemeralSettings, ...locationOptions, ...attOptions, ...quotedMsgOptions, @@ -318,6 +325,7 @@ exports.LoadUtils = () => { window.WWebJS.getMessageModel = message => { const msg = message.serialize(); + msg.isEphemeral = message.isEphemeral; msg.isStatusV3 = message.isStatusV3; msg.links = (message.getLinks()).map(link => ({ link: link.href, From 93c8a2b05e204c6d63615f3351ea87c75c4e2b6f Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 6 Feb 2022 22:20:08 -0400 Subject: [PATCH 11/14] chore: mark version v1.15.5 --- docs/Base.html | 6 +- docs/BusinessContact.html | 6 +- docs/Buttons.html | 17 +- docs/Call.html | 6 +- docs/Chat.html | 29 +- docs/Client.html | 33 ++- docs/Client.js.html | 57 ++-- docs/ClientInfo.html | 15 +- docs/Contact.html | 6 +- docs/GroupChat.html | 24 +- docs/GroupNotification.html | 6 +- docs/InterfaceController.html | 6 +- docs/Label.html | 6 +- docs/List.html | 6 +- docs/Location.html | 6 +- docs/Message.html | 20 +- docs/MessageMedia.html | 6 +- docs/Order.html | 6 +- docs/PrivateChat.html | 9 +- docs/PrivateContact.html | 6 +- docs/Product.html | 6 +- docs/Util.html | 15 +- docs/global.html | 341 +++++++++++++++++++++- docs/index.html | 116 +++++++- docs/structures_Base.js.html | 6 +- docs/structures_BusinessContact.js.html | 6 +- docs/structures_Buttons.js.html | 28 +- docs/structures_Call.js.html | 6 +- docs/structures_Chat.js.html | 34 ++- docs/structures_ClientInfo.js.html | 6 +- docs/structures_Contact.js.html | 6 +- docs/structures_GroupChat.js.html | 6 +- docs/structures_GroupNotification.js.html | 6 +- docs/structures_Label.js.html | 6 +- docs/structures_List.js.html | 6 +- docs/structures_Location.js.html | 6 +- docs/structures_Message.js.html | 14 +- docs/structures_MessageMedia.js.html | 6 +- docs/structures_Order.js.html | 6 +- docs/structures_Payment.js.html | 6 +- docs/structures_PrivateChat.js.html | 6 +- docs/structures_PrivateContact.js.html | 6 +- docs/structures_Product.js.html | 6 +- docs/structures_ProductMetadata.js.html | 6 +- docs/util_Constants.js.html | 25 +- docs/util_InterfaceController.js.html | 6 +- docs/util_Util.js.html | 6 +- package.json | 2 +- 48 files changed, 743 insertions(+), 228 deletions(-) diff --git a/docs/Base.html b/docs/Base.html index f82b9d5..53e96d3 100644 --- a/docs/Base.html +++ b/docs/Base.html @@ -4,7 +4,7 @@ - whatsapp-web.js 1.15.4 » Class: Base + whatsapp-web.js 1.15.5 » Class: Base @@ -15,7 +15,7 @@ @@ -50,7 +50,7 @@
diff --git a/docs/BusinessContact.html b/docs/BusinessContact.html index 35359d5..27444b8 100644 --- a/docs/BusinessContact.html +++ b/docs/BusinessContact.html @@ -4,7 +4,7 @@ - whatsapp-web.js 1.15.4 » Class: BusinessContact + whatsapp-web.js 1.15.5 » Class: BusinessContact @@ -15,7 +15,7 @@ @@ -314,7 +314,7 @@
diff --git a/docs/Buttons.html b/docs/Buttons.html index 185feb7..782fd95 100644 --- a/docs/Buttons.html +++ b/docs/Buttons.html @@ -4,7 +4,7 @@ - whatsapp-web.js 1.15.4 » Class: Buttons + whatsapp-web.js 1.15.5 » Class: Buttons @@ -15,7 +15,7 @@ @@ -26,7 +26,7 @@