From aedf41b76232f5212eefb34206ba6be3d26b22e2 Mon Sep 17 00:00:00 2001 From: Aliyss Snow <33941859+Aliyss@users.noreply.github.com> Date: Sun, 11 Oct 2020 21:50:12 +0200 Subject: [PATCH] feat: Pin and unpin Chats (#166) * Added Pinning - chat.pin() to pin - chat.unpin() to unpin Co-authored-by: Pedro S. Lopez --- example.js | 5 ++++- index.d.ts | 12 +++++++++++- src/Client.js | 37 +++++++++++++++++++++++++++++++++++++ src/structures/Chat.js | 22 ++++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/example.js b/example.js index c495e80..532533a 100644 --- a/example.js +++ b/example.js @@ -166,9 +166,12 @@ client.on('message', async msg => { } else { msg.reply('I can only delete my own messages'); } + } else if (msg.body === '!pin') { + const chat = await msg.getChat(); + await chat.pin(); } else if (msg.body === '!archive') { const chat = await msg.getChat(); - chat.archive(); + await chat.archive(); } else if (msg.body === '!mute') { const chat = await msg.getChat(); // mute the chat for 20 seconds diff --git a/index.d.ts b/index.d.ts index 06c23e3..2c59594 100644 --- a/index.d.ts +++ b/index.d.ts @@ -17,7 +17,13 @@ declare namespace WAWebJS { getInviteInfo(inviteCode: string): Promise /** Enables and returns the archive state of the Chat */ - archiveChat(): Promise + archiveChat(chatId: string): Promise + + /** Pins the Chat and returns its new Pin state */ + pinChat(chatId: string): Promise + + /** Unpins the Chat and returns its new Pin state */ + unpinChat(chatId: string): Promise /** * Create a new group @@ -679,6 +685,10 @@ declare namespace WAWebJS { /** Archives this chat */ archive: () => Promise, + /** Pins this chat and returns its new Pin state */ + pin: () => Promise, + /** Unpins this chat and returns its new Pin state */ + unpin: () => Promise, /** Clears all messages from the chat */ clearMessages: () => Promise, /** Stops typing or recording in chat immediately. */ diff --git a/src/Client.js b/src/Client.js index dc3d82b..6eda30e 100644 --- a/src/Client.js +++ b/src/Client.js @@ -610,6 +610,43 @@ class Client extends EventEmitter { }, chatId); } + /** + * Pins the Chat + * @returns {Promise} New pin state. Could be false if the max number of pinned chats was reached. + */ + async pinChat(chatId) { + return this.pupPage.evaluate(async chatId => { + let chat = window.Store.Chat.get(chatId); + if (chat.pin) { + return true; + } + const MAX_PIN_COUNT = 3; + if (window.Store.Chat.models.length > MAX_PIN_COUNT) { + let maxPinned = window.Store.Chat.models[MAX_PIN_COUNT - 1].pin; + if (maxPinned) { + return false; + } + } + await window.Store.Cmd.pinChat(chat, true); + return true; + }, chatId); + } + + /** + * Unpins the Chat + * @returns {Promise} New pin state + */ + async unpinChat(chatId) { + return this.pupPage.evaluate(async chatId => { + let chat = window.Store.Chat.get(chatId); + if (!chat.pin) { + return false; + } + await window.Store.Cmd.pinChat(chat, false); + return false; + }, chatId); + } + /** * Mutes the Chat until a specified date * @param {string} chatId ID of the chat that will be muted diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 70f6c8d..13d5af5 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -57,6 +57,12 @@ class Chat extends Base { */ this.archived = data.archive; + /** + * Indicates if the Chat is pinned + * @type {boolean} + */ + this.pinned = !!data.pin; + /** * Indicates if the chat is muted or not * @type {number} @@ -124,6 +130,22 @@ class Chat extends Base { return this.client.unarchiveChat(this.id._serialized); } + /** + * Pins this chat + * @returns {Promise} New pin state. Could be false if the max number of pinned chats was reached. + */ + async pin() { + return this.client.pinChat(this.id._serialized); + } + + /** + * Unpins this chat + * @returns {Promise} New pin state + */ + async unpin() { + return this.client.unpinChat(this.id._serialized); + } + /** * Mutes this chat until a specified date * @param {Date} unmuteDate Date at which the Chat will be unmuted