fix: Chat.isMuted (#2999)

* fix: isMuted returns true or false

* docs: add missing parts

* refactor: apply simplification

* fix: update chat instance after calling mute/unmute

* stylee: clarify and fix docs

* fix: fix convertation of Date to timestamp

* refactor: small code refactor
This commit is contained in:
alechkos 2025-05-30 16:41:31 +03:00 • committed by GitHub
commit 91217f87ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 41 additions and 20 deletions

10
index.d.ts vendored
View file

@ -116,7 +116,7 @@ declare namespace WAWebJS {
* @param chatId ID of the chat that will be muted * @param chatId ID of the chat that will be muted
* @param unmuteDate Date when the chat will be unmuted, leave as is to mute forever * @param unmuteDate Date when the chat will be unmuted, leave as is to mute forever
*/ */
muteChat(chatId: string, unmuteDate?: Date): Promise<void> muteChat(chatId: string, unmuteDate?: Date): Promise<{ isMuted: boolean, muteExpiration: number }>
/** /**
* Request authentication via pairing code instead of QR code * Request authentication via pairing code instead of QR code
@ -208,7 +208,7 @@ declare namespace WAWebJS {
unarchiveChat(chatId: string): Promise<boolean> unarchiveChat(chatId: string): Promise<boolean>
/** Unmutes the Chat */ /** Unmutes the Chat */
unmuteChat(chatId: string): Promise<void> unmuteChat(chatId: string): Promise<{ isMuted: boolean, muteExpiration: number }>
/** Sets the current user's profile picture */ /** Sets the current user's profile picture */
setProfilePicture(media: MessageMedia): Promise<boolean> setProfilePicture(media: MessageMedia): Promise<boolean>
@ -1468,10 +1468,10 @@ declare namespace WAWebJS {
/** Loads chat messages, sorted from earliest to latest. */ /** Loads chat messages, sorted from earliest to latest. */
fetchMessages: (searchOptions: MessageSearchOptions) => Promise<Message[]>, fetchMessages: (searchOptions: MessageSearchOptions) => Promise<Message[]>,
/** Mutes this chat forever, unless a date is specified */ /** Mutes this chat forever, unless a date is specified */
mute: (unmuteDate?: Date) => Promise<void>, mute: (unmuteDate?: Date) => Promise<{ isMuted: boolean, muteExpiration: number }>,
/** Send a message to this chat */ /** Send a message to this chat */
sendMessage: (content: MessageContent, options?: MessageSendOptions) => Promise<Message>, sendMessage: (content: MessageContent, options?: MessageSendOptions) => Promise<Message>,
/** Set the message as seen */ /** Set the chat as seen */
sendSeen: () => Promise<void>, sendSeen: () => Promise<void>,
/** Simulate recording audio in chat. This will last for 25 seconds */ /** Simulate recording audio in chat. This will last for 25 seconds */
sendStateRecording: () => Promise<void>, sendStateRecording: () => Promise<void>,
@ -1480,7 +1480,7 @@ declare namespace WAWebJS {
/** un-archives this chat */ /** un-archives this chat */
unarchive: () => Promise<void>, unarchive: () => Promise<void>,
/** Unmutes this chat */ /** Unmutes this chat */
unmute: () => Promise<void>, unmute: () => Promise<{ isMuted: boolean, muteExpiration: number }>,
/** Returns the Contact that corresponds to this Chat. */ /** Returns the Contact that corresponds to this Chat. */
getContact: () => Promise<Contact>, getContact: () => Promise<Contact>,
/** Marks this Chat as unread */ /** Marks this Chat as unread */

View file

@ -1193,25 +1193,38 @@ class Client extends EventEmitter {
/** /**
* Mutes this chat forever, unless a date is specified * Mutes this chat forever, unless a date is specified
* @param {string} chatId ID of the chat that will be muted * @param {string} chatId ID of the chat that will be muted
* @param {?Date} unmuteDate Date when the chat will be unmuted, leave as is to mute forever * @param {?Date} unmuteDate Date when the chat will be unmuted, don't provide a value to mute forever
* @returns {Promise<{isMuted: boolean, muteExpiration: number}>}
*/ */
async muteChat(chatId, unmuteDate) { async muteChat(chatId, unmuteDate) {
unmuteDate = unmuteDate ? unmuteDate.getTime() / 1000 : -1; unmuteDate = unmuteDate ? Math.floor(unmuteDate.getTime() / 1000) : -1;
await this.pupPage.evaluate(async (chatId, timestamp) => { return this._muteUnmuteChat(chatId, 'MUTE', unmuteDate);
let chat = await window.Store.Chat.get(chatId);
await chat.mute.mute({expiration: timestamp, sendDevice:!0});
}, chatId, unmuteDate || -1);
} }
/** /**
* Unmutes the Chat * Unmutes the Chat
* @param {string} chatId ID of the chat that will be unmuted * @param {string} chatId ID of the chat that will be unmuted
* @returns {Promise<{isMuted: boolean, muteExpiration: number}>}
*/ */
async unmuteChat(chatId) { async unmuteChat(chatId) {
await this.pupPage.evaluate(async chatId => { return this._muteUnmuteChat(chatId, 'UNMUTE');
let chat = await window.Store.Chat.get(chatId); }
await window.Store.Cmd.muteChat(chat, false);
}, chatId); /**
* Internal method to mute or unmute the chat
* @param {string} chatId ID of the chat that will be muted/unmuted
* @param {string} action The action: 'MUTE' or 'UNMUTE'
* @param {number} unmuteDateTs Timestamp at which the chat will be unmuted
* @returns {Promise<{isMuted: boolean, muteExpiration: number}>}
*/
async _muteUnmuteChat (chatId, action, unmuteDateTs) {
return this.pupPage.evaluate(async (chatId, action, unmuteDateTs) => {
const chat = window.Store.Chat.get(chatId) ?? await window.Store.Chat.find(chatId);
action === 'MUTE'
? await chat.mute.mute({ expiration: unmuteDateTs, sendDevice: true })
: await chat.mute.unmute({ sendDevice: true });
return { isMuted: chat.mute.expiration !== 0, muteExpiration: chat.mute.expiration };
}, chatId, action, unmuteDateTs || -1);
} }
/** /**

View file

@ -95,7 +95,7 @@ class Chat extends Base {
} }
/** /**
* Set the message as seen * Set the chat as seen
* @returns {Promise<Boolean>} result * @returns {Promise<Boolean>} result
*/ */
async sendSeen() { async sendSeen() {
@ -154,17 +154,25 @@ class Chat extends Base {
/** /**
* Mutes this chat forever, unless a date is specified * Mutes this chat forever, unless a date is specified
* @param {?Date} unmuteDate Date at which the Chat will be unmuted, leave as is to mute forever * @param {?Date} unmuteDate Date when the chat will be unmuted, don't provide a value to mute forever
* @returns {Promise<{isMuted: boolean, muteExpiration: number}>}
*/ */
async mute(unmuteDate) { async mute(unmuteDate) {
return this.client.muteChat(this.id._serialized, unmuteDate); const result = await this.client.muteChat(this.id._serialized, unmuteDate);
this.isMuted = result.isMuted;
this.muteExpiration = result.muteExpiration;
return result;
} }
/** /**
* Unmutes this chat * Unmutes this chat
* @returns {Promise<{isMuted: boolean, muteExpiration: number}>}
*/ */
async unmute() { async unmute() {
return this.client.unmuteChat(this.id._serialized); const result = await this.client.unmuteChat(this.id._serialized);
this.isMuted = result.isMuted;
this.muteExpiration = result.muteExpiration;
return result;
} }
/** /**

View file

@ -461,7 +461,7 @@ exports.LoadUtils = () => {
let res = chat.serialize(); let res = chat.serialize();
res.isGroup = false; res.isGroup = false;
res.formattedTitle = chat.formattedTitle; res.formattedTitle = chat.formattedTitle;
res.isMuted = chat.muteExpiration == 0 ? false : true; res.isMuted = chat.mute?.expiration !== 0;
if (chat.groupMetadata) { if (chat.groupMetadata) {
res.isGroup = true; res.isGroup = true;