diff --git a/.changeset/migrate-chat-reportMessage-to-openapi.md b/.changeset/migrate-chat-reportMessage-to-openapi.md new file mode 100644 index 0000000000000..0fa9734b7e1d5 --- /dev/null +++ b/.changeset/migrate-chat-reportMessage-to-openapi.md @@ -0,0 +1,6 @@ +--- +'@rocket.chat/meteor': minor +'@rocket.chat/rest-typings': minor +--- + +Migrated chat.reportMessage endpoint to new OpenAPI pattern with AJV validation \ No newline at end of file diff --git a/apps/meteor/app/api/server/v1/chat.ts b/apps/meteor/app/api/server/v1/chat.ts index a00d57e46ae72..1aae3fe767a29 100644 --- a/apps/meteor/app/api/server/v1/chat.ts +++ b/apps/meteor/app/api/server/v1/chat.ts @@ -4,7 +4,6 @@ import { MessageTypes } from '@rocket.chat/message-types'; import { Messages, Users, Rooms, Subscriptions } from '@rocket.chat/models'; import { ajv, - isChatReportMessageProps, isChatGetURLPreviewProps, isChatUpdateProps, isChatGetThreadsListProps, @@ -275,6 +274,23 @@ const isChatPinMessageProps = ajv.compile(ChatPinMessageSchema); const isChatUnpinMessageProps = ajv.compile(ChatUnpinMessageSchema); +type ChatReportMessage = { + messageId: IMessage['_id']; + description: string; +}; + +const ChatReportMessageSchema = { + type: 'object', + properties: { + messageId: { type: 'string', minLength: 1 }, + description: { type: 'string', minLength: 1 }, + }, + required: ['messageId', 'description'], + additionalProperties: false, +}; + +const isChatReportMessageLocalProps = ajv.compile(ChatReportMessageSchema); + const chatEndpoints = API.v1 .post( 'chat.pinMessage', @@ -419,6 +435,30 @@ const chatEndpoints = API.v1 }); }, ) + .post( + 'chat.reportMessage', + { + authRequired: true, + body: isChatReportMessageLocalProps, + response: { + 200: ajv.compile({ + type: 'object', + properties: { + success: { type: 'boolean', enum: [true] }, + }, + required: ['success'], + additionalProperties: false, + }), + 400: validateBadRequestErrorResponse, + 401: validateUnauthorizedErrorResponse, + }, + }, + async function action() { + const { messageId, description } = this.bodyParams; + await reportMessage(messageId, description, this.userId); + return API.v1.success(); + }, + ) .post( 'chat.starMessage', { @@ -555,7 +595,6 @@ const chatEndpoints = API.v1 } await unfollowMessage(this.user, { mid }); - return API.v1.success(); }, ); @@ -677,27 +716,6 @@ API.v1.addRoute( }, ); -API.v1.addRoute( - 'chat.reportMessage', - { authRequired: true, validateParams: isChatReportMessageProps }, - { - async post() { - const { messageId, description } = this.bodyParams; - if (!messageId) { - return API.v1.failure('The required "messageId" param is missing.'); - } - - if (!description) { - return API.v1.failure('The required "description" param is missing.'); - } - - await reportMessage(messageId, description, this.userId); - - return API.v1.success(); - }, - }, -); - API.v1.addRoute( 'chat.ignoreUser', { authRequired: true, validateParams: isChatIgnoreUserProps }, diff --git a/packages/rest-typings/src/v1/chat.ts b/packages/rest-typings/src/v1/chat.ts index b52bba2d61ed5..b6072426a815e 100644 --- a/packages/rest-typings/src/v1/chat.ts +++ b/packages/rest-typings/src/v1/chat.ts @@ -125,27 +125,6 @@ const ChatGetDiscussionsSchema = { export const isChatGetDiscussionsProps = ajv.compile(ChatGetDiscussionsSchema); -type ChatReportMessage = { - messageId: IMessage['_id']; - description: string; -}; - -const ChatReportMessageSchema = { - type: 'object', - properties: { - messageId: { - type: 'string', - }, - description: { - type: 'string', - }, - }, - required: ['messageId', 'description'], - additionalProperties: false, -}; - -export const isChatReportMessageProps = ajv.compile(ChatReportMessageSchema); - type ChatGetThreadsList = PaginatedRequest<{ rid: IRoom['_id']; type?: 'unread' | 'following'; @@ -897,9 +876,6 @@ export type ChatEndpoints = { message: IMessage; }; }; - '/v1/chat.reportMessage': { - POST: (params: ChatReportMessage) => void; - }; '/v1/chat.getDiscussions': { GET: (params: ChatGetDiscussions) => { messages: IMessage[];