diff --git a/.changeset/migrate-chat-getDeletedMessages-to-AJV.md b/.changeset/migrate-chat-getDeletedMessages-to-AJV.md new file mode 100644 index 0000000000000..4523a861326a8 --- /dev/null +++ b/.changeset/migrate-chat-getDeletedMessages-to-AJV.md @@ -0,0 +1,7 @@ +--- +'@rocket.chat/core-typings': minor +'@rocket.chat/rest-typings': minor +'@rocket.chat/meteor': minor +--- + +Migrate chat.getDeletedMessages endpoint to make it openAPI and AJV compatible diff --git a/apps/meteor/app/api/server/v1/chat.ts b/apps/meteor/app/api/server/v1/chat.ts index a00d57e46ae72..a87bce1866a6a 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 { ICustomMessage, IMessage, IRoom, IThreadMainMessage } from '@rocket.chat/core-typings'; import { MessageTypes } from '@rocket.chat/message-types'; import { Messages, Users, Rooms, Subscriptions } from '@rocket.chat/models'; import { @@ -18,7 +18,6 @@ import { isChatGetPinnedMessagesProps, isChatGetMentionedMessagesProps, isChatReactProps, - isChatGetDeletedMessagesProps, isChatSyncThreadsListProps, isChatGetThreadMessagesProps, isChatSyncThreadMessagesProps, @@ -26,6 +25,7 @@ import { isChatGetDiscussionsProps, validateBadRequestErrorResponse, validateUnauthorizedErrorResponse, + type PaginatedRequest, } from '@rocket.chat/rest-typings'; import { escapeRegExp } from '@rocket.chat/string-helpers'; import { Meteor } from 'meteor/meteor'; @@ -119,6 +119,42 @@ const ChatUnfollowMessageLocalSchema = { additionalProperties: false, }; +//chat.getDeletedMessages starts +type ChatGetDeletedMessages = PaginatedRequest<{ + roomId: IRoom['_id']; + since: string; +}>; + +const ChatGetDeletedMessagesSchema = { + type: 'object', + properties: { + roomId: { + type: 'string', + minLength: 1, + }, + since: { + type: 'string', + minLength: 1, + format: 'iso-date-time', + }, + count: { + type: 'number', + nullable: true, + }, + offset: { + type: 'number', + nullable: true, + }, + sort: { + type: 'string', + nullable: true, + }, + }, + required: ['roomId', 'since'], + additionalProperties: false, +}; +//chat.getDeletedMessages ends + const isChatStarMessageLocalProps = ajv.compile(ChatStarMessageLocalSchema); const isChatUnstarMessageLocalProps = ajv.compile(ChatUnstarMessageLocalSchema); @@ -127,6 +163,8 @@ const isChatFollowMessageLocalProps = ajv.compile(ChatFo const isChatUnfollowMessageLocalProps = ajv.compile(ChatUnfollowMessageLocalSchema); +const isChatGetDeletedMessagesLocalProps = ajv.compile(ChatGetDeletedMessagesSchema); + API.v1.addRoute( 'chat.delete', { authRequired: true, validateParams: isChatDeleteProps }, @@ -558,6 +596,64 @@ const chatEndpoints = API.v1 return API.v1.success(); }, + ) + .get( + 'chat.getDeletedMessages', + { + authRequired: true, + query: isChatGetDeletedMessagesLocalProps, + response: { + 400: validateBadRequestErrorResponse, + 401: validateUnauthorizedErrorResponse, + 200: ajv.compile<{ messages: ICustomMessage[] }>({ + type: 'object', + properties: { + messages: { + type: 'array', + items: { $ref: '#/components/schemas/ICustomMessage' }, + }, + count: { + type: 'number', + }, + offset: { + type: 'number', + }, + total: { + type: 'number', + }, + success: { + type: 'boolean', + enum: [true], + }, + }, + required: ['messages', 'count', 'offset', 'total', 'success'], + additionalProperties: false, + }), + }, + }, + async function action() { + const { roomId, since } = this.queryParams; + const { offset, count } = await getPaginationItems(this.queryParams); + + const { cursor, totalCount } = Messages.trashFindPaginatedDeletedAfter( + new Date(since), + { rid: roomId }, + { + skip: offset, + limit: count, + projection: { _id: 1 }, + }, + ); + + const [messages, total] = await Promise.all([cursor.toArray(), totalCount]); + + return API.v1.success({ + messages, + count: messages.length, + offset, + total, + }); + }, ); API.v1.addRoute( @@ -723,36 +819,6 @@ API.v1.addRoute( }, ); -API.v1.addRoute( - 'chat.getDeletedMessages', - { authRequired: true, validateParams: isChatGetDeletedMessagesProps }, - { - async get() { - const { roomId, since } = this.queryParams; - const { offset, count } = await getPaginationItems(this.queryParams); - - const { cursor, totalCount } = Messages.trashFindPaginatedDeletedAfter( - new Date(since), - { rid: roomId }, - { - skip: offset, - limit: count, - projection: { _id: 1 }, - }, - ); - - const [messages, total] = await Promise.all([cursor.toArray(), totalCount]); - - return API.v1.success({ - messages, - count: messages.length, - offset, - total, - }); - }, - }, -); - API.v1.addRoute( 'chat.getPinnedMessages', { authRequired: true, validateParams: isChatGetPinnedMessagesProps }, diff --git a/packages/core-typings/src/Ajv.ts b/packages/core-typings/src/Ajv.ts index e33f94e99e2ae..610ea6f6bbaa7 100644 --- a/packages/core-typings/src/Ajv.ts +++ b/packages/core-typings/src/Ajv.ts @@ -4,6 +4,7 @@ import type { IBanner } from './IBanner'; import type { ICalendarEvent } from './ICalendarEvent'; import type { CallHistoryItem } from './ICallHistoryItem'; import type { CloudConfirmationPollData, CloudRegistrationIntentData, CloudRegistrationStatus } from './ICloud'; +import type { ICustomMessage } from './ICustomMessage'; import type { ICustomSound } from './ICustomSound'; import type { ICustomUserStatus } from './ICustomUserStatus'; import type { IEmailInbox } from './IEmailInbox'; @@ -28,6 +29,7 @@ export const schemas = typia.json.schemas< | ISubscription | IInvite | ICustomSound + | ICustomMessage | IMessage | IOAuthApps | IPermission diff --git a/packages/core-typings/src/ICustomMessage.ts b/packages/core-typings/src/ICustomMessage.ts new file mode 100644 index 0000000000000..3bb9b0d38141f --- /dev/null +++ b/packages/core-typings/src/ICustomMessage.ts @@ -0,0 +1,3 @@ +import type { IMessage } from './IMessage'; + +export type ICustomMessage = Pick; diff --git a/packages/core-typings/src/index.ts b/packages/core-typings/src/index.ts index 011df49c0f28c..006bdb71eebcf 100644 --- a/packages/core-typings/src/index.ts +++ b/packages/core-typings/src/index.ts @@ -55,6 +55,7 @@ export type * from './ICredentialToken'; export type * from './IAvatar'; export type * from './ICustomUserStatus'; export type * from './IEmailMessageHistory'; +export type * from './ICustomMessage'; export type * from './IReadReceipt'; export type * from './MessageReads'; diff --git a/packages/rest-typings/src/v1/chat.ts b/packages/rest-typings/src/v1/chat.ts index f3a6e61f677b0..20db0054d26b1 100644 --- a/packages/rest-typings/src/v1/chat.ts +++ b/packages/rest-typings/src/v1/chat.ts @@ -703,42 +703,6 @@ const ChatGetThreadMessagesSchema = { export const isChatGetThreadMessagesProps = ajvQuery.compile(ChatGetThreadMessagesSchema); -type ChatGetDeletedMessages = PaginatedRequest<{ - roomId: IRoom['_id']; - since: string; -}>; - -const ChatGetDeletedMessagesSchema = { - type: 'object', - properties: { - roomId: { - type: 'string', - minLength: 1, - }, - since: { - type: 'string', - minLength: 1, - format: 'iso-date-time', - }, - count: { - type: 'number', - nullable: true, - }, - offset: { - type: 'number', - nullable: true, - }, - sort: { - type: 'string', - nullable: true, - }, - }, - required: ['roomId', 'since'], - additionalProperties: false, -}; - -export const isChatGetDeletedMessagesProps = ajvQuery.compile(ChatGetDeletedMessagesSchema); - type ChatPostMessage = | { roomId: string | string[]; @@ -1005,14 +969,6 @@ export type ChatEndpoints = { total: number; }; }; - '/v1/chat.getDeletedMessages': { - GET: (params: ChatGetDeletedMessages) => { - messages: IMessage[]; - count: number; - offset: number; - total: number; - }; - }; '/v1/chat.getURLPreview': { GET: (params: ChatGetURLPreview) => { urlPreview: MessageUrl }; };