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

Conversation

MarcosSpessatto
Copy link
Member

Add rooms.favorite endpoint to favorite and unfavorite room, and tests.
Closes #10317.

RocketChat.API.v1.addRoute('rooms.favorite/:roomId', { authRequired: true }, {
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.

@@ -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.

@engelgabriel engelgabriel temporarily deployed to rocket-chat-pr-10342 April 6, 2018 18:02 Inactive

RocketChat.API.v1.addRoute('rooms.favorite', { authRequired: true }, {
post() {
const { favorite, roomId } = this.bodyParams;
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 make it so we also accept roomName like we do for channels. Like this method but allow it to accept any type of room and for features such as favorite, ensure they are part of that room (have a subscription). Especially since the toggleFavorite method doesn't check to ensure they have a subscription or are part of that room.

//Returns the channel IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property
function findChannelByIdOrName({ params, checkedArchived = true, returnUsernames = false }) {
if ((!params.roomId || !params.roomId.trim()) && (!params.roomName || !params.roomName.trim())) {
throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" or "roomName" is required');
}
const fields = { ...RocketChat.API.v1.defaultFieldsToExclude };
if (returnUsernames) {
delete fields.usernames;
}
let room;
if (params.roomId) {
room = RocketChat.models.Rooms.findOneById(params.roomId, { fields });
} else if (params.roomName) {
room = RocketChat.models.Rooms.findOneByName(params.roomName, { fields });
}
if (!room || room.t !== 'c') {
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "roomName" param provided does not match any channel');
}
if (checkedArchived && room.archived) {
throw new Meteor.Error('error-room-archived', `The channel, ${ room.name }, is archived`);
}
return room;
}

@engelgabriel engelgabriel temporarily deployed to rocket-chat-pr-10342 April 6, 2018 20:33 Inactive
const userSubscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId());
if (!userSubscription) {
throw new Meteor.Error('error-invalid-subscription',
'Invalid subscription',
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a better error would be "you can't favorite" a room you aren't part of... I need to test, but before this change what happens when you try to favorite a room that you aren't part of?

Copy link
Member Author

Choose a reason for hiding this comment

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

The endpoint returns success, but no data is changed. =/

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good change. Let's keep it how it is but change the Invalid subscription to You must be part of a room to favorite it?

Copy link
Contributor

@graywolf336 graywolf336 left a comment

Choose a reason for hiding this comment

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

Change the message returned from the toggleFavorite method then it'll be good to go in my opinion. 👍

@engelgabriel engelgabriel temporarily deployed to rocket-chat-pr-10342 April 10, 2018 13:39 Inactive
@engelgabriel engelgabriel temporarily deployed to rocket-chat-pr-10342 April 10, 2018 13:39 Inactive
@rodrigok rodrigok changed the title [NEW] REST API rooms.favorite endpoint to favorite and unfavorite room [NEW] REST API endpoint rooms.favorite to favorite and unfavorite rooms Apr 17, 2018
@rodrigok rodrigok merged commit 1c2803c into develop Apr 17, 2018
@rodrigok rodrigok deleted the feature/rest-favorite-room branch April 17, 2018 17:45
@rodrigok rodrigok mentioned this pull request Apr 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants