diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 1982d0bef71f5..ef07da8d1df72 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -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(); diff --git a/packages/rocketchat-mentions/package.js b/packages/rocketchat-mentions/package.js index 55e35d9efff72..8ea9346b8731b 100644 --- a/packages/rocketchat-mentions/package.js +++ b/packages/rocketchat-mentions/package.js @@ -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'); }); diff --git a/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js b/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js new file mode 100644 index 0000000000000..6c2bba4ce3e9a --- /dev/null +++ b/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js @@ -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(); + } +}); diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 4924f3fcae7a0..65bbcf98ee78e 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -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); + }); + }); });