Skip to content

Commit

Permalink
Fix: chat.react api not accepting previous emojis (#10290)
Browse files Browse the repository at this point in the history
* Fix the chat.react api being invalid

* Remove duplicated code

* Add tests for chat.react endpoint
  • Loading branch information
graywolf336 authored and rodrigok committed Apr 3, 2018
1 parent 7145112 commit 0afab32
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/rocketchat-api/server/v1/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ RocketChat.API.v1.addRoute('chat.react', { authRequired: true }, {
throw new Meteor.Error('error-message-not-found', 'The provided "messageId" does not match any existing message.');
}

const emoji = this.bodyParams.emoji;
const emoji = this.bodyParams.emoji || this.bodyParams.reaction;

if (!emoji) {
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));

Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-reactions/setReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Meteor.methods({
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'setReaction' });
}

reaction = `:${ reaction.replace(/:/g, '') }:`;

if (!RocketChat.emoji.list[reaction] && RocketChat.models.EmojiCustom.findByNameOrAlias(reaction).count() === 0) {
throw new Meteor.Error('error-not-allowed', 'Invalid emoji provided.', { method: 'setReaction' });
}
Expand All @@ -37,8 +39,6 @@ Meteor.methods({
return false;
}

reaction = `:${ reaction.replace(/:/g, '') }:`;

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);

Expand Down
30 changes: 30 additions & 0 deletions tests/end-to-end/api/05-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,36 @@ describe('[Chat]', function() {
})
.end(done);
});

it('should return statusCode: 200 when the emoji is valid and has no colons', (done) => {
request.post(api('chat.react'))
.set(credentials)
.send({
emoji: 'bee',
messageId: message._id
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});

it('should return statusCode: 200 for reaction property when the emoji is valid', (done) => {
request.post(api('chat.react'))
.set(credentials)
.send({
reaction: 'ant',
messageId: message._id
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
});

describe('[/chat.getMessageReadReceipts]', () => {
Expand Down

0 comments on commit 0afab32

Please sign in to comment.