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

[FIX] Add parameter to REST chat.react endpoint, to make it work like a setter #10447

Merged
merged 6 commits into from
Jun 20, 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
7 changes: 2 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/rocketchat-api/server/v1/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ RocketChat.API.v1.addRoute('chat.react', { authRequired: true }, {
throw new Meteor.Error('error-emoji-param-not-provided', 'The required "emoji" param is missing.');
}

Meteor.runAsUser(this.userId, () => Meteor.call('setReaction', emoji, msg._id));
Meteor.runAsUser(this.userId, () => Meteor.call('setReaction', emoji, msg._id, this.bodyParams.shouldReact));

return RocketChat.API.v1.success();
}
Expand Down
22 changes: 15 additions & 7 deletions packages/rocketchat-reactions/setReaction.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
/* globals msgStream */
import _ from 'underscore';

const removeUserReaction = (message, reaction, username) => {
message.reactions[reaction].usernames.splice(message.reactions[reaction].usernames.indexOf(username), 1);
if (message.reactions[reaction].usernames.length === 0) {
delete message.reactions[reaction];
}
return message;
};

Meteor.methods({
setReaction(reaction, messageId) {
setReaction(reaction, messageId, shouldReact) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setReaction' });
}
Expand Down Expand Up @@ -39,12 +47,12 @@ Meteor.methods({
return false;
}

if (message.reactions && message.reactions[reaction] && message.reactions[reaction].usernames.indexOf(user.username) !== -1) {
message.reactions[reaction].usernames.splice(message.reactions[reaction].usernames.indexOf(user.username), 1);

if (message.reactions[reaction].usernames.length === 0) {
delete message.reactions[reaction];
}
const userAlreadyReacted = Boolean(message.reactions) && message.reactions[reaction] && message.reactions[reaction].usernames.indexOf(user.username) !== -1;
if (userAlreadyReacted === shouldReact) {
return;
}
if (userAlreadyReacted) {
removeUserReaction(message, reaction, user.username);

if (_.isEmpty(message.reactions)) {
delete message.reactions;
Expand Down
65 changes: 61 additions & 4 deletions tests/end-to-end/api/05-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,22 @@ describe('[Chat]', function() {
.end(done);
});
});

describe('/chat.react', () => {
describe('[/chat.react]', () => {
it('should return statusCode: 200 and success when try unreact a message that\'s no reacted yet', (done) => {
request.post(api('chat.react'))
.set(credentials)
.send({
emoji: ':squid:',
messageId: message._id,
shouldReact: false
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
it('should react a message successfully', (done) => {
request.post(api('chat.react'))
.set(credentials)
Expand Down Expand Up @@ -394,7 +408,51 @@ describe('[Chat]', function() {
})
.end(done);
});

it('should return statusCode: 200 and success when try react a message that\'s already reacted', (done) => {
request.post(api('chat.react'))
.set(credentials)
.send({
emoji: ':squid:',
messageId: message._id,
shouldReact: true
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
it('should return statusCode: 200 when unreact a message with flag, shouldReact: false', (done) => {
request.post(api('chat.react'))
.set(credentials)
.send({
emoji: ':squid:',
messageId: message._id,
shouldReact: false
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
it('should return statusCode: 200 when react a message with flag, shouldReact: true', (done) => {
request.post(api('chat.react'))
.set(credentials)
.send({
emoji: ':squid:',
messageId: message._id,
shouldReact: true
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
it('should return statusCode: 200 when the emoji is valid and has no colons', (done) => {
request.post(api('chat.react'))
.set(credentials)
Expand All @@ -409,7 +467,6 @@ describe('[Chat]', function() {
})
.end(done);
});

it('should return statusCode: 200 for reaction property when the emoji is valid', (done) => {
request.post(api('chat.react'))
.set(credentials)
Expand Down