-
Notifications
You must be signed in to change notification settings - Fork 13.5k
refactor(chat): migrate chat.syncThreadMessages to typed REST endpoint #39669
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
Closed
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fe8f9c1
refactor(chat): migrate chat.syncThreadMessages to typed REST endpoint
Harxhit 2fa3df8
fix(chat.syncThreadMessages): relax response schema to allow projecti…
Harxhit ab1eeb7
fix(chat.syncThreadMessages): relax response schema to allow projecti…
Harxhit 76c8328
fix(chat.syncThreadMessages): remove _id requirement from response sc…
Harxhit 102356d
fix(chat): address review feedback for chat.syncThreadMessages schema…
Harxhit e62545f
fix(chat): allow query and fields in syncThreadMessages schema
Harxhit 49d75bf
fix(types): add chat.syncMessages to rest-typings endpoints
Harxhit c5e635e
fix(chat): align query and fields types with runtime parsing in chat.…
Harxhit 84453e4
(fix):correct AJV response schema for chat.syncThreadMessages by prop…
Harxhit 53f61e5
(fix): conflict changes fixed
Harxhit 940e9d5
fix: Removed errorTypes
Harxhit 4c6c5c9
fix: Reverting changes added for testing only
Harxhit 425fdce
fix: Added changeset and eslint fix
Harxhit f2b6a55
Added minor changset and rest-typings
Harxhit 11e3151
fix: Added changeset
Harxhit 65c75c2
Delete .changeset/giant-eels-shout.md
Harxhit 545a878
Delete .changeset/thin-snails-talk.md and added .changeset with rest-…
Harxhit 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
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 |
|---|---|---|
|
|
@@ -21,12 +21,12 @@ import { | |
| isChatGetDeletedMessagesProps, | ||
| isChatSyncThreadsListProps, | ||
| isChatGetThreadMessagesProps, | ||
| isChatSyncThreadMessagesProps, | ||
| isChatGetStarredMessagesProps, | ||
| isChatGetDiscussionsProps, | ||
| validateBadRequestErrorResponse, | ||
| validateUnauthorizedErrorResponse, | ||
| } from '@rocket.chat/rest-typings'; | ||
| import type { PaginatedRequest } from '@rocket.chat/rest-typings'; | ||
| import { escapeRegExp } from '@rocket.chat/string-helpers'; | ||
| import { Meteor } from 'meteor/meteor'; | ||
|
|
||
|
|
@@ -50,8 +50,8 @@ import { settings } from '../../../settings/server'; | |
| import { followMessage } from '../../../threads/server/methods/followMessage'; | ||
| import { unfollowMessage } from '../../../threads/server/methods/unfollowMessage'; | ||
| import { normalizeMessagesForUser } from '../../../utils/server/lib/normalizeMessagesForUser'; | ||
| import type { ExtractRoutesFromAPI } from '../ApiClass'; | ||
| import { API } from '../api'; | ||
| import type { ExtractRoutesFromAPI } from '../ApiClass'; | ||
| import { getPaginationItems } from '../helpers/getPaginationItems'; | ||
| import { findDiscussionsFromRoom, findMentionedMessages, findStarredMessages } from '../lib/messages'; | ||
|
|
||
|
|
@@ -275,6 +275,87 @@ const isChatPinMessageProps = ajv.compile<ChatPinMessage>(ChatPinMessageSchema); | |
|
|
||
| const isChatUnpinMessageProps = ajv.compile<ChatUnpinMessage>(ChatUnpinMessageSchema); | ||
|
|
||
| type ChatSyncThreadMessages = PaginatedRequest<{ | ||
| tmid: string; | ||
| updatedSince: string; | ||
| }>; | ||
|
|
||
| const ChatSyncThreadMessagesSchema = { | ||
| type: 'object', | ||
| properties: { | ||
| tmid: { | ||
| type: 'string', | ||
| minLength: 1, | ||
| }, | ||
| updatedSince: { | ||
| type: 'string', | ||
| format: 'iso-date-time', | ||
| }, | ||
| count: { | ||
| type: 'number', | ||
| nullable: true, | ||
| }, | ||
| offset: { | ||
| type: 'number', | ||
| nullable: true, | ||
| }, | ||
| sort: { | ||
| type: 'string', | ||
| nullable: true, | ||
| }, | ||
| query: { | ||
| type: 'object', | ||
| nullable: true, | ||
| }, | ||
| fields: { | ||
| type: 'object', | ||
| nullable: true, | ||
| }, | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| required: ['tmid', 'updatedSince'], | ||
| additionalProperties: false, | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| const isChatSyncThreadMessagesLocalProps = ajv.compile<ChatSyncThreadMessages>(ChatSyncThreadMessagesSchema); | ||
|
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: Query validator for Prompt for AI agents |
||
|
|
||
| const isSyncThreadMessagesResponse = ajv.compile<{ | ||
| messages: { | ||
| update: Partial<IMessage>[]; | ||
| remove: Partial<IMessage>[]; | ||
| }; | ||
| }>({ | ||
| type: 'object', | ||
| properties: { | ||
| messages: { | ||
| type: 'object', | ||
| properties: { | ||
| update: { | ||
| type: 'array', | ||
| items: { | ||
| type: 'object', | ||
| additionalProperties: true, | ||
| }, | ||
| }, | ||
| remove: { | ||
| type: 'array', | ||
| items: { | ||
| type: 'object', | ||
| additionalProperties: true, | ||
| }, | ||
| }, | ||
| }, | ||
| required: ['update', 'remove'], | ||
| additionalProperties: false, | ||
| }, | ||
| success: { | ||
| type: 'boolean', | ||
| enum: [true], | ||
| }, | ||
| }, | ||
| required: ['messages', 'success'], | ||
| additionalProperties: false, | ||
| }); | ||
|
|
||
| const chatEndpoints = API.v1 | ||
| .post( | ||
| 'chat.pinMessage', | ||
|
|
@@ -558,6 +639,51 @@ const chatEndpoints = API.v1 | |
|
|
||
| return API.v1.success(); | ||
| }, | ||
| ) | ||
| .get( | ||
| 'chat.syncThreadMessages', | ||
| { | ||
| authRequired: true, | ||
| query: isChatSyncThreadMessagesLocalProps, | ||
| response: { | ||
| 400: validateBadRequestErrorResponse, | ||
| 401: validateUnauthorizedErrorResponse, | ||
| 200: isSyncThreadMessagesResponse, | ||
| }, | ||
| }, | ||
| async function action() { | ||
| const { tmid } = this.queryParams; | ||
| const { query, fields, sort } = await this.parseJsonQuery(); | ||
| const { updatedSince } = this.queryParams; | ||
| let updatedSinceDate; | ||
| if (!settings.get<boolean>('Threads_enabled')) { | ||
| throw new Meteor.Error('error-not-allowed', 'Threads Disabled'); | ||
| } | ||
|
|
||
| if (isNaN(Date.parse(updatedSince))) { | ||
| throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.'); | ||
| } else { | ||
| updatedSinceDate = new Date(updatedSince); | ||
| } | ||
| const thread = await Messages.findOneById(tmid, { projection: { rid: 1 } }); | ||
| if (!thread?.rid) { | ||
| throw new Meteor.Error('error-invalid-message', 'Invalid Message'); | ||
| } | ||
| const [user, room] = await Promise.all([ | ||
| Users.findOneById(this.userId, { projection: { _id: 1 } }), | ||
| 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'); | ||
| } | ||
| return API.v1.success({ | ||
| messages: { | ||
| update: await Messages.find({ ...query, tmid, _updatedAt: { $gt: updatedSinceDate } }, { projection: fields, sort }).toArray(), | ||
| remove: await Messages.trashFindDeletedAfter(updatedSinceDate, { ...query, tmid }, { projection: fields, sort }).toArray(), | ||
| }, | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| API.v1.addRoute( | ||
|
|
@@ -918,45 +1044,6 @@ API.v1.addRoute( | |
| }, | ||
| ); | ||
|
|
||
| API.v1.addRoute( | ||
| 'chat.syncThreadMessages', | ||
| { authRequired: true, validateParams: isChatSyncThreadMessagesProps }, | ||
| { | ||
| async get() { | ||
| const { tmid } = this.queryParams; | ||
| const { query, fields, sort } = await this.parseJsonQuery(); | ||
| const { updatedSince } = this.queryParams; | ||
| let updatedSinceDate; | ||
| if (!settings.get<boolean>('Threads_enabled')) { | ||
| throw new Meteor.Error('error-not-allowed', 'Threads Disabled'); | ||
| } | ||
|
|
||
| if (isNaN(Date.parse(updatedSince))) { | ||
| throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.'); | ||
| } else { | ||
| updatedSinceDate = new Date(updatedSince); | ||
| } | ||
| const thread = await Messages.findOneById(tmid, { projection: { rid: 1 } }); | ||
| if (!thread?.rid) { | ||
| throw new Meteor.Error('error-invalid-message', 'Invalid Message'); | ||
| } | ||
| // TODO: promise.all? this.user? | ||
| 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'); | ||
| } | ||
| return API.v1.success({ | ||
| messages: { | ||
| update: await Messages.find({ ...query, tmid, _updatedAt: { $gt: updatedSinceDate } }, { projection: fields, sort }).toArray(), | ||
| remove: await Messages.trashFindDeletedAfter(updatedSinceDate, { ...query, tmid }, { projection: fields, sort }).toArray(), | ||
| }, | ||
| }); | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| API.v1.addRoute( | ||
| 'chat.getMentionedMessages', | ||
| { authRequired: true, validateParams: isChatGetMentionedMessagesProps }, | ||
|
|
||
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.