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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,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 }>

/** Force reset of connection state for the client */
resetState(): Promise<void>
Expand Down Expand Up @@ -169,7 +169,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 @@ -660,13 +660,17 @@ declare namespace WAWebJS {
AUTHENTICATED = 'authenticated',
AUTHENTICATION_FAILURE = 'auth_failure',
READY = 'ready',
CHAT_REMOVED = 'chat_removed',
CHAT_ARCHIVED = 'chat_archived',
MESSAGE_RECEIVED = 'message',
MESSAGE_CIPHERTEXT = 'message_ciphertext',
MESSAGE_CREATE = 'message_create',
MESSAGE_REVOKED_EVERYONE = 'message_revoke_everyone',
MESSAGE_REVOKED_ME = 'message_revoke_me',
MESSAGE_ACK = 'message_ack',
MESSAGE_EDIT = 'message_edit',
UNREAD_COUNT = 'unread_count',
MESSAGE_REACTION = 'message_reaction',
MEDIA_UPLOADED = 'media_uploaded',
CONTACT_CHANGED = 'contact_changed',
GROUP_JOIN = 'group_join',
Expand All @@ -680,7 +684,8 @@ declare namespace WAWebJS {
STATE_CHANGED = 'change_state',
BATTERY_CHANGED = 'change_battery',
REMOTE_SESSION_SAVED = 'remote_session_saved',
CALL = 'call'
INCOMING_CALL = 'call',
VOTE_UPDATE = 'vote_update'
}

/** Group notification types */
Expand Down Expand Up @@ -1397,10 +1402,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 @@ -1409,7 +1414,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
37 changes: 29 additions & 8 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ const NoAuth = require('./authStrategies/NoAuth');
* @fires Client#qr
* @fires Client#authenticated
* @fires Client#auth_failure
* @fires Client#loading_screen
* @fires Client#ready
* @fires Client#change_battery
* @fires Client#chat_removed
* @fires Client#chat_archived
* @fires Client#message
* @fires Client#message_ack
* @fires Client#message_create
* @fires Client#message_revoke_me
* @fires Client#message_revoke_everyone
* @fires Client#message_ciphertext
* @fires Client#message_edit
* @fires Client#message_reaction
* @fires Client#media_uploaded
* @fires Client#group_join
* @fires Client#group_leave
Expand All @@ -54,7 +59,10 @@ const NoAuth = require('./authStrategies/NoAuth');
* @fires Client#contact_changed
* @fires Client#group_admin_changed
* @fires Client#group_membership_request
* @fires Client#remote_session_saved
* @fires Client#vote_update
* @fires Client#unread_count
* @fires Client#call
*/
class Client extends EventEmitter {
constructor(options = {}) {
Expand Down Expand Up @@ -1210,24 +1218,37 @@ 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
* @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);
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 {?Date} unmuteDate Date at which the Chat will be unmuted, leave as is to mute forever
* @returns {Promise<{isMuted: boolean, muteExpiration: number}>}
*/
async _muteUnmuteChat (chatId, action, unmuteDate) {
return this.pupPage.evaluate(async (chatId, action, unmuteDate) => {
const chat = window.Store.Chat.get(chatId);
action === 'MUTE'
? await chat.mute.mute({ expiration: unmuteDate, sendDevice: true })
: await chat.mute.unmute({ sendDevice: true });
return { isMuted: chat.mute.expiration !== 0, muteExpiration: chat.mute.expiration };
}, chatId, action, unmuteDate || -1);
}

/**
Expand Down
14 changes: 11 additions & 3 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 @@ -155,16 +155,24 @@ 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
* @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 { isMuted: result.isMuted, muteExpiration: result.muteExpiration };
}

/**
* 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 { isMuted: result.isMuted, muteExpiration: result.muteExpiration };
}

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

if (chat.groupMetadata) {
const chatWid = window.Store.WidFactory.createWid((chat.id._serialized));
Expand Down
Loading