@@ -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/*
411394TO-DO: 8.0.0 should use the ajv validation
412395which 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+
938929const 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+
970982const isRoomsFavoriteProps = ajv . compile < RoomsFavorite > ( RoomsFavoriteSchema ) ;
983+ const isRoomsLeaveProps = ajv . compile < RoomsLeave > ( isRoomsLeavePropsSchema ) ;
971984
972985export 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 ) ;
0 commit comments