-
Notifications
You must be signed in to change notification settings - Fork 13.3k
chore: migrate chat.reportMessage endpoint to new OpenAPI pattern with AJV validation #39230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
6d531c1
f2c43fd
4a7717c
4c4ed70
a44e004
aecf048
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.reportMessage endpoint to new OpenAPI pattern with AJV validation |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<ChatPinMessage>(ChatPinMessageSchema); | |
|
|
||
| const isChatUnpinMessageProps = ajv.compile<ChatUnpinMessage>(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<ChatReportMessage>(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<void>({ | ||
| 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(); | ||
| }, | ||
|
Comment on lines
+452
to
+460
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have removed the old checks for messageId and description because they are now handled by the body schema before entering the action function. That makes sense, nice |
||
| ) | ||
| .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 }, | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You moved the JSON schema, Ajv validation, and interface from rest-typings to chat.ts, but you should also remove the chat.reportMessage related code (the schema, Ajv, and interface) from rest-typings. Since it’s no longer needed there and we are making Meteor the single source of truth for the API, we need to ensure everything is moved to the Meteor file and fully removed from rest-typings to avoid redundancy. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve non-empty validation for
messageIdanddescription.Line 217 and Line 218 allow empty strings, so
{ "messageId": "", "description": "" }passes schema validation. That relaxes input checks during a migration that should keep behavior unchanged.Proposed minimal fix
const ChatReportMessageSchema = { type: 'object', properties: { - messageId: { type: 'string' }, - description: { type: 'string' }, + messageId: { type: 'string', minLength: 1 }, + description: { type: 'string', minLength: 1 }, }, required: ['messageId', 'description'], additionalProperties: false, };Based on learnings: In Rocket.Chat OpenAPI migration PRs for endpoints under apps/meteor/app/api/server/v1, avoid introducing logic changes and keep behavior preserved.
📝 Committable suggestion
🤖 Prompt for AI Agents
✅ Addressed in commit f2c43fd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 1546
@ahmed-n-abdeltwabActually, addingminLengthvalidation is in scope for this migration! Here's why:The original
reportMessagefunction (which the legacy endpoint called) validates the description:So the legacy endpoint did reject empty descriptions — it just happened later in the call chain rather than at schema validation. Adding
minLength: 1preserves this existing behavior by moving the validation earlier (to the schema layer), which is the correct approach for OpenAPI migrations.Result: Empty strings were already being rejected before your PR, so adding
minLength: 1maintains the original behavior rather than introducing a new restriction. This aligns with keeping the migration scope tight and behavior-preserving. ✅🐰
✏️ Learnings added
🧠 Learnings used