Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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/migrate-chat-getDiscussions-to-openapi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/meteor': minor
'@rocket.chat/rest-typings': minor
---

Migrated chat.getDiscussions endpoint to new OpenAPI pattern with AJV validation
108 changes: 82 additions & 26 deletions apps/meteor/app/api/server/v1/chat.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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';
Expand Down Expand Up @@ -275,6 +275,37 @@ const isChatPinMessageProps = ajv.compile<ChatPinMessage>(ChatPinMessageSchema);

const isChatUnpinMessageProps = ajv.compile<ChatUnpinMessage>(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',
},
},
required: ['roomId'],
additionalProperties: false,
};

const isChatGetDiscussionsLocalProps = ajv.compile<ChatGetDiscussions>(ChatGetDiscussionsSchema);

const chatEndpoints = API.v1
.post(
'chat.pinMessage',
Expand Down Expand Up @@ -558,6 +589,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,
}),
Comment on lines +603 to +623
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take a proper look at this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added $ref: '#/components/schemas/IMessage' for the messages array

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(
Expand Down Expand Up @@ -1007,30 +1087,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 },
Expand Down
34 changes: 0 additions & 34 deletions packages/rest-typings/src/v1/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,6 @@ const ChatGetMessageSchema = {

export const isChatGetMessageProps = ajv.compile<ChatGetMessage>(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<ChatGetDiscussions>(ChatGetDiscussionsSchema);

type ChatReportMessage = {
messageId: IMessage['_id'];
description: string;
Expand Down Expand Up @@ -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[];
Expand Down