diff --git a/src/channel.js b/src/channel.js index 5bd91eca6..a9376ca41 100644 --- a/src/channel.js +++ b/src/channel.js @@ -530,8 +530,8 @@ export class Channel { * * @return {object} Server response */ - async getReactions(message_id, options) { - return await this.getClient().get( + getReactions(message_id, options) { + return this.getClient().get( this.getClient().baseURL + `/messages/${message_id}/reactions`, { ...options, @@ -539,6 +539,19 @@ export class Channel { ); } + /** + * getMessagesById - Retrieves a list of messages by ID + * + * @param {string} messageIds The ids of the messages to retrieve from this channel + * + * @return {object} Server response + */ + getMessagesById(messageIds) { + return this.getClient().get(this._channelURL() + '/messages', { + ids: messageIds.join(','), + }); + } + /** * lastRead - returns the last time the user marked the channel as read if the user never marked the channel as read, this will return null * @return {date} diff --git a/test/test.js b/test/test.js index 6eb3f867f..78200adb0 100644 --- a/test/test.js +++ b/test/test.js @@ -2624,10 +2624,54 @@ describe('Chat', function() { }); }); + describe('channel.getMessagesById', function() { + let channel; + const user = uuidv4(); + const msgIds = [uuidv4(), uuidv4()]; + + before(async function() { + const client = await getTestClientForUser(user); + channel = client.channel('messaging', uuidv4(), { + members: [user], + }); + await channel.create(); + await channel.sendMessage({ id: msgIds[0], text: 'text' }); + await channel.sendMessage({ id: msgIds[1], text: 'text' }); + await channel.sendReaction(msgIds[0], { type: 'like' }); + await channel.sendReaction(msgIds[1], { type: 'like' }); + }); + + it('empty list', function(done) { + channel + .getMessagesById([]) + .then(() => done('should have failed')) + .catch(() => done()); + }); + + it('get one message and check reactions are populated', async function() { + const response = await channel.getMessagesById([msgIds[0]]); + expect(response.messages).to.be.an('array'); + expect(response.messages).to.have.length(1); + expect(response.messages[0].id).to.eq(msgIds[0]); + expect(response.messages[0].own_reactions).to.be.an('array'); + expect(response.messages[0].own_reactions).to.have.length(1); + expect(response.messages[0].latest_reactions).to.be.an('array'); + expect(response.messages[0].latest_reactions).to.have.length(1); + }); + + it('get two message', async function() { + const response = await channel.getMessagesById(msgIds); + expect(response.messages).to.be.an('array'); + expect(response.messages).to.have.length(2); + expect(msgIds.indexOf(response.messages[0].id)).to.not.eq(-1); + expect(msgIds.indexOf(response.messages[1].id)).to.not.eq(-1); + }); + }); + describe('unread counts for messages send by muted users', function() { - let user1 = uuidv4(); - let user2 = uuidv4(); //muted by user 1 - let user3 = uuidv4(); + const user1 = uuidv4(); + const user2 = uuidv4(); //muted by user 1 + const user3 = uuidv4(); let channel; let client1, client2, client3; diff --git a/types/stream-chat/index.d.ts b/types/stream-chat/index.d.ts index 6ee7b7d90..9940d9bbd 100644 --- a/types/stream-chat/index.d.ts +++ b/types/stream-chat/index.d.ts @@ -356,6 +356,7 @@ export class Channel { off(callbackOrString: string, callbackOrNothing: any): void; hide(userId?: string, clearHistory?: boolean): Promise; show(userId?: string): Promise; + getMessagesById(messageIds: string[]): Promise; } export class ChannelState {