|
@@ -1,4 +1,5 @@
|
|
|
|
|
|
|
|
|
|
+import { Comment, CommentEvent, commentEvent } from '~/features/comment/server';
|
|
|
import { SupportedAction, SupportedTargetModel, SupportedEventModel } from '~/interfaces/activity';
|
|
import { SupportedAction, SupportedTargetModel, SupportedEventModel } from '~/interfaces/activity';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
@@ -49,7 +50,6 @@ const { serializeUserSecurely } = require('../models/serializers/user-serializer
|
|
|
|
|
|
|
|
module.exports = function(crowi, app) {
|
|
module.exports = function(crowi, app) {
|
|
|
const logger = loggerFactory('growi:routes:comment');
|
|
const logger = loggerFactory('growi:routes:comment');
|
|
|
- const Comment = crowi.model('Comment');
|
|
|
|
|
const User = crowi.model('User');
|
|
const User = crowi.model('User');
|
|
|
const Page = crowi.model('Page');
|
|
const Page = crowi.model('Page');
|
|
|
const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');
|
|
const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');
|
|
@@ -124,21 +124,21 @@ module.exports = function(crowi, app) {
|
|
|
return res.json(ApiResponse.error('Current user is not accessible to this page.'));
|
|
return res.json(ApiResponse.error('Current user is not accessible to this page.'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- let fetcher = null;
|
|
|
|
|
|
|
+ let query = null;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
if (revisionId) {
|
|
if (revisionId) {
|
|
|
- fetcher = Comment.getCommentsByRevisionId(revisionId);
|
|
|
|
|
|
|
+ query = Comment.findCommentsByRevisionId(revisionId);
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- fetcher = Comment.getCommentsByPageId(pageId);
|
|
|
|
|
|
|
+ query = Comment.findCommentsByPageId(pageId);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
return res.json(ApiResponse.error(err));
|
|
return res.json(ApiResponse.error(err));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const comments = await fetcher.populate('creator');
|
|
|
|
|
|
|
+ const comments = await query.populate('creator');
|
|
|
comments.forEach((comment) => {
|
|
comments.forEach((comment) => {
|
|
|
if (comment.creator != null && comment.creator instanceof User) {
|
|
if (comment.creator != null && comment.creator instanceof User) {
|
|
|
comment.creator = serializeUserSecurely(comment.creator);
|
|
comment.creator = serializeUserSecurely(comment.creator);
|
|
@@ -233,9 +233,7 @@ module.exports = function(crowi, app) {
|
|
|
const revisionId = commentForm.revision_id;
|
|
const revisionId = commentForm.revision_id;
|
|
|
const comment = commentForm.comment;
|
|
const comment = commentForm.comment;
|
|
|
const position = commentForm.comment_position || -1;
|
|
const position = commentForm.comment_position || -1;
|
|
|
- const isMarkdown = commentForm.is_markdown ?? true; // comment is always markdown (https://github.com/weseek/growi/pull/6096)
|
|
|
|
|
const replyTo = commentForm.replyTo;
|
|
const replyTo = commentForm.replyTo;
|
|
|
- const commentEvent = crowi.event('comment');
|
|
|
|
|
|
|
|
|
|
// check whether accessible
|
|
// check whether accessible
|
|
|
const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
|
|
const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
|
|
@@ -245,8 +243,8 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
let createdComment;
|
|
let createdComment;
|
|
|
try {
|
|
try {
|
|
|
- createdComment = await Comment.create(pageId, req.user._id, revisionId, comment, position, isMarkdown, replyTo);
|
|
|
|
|
- commentEvent.emit('create', createdComment);
|
|
|
|
|
|
|
+ createdComment = await Comment.add(pageId, req.user._id, revisionId, comment, position, replyTo);
|
|
|
|
|
+ commentEvent.emit(CommentEvent.CREATE, createdComment);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
logger.error(err);
|
|
@@ -355,12 +353,9 @@ module.exports = function(crowi, app) {
|
|
|
const { commentForm } = req.body;
|
|
const { commentForm } = req.body;
|
|
|
|
|
|
|
|
const commentStr = commentForm.comment;
|
|
const commentStr = commentForm.comment;
|
|
|
- const isMarkdown = commentForm.is_markdown ?? true; // comment is always markdown (https://github.com/weseek/growi/pull/6096)
|
|
|
|
|
const commentId = commentForm.comment_id;
|
|
const commentId = commentForm.comment_id;
|
|
|
const revision = commentForm.revision_id;
|
|
const revision = commentForm.revision_id;
|
|
|
|
|
|
|
|
- const commentEvent = crowi.event('comment');
|
|
|
|
|
-
|
|
|
|
|
if (commentStr === '') {
|
|
if (commentStr === '') {
|
|
|
return res.json(ApiResponse.error('Comment text is required'));
|
|
return res.json(ApiResponse.error('Comment text is required'));
|
|
|
}
|
|
}
|
|
@@ -389,9 +384,9 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
updatedComment = await Comment.findOneAndUpdate(
|
|
updatedComment = await Comment.findOneAndUpdate(
|
|
|
{ _id: commentId },
|
|
{ _id: commentId },
|
|
|
- { $set: { comment: commentStr, isMarkdown, revision } },
|
|
|
|
|
|
|
+ { $set: { comment: commentStr, revision } },
|
|
|
);
|
|
);
|
|
|
- commentEvent.emit('update', updatedComment);
|
|
|
|
|
|
|
+ commentEvent.emit(CommentEvent.UPDATE, updatedComment);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
logger.error(err);
|
|
logger.error(err);
|
|
@@ -448,8 +443,6 @@ module.exports = function(crowi, app) {
|
|
|
* @apiParam {String} comment_id Comment Id.
|
|
* @apiParam {String} comment_id Comment Id.
|
|
|
*/
|
|
*/
|
|
|
api.remove = async function(req, res) {
|
|
api.remove = async function(req, res) {
|
|
|
- const commentEvent = crowi.event('comment');
|
|
|
|
|
-
|
|
|
|
|
const commentId = req.body.comment_id;
|
|
const commentId = req.body.comment_id;
|
|
|
if (!commentId) {
|
|
if (!commentId) {
|
|
|
return Promise.resolve(res.json(ApiResponse.error('\'comment_id\' is undefined')));
|
|
return Promise.resolve(res.json(ApiResponse.error('\'comment_id\' is undefined')));
|
|
@@ -474,7 +467,7 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
|
|
await comment.removeWithReplies(comment);
|
|
await comment.removeWithReplies(comment);
|
|
|
await Page.updateCommentCount(comment.page);
|
|
await Page.updateCommentCount(comment.page);
|
|
|
- commentEvent.emit('delete', comment);
|
|
|
|
|
|
|
+ commentEvent.emit(CommentEvent.DELETE, comment);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
return res.json(ApiResponse.error(err));
|
|
return res.json(ApiResponse.error(err));
|