Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Endpoint to retrieve message read receipts #9907

Merged
merged 2 commits into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
25 changes: 23 additions & 2 deletions packages/rocketchat-api/server/v1/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }".`);
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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
});
}
}
});
42 changes: 41 additions & 1 deletion tests/end-to-end/api/05-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
});
});
});
});