feat: mute/unmute chats (#168)

Co-authored-by: Pedro S. Lopez <pedroslopez@me.com>
This commit is contained in:
Aliyss Snow
2020-05-24 06:10:26 +02:00
committed by GitHub
parent cca74ec8b9
commit 4beeab9b2c
3 changed files with 49 additions and 0 deletions

View File

@@ -169,6 +169,12 @@ client.on('message', async msg => {
} else if (msg.body === '!archive') {
const chat = await msg.getChat();
chat.archive();
} else if (msg.body === '!mute') {
const chat = await msg.getChat();
// mute the chat for 20 seconds
const unmuteDate = new Date();
unmuteDate.setSeconds(unmuteDate.getSeconds() + 20);
await chat.mute(unmuteDate);
} else if (msg.body === '!typing') {
const chat = await msg.getChat();
// simulates typing in the chat

View File

@@ -535,6 +535,32 @@ class Client extends EventEmitter {
}, chatId);
}
/**
* Mutes the Chat until a specified date and returns the new mute state
* @param {string} chatId ID of the chat that will be muted
* @param {Date} unmuteDate Date when the chat will be unmuted
* @returns {Promise<Boolean>} new mute state
*/
async muteChat(chatId, unmuteDate) {
return await this.pupPage.evaluate(async (chatId, timestamp) => {
let chat = await window.Store.Chat.get(chatId);
await chat.mute.mute(timestamp, !0);
}, chatId, unmuteDate.getTime() / 1000);
}
/**
* Unmutes the Chat and returns the new mute state
* @param {string} chatId ID of the chat that will be unmuted
* @returns {Promise<Boolean>} new mute state
*/
async unmuteChat(chatId) {
return await this.pupPage.evaluate(async chatId => {
let chat = await window.Store.Chat.get(chatId);
await window.Store.Cmd.muteChat(chat, false);
return chat.mute.isMuted;
}, chatId);
}
/**
* Returns the contact ID's profile picture URL, if privacy settings allow it
* @param {string} contactId the whatsapp user's ID

View File

@@ -112,6 +112,23 @@ class Chat extends Base {
return this.client.unarchiveChat(this.id._serialized);
}
/**
* Mutes this chat until a specified date
* @param {Date} unmuteDate Date at which the Chat will be unmuted
* @returns {Promise<Boolean>} new mute state
*/
async mute(unmuteDate) {
return this.client.muteChat(this.id._serialized, unmuteDate);
}
/**
* Unmutes this chat
* @returns {Promise<Boolean>} new mute state
*/
async unmute() {
return this.client.unmuteChat(this.id._serialized);
}
/**
* Loads chat messages, sorted from earliest to latest.
* @param {Object} searchOptions Options for searching messages. Right now only limit is supported.