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] REST API endpoint rooms.favorite to favorite and unfavorite rooms #10342

Merged
merged 8 commits into from
Apr 17, 2018
20 changes: 20 additions & 0 deletions packages/rocketchat-api/server/v1/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,23 @@ RocketChat.API.v1.addRoute('rooms.saveNotification', { authRequired: true }, {
return RocketChat.API.v1.success();
}
});

RocketChat.API.v1.addRoute('rooms.favorite/:roomId', { authRequired: true }, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's convert this to be like the other v1 items, where the data is passed into it via the body params. This way it can be either the room name or the id and so we're not changing on people.

post() {
const { favorite } = this.bodyParams;
const { roomId } = this.urlParams;
Copy link
Contributor

@cardoso cardoso Apr 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is "roomId" in the url path and "favorite" in the body? Why not everything in the body? 🤔

Like:
https://rocket.chat/docs/developer-guides/rest-api/channels/invite/
https://rocket.chat/docs/developer-guides/rest-api/channels/leave/
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to follow what was instructed here, but we can put it in the body, no problem

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you'll notice the suggestion you linked to, both the roomId and the favoriting was in the query parameters and not the body or the url path.


if (!roomId) {
return RocketChat.API.v1.failure('The \'roomId\' param is required');
}

if (!this.bodyParams.hasOwnProperty('favorite')) {
return RocketChat.API.v1.failure('The \'favorite\' param is required');
}

Meteor.runAsUser(this.userId, () => Meteor.call('toggleFavorite', roomId, favorite));

return RocketChat.API.v1.success();
}
});

40 changes: 40 additions & 0 deletions tests/end-to-end/api/09-rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,44 @@ describe('[Rooms]', function() {
.end(done);
});
});

describe('/rooms.favorite/:roomId', () => {
let testChannel;
it('create an channel', (done) => {
request.post(api('channels.create'))
.set(credentials)
.send({
name: `channel.test.${ Date.now() }`
})
.end((err, res) => {
testChannel = res.body.channel;
done();
});
});
it('should favorite the room when send favorite: true', (done) => {
request.post(api(`rooms.favorite/${ testChannel._id }`))
.set(credentials)
.send({
favorite: true
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});

it('should unfavorite room when send favorite: false', (done) => {
request.post(api(`rooms.favorite/${ testChannel._id }`))
.set(credentials)
.send({
favorite: false
})
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
})
.end(done);
});
});
});