-
Notifications
You must be signed in to change notification settings - Fork 13.5k
chore: migrate chat.getDiscussions endpoint to new OpenAPI pattern with AJV validation #39649
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
4
commits into
RocketChat:develop
from
alchemycodess:chore/migrate-chat-getDiscussions-to-openapi
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
977f3ef
chore: migrate chat.getDiscussions endpoint to new OpenAPI pattern wi…
alchemycodess 86a062d
fix: add sort property to ChatGetDiscussionsSchema
alchemycodess 512ba1d
fix: use PaginatedRequest type and add IMessage ref to response schema
alchemycodess 760c765
fix: add query property to ChatGetDiscussionsSchema
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
Some comments aren't visible on the classic Files Changed page.
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.getDiscussions 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 |
|---|---|---|
| @@ -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 { | ||
|
|
@@ -23,7 +23,6 @@ import { | |
| isChatGetThreadMessagesProps, | ||
| isChatSyncThreadMessagesProps, | ||
| isChatGetStarredMessagesProps, | ||
| isChatGetDiscussionsProps, | ||
| validateBadRequestErrorResponse, | ||
| validateUnauthorizedErrorResponse, | ||
| } from '@rocket.chat/rest-typings'; | ||
|
|
@@ -275,6 +274,40 @@ const isChatPinMessageProps = ajv.compile<ChatPinMessage>(ChatPinMessageSchema); | |
|
|
||
| const isChatUnpinMessageProps = ajv.compile<ChatUnpinMessage>(ChatUnpinMessageSchema); | ||
|
|
||
| type ChatGetDiscussions = { | ||
| roomId: IRoom['_id']; | ||
| text?: string; | ||
| count?: number; | ||
| offset?: number; | ||
| sort?: 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', | ||
|
|
@@ -558,6 +591,52 @@ 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' }, | ||
| 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
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. take a proper look at this
Author
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. 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( | ||
|
|
@@ -1007,30 +1086,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 }, | ||
|
|
||
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.
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.
you could add the paginated props like this but if you use the PaginatedRequest you could reduce the duplication
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.
Got it! should i import PaginatedRequest or is there another preferred way?
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.
Fixed, used PaginatedRequest