Skip to content

Commit

Permalink
Merge pull request #10105 from RocketChat/feature/rest-api-mentions-o…
Browse files Browse the repository at this point in the history
…f-channel

[NEW] Added endpoint to retrieve mentions of a channel
  • Loading branch information
rodrigok authored Mar 26, 2018
2 parents 4ab41bc + 5287b16 commit eee685f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/rocketchat-api/server/v1/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,39 @@ RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, {
}
});

RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequired: true }, {
get() {
const { roomId } = this.requestParams();
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();

if (!roomId) {
return RocketChat.API.v1.failure('The request param "roomId" is required');
}

const mentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', {
roomId,
options: {
sort: sort ? sort : { ts: 1 },
skip: offset,
limit: count
}
}));

const allMentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', {
roomId,
options: {}
}));

return RocketChat.API.v1.success({
mentions,
count: mentions.length,
offset,
total: allMentions.length
});
}
});

RocketChat.API.v1.addRoute('channels.notifications', { authRequired: true }, {
get() {
const { roomId } = this.requestParams();
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-mentions/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ Package.onUse(function(api) {
]);

api.addFiles('server/server.js', 'server');
api.addFiles('server/methods/getUserMentionsByChannel.js', 'server');
api.addFiles('client/client.js', 'client');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Meteor.methods({
getUserMentionsByChannel({ roomId, options }) {
check(roomId, String);

if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getUserMentionsByChannel' });
}

const room = RocketChat.models.Rooms.findOneById(roomId);

if (!room) {
throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'getUserMentionsByChannel' });
}

const user = RocketChat.models.Users.findOneById(Meteor.userId());

return RocketChat.models.Messages.findVisibleByMentionAndRoomId(user.username, roomId, options).fetch();
}
});
20 changes: 20 additions & 0 deletions tests/end-to-end/api/02-channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,24 @@ describe('[Channels]', function() {
.end(done);
});
});

describe('/channels.getAllUserMentionsByChannel', () => {
it('should return and array of mentions by channel', (done) => {
request.get(api('channels.getAllUserMentionsByChannel'))
.set(credentials)
.query({
roomId: channel._id
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('mentions').and.to.be.an('array');
expect(res.body).to.have.property('count');
expect(res.body).to.have.property('offset');
expect(res.body).to.have.property('total');
})
.end(done);
});
});
});

0 comments on commit eee685f

Please sign in to comment.