diff --git a/imports/message-read-receipt/server/api/methods/getReadReceipts.js b/imports/message-read-receipt/server/api/methods/getReadReceipts.js index 0e6684fa129c..da9c7ecbe995 100644 --- a/imports/message-read-receipt/server/api/methods/getReadReceipts.js +++ b/imports/message-read-receipt/server/api/methods/getReadReceipts.js @@ -8,8 +8,16 @@ Meteor.methods({ throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getReadReceipts' }); } + if (!messageId) { + throw new Meteor.Error('error-invalid-message', 'The required \'messageId\' param is missing.', { method: 'getReadReceipts' }); + } + const message = RocketChat.models.Messages.findOneById(messageId); + if (!message) { + throw new Meteor.Error('error-invalid-message', 'Invalid message', { method: 'getReadReceipts' }); + } + const room = Meteor.call('canAccessRoom', message.rid, Meteor.userId()); if (!room) { throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'getReadReceipts' }); diff --git a/packages/rocketchat-api/server/v1/chat.js b/packages/rocketchat-api/server/v1/chat.js index 6a866923121c..69b2a2b9170e 100644 --- a/packages/rocketchat-api/server/v1/chat.js +++ b/packages/rocketchat-api/server/v1/chat.js @@ -7,7 +7,7 @@ RocketChat.API.v1.addRoute('chat.delete', { authRequired: true }, { asUser: Match.Maybe(Boolean) })); - const msg = RocketChat.models.Messages.findOneById(this.bodyParams.msgId, { fields: { u: 1, rid: 1 }}); + const msg = RocketChat.models.Messages.findOneById(this.bodyParams.msgId, { fields: { u: 1, rid: 1 } }); if (!msg) { return RocketChat.API.v1.failure(`No message found with the id of "${ this.bodyParams.msgId }".`); @@ -247,7 +247,6 @@ RocketChat.API.v1.addRoute('chat.update', { authRequired: true }, { //Permission checks are already done in the updateMessage method, so no need to duplicate them Meteor.runAsUser(this.userId, () => { Meteor.call('updateMessage', { _id: msg._id, msg: this.bodyParams.text, rid: msg.rid }); - }); return RocketChat.API.v1.success({ @@ -275,3 +274,25 @@ RocketChat.API.v1.addRoute('chat.react', { authRequired: true }, { return RocketChat.API.v1.success(); } }); + +RocketChat.API.v1.addRoute('chat.getMessageReadReceipts', { authRequired: true }, { + get() { + const { messageId } = this.queryParams; + if (!messageId) { + return RocketChat.API.v1.failure({ + error: 'The required \'messageId\' param is missing.' + }); + } + + try { + const messageReadReceipts = Meteor.runAsUser(this.userId, () => Meteor.call('getReadReceipts', { messageId })); + return RocketChat.API.v1.success({ + receipts: messageReadReceipts + }); + } catch (error) { + return RocketChat.API.v1.failure({ + error: error.message + }); + } + } +}); diff --git a/tests/end-to-end/api/05-chat.js b/tests/end-to-end/api/05-chat.js index c37cc7a5776b..2fc3c944fd2d 100644 --- a/tests/end-to-end/api/05-chat.js +++ b/tests/end-to-end/api/05-chat.js @@ -2,7 +2,16 @@ /* globals expect */ /* eslint no-unused-vars: 0 */ -import { getCredentials, api, login, request, credentials, message, log, apiPrivateChannelName } from '../../data/api-data.js'; +import { + getCredentials, + api, + login, + request, + credentials, + message, + log, + apiPrivateChannelName +} from '../../data/api-data.js'; import { adminEmail, password } from '../../data/user.js'; import supertest from 'supertest'; @@ -185,4 +194,35 @@ describe('[Chat]', function() { .end(done); }); }); + + describe('[/chat.getMessageReadReceipts]', () => { + describe('when execute successfully', () => { + it('should return the statusCode 200 and \'receipts\' property and should be equal an array', (done) => { + request.get(api(`chat.getMessageReadReceipts?messageId=${ message._id }`)) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('receipts').and.to.be.an('array'); + expect(res.body).to.have.property('success', true); + }) + .end(done); + }); + }); + + describe('when an error occurs', () => { + it('should return statusCode 400 and an error', (done) => { + request.get(api('chat.getMessageReadReceipts')) + .set(credentials) + .expect('Content-Type', 'application/json') + .expect(400) + .expect((res) => { + expect(res.body).not.have.property('receipts'); + expect(res.body).to.have.property('success', false); + expect(res.body).to.have.property('error'); + }) + .end(done); + }); + }); + }); });