Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Chat.isMuted #2999

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ declare namespace WAWebJS {
* @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
*/
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
Expand Down Expand Up @@ -195,7 +195,7 @@ declare namespace WAWebJS {
unarchiveChat(chatId: string): Promise<boolean>

/** Unmutes the Chat */
unmuteChat(chatId: string): Promise<void>
unmuteChat(chatId: string): Promise<{ isMuted: boolean, muteExpiration: number }>

/** Sets the current user's profile picture */
setProfilePicture(media: MessageMedia): Promise<boolean>
Expand Down Expand Up @@ -1447,10 +1447,10 @@ declare namespace WAWebJS {
/** Loads chat messages, sorted from earliest to latest. */
fetchMessages: (searchOptions: MessageSearchOptions) => Promise<Message[]>,
/** 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 */
sendMessage: (content: MessageContent, options?: MessageSendOptions) => Promise<Message>,
/** Set the message as seen */
/** Set the chat as seen */
sendSeen: () => Promise<void>,
/** Simulate recording audio in chat. This will last for 25 seconds */
sendStateRecording: () => Promise<void>,
Expand All @@ -1459,7 +1459,7 @@ declare namespace WAWebJS {
/** un-archives this chat */
unarchive: () => Promise<void>,
/** Unmutes this chat */
unmute: () => Promise<void>,
unmute: () => Promise<{ isMuted: boolean, muteExpiration: number }>,
/** Returns the Contact that corresponds to this Chat. */
getContact: () => Promise<Contact>,
/** Marks this Chat as unread */
Expand Down
33 changes: 23 additions & 10 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1193,25 +1193,38 @@ class Client extends EventEmitter {
/**
* Mutes this chat forever, unless a date is specified
* @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) {
unmuteDate = unmuteDate ? unmuteDate.getTime() / 1000 : -1;
await this.pupPage.evaluate(async (chatId, timestamp) => {
let chat = await window.Store.Chat.get(chatId);
await chat.mute.mute({expiration: timestamp, sendDevice:!0});
}, chatId, unmuteDate || -1);
unmuteDate = unmuteDate ? Math.floor(unmuteDate.getTime() / 1000) : -1;
return this._muteUnmuteChat(chatId, 'MUTE', unmuteDate);
}

/**
* Unmutes the Chat
* @param {string} chatId ID of the chat that will be unmuted
* @returns {Promise<{isMuted: boolean, muteExpiration: number}>}
*/
async unmuteChat(chatId) {
await this.pupPage.evaluate(async chatId => {
let chat = await window.Store.Chat.get(chatId);
await window.Store.Cmd.muteChat(chat, false);
}, chatId);
return this._muteUnmuteChat(chatId, 'UNMUTE');
}

/**
* 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);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/structures/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Chat extends Base {
}

/**
* Set the message as seen
* Set the chat as seen
* @returns {Promise<Boolean>} result
*/
async sendSeen() {
Expand Down Expand Up @@ -154,17 +154,25 @@ class Chat extends Base {

/**
* 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) {
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
* @returns {Promise<{isMuted: boolean, muteExpiration: number}>}
*/
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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/util/Injected/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ exports.LoadUtils = () => {
let res = chat.serialize();
res.isGroup = false;
res.formattedTitle = chat.formattedTitle;
res.isMuted = chat.muteExpiration == 0 ? false : true;
res.isMuted = chat.mute?.expiration !== 0;

if (chat.groupMetadata) {
res.isGroup = true;
Expand Down
Loading