-
Notifications
You must be signed in to change notification settings - Fork 13.5k
chore: migrate chat.getThreadMessages endpoint to new OpenAPI pattern with AJV validation #39654
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
Closed
alchemycodess
wants to merge
3
commits into
RocketChat:develop
from
alchemycodess:chore/migrate-chat-getThreadMessages-to-openapi
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a7b2b34
chore: migrate chat.getThreadMessages endpoint to new OpenAPI pattern…
alchemycodess 08df50a
fix: add query and fields properties to ChatGetThreadMessagesSchema
alchemycodess a77f02b
fix: use PaginatedRequest type and add IMessage ref to response schema
alchemycodess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.getThreadMessages endpoint to new OpenAPI pattern with AJV validation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,12 +20,12 @@ import { | |||||
| isChatReactProps, | ||||||
| isChatGetDeletedMessagesProps, | ||||||
| isChatSyncThreadsListProps, | ||||||
| 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,44 @@ const isChatPinMessageProps = ajv.compile<ChatPinMessage>(ChatPinMessageSchema); | |||||
|
|
||||||
| const isChatUnpinMessageProps = ajv.compile<ChatUnpinMessage>(ChatUnpinMessageSchema); | ||||||
|
|
||||||
| type ChatGetThreadMessages = PaginatedRequest<{ | ||||||
| tmid: string; | ||||||
| }>; | ||||||
|
|
||||||
| const ChatGetThreadMessagesSchema = { | ||||||
| type: 'object', | ||||||
| properties: { | ||||||
| tmid: { | ||||||
| type: 'string', | ||||||
| minLength: 1, | ||||||
| }, | ||||||
| count: { | ||||||
| type: 'number', | ||||||
| nullable: true, | ||||||
| }, | ||||||
| offset: { | ||||||
| type: 'number', | ||||||
| nullable: true, | ||||||
| }, | ||||||
| sort: { | ||||||
| type: 'string', | ||||||
| nullable: true, | ||||||
| }, | ||||||
| query: { | ||||||
| type: 'string', | ||||||
| nullable: true, | ||||||
| }, | ||||||
| fields: { | ||||||
| type: 'string', | ||||||
| nullable: true, | ||||||
| }, | ||||||
| }, | ||||||
| required: ['tmid'], | ||||||
| additionalProperties: false, | ||||||
| }; | ||||||
|
|
||||||
| const isChatGetThreadMessagesLocalProps = ajv.compile<ChatGetThreadMessages>(ChatGetThreadMessagesSchema); | ||||||
|
|
||||||
| const chatEndpoints = API.v1 | ||||||
| .post( | ||||||
| 'chat.pinMessage', | ||||||
|
|
@@ -558,6 +596,76 @@ const chatEndpoints = API.v1 | |||||
|
|
||||||
| return API.v1.success(); | ||||||
| }, | ||||||
| ) | ||||||
| .get( | ||||||
| 'chat.getThreadMessages', | ||||||
| { | ||||||
| authRequired: true, | ||||||
| query: isChatGetThreadMessagesLocalProps, | ||||||
| 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' }, | ||||||
|
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. P2: Response schema now enforces full Prompt for AI agents
Suggested change
|
||||||
| }, | ||||||
| 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 { tmid } = this.queryParams; | ||||||
| const { query, fields, sort } = await this.parseJsonQuery(); | ||||||
| const { offset, count } = await getPaginationItems(this.queryParams); | ||||||
|
|
||||||
| if (!settings.get('Threads_enabled')) { | ||||||
| throw new Meteor.Error('error-not-allowed', 'Threads Disabled'); | ||||||
| } | ||||||
|
|
||||||
| const thread = await Messages.findOneById(tmid, { projection: { rid: 1 } }); | ||||||
| if (!thread?.rid) { | ||||||
| throw new Meteor.Error('error-invalid-message', 'Invalid Message'); | ||||||
| } | ||||||
| const user = await Users.findOneById(this.userId, { projection: { _id: 1 } }); | ||||||
| const room = await Rooms.findOneById(thread.rid, { projection: { ...roomAccessAttributes, t: 1, _id: 1 } }); | ||||||
|
|
||||||
| if (!room || !user || !(await canAccessRoomAsync(room, user))) { | ||||||
| throw new Meteor.Error('error-not-allowed', 'Not Allowed'); | ||||||
| } | ||||||
| const { cursor, totalCount } = Messages.findPaginated( | ||||||
| { ...query, tmid }, | ||||||
| { | ||||||
| sort: sort || { ts: 1 }, | ||||||
| skip: offset, | ||||||
| limit: count, | ||||||
| projection: fields, | ||||||
| }, | ||||||
| ); | ||||||
|
|
||||||
| const [messages, total] = await Promise.all([cursor.toArray(), totalCount]); | ||||||
|
|
||||||
| return API.v1.success({ | ||||||
| messages, | ||||||
| count: messages.length, | ||||||
| offset, | ||||||
| total, | ||||||
| }); | ||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| }, | ||||||
| ); | ||||||
|
|
||||||
| API.v1.addRoute( | ||||||
|
|
@@ -873,51 +981,6 @@ API.v1.addRoute( | |||||
| }, | ||||||
| ); | ||||||
|
|
||||||
| API.v1.addRoute( | ||||||
| 'chat.getThreadMessages', | ||||||
| { authRequired: true, validateParams: isChatGetThreadMessagesProps }, | ||||||
| { | ||||||
| async get() { | ||||||
| const { tmid } = this.queryParams; | ||||||
| const { query, fields, sort } = await this.parseJsonQuery(); | ||||||
| const { offset, count } = await getPaginationItems(this.queryParams); | ||||||
|
|
||||||
| if (!settings.get('Threads_enabled')) { | ||||||
| throw new Meteor.Error('error-not-allowed', 'Threads Disabled'); | ||||||
| } | ||||||
|
|
||||||
| const thread = await Messages.findOneById(tmid, { projection: { rid: 1 } }); | ||||||
| if (!thread?.rid) { | ||||||
| throw new Meteor.Error('error-invalid-message', 'Invalid Message'); | ||||||
| } | ||||||
| const user = await Users.findOneById(this.userId, { projection: { _id: 1 } }); | ||||||
| const room = await Rooms.findOneById(thread.rid, { projection: { ...roomAccessAttributes, t: 1, _id: 1 } }); | ||||||
|
|
||||||
| if (!room || !user || !(await canAccessRoomAsync(room, user))) { | ||||||
| throw new Meteor.Error('error-not-allowed', 'Not Allowed'); | ||||||
| } | ||||||
| const { cursor, totalCount } = Messages.findPaginated( | ||||||
| { ...query, tmid }, | ||||||
| { | ||||||
| sort: sort || { ts: 1 }, | ||||||
| skip: offset, | ||||||
| limit: count, | ||||||
| projection: fields, | ||||||
| }, | ||||||
| ); | ||||||
|
|
||||||
| const [messages, total] = await Promise.all([cursor.toArray(), totalCount]); | ||||||
|
|
||||||
| return API.v1.success({ | ||||||
| messages, | ||||||
| count: messages.length, | ||||||
| offset, | ||||||
| total, | ||||||
| }); | ||||||
| }, | ||||||
| }, | ||||||
| ); | ||||||
|
|
||||||
| API.v1.addRoute( | ||||||
| 'chat.syncThreadMessages', | ||||||
| { authRequired: true, validateParams: isChatSyncThreadMessagesProps }, | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.