diff --git a/.changeset/migrate-chat-getPinnedMessages.md b/.changeset/migrate-chat-getPinnedMessages.md new file mode 100644 index 0000000000000..bd630a1eff53c --- /dev/null +++ b/.changeset/migrate-chat-getPinnedMessages.md @@ -0,0 +1,6 @@ +--- +"@rocket.chat/meteor": minor +"@rocket.chat/rest-typings": minor +--- + +Add OpenAPI support for the chat.getPinnedMessages API endpoint by migrating to a modern chained route definition syntax and utilizing AJV schemas for query and response validation. diff --git a/apps/meteor/app/api/server/v1/chat.ts b/apps/meteor/app/api/server/v1/chat.ts index a00d57e46ae72..19274096a5e9f 100644 --- a/apps/meteor/app/api/server/v1/chat.ts +++ b/apps/meteor/app/api/server/v1/chat.ts @@ -15,7 +15,6 @@ import { isChatSearchProps, isChatSendMessageProps, isChatIgnoreUserProps, - isChatGetPinnedMessagesProps, isChatGetMentionedMessagesProps, isChatReactProps, isChatGetDeletedMessagesProps, @@ -275,6 +274,39 @@ const isChatPinMessageProps = ajv.compile(ChatPinMessageSchema); const isChatUnpinMessageProps = ajv.compile(ChatUnpinMessageSchema); +type GetPinnedMessages = { + roomId: string; + count?: number; + offset?: number; + sort?: string; +}; + +const GetPinnedMessagesSchema = { + type: 'object', + properties: { + roomId: { + type: 'string', + minLength: 1, + }, + count: { + type: 'number', + nullable: true, + }, + offset: { + type: 'number', + nullable: true, + }, + sort: { + type: 'string', + nullable: true, + }, + }, + required: ['roomId'], + additionalProperties: false, +}; + +const isChatGetPinnedMessagesProps = ajv.compile(GetPinnedMessagesSchema); + const chatEndpoints = API.v1 .post( 'chat.pinMessage', @@ -558,6 +590,61 @@ const chatEndpoints = API.v1 return API.v1.success(); }, + ) + .get( + 'chat.getPinnedMessages', + { + authRequired: true, + query: isChatGetPinnedMessagesProps, + response: { + 200: ajv.compile<{ messages: IMessage[]; count: number; offset: number; total: number }>({ + 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 } = this.queryParams; + const { offset, count } = await getPaginationItems(this.queryParams); + const { sort } = await this.parseJsonQuery(); + + if (!(await canAccessRoomIdAsync(roomId, this.userId))) { + throw new Meteor.Error('error-not-allowed', 'Not allowed'); + } + + const { cursor, totalCount } = Messages.findPaginatedPinnedByRoom(roomId, { + skip: offset, + limit: count, + sort, + }); + + const [messages, total] = await Promise.all([cursor.toArray(), totalCount]); + + return API.v1.success({ + messages: await normalizeMessagesForUser(messages, this.userId), + count: messages.length, + offset, + total, + }); + }, ); API.v1.addRoute( @@ -753,35 +840,6 @@ API.v1.addRoute( }, ); -API.v1.addRoute( - 'chat.getPinnedMessages', - { authRequired: true, validateParams: isChatGetPinnedMessagesProps }, - { - async get() { - const { roomId } = this.queryParams; - const { offset, count } = await getPaginationItems(this.queryParams); - - if (!(await canAccessRoomIdAsync(roomId, this.userId))) { - throw new Meteor.Error('error-not-allowed', 'Not allowed'); - } - - const { cursor, totalCount } = Messages.findPaginatedPinnedByRoom(roomId, { - skip: offset, - limit: count, - }); - - const [messages, total] = await Promise.all([cursor.toArray(), totalCount]); - - return API.v1.success({ - messages: await normalizeMessagesForUser(messages, this.userId), - count: messages.length, - offset, - total, - }); - }, - }, -); - API.v1.addRoute( 'chat.getThreadsList', { authRequired: true, validateParams: isChatGetThreadsListProps }, diff --git a/packages/rest-typings/src/v1/chat.ts b/packages/rest-typings/src/v1/chat.ts index b52bba2d61ed5..cd29150b01b54 100644 --- a/packages/rest-typings/src/v1/chat.ts +++ b/packages/rest-typings/src/v1/chat.ts @@ -529,39 +529,6 @@ const GetStarredMessagesSchema = { export const isChatGetStarredMessagesProps = ajv.compile(GetStarredMessagesSchema); -type GetPinnedMessages = { - roomId: IRoom['_id']; - count?: number; - offset?: number; - sort?: string; -}; - -const GetPinnedMessagesSchema = { - type: 'object', - properties: { - roomId: { - type: 'string', - minLength: 1, - }, - count: { - type: 'number', - nullable: true, - }, - offset: { - type: 'number', - nullable: true, - }, - sort: { - type: 'string', - nullable: true, - }, - }, - required: ['roomId'], - additionalProperties: false, -}; - -export const isChatGetPinnedMessagesProps = ajv.compile(GetPinnedMessagesSchema); - type GetMentionedMessages = { roomId: IRoom['_id']; count?: number; @@ -954,14 +921,6 @@ export type ChatEndpoints = { total: number; }; }; - '/v1/chat.getPinnedMessages': { - GET: (params: GetPinnedMessages) => { - messages: IMessage[]; - count: number; - offset: number; - total: number; - }; - }; '/v1/chat.getMentionedMessages': { GET: (params: GetMentionedMessages) => { messages: IMessage[];