From 99065f7518bc88341210c0e38678bc3c97e3b58a Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Mon, 12 Mar 2018 12:26:07 -0300 Subject: [PATCH 1/4] added endpoint to retrieve all the mentions of a channel --- packages/rocketchat-api/server/v1/channels.js | 47 +++++++++++++++++-- packages/rocketchat-mentions/package.js | 1 + .../methods/getUserMentionsByChannel.js | 19 ++++++++ tests/end-to-end/api/02-channels.js | 17 +++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index c8dd2905385c..6879b1634ad0 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -330,7 +330,14 @@ RocketChat.API.v1.addRoute('channels.history', { authRequired: true }, { let result; Meteor.runAsUser(this.userId, () => { - result = Meteor.call('getChannelHistory', { rid: findResult._id, latest: latestDate, oldest: oldestDate, inclusive, count, unreads }); + result = Meteor.call('getChannelHistory', { + rid: findResult._id, + latest: latestDate, + oldest: oldestDate, + inclusive, + count, + unreads + }); }); if (!result) { @@ -476,7 +483,11 @@ RocketChat.API.v1.addRoute('channels.list.joined', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.members', { authRequired: true }, { get() { - const findResult = findChannelByIdOrName({ params: this.requestParams(), checkedArchived: false, returnUsernames: true }); + const findResult = findChannelByIdOrName({ + params: this.requestParams(), + checkedArchived: false, + returnUsernames: true + }); const { offset, count } = this.getPaginationItems(); const { sort } = this.parseJsonQuery(); @@ -625,7 +636,7 @@ RocketChat.API.v1.addRoute('channels.rename', { authRequired: true }, { return RocketChat.API.v1.failure('The bodyParam "name" is required'); } - const findResult = findChannelByIdOrName({ params: { roomId: this.bodyParams.roomId} }); + const findResult = findChannelByIdOrName({ params: { roomId: this.bodyParams.roomId } }); if (findResult.name === this.bodyParams.name) { return RocketChat.API.v1.failure('The channel name is the same as what it would be renamed to.'); @@ -784,3 +795,33 @@ RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, { return RocketChat.API.v1.success(); } }); + +RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequired: true }, { + get() { + const mountMentionObject = (mention) => { + return { + messageId: mention._id, + roomId: mention.rid, + timestamp: mention.ts, + user: { + id: mention.u._id, + username: mention.u.username, + name: mention.u.name + } + }; + }; + const { roomId } = this.requestParams(); + + if (!roomId) { + return RocketChat.API.v1.failure('The request param \'roomId\' is required'); + } + + const mentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', { roomId })); + + return RocketChat.API.v1.success({ + mentions: mentions.map(mountMentionObject) + }); + } +}); + + diff --git a/packages/rocketchat-mentions/package.js b/packages/rocketchat-mentions/package.js index 55e35d9efff7..8ea9346b8731 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 000000000000..466925c9d670 --- /dev/null +++ b/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js @@ -0,0 +1,19 @@ +Meteor.methods({ + getUserMentionsByChannel({ roomId }) { + 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).fetch(); + } +}); diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 784263e7b8ce..3d82624e02b2 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -559,4 +559,21 @@ 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'); + }) + .end(done); + }); + }); }); From 9796c1d4e373bb7728816e2f9e160b14fa8cbe6d Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Wed, 21 Mar 2018 12:37:58 -0300 Subject: [PATCH 2/4] added pagination to channels.getAllUserMentionsByChannel endpoint. --- packages/rocketchat-api/server/v1/channels.js | 27 +++++++++---------- .../methods/getUserMentionsByChannel.js | 4 +-- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 6879b1634ad0..91e76c7bb774 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -430,7 +430,7 @@ RocketChat.API.v1.addRoute('channels.list', { authRequired: true }, { //Special check for the permissions if (RocketChat.authz.hasPermission(this.userId, 'view-joined-room')) { ourQuery.usernames = { - $in: [ this.user.username ] + $in: [this.user.username] }; } else if (!RocketChat.authz.hasPermission(this.userId, 'view-c-room')) { return RocketChat.API.v1.unauthorized(); @@ -798,28 +798,25 @@ RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequired: true }, { get() { - const mountMentionObject = (mention) => { - return { - messageId: mention._id, - roomId: mention.rid, - timestamp: mention.ts, - user: { - id: mention.u._id, - username: mention.u.username, - name: mention.u.name - } - }; - }; 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 })); + const mentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', { + roomId, + options: { + sort: sort ? sort : { ts: 1 }, + skip: offset, + limit: count + } + })); return RocketChat.API.v1.success({ - mentions: mentions.map(mountMentionObject) + mentions }); } }); diff --git a/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js b/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js index 466925c9d670..6c2bba4ce3e9 100644 --- a/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js +++ b/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js @@ -1,5 +1,5 @@ Meteor.methods({ - getUserMentionsByChannel({ roomId }) { + getUserMentionsByChannel({ roomId, options }) { check(roomId, String); if (!Meteor.userId()) { @@ -14,6 +14,6 @@ Meteor.methods({ const user = RocketChat.models.Users.findOneById(Meteor.userId()); - return RocketChat.models.Messages.findVisibleByMentionAndRoomId(user.username, roomId).fetch(); + return RocketChat.models.Messages.findVisibleByMentionAndRoomId(user.username, roomId, options).fetch(); } }); From 90a44b83e88e122c2db82a036231cc55bf76cba9 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Wed, 21 Mar 2018 12:37:58 -0300 Subject: [PATCH 3/4] added pagination to channels.getAllUserMentionsByChannel endpoint. --- packages/rocketchat-api/server/v1/channels.js | 29 +++++++++---------- .../methods/getUserMentionsByChannel.js | 4 +-- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index 6879b1634ad0..d494e4b8074c 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -430,7 +430,7 @@ RocketChat.API.v1.addRoute('channels.list', { authRequired: true }, { //Special check for the permissions if (RocketChat.authz.hasPermission(this.userId, 'view-joined-room')) { ourQuery.usernames = { - $in: [ this.user.username ] + $in: [this.user.username] }; } else if (!RocketChat.authz.hasPermission(this.userId, 'view-c-room')) { return RocketChat.API.v1.unauthorized(); @@ -798,28 +798,25 @@ RocketChat.API.v1.addRoute('channels.unarchive', { authRequired: true }, { RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequired: true }, { get() { - const mountMentionObject = (mention) => { - return { - messageId: mention._id, - roomId: mention.rid, - timestamp: mention.ts, - user: { - id: mention.u._id, - username: mention.u.username, - name: mention.u.name - } - }; - }; 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'); + return RocketChat.API.v1.failure('The request param "roomId" is required'); } - const mentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', { roomId })); + const mentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', { + roomId, + options: { + sort: sort ? sort : { ts: 1 }, + skip: offset, + limit: count + } + })); return RocketChat.API.v1.success({ - mentions: mentions.map(mountMentionObject) + mentions }); } }); diff --git a/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js b/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js index 466925c9d670..6c2bba4ce3e9 100644 --- a/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js +++ b/packages/rocketchat-mentions/server/methods/getUserMentionsByChannel.js @@ -1,5 +1,5 @@ Meteor.methods({ - getUserMentionsByChannel({ roomId }) { + getUserMentionsByChannel({ roomId, options }) { check(roomId, String); if (!Meteor.userId()) { @@ -14,6 +14,6 @@ Meteor.methods({ const user = RocketChat.models.Users.findOneById(Meteor.userId()); - return RocketChat.models.Messages.findVisibleByMentionAndRoomId(user.username, roomId).fetch(); + return RocketChat.models.Messages.findVisibleByMentionAndRoomId(user.username, roomId, options).fetch(); } }); From 49c300cfb006d135bf16832e50e0fa5569247c5c Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Mon, 26 Mar 2018 12:02:56 -0300 Subject: [PATCH 4/4] added count, total, and offset fields in response object --- packages/rocketchat-api/server/v1/channels.js | 10 +++++++++- tests/end-to-end/api/02-channels.js | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-api/server/v1/channels.js b/packages/rocketchat-api/server/v1/channels.js index a75eff73a0c9..07077bd786d1 100644 --- a/packages/rocketchat-api/server/v1/channels.js +++ b/packages/rocketchat-api/server/v1/channels.js @@ -833,8 +833,16 @@ RocketChat.API.v1.addRoute('channels.getAllUserMentionsByChannel', { authRequire } })); + const allMentions = Meteor.runAsUser(this.userId, () => Meteor.call('getUserMentionsByChannel', { + roomId, + options: {} + })); + return RocketChat.API.v1.success({ - mentions + mentions, + count: mentions.length, + offset, + total: allMentions.length }); } }); diff --git a/tests/end-to-end/api/02-channels.js b/tests/end-to-end/api/02-channels.js index 72087dc94055..370e833d4ad5 100644 --- a/tests/end-to-end/api/02-channels.js +++ b/tests/end-to-end/api/02-channels.js @@ -588,6 +588,9 @@ describe('[Channels]', function() { .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); });