From 2f40530984968960ca3f95a1808e89089b7831db Mon Sep 17 00:00:00 2001 From: Allan Chain <36528777+AllanChain@users.noreply.github.com> Date: Mon, 14 Oct 2024 21:06:08 +0800 Subject: [PATCH] feat(chat): enable review and comment --- .../app/src/Features/Chat/ChatController.js | 134 ++++++++++++++++++ .../Features/Project/ProjectEditorHandler.js | 2 +- services/web/app/src/router.mjs | 34 +++++ 3 files changed, 169 insertions(+), 1 deletion(-) diff --git a/services/web/app/src/Features/Chat/ChatController.js b/services/web/app/src/Features/Chat/ChatController.js index 51d217ed9e..f30e029b27 100644 --- a/services/web/app/src/Features/Chat/ChatController.js +++ b/services/web/app/src/Features/Chat/ChatController.js @@ -81,4 +81,138 @@ module.exports = ChatController = { } ) }, + + sendComment(req, res, next) { + const { project_id: projectId, thread_id: threadId } = req.params + const { content, client_id: clientId } = req.body + const userId = SessionManager.getLoggedInUserId(req.session) + if (userId == null) { + const err = new Error('no logged-in user') + return next(err) + } + return ChatApiHandler.sendComment( + projectId, + threadId, + userId, + content, + function (err, message) { + if (err != null) { + return next(err) + } + return UserInfoManager.getPersonalInfo( + message.user_id, + function (err, user) { + if (err != null) { + return next(err) + } + message.user = UserInfoController.formatPersonalInfo(user) + message.clientId = clientId + EditorRealTimeController.emitToRoom( + projectId, + 'new-comment', + threadId, + message + ) + return res.sendStatus(204) + } + ) + } + ) + }, + + editMessage(req, res, next) { + const { project_id: projectId, thread_id: threadId, message_id: messageId } = req.params + const { content, client_id: clientId } = req.body + const userId = SessionManager.getLoggedInUserId(req.session) + if (userId == null) { + const err = new Error('no logged-in user') + return next(err) + } + return ChatApiHandler.editMessage( + projectId, + threadId, + messageId, + userId, + content, + function (err) { + if (err != null) { + return next(err) + } + EditorRealTimeController.emitToRoom( + projectId, + 'edit-message', + threadId, + messageId, + content + ) + return res.sendStatus(204) + } + ) + }, + + deleteMessage(req, res, next) { + const { project_id: projectId, thread_id: threadId, message_id: messageId } = req.params + const { content, client_id: clientId } = req.body + const userId = SessionManager.getLoggedInUserId(req.session) + if (userId == null) { + const err = new Error('no logged-in user') + return next(err) + } + return ChatApiHandler.deleteMessage( + projectId, + threadId, + messageId, + function (err) { + if (err != null) { + return next(err) + } + return res.sendStatus(204) + } + ) + }, + + getThreads(req, res, next) { + const { project_id: projectId } = req.params + const { query } = req + return ChatApiHandler.getThreads( + projectId, + query.limit, + query.before, + function (err, threads) { + if (err != null) { + return next(err) + } + return ChatManager.injectUserInfoIntoThreads( + threads, + function (err) { + if (err != null) { + return next(err) + } + return res.json(threads) + } + ) + } + ) + }, + + resolveThread(req, res, next) { + const { project_id: projectId, thread_id: threadId } = req.params + const { client_id: clientId } = req.body + const userId = SessionManager.getLoggedInUserId(req.session) + if (userId == null) { + const err = new Error('no logged-in user') + return next(err) + } + return ChatApiHandler.resolveThread( + projectId, + threadId, + userId, + function (err) { + if (err != null) { + return next(err) + } + return res.sendStatus(204) + } + ) + }, } diff --git a/services/web/app/src/Features/Project/ProjectEditorHandler.js b/services/web/app/src/Features/Project/ProjectEditorHandler.js index 17c251357b..d9c4978e9f 100644 --- a/services/web/app/src/Features/Project/ProjectEditorHandler.js +++ b/services/web/app/src/Features/Project/ProjectEditorHandler.js @@ -8,7 +8,7 @@ function mergeDeletedDocs(a, b) { } module.exports = ProjectEditorHandler = { - trackChangesAvailable: false, + trackChangesAvailable: true, buildProjectModelView(project, members, invites, deletedDocsFromDocstore) { let owner, ownerFeatures diff --git a/services/web/app/src/router.mjs b/services/web/app/src/router.mjs index 8fefea1a10..3b2fe639e0 100644 --- a/services/web/app/src/router.mjs +++ b/services/web/app/src/router.mjs @@ -1153,6 +1153,40 @@ async function initialize(webRouter, privateApiRouter, publicApiRouter) { RateLimiterMiddleware.rateLimit(rateLimiters.sendChatMessage), ChatController.sendMessage ) + webRouter.get( + '/project/:project_id/threads', + AuthorizationMiddleware.blockRestrictedUserFromProject, + AuthorizationMiddleware.ensureUserCanReadProject, + ChatController.getThreads + ) + webRouter.post( + '/project/:project_id/thread/:thread_id/messages', + AuthorizationMiddleware.blockRestrictedUserFromProject, + AuthorizationMiddleware.ensureUserCanReadProject, + RateLimiterMiddleware.rateLimit(rateLimiters.sendChatMessage), + ChatController.sendComment + ) + webRouter.delete( + '/project/:project_id/thread/:thread_id/messages/:message_id', + AuthorizationMiddleware.blockRestrictedUserFromProject, + AuthorizationMiddleware.ensureUserCanReadProject, + RateLimiterMiddleware.rateLimit(rateLimiters.sendChatMessage), + ChatController.deleteMessage + ) + webRouter.post( + '/project/:project_id/thread/:thread_id/messages/:message_id/edit', + AuthorizationMiddleware.blockRestrictedUserFromProject, + AuthorizationMiddleware.ensureUserCanReadProject, + RateLimiterMiddleware.rateLimit(rateLimiters.sendChatMessage), + ChatController.editMessage + ) + webRouter.post( + '/project/:project_id/doc/:doc_id/thread/:thread_id/resolve', + AuthorizationMiddleware.blockRestrictedUserFromProject, + AuthorizationMiddleware.ensureUserCanReadProject, + RateLimiterMiddleware.rateLimit(rateLimiters.sendChatMessage), + ChatController.resolveThread + ) } webRouter.post(