Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changeset/groups.removeModerator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": minor
"@rocket.chat/rest-typings": minor
---

Add OpenAPI support for the Rocket.Chat groups.removeModerator API endpoints by migrating to a modern chained route definition syntax and utilizing shared AJV schemas for validation
72 changes: 57 additions & 15 deletions apps/meteor/app/api/server/v1/groups.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { Team, isMeteorError } from '@rocket.chat/core-services';
import type { IIntegration, IUser, IRoom, RoomType, UserStatus } from '@rocket.chat/core-typings';
import { Integrations, Messages, Rooms, Subscriptions, Uploads, Users } from '@rocket.chat/models';
import { isGroupsOnlineProps, isGroupsMessagesProps, isGroupsFilesProps } from '@rocket.chat/rest-typings';
import {
isGroupsOnlineProps,
isGroupsMessagesProps,
isGroupsFilesProps,
ajv,
validateBadRequestErrorResponse,
validateUnauthorizedErrorResponse,
withGroupBaseProperties,
GroupsBaseProps
} from '@rocket.chat/rest-typings';
import { isTruthy } from '@rocket.chat/tools';
import { check, Match } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
Expand Down Expand Up @@ -31,6 +40,7 @@ import { executeGetRoomRoles } from '../../../lib/server/methods/getRoomRoles';
import { leaveRoomMethod } from '../../../lib/server/methods/leaveRoom';
import { executeUnarchiveRoom } from '../../../lib/server/methods/unarchiveRoom';
import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser';
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';
import { addUserToFileObj } from '../helpers/addUserToFileObj';
import { composeRoomWithLastMessage } from '../helpers/composeRoomWithLastMessage';
Expand Down Expand Up @@ -67,7 +77,7 @@ async function getRoomFromParams(params: { roomId?: string } | { roomName?: stri
}
})();

if (!room || room.t !== 'p') {
if (room?.t !== 'p') {
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "roomName" param provided does not match any group');
}

Expand Down Expand Up @@ -273,7 +283,7 @@ API.v1.addRoute(
room = await Rooms.findOneByName(params.roomName || '');
}

if (!room || room.t !== 'p') {
if (room?.t !== 'p') {
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "roomName" param provided does not match any group');
}

Expand Down Expand Up @@ -791,7 +801,7 @@ API.v1.addRoute(
rid: findResult.rid,
...parseIds(mentionIds, 'mentions._id'),
...parseIds(starredIds, 'starred._id'),
...(pinned && pinned.toLowerCase() === 'true' ? { pinned: true } : {}),
...(pinned?.toLowerCase() === 'true' ? { pinned: true } : {}),
_hidden: { $ne: true },
};

Expand Down Expand Up @@ -891,22 +901,47 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
type WithUserId = GroupsBaseProps & { userId: string };
const withUserIdSchema = withGroupBaseProperties(
{
userId: {
type: 'string',
},
},
['userId'],
);

const withUserIdProps = ajv.compile<WithUserId>(withUserIdSchema);

const groupsEndpoints = API.v1.post(
'groups.removeModerator',
{ authRequired: true },
{
async post() {
const findResult = await findPrivateGroupByIdOrName({
params: this.bodyParams,
userId: this.userId,
});
authRequired: true,
body: withUserIdProps,
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
200: ajv.compile<void>({
type: 'object',
properties: {
success: { type: 'boolean', enum: [true] },
},
required: ['success'],
additionalProperties: false,
}),
},
},
async function action() {
const findResult = await findPrivateGroupByIdOrName({
params: this.bodyParams,
userId: this.userId,
});

const user = await getUserFromParams(this.bodyParams);
const user = await getUserFromParams(this.bodyParams);

await removeRoomModerator(this.userId, findResult.rid, user._id);
await removeRoomModerator(this.userId, findResult.rid, user._id);

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

Expand Down Expand Up @@ -1300,3 +1335,10 @@ API.v1.addRoute(
},
},
);

type GroupEndpoints = ExtractRoutesFromAPI<typeof groupsEndpoints>;

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

This file was deleted.

5 changes: 1 addition & 4 deletions packages/rest-typings/src/v1/groups/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type { GroupsModeratorsProps } from './GroupsModeratorsProps';
import type { GroupsOnlineProps } from './GroupsOnlineProps';
import type { GroupsOpenProps } from './GroupsOpenProps';
import type { GroupsRemoveLeaderProps } from './GroupsRemoveLeaderProps';
import type { GroupsRemoveModeratorProps } from './GroupsRemoveModeratorProps';
import type { GroupsRemoveOwnerProps } from './GroupsRemoveOwnerProps';
import type { GroupsRenameProps } from './GroupsRenameProps';
import type { GroupsRolesProps } from './GroupsRolesProps';
Expand Down Expand Up @@ -107,9 +106,7 @@ export type GroupsEndpoints = {
'/v1/groups.addModerator': {
POST: (params: GroupsAddModeratorProps) => void;
};
'/v1/groups.removeModerator': {
POST: (params: GroupsRemoveModeratorProps) => void;
};

'/v1/groups.addOwner': {
POST: (params: GroupsAddOwnerProps) => void;
};
Expand Down
1 change: 0 additions & 1 deletion packages/rest-typings/src/v1/groups/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export * from './GroupsOnlineProps';
export * from './GroupsOpenProps';
export * from './GroupsRenameProps';
export * from './GroupsRemoveLeaderProps';
export * from './GroupsRemoveModeratorProps';
export * from './GroupsRemoveOwnerProps';
export * from './GroupsSetAnnouncementProps';
export * from './GroupsSetCustomFieldsProps';
Expand Down