-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Conversation
RocketChat.API.v1.addRoute('rooms.favorite/:roomId', { authRequired: true }, { | ||
post() { | ||
const { favorite } = this.bodyParams; | ||
const { roomId } = this.urlParams; |
There was a problem hiding this comment.
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/
...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 }, { |
There was a problem hiding this comment.
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.
|
||
RocketChat.API.v1.addRoute('rooms.favorite', { authRequired: true }, { | ||
post() { | ||
const { favorite, roomId } = this.bodyParams; |
There was a problem hiding this comment.
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.
Rocket.Chat/packages/rocketchat-api/server/v1/channels.js
Lines 3 to 30 in a9aea36
//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; | |
} |
server/methods/toogleFavorite.js
Outdated
const userSubscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(rid, Meteor.userId()); | ||
if (!userSubscription) { | ||
throw new Meteor.Error('error-invalid-subscription', | ||
'Invalid subscription', |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. =/
There was a problem hiding this comment.
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
?
There was a problem hiding this 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. 👍
rooms.favorite
to favorite and unfavorite rooms
Add rooms.favorite endpoint to favorite and unfavorite room, and tests.
Closes #10317.