Skip to content

Commit 03b6340

Browse files
committed
chore: migrate rooms.muteUser and rooms.unmuteUser endpoints to the new pattern with AJV validation
1 parent 46a1774 commit 03b6340

2 files changed

Lines changed: 98 additions & 82 deletions

File tree

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

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
ajv,
88
isGETRoomsNameExists,
99
isRoomsImagesProps,
10-
isRoomsMuteUnmuteUserProps,
1110
isRoomsExportProps,
1211
isRoomsIsMemberProps,
1312
isRoomsCleanHistoryProps,
@@ -875,42 +874,6 @@ API.v1.addRoute(
875874
},
876875
);
877876

878-
API.v1.addRoute(
879-
'rooms.muteUser',
880-
{ authRequired: true, validateParams: isRoomsMuteUnmuteUserProps },
881-
{
882-
async post() {
883-
const user = await getUserFromParams(this.bodyParams);
884-
885-
if (!user.username) {
886-
return API.v1.failure('Invalid user');
887-
}
888-
889-
await muteUserInRoom(this.userId, { rid: this.bodyParams.roomId, username: user.username });
890-
891-
return API.v1.success();
892-
},
893-
},
894-
);
895-
896-
API.v1.addRoute(
897-
'rooms.unmuteUser',
898-
{ authRequired: true, validateParams: isRoomsMuteUnmuteUserProps },
899-
{
900-
async post() {
901-
const user = await getUserFromParams(this.bodyParams);
902-
903-
if (!user.username) {
904-
return API.v1.failure('Invalid user');
905-
}
906-
907-
await unmuteUserInRoom(this.userId, { rid: this.bodyParams.roomId, username: user.username });
908-
909-
return API.v1.success();
910-
},
911-
},
912-
);
913-
914877
API.v1.addRoute(
915878
'rooms.open',
916879
{ authRequired: true, validateParams: isRoomsOpenProps },
@@ -1027,6 +990,44 @@ const isRoomsLeavePropsSchema = {
1027990
const isRoomsFavoriteProps = ajv.compile<RoomsFavorite>(RoomsFavoriteSchema);
1028991
const isRoomsLeaveProps = ajv.compile<RoomsLeave>(isRoomsLeavePropsSchema);
1029992

993+
type RoomsMuteUnmuteUser = { userId: string; roomId: string } | { username: string; roomId: string };
994+
995+
const RoomsMuteUnmuteUserSchema = {
996+
type: 'object',
997+
oneOf: [
998+
{
999+
properties: {
1000+
userId: {
1001+
type: 'string',
1002+
minLength: 1,
1003+
},
1004+
roomId: {
1005+
type: 'string',
1006+
minLength: 1,
1007+
},
1008+
},
1009+
required: ['userId', 'roomId'],
1010+
additionalProperties: false,
1011+
},
1012+
{
1013+
properties: {
1014+
username: {
1015+
type: 'string',
1016+
minLength: 1,
1017+
},
1018+
roomId: {
1019+
type: 'string',
1020+
minLength: 1,
1021+
},
1022+
},
1023+
required: ['username', 'roomId'],
1024+
additionalProperties: false,
1025+
},
1026+
],
1027+
};
1028+
1029+
const isRoomsMuteUnmuteUserProps = ajv.compile<RoomsMuteUnmuteUser>(RoomsMuteUnmuteUserSchema);
1030+
10301031
export const roomEndpoints = API.v1
10311032
.get(
10321033
'rooms.roles',
@@ -1233,6 +1234,66 @@ export const roomEndpoints = API.v1
12331234

12341235
return API.v1.success();
12351236
},
1237+
)
1238+
.post(
1239+
'rooms.muteUser',
1240+
{
1241+
authRequired: true,
1242+
body: isRoomsMuteUnmuteUserProps,
1243+
response: {
1244+
200: ajv.compile<{ success: true }>({
1245+
type: 'object',
1246+
properties: {
1247+
success: { type: 'boolean', enum: [true] },
1248+
},
1249+
required: ['success'],
1250+
additionalProperties: false,
1251+
}),
1252+
400: validateBadRequestErrorResponse,
1253+
401: validateUnauthorizedErrorResponse,
1254+
},
1255+
},
1256+
async function action() {
1257+
const user = await getUserFromParams(this.bodyParams);
1258+
1259+
if (!user.username) {
1260+
return API.v1.failure('Invalid user');
1261+
}
1262+
1263+
await muteUserInRoom(this.userId, { rid: this.bodyParams.roomId, username: user.username });
1264+
1265+
return API.v1.success({ success: true });
1266+
},
1267+
)
1268+
.post(
1269+
'rooms.unmuteUser',
1270+
{
1271+
authRequired: true,
1272+
body: isRoomsMuteUnmuteUserProps,
1273+
response: {
1274+
200: ajv.compile<{ success: true }>({
1275+
type: 'object',
1276+
properties: {
1277+
success: { type: 'boolean', enum: [true] },
1278+
},
1279+
required: ['success'],
1280+
additionalProperties: false,
1281+
}),
1282+
400: validateBadRequestErrorResponse,
1283+
401: validateUnauthorizedErrorResponse,
1284+
},
1285+
},
1286+
async function action() {
1287+
const user = await getUserFromParams(this.bodyParams);
1288+
1289+
if (!user.username) {
1290+
return API.v1.failure('Invalid user');
1291+
}
1292+
1293+
await unmuteUserInRoom(this.userId, { rid: this.bodyParams.roomId, username: user.username });
1294+
1295+
return API.v1.success({ success: true });
1296+
},
12361297
);
12371298

12381299
type RoomEndpoints = ExtractRoutesFromAPI<typeof roomEndpoints> &

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -485,43 +485,6 @@ export type Notifications = {
485485

486486
type RoomsGetDiscussionsProps = PaginatedRequest<BaseRoomsProps>;
487487

488-
type RoomsMuteUnmuteUser = { userId: string; roomId: string } | { username: string; roomId: string };
489-
490-
const RoomsMuteUnmuteUserSchema = {
491-
type: 'object',
492-
oneOf: [
493-
{
494-
properties: {
495-
userId: {
496-
type: 'string',
497-
minLength: 1,
498-
},
499-
roomId: {
500-
type: 'string',
501-
minLength: 1,
502-
},
503-
},
504-
required: ['userId', 'roomId'],
505-
additionalProperties: false,
506-
},
507-
{
508-
properties: {
509-
username: {
510-
type: 'string',
511-
minLength: 1,
512-
},
513-
roomId: {
514-
type: 'string',
515-
minLength: 1,
516-
},
517-
},
518-
required: ['username', 'roomId'],
519-
additionalProperties: false,
520-
},
521-
],
522-
};
523-
524-
export const isRoomsMuteUnmuteUserProps = ajv.compile<RoomsMuteUnmuteUser>(RoomsMuteUnmuteUserSchema);
525488
export type RoomsImagesProps = {
526489
roomId: string;
527490
startingFromId?: string;
@@ -823,14 +786,6 @@ export type RoomsEndpoints = {
823786
GET: (params: RoomsIsMemberProps) => { isMember: boolean };
824787
};
825788

826-
'/v1/rooms.muteUser': {
827-
POST: (params: RoomsMuteUnmuteUser) => void;
828-
};
829-
830-
'/v1/rooms.unmuteUser': {
831-
POST: (params: RoomsMuteUnmuteUser) => void;
832-
};
833-
834789
'/v1/rooms.images': {
835790
GET: (params: RoomsImagesProps) => PaginatedResult<{
836791
files: IUpload[];

0 commit comments

Comments
 (0)