From 18b1d328258aa62b3affd588bd7e3c3003f1a4f2 Mon Sep 17 00:00:00 2001 From: alechkos <93551621+alechkos@users.noreply.github.com> Date: Sat, 9 Aug 2025 00:34:52 +0300 Subject: [PATCH] feat: get pinned messages (#3004) * feat: add getPinnedMessage method * docs: add docs for the method * chore: fix eslint * refactor: change logic of getting pinned messages * refactor: combine all PinnedMsgUtils * chore: remove comments and mark as ready for review * style: fix typo Co-authored-by: crazynds <36674096+crazynds@users.noreply.github.com> --------- Co-authored-by: crazynds <36674096+crazynds@users.noreply.github.com> --- index.d.ts | 7 ++++++- src/Client.js | 23 +++++++++++++++++++++++ src/structures/Chat.js | 8 ++++++++ src/util/Injected/Store.js | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index e5f5c10..db5ebcc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -99,6 +99,9 @@ declare namespace WAWebJS { /** Get message by ID */ getMessageById(messageId: string): Promise + /** Gets instances of all pinned messages in a chat */ + getPinnedMessages(chatId: string): Promise<[Message]|[]> + /** Get all current contact instances */ getContacts(): Promise @@ -370,7 +373,7 @@ declare namespace WAWebJS { ack: MessageAck ) => void): this - /** Emitted when an ack event occurrs on message type */ + /** Emitted when an edit event occurrs on message type */ on(event: 'message_edit', listener: ( /** The message that was affected */ message: Message, @@ -1601,6 +1604,8 @@ declare namespace WAWebJS { getLabels: () => Promise, /** Add or remove labels to this Chat */ changeLabels: (labelIds: Array) => Promise + /** Gets instances of all pinned messages in a chat */ + getPinnedMessages: () => Promise<[Message]|[]> /** Sync history conversation of the Chat */ syncHistory: () => Promise } diff --git a/src/Client.js b/src/Client.js index ec8d1f6..1b4e4bf 100644 --- a/src/Client.js +++ b/src/Client.js @@ -1143,6 +1143,29 @@ class Client extends EventEmitter { return null; } + /** + * Gets instances of all pinned messages in a chat + * @param {string} chatId The chat ID + * @returns {Promise<[Message]|[]>} + */ + async getPinnedMessages(chatId) { + const pinnedMsgs = await this.pupPage.evaluate(async (chatId) => { + const chatWid = window.Store.WidFactory.createWid(chatId); + const chat = window.Store.Chat.get(chatWid) ?? await window.Store.Chat.find(chatWid); + if (!chat) return []; + + const msgs = await window.Store.PinnedMsgUtils.getTable().equals(['chatId'], chatWid.toString()); + + const pinnedMsgs = msgs.map((msg) => window.Store.Msg.get(msg.parentMsgKey)); + + return !pinnedMsgs.length + ? [] + : pinnedMsgs.map((msg) => window.WWebJS.getMessageModel(msg)); + }, chatId); + + return pinnedMsgs.map((msg) => new Message(this, msg)); + } + /** * Returns an object with information about the invite code's group * @param {string} inviteCode diff --git a/src/structures/Chat.js b/src/structures/Chat.js index dca57ab..be322c5 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -279,6 +279,14 @@ class Chat extends Base { return this.client.addOrRemoveLabels(labelIds, [this.id._serialized]); } + /** + * Gets instances of all pinned messages in a chat + * @returns {Promise<[Message]|[]>} + */ + async getPinnedMessages() { + return this.client.getPinnedMessages(this.id._serialized); + } + /** * Sync chat history conversation * @return {Promise} True if operation completed successfully, false otherwise. diff --git a/src/util/Injected/Store.js b/src/util/Injected/Store.js index f14ce74..97b9256 100644 --- a/src/util/Injected/Store.js +++ b/src/util/Injected/Store.js @@ -97,6 +97,7 @@ exports.ExposeStore = () => { window.Store.DeviceList = window.require('WAWebApiDeviceList'); window.Store.HistorySync = window.require('WAWebSendNonMessageDataRequest'); window.Store.AddonReactionTable = window.require('WAWebAddonReactionTableMode').reactionTableMode; + window.Store.PinnedMsgUtils = window.require('WAWebPinInChatSchema'); window.Store.ChatGetters = window.require('WAWebChatGetters'); window.Store.UploadUtils = window.require('WAWebUploadManager'); window.Store.WAWebStreamModel = window.require('WAWebStreamModel');