Skip to content

Commit

Permalink
add getMessagesById endpoint (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarbugli authored Jan 24, 2020
1 parent 346048f commit cdc2a8e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,28 @@ 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,
},
);
}

/**
* 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}
Expand Down
50 changes: 47 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions types/stream-chat/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ export class Channel {
off(callbackOrString: string, callbackOrNothing: any): void;
hide(userId?: string, clearHistory?: boolean): Promise<APIResponse>;
show(userId?: string): Promise<APIResponse>;
getMessagesById(messageIds: string[]): Promise<APIResponse>;
}

export class ChannelState {
Expand Down

0 comments on commit cdc2a8e

Please sign in to comment.