Skip to content

Commit 4025314

Browse files
chore: migrate rooms.leave endpoint to new OpenAPI pattern with AJV validation (#38957)
Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>
1 parent fd5b687 commit 4025314

3 files changed

Lines changed: 68 additions & 22 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@rocket.chat/meteor': minor
3+
'@rocket.chat/rest-typings': minor
4+
---
5+
6+
Migrated rooms.leave endpoint to new OpenAPI pattern with AJV validation

apps/meteor/app/api/server/v1/rooms.ts

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -390,23 +390,6 @@ API.v1.addRoute(
390390
},
391391
);
392392

393-
API.v1.addRoute(
394-
'rooms.leave',
395-
{ authRequired: true },
396-
{
397-
async post() {
398-
const room = await findRoomByIdOrName({ params: this.bodyParams });
399-
const user = await Users.findOneById(this.userId);
400-
if (!user) {
401-
return API.v1.failure('Invalid user');
402-
}
403-
await leaveRoomMethod(user, room._id);
404-
405-
return API.v1.success();
406-
},
407-
},
408-
);
409-
410393
/*
411394
TO-DO: 8.0.0 should use the ajv validation
412395
which will change this endpoint's
@@ -935,6 +918,14 @@ type RoomsFavorite =
935918
favorite: boolean;
936919
};
937920

921+
type RoomsLeave =
922+
| {
923+
roomId: string;
924+
}
925+
| {
926+
roomName: string;
927+
};
928+
938929
const isRoomGetRolesPropsSchema = {
939930
type: 'object',
940931
properties: {
@@ -967,7 +958,29 @@ const RoomsFavoriteSchema = {
967958
],
968959
};
969960

961+
const isRoomsLeavePropsSchema = {
962+
anyOf: [
963+
{
964+
type: 'object',
965+
properties: {
966+
roomId: { type: 'string' },
967+
},
968+
required: ['roomId'],
969+
additionalProperties: false,
970+
},
971+
{
972+
type: 'object',
973+
properties: {
974+
roomName: { type: 'string' },
975+
},
976+
required: ['roomName'],
977+
additionalProperties: false,
978+
},
979+
],
980+
};
981+
970982
const isRoomsFavoriteProps = ajv.compile<RoomsFavorite>(RoomsFavoriteSchema);
983+
const isRoomsLeaveProps = ajv.compile<RoomsLeave>(isRoomsLeavePropsSchema);
971984

972985
export const roomEndpoints = API.v1
973986
.get(
@@ -1141,6 +1154,38 @@ export const roomEndpoints = API.v1
11411154

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

1157+
return API.v1.success();
1158+
},
1159+
)
1160+
.post(
1161+
'rooms.leave',
1162+
{
1163+
authRequired: true,
1164+
body: isRoomsLeaveProps,
1165+
response: {
1166+
200: ajv.compile<void>({
1167+
type: 'object',
1168+
properties: {
1169+
success: { type: 'boolean', enum: [true] },
1170+
},
1171+
required: ['success'],
1172+
additionalProperties: false,
1173+
}),
1174+
400: validateBadRequestErrorResponse,
1175+
401: validateUnauthorizedErrorResponse,
1176+
},
1177+
},
1178+
async function action() {
1179+
const room = await findRoomByIdOrName({ params: this.bodyParams });
1180+
1181+
const user = await Users.findOneById(this.userId);
1182+
1183+
if (!user) {
1184+
return API.v1.failure('error-invalid-user');
1185+
}
1186+
1187+
await leaveRoomMethod(user, room._id);
1188+
11441189
return API.v1.success();
11451190
},
11461191
);

packages/rest-typings/src/v1/rooms.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ export const isRoomsAutocompleteAdminRoomsPayload = ajv.compile<RoomsAutocomplet
9090

9191
type BaseRoomsProps = { roomId: string } | { roomName: string };
9292
type RoomsInfoProps = BaseRoomsProps;
93-
type RoomsLeaveProps = BaseRoomsProps;
9493

9594
const RoomsInfoSchema = {
9695
oneOf: [
@@ -822,10 +821,6 @@ export type RoomsEndpoints = {
822821
};
823822
};
824823

825-
'/v1/rooms.leave': {
826-
POST: (params: RoomsLeaveProps) => void;
827-
};
828-
829824
'/v1/rooms.getDiscussions': {
830825
GET: (params: RoomsGetDiscussionsProps) => PaginatedResult<{
831826
discussions: IRoom[];

0 commit comments

Comments
 (0)