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

feat: get pinned messages #3004

Draft
wants to merge 8 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
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ declare namespace WAWebJS {
/** Get message by ID */
getMessageById(messageId: string): Promise<Message>

/** Gets instances of all pinned messages in a chat */
getPinnedMessages(chatId: string): Promise<[Message]|[]>

/** Get all current contact instances */
getContacts(): Promise<Contact[]>

Expand Down Expand Up @@ -1468,6 +1471,8 @@ declare namespace WAWebJS {
getLabels: () => Promise<Label[]>,
/** Add or remove labels to this Chat */
changeLabels: (labelIds: Array<string | number>) => Promise<void>
/** Gets instances of all pinned messages in a chat */
getPinnedMessages: () => Promise<[Message]|[]>
/** Sync history conversation of the Chat */
syncHistory: () => Promise<boolean>
}
Expand Down
34 changes: 34 additions & 0 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,40 @@ 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 = await window.Store.Chat.find(chatWid);
if (!chat) return [];

/* await window.Store.PinnedMsgUtils.seekAndDestroyExpiredPins(
window.Store.PinnedMsgUtils.PinInChatCollection.byChatId(chatWid).toArray()
).catch((_) => _); */

const msgs = await window.Store.PinnedMsgUtils.getTable().equals(['chatId'], chatWid.toString());
/* window.Store.PinnedMsgUtils.PinInChatCollection.add(
msgs.map(window.Store.PinnedMsgUtils.createPinInChatModel)
); */

/* for (const msg of window.Store.PinnedMsgUtils.PinInChatCollection.getModelsArray()) {
await window.Store.MsgCollection.hydrateOrGetMessages([msg.parentMsgKey.toString()]).catch((_) => _);
} */

const pinnedMsgs = msgs.map((msg) => window.Store.Msg.get(msg.parentMsgKey/* ._serialized */));

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
Expand Down
8 changes: 8 additions & 0 deletions src/structures/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,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<boolean>} True if operation completed successfully, false otherwise.
Expand Down
6 changes: 5 additions & 1 deletion src/util/Injected/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ exports.ExposeStore = () => {
window.Store.ForwardUtils = {
...window.require('WAWebForwardMessagesToChat')
};

window.Store.PinnedMsgUtils = {
/* ...window.require('WAWebPinInChatCollection'),
...window.require('WAWebPinMessageAction'), */
...window.require('WAWebPinInChatSchema')
};
window.Store.StickerTools = {
...window.require('WAWebImageUtils'),
...window.require('WAWebAddWebpMetadata')
Expand Down
Loading