Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/migrate-rooms-favorite-endpoint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Migrated rooms.favorite endpoint to new OpenAPI pattern with AJV validation
52 changes: 39 additions & 13 deletions apps/meteor/app/api/server/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,26 +311,52 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
export const roomsFavoriteEndpoint = API.v1.post(
'rooms.favorite',
{ authRequired: true },
{
async post() {
const { favorite } = this.bodyParams;

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

const room = await findRoomByIdOrName({ params: this.bodyParams });
authRequired: true,
body: ajv.compile<
{
favorite: boolean;
} & ({ roomId: string } | { roomName: string })
>({
type: 'object',
properties: {
roomId: { type: 'string', minLength: 1 },
roomName: { type: 'string', minLength: 1 },
favorite: { type: 'boolean' },
},
oneOf: [{ required: ['roomId', 'favorite'] }, { required: ['roomName', 'favorite'] }],
additionalProperties: false,
}),
response: {
200: ajv.compile({
type: 'object',
properties: { success: { type: 'boolean', enum: [true] } },
required: ['success'],
additionalProperties: false,
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { favorite } = this.bodyParams;
const room = await findRoomByIdOrName({ params: this.bodyParams });

await toggleFavoriteMethod(this.userId, room._id, favorite);
await toggleFavoriteMethod(this.userId, room._id, favorite);

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

type RoomsFavoriteEndpoint = ExtractRoutesFromAPI<typeof roomsFavoriteEndpoint>;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends RoomsFavoriteEndpoint {}
}

API.v1.addRoute(
'rooms.cleanHistory',
{ authRequired: true, validateParams: isRoomsCleanHistoryProps },
Expand Down
14 changes: 0 additions & 14 deletions packages/rest-typings/src/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,20 +805,6 @@ export type RoomsEndpoints = {
POST: (params: { roomId: string; notifications: Notifications }) => void;
};

'/v1/rooms.favorite': {
POST: (
params:
| {
roomId: string;
favorite: boolean;
}
| {
roomName: string;
favorite: boolean;
},
) => void;
};

'/v1/rooms.nameExists': {
GET: (params: { roomName: string }) => {
exists: boolean;
Expand Down