Compare commits

...

5 Commits

Author SHA1 Message Date
Rajeh Taher
308ab9826c Merge branch 'main' into message-by-id 2021-09-21 21:07:26 +03:00
Rajeh Taher
6cf9c2a0f3 Merge branch 'main' into message-by-id 2021-09-02 18:43:27 +03:00
Rajeh Taher
60761399ca ESLint fix 2021-08-28 00:45:40 +03:00
Rajeh Taher
7a8e922696 Added Tests 2021-08-28 00:43:29 +03:00
Pedro Lopez
841719ff4c feat: get message by id 2021-08-21 03:56:42 -04:00
2 changed files with 44 additions and 3 deletions

View File

@@ -617,6 +617,37 @@ class Client extends EventEmitter {
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');
const [fromMe, chatId, id] = params;
const chatWid = window.Store.WidFactory.createWid(chatId);
const fullMsgId = {
fromMe: Boolean(fromMe),
remote: chatWid,
id,
};
const msgKey = new window.Store.MsgKey(fullMsgId);
const chat = await window.Store.Chat.find(msgKey.remote);
const ctx = await chat.getSearchContext(msgKey);
if(ctx.collection && ctx.collection.loadAroundPromise) {
await ctx.collection.loadAroundPromise;
}
msg = window.Store.Msg.get(messageId);
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
* @param {string} inviteCode

View File

@@ -349,7 +349,18 @@ END:VCARD`;
expect(msg.vCards[1]).to.match(/BEGIN:VCARD/);
});
});
describe('Get Messages', function() {
it ('can get a message by it\'s ID', async function() {
const chat = await client.getChatById(remoteId);
const [message] = await chat.fetchMessages({limit: 1});
const messageById = await client.getMessageById(message.id._serialized);
expect(messageById).to.exist;
expect(messageById).to.be.instanceOf(Message);
expect(messageById).to.be.equal(message);
});
});
describe('Get Chats', function () {
it('can get a chat by its ID', async function () {
const chat = await client.getChatById(remoteId);
@@ -385,7 +396,6 @@ END:VCARD`;
expect(contact).to.be.instanceOf(Contact);
});
});
describe('Numbers and Users', function () {
it('can verify that a user is registered', async function () {
const isRegistered = await client.isRegisteredUser(remoteId);
@@ -414,4 +424,4 @@ END:VCARD`;
});
});
});
});
});