diff --git a/.changeset/migrate-chat-getDiscussions-to-openapi.md b/.changeset/migrate-chat-getDiscussions-to-openapi.md new file mode 100644 index 0000000000000..2d63aec428b78 --- /dev/null +++ b/.changeset/migrate-chat-getDiscussions-to-openapi.md @@ -0,0 +1,6 @@ +--- +'@rocket.chat/meteor': minor +'@rocket.chat/rest-typings': minor +--- + +Migrated chat.getDiscussions endpoint to new OpenAPI pattern with AJV validation diff --git a/apps/meteor/app/api/server/v1/chat.ts b/apps/meteor/app/api/server/v1/chat.ts index a00d57e46ae72..d33dffcde99b2 100644 --- a/apps/meteor/app/api/server/v1/chat.ts +++ b/apps/meteor/app/api/server/v1/chat.ts @@ -1,5 +1,5 @@ import { Message } from '@rocket.chat/core-services'; -import type { IMessage, IThreadMainMessage } from '@rocket.chat/core-typings'; +import type { IMessage, IThreadMainMessage, IRoom } from '@rocket.chat/core-typings'; import { MessageTypes } from '@rocket.chat/message-types'; import { Messages, Users, Rooms, Subscriptions } from '@rocket.chat/models'; import { @@ -23,9 +23,9 @@ import { isChatGetThreadMessagesProps, isChatSyncThreadMessagesProps, isChatGetStarredMessagesProps, - isChatGetDiscussionsProps, validateBadRequestErrorResponse, validateUnauthorizedErrorResponse, + type PaginatedRequest, } from '@rocket.chat/rest-typings'; import { escapeRegExp } from '@rocket.chat/string-helpers'; import { Meteor } from 'meteor/meteor'; @@ -275,6 +275,41 @@ const isChatPinMessageProps = ajv.compile(ChatPinMessageSchema); const isChatUnpinMessageProps = ajv.compile(ChatUnpinMessageSchema); +type ChatGetDiscussions = PaginatedRequest<{ + roomId: IRoom['_id']; + text?: string; +}>; + +const ChatGetDiscussionsSchema = { + type: 'object', + properties: { + roomId: { + type: 'string', + minLength: 1, + }, + text: { + type: 'string', + }, + offset: { + type: 'number', + }, + count: { + type: 'number', + }, + sort: { + type: 'string', + }, + query: { + type: 'string', + nullable: true, + }, + }, + required: ['roomId'], + additionalProperties: false, +}; + +const isChatGetDiscussionsLocalProps = ajv.compile(ChatGetDiscussionsSchema); + const chatEndpoints = API.v1 .post( 'chat.pinMessage', @@ -558,6 +593,55 @@ const chatEndpoints = API.v1 return API.v1.success(); }, + ) + .get( + 'chat.getDiscussions', + { + authRequired: true, + query: isChatGetDiscussionsLocalProps, + response: { + 200: ajv.compile<{ + messages: IMessage[]; + count: number; + offset: number; + total: number; + success: boolean; + }>({ + type: 'object', + properties: { + messages: { + type: 'array', + items: { $ref: '#/components/schemas/IMessage' }, + }, + count: { type: 'number' }, + offset: { type: 'number' }, + total: { type: 'number' }, + success: { type: 'boolean', enum: [true] }, + }, + required: ['messages', 'count', 'offset', 'total', 'success'], + additionalProperties: false, + }), + 400: validateBadRequestErrorResponse, + 401: validateUnauthorizedErrorResponse, + }, + }, + async function action() { + const { roomId, text } = this.queryParams; + const { sort } = await this.parseJsonQuery(); + const { offset, count } = await getPaginationItems(this.queryParams); + + const messages = await findDiscussionsFromRoom({ + uid: this.userId, + roomId, + text: text || '', + pagination: { + offset, + count, + sort, + }, + }); + return API.v1.success(messages); + }, ); API.v1.addRoute( @@ -1007,30 +1091,6 @@ API.v1.addRoute( }, ); -API.v1.addRoute( - 'chat.getDiscussions', - { authRequired: true, validateParams: isChatGetDiscussionsProps }, - { - async get() { - const { roomId, text } = this.queryParams; - const { sort } = await this.parseJsonQuery(); - const { offset, count } = await getPaginationItems(this.queryParams); - - const messages = await findDiscussionsFromRoom({ - uid: this.userId, - roomId, - text: text || '', - pagination: { - offset, - count, - sort, - }, - }); - return API.v1.success(messages); - }, - }, -); - API.v1.addRoute( 'chat.getURLPreview', { authRequired: true, validateParams: isChatGetURLPreviewProps }, diff --git a/packages/rest-typings/src/v1/chat.ts b/packages/rest-typings/src/v1/chat.ts index b52bba2d61ed5..387ee937de140 100644 --- a/packages/rest-typings/src/v1/chat.ts +++ b/packages/rest-typings/src/v1/chat.ts @@ -97,34 +97,6 @@ const ChatGetMessageSchema = { export const isChatGetMessageProps = ajv.compile(ChatGetMessageSchema); -type ChatGetDiscussions = PaginatedRequest<{ - roomId: IRoom['_id']; - text?: string; -}>; - -const ChatGetDiscussionsSchema = { - type: 'object', - properties: { - roomId: { - type: 'string', - minLength: 1, - }, - text: { - type: 'string', - }, - offset: { - type: 'number', - }, - count: { - type: 'number', - }, - }, - required: ['roomId'], - additionalProperties: false, -}; - -export const isChatGetDiscussionsProps = ajv.compile(ChatGetDiscussionsSchema); - type ChatReportMessage = { messageId: IMessage['_id']; description: string; @@ -900,12 +872,6 @@ export type ChatEndpoints = { '/v1/chat.reportMessage': { POST: (params: ChatReportMessage) => void; }; - '/v1/chat.getDiscussions': { - GET: (params: ChatGetDiscussions) => { - messages: IMessage[]; - total: number; - }; - }; '/v1/chat.getThreadsList': { GET: (params: ChatGetThreadsList) => { threads: IThreadMainMessage[];