From db3ee514da6755524a12d703c8bd389347231b10 Mon Sep 17 00:00:00 2001 From: Joaquin GT Date: Wed, 24 Jan 2018 12:44:54 +0100 Subject: [PATCH 1/3] adding chat.react API EndPoint --- packages/rocketchat-api/server/v1/chat.js | 20 +++++++++++++++++++ .../client/methods/setReaction.js | 4 ++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-api/server/v1/chat.js b/packages/rocketchat-api/server/v1/chat.js index 15f1500d0b2a..bb3aeb50c090 100644 --- a/packages/rocketchat-api/server/v1/chat.js +++ b/packages/rocketchat-api/server/v1/chat.js @@ -255,3 +255,23 @@ RocketChat.API.v1.addRoute('chat.update', { authRequired: true }, { }); } }); + +RocketChat.API.v1.addRoute('chat.react', { authRequired: true }, { + post() { + if (!this.bodyParams.messageId || !this.bodyParams.messageId.trim()) { + throw new Meteor.Error('error-messageid-param-not-provided', 'The required "messageId" param is missing.'); + } + + const msg = RocketChat.models.Messages.findOneById(this.bodyParams.messageId); + + if (!msg) { + throw new Meteor.Error('error-message-not-found', 'The provided "messageId" does not match any existing message.'); + } + + const emoji = this.bodyParams.emoji; + + Meteor.runAsUser(this.userId, () => Meteor.call('setReaction', emoji, msg._id, this.userId)); + + return RocketChat.API.v1.success(); + } +}); diff --git a/packages/rocketchat-reactions/client/methods/setReaction.js b/packages/rocketchat-reactions/client/methods/setReaction.js index 59d05ae71dc7..75caf00e7f99 100644 --- a/packages/rocketchat-reactions/client/methods/setReaction.js +++ b/packages/rocketchat-reactions/client/methods/setReaction.js @@ -1,12 +1,12 @@ import _ from 'underscore'; Meteor.methods({ - setReaction(reaction, messageId) { + setReaction(reaction, messageId, usr) { if (!Meteor.userId()) { throw new Meteor.Error(203, 'User_logged_out'); } - const user = Meteor.user(); + const user = usr ? usr : Meteor.user(); const message = RocketChat.models.Messages.findOne({ _id: messageId }); const room = RocketChat.models.Rooms.findOne({ _id: message.rid }); From 6bb1734959639217c2a64c467624bb412cbcc486 Mon Sep 17 00:00:00 2001 From: Marcos Vinicius Spessatto Defendi Date: Thu, 15 Feb 2018 18:16:09 -0200 Subject: [PATCH 2/3] removed uncessary parameter --- packages/rocketchat-api/server/v1/chat.js | 2 +- packages/rocketchat-reactions/client/methods/setReaction.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/rocketchat-api/server/v1/chat.js b/packages/rocketchat-api/server/v1/chat.js index bb3aeb50c090..6a866923121c 100644 --- a/packages/rocketchat-api/server/v1/chat.js +++ b/packages/rocketchat-api/server/v1/chat.js @@ -270,7 +270,7 @@ RocketChat.API.v1.addRoute('chat.react', { authRequired: true }, { const emoji = this.bodyParams.emoji; - Meteor.runAsUser(this.userId, () => Meteor.call('setReaction', emoji, msg._id, this.userId)); + Meteor.runAsUser(this.userId, () => Meteor.call('setReaction', emoji, msg._id)); return RocketChat.API.v1.success(); } diff --git a/packages/rocketchat-reactions/client/methods/setReaction.js b/packages/rocketchat-reactions/client/methods/setReaction.js index 75caf00e7f99..59d05ae71dc7 100644 --- a/packages/rocketchat-reactions/client/methods/setReaction.js +++ b/packages/rocketchat-reactions/client/methods/setReaction.js @@ -1,12 +1,12 @@ import _ from 'underscore'; Meteor.methods({ - setReaction(reaction, messageId, usr) { + setReaction(reaction, messageId) { if (!Meteor.userId()) { throw new Meteor.Error(203, 'User_logged_out'); } - const user = usr ? usr : Meteor.user(); + const user = Meteor.user(); const message = RocketChat.models.Messages.findOne({ _id: messageId }); const room = RocketChat.models.Rooms.findOne({ _id: message.rid }); From 6a45ef801ece36cd83332faf122059c984af4ee3 Mon Sep 17 00:00:00 2001 From: Marcos Defendi Date: Thu, 15 Feb 2018 20:30:36 -0200 Subject: [PATCH 3/3] adding end to end tests for chat.react endpoint --- tests/end-to-end/api/05-chat.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/end-to-end/api/05-chat.js b/tests/end-to-end/api/05-chat.js index 5e41eca0b8ae..c6dca60d2c28 100644 --- a/tests/end-to-end/api/05-chat.js +++ b/tests/end-to-end/api/05-chat.js @@ -168,4 +168,19 @@ describe('[Chat]', function() { }) .end(done); }); + + it('/chat.react', (done) => { + request.post(api('chat.react')) + .set(credentials) + .send({ + emoji: 'smile', + messageId: message._id + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); });