Feat: get message by id (#1634)

* feat: get message by id

Add the feature of getting messages by id.
It's loads messages that are not loaded in the Store.Msg, and add them into the Store.Msg.
I don't use the draft because this is already work

* Fix typo

* Fix for test

* Update index.d.ts

---------

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>
Co-authored-by: Codeboss <131376628+codebossdev@users.noreply.github.com>
This commit is contained in:
Shir Serlui 2023-08-07 20:10:11 +03:00 • committed by GitHub
commit b4d82d8425
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

3
index.d.ts vendored
View file

@ -60,6 +60,9 @@ declare namespace WAWebJS {
/** Get contact instance by ID */ /** Get contact instance by ID */
getContactById(contactId: string): Promise<Contact> getContactById(contactId: string): Promise<Contact>
/** Get message by ID */
getMessageById(messageId: string): Promise<Message>
/** Get all current contact instances */ /** Get all current contact instances */
getContacts(): Promise<Contact[]> getContacts(): Promise<Contact[]>

View file

@ -898,6 +898,24 @@ class Client extends EventEmitter {
return ContactFactory.create(this, contact); return ContactFactory.create(this, contact);
} }
async getMessageById(messageId) {
const msg = await this.pupPage.evaluate(async messageId => {
let msg = window.Store.Msg.get(messageId);
if(msg) return window.WWebJS.getMessageModel(msg);
const params = messageId.split('_');
if(params.length !== 3) throw new Error('Invalid serialized message id specified');
let messagesObject = await window.Store.Msg.getMessagesById([messageId]);
if (messagesObject && messagesObject.messages.length) msg = messagesObject.messages[0];
if(msg) return window.WWebJS.getMessageModel(msg);
}, messageId);
if(msg) return new Message(this, msg);
return null;
}
/** /**
* Returns an object with information about the invite code's group * Returns an object with information about the invite code's group