diff --git a/index.d.ts b/index.d.ts index 1e94099..98e3a19 100644 --- a/index.d.ts +++ b/index.d.ts @@ -265,6 +265,22 @@ declare namespace WAWebJS { reaction: Reaction ) => void): this + /** Emitted when a chat is removed */ + on(event: 'chat_removed', listener: ( + /** The chat that was removed */ + chat: Chat + ) => void): this + + /** Emitted when a chat is archived/unarchived */ + on(event: 'chat_archived', listener: ( + /** The chat that was archived/unarchived */ + chat: Chat, + /** State the chat is currently in */ + currState: boolean, + /** State the chat was previously in */ + prevState: boolean + ) => void): this + /** Emitted when loading screen is appearing */ on(event: 'loading_screen', listener: (percent: string, message: string) => void): this diff --git a/src/Client.js b/src/Client.js index 586fee8..5ed0114 100644 --- a/src/Client.js +++ b/src/Client.js @@ -10,7 +10,7 @@ const { WhatsWebURL, DefaultOptions, Events, WAState } = require('./util/Constan const { ExposeStore, LoadUtils } = require('./util/Injected'); const ChatFactory = require('./factories/ChatFactory'); const ContactFactory = require('./factories/ContactFactory'); -const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification, Label, Call, Buttons, List, Reaction } = require('./structures'); +const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification, Label, Call, Buttons, List, Reaction, Chat } = require('./structures'); const LegacySessionAuth = require('./authStrategies/LegacySessionAuth'); const NoAuth = require('./authStrategies/NoAuth'); @@ -524,6 +524,26 @@ class Client extends EventEmitter { } }); + await page.exposeFunction('onRemoveChatEvent', (chat) => { + /** + * Emitted when a chat is removed + * @event Client#chat_removed + * @param {Chat} chat + */ + this.emit(Events.CHAT_REMOVED, new Chat(this, chat)); + }); + + await page.exposeFunction('onArchiveChatEvent', (chat, currState, prevState) => { + /** + * Emitted when a chat is archived/unarchived + * @event Client#chat_archived + * @param {Chat} chat + * @param {boolean} currState + * @param {boolean} prevState + */ + this.emit(Events.CHAT_ARCHIVED, new Chat(this, chat), currState, prevState); + }); + await page.evaluate(() => { window.Store.Msg.on('change', (msg) => { window.onChangeMessageEvent(window.WWebJS.getMessageModel(msg)); }); window.Store.Msg.on('change:type', (msg) => { window.onChangeMessageTypeEvent(window.WWebJS.getMessageModel(msg)); }); @@ -533,6 +553,8 @@ class Client extends EventEmitter { window.Store.AppState.on('change:state', (_AppState, state) => { window.onAppStateChangedEvent(state); }); window.Store.Conn.on('change:battery', (state) => { window.onBatteryStateChangedEvent(state); }); window.Store.Call.on('add', (call) => { window.onIncomingCall(call); }); + window.Store.Chat.on('remove', async (chat) => { window.onRemoveChatEvent(await window.WWebJS.getChatModel(chat)); }); + window.Store.Chat.on('change:archive', async (chat, currState, prevState) => { window.onArchiveChatEvent(await window.WWebJS.getChatModel(chat), currState, prevState); }); window.Store.Msg.on('add', (msg) => { if (msg.isNewMsg) { if(msg.type === 'ciphertext') { diff --git a/src/util/Constants.js b/src/util/Constants.js index 39c85df..2d77de0 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -36,6 +36,8 @@ exports.Events = { AUTHENTICATED: 'authenticated', AUTHENTICATION_FAILURE: 'auth_failure', READY: 'ready', + CHAT_REMOVED: 'chat_removed', + CHAT_ARCHIVED: 'chat_archived', MESSAGE_RECEIVED: 'message', MESSAGE_CREATE: 'message_create', MESSAGE_REVOKED_EVERYONE: 'message_revoke_everyone',