Yuki Takei 2 лет назад
Родитель
Сommit
65017b7284

+ 8 - 11
apps/app/src/components/PageComment/Comment.tsx

@@ -52,7 +52,6 @@ export const Comment = (props: CommentProps): JSX.Element => {
 
 
   const commentId = comment._id;
   const commentId = comment._id;
   const creator = comment.creator;
   const creator = comment.creator;
-  const isMarkdown = comment.isMarkdown;
   const createdAt = new Date(comment.createdAt);
   const createdAt = new Date(comment.createdAt);
   const updatedAt = new Date(comment.updatedAt);
   const updatedAt = new Date(comment.updatedAt);
   const isEdited = createdAt < updatedAt;
   const isEdited = createdAt < updatedAt;
@@ -122,16 +121,14 @@ export const Comment = (props: CommentProps): JSX.Element => {
       return <></>;
       return <></>;
     }
     }
 
 
-    return isMarkdown
-      ? (
-        <RevisionRenderer
-          rendererOptions={rendererOptions}
-          markdown={markdown}
-          additionalClassName="comment"
-        />
-      )
-      : renderText(comment.comment);
-  }, [comment, isMarkdown, markdown, rendererOptions]);
+    return (
+      <RevisionRenderer
+        rendererOptions={rendererOptions}
+        markdown={markdown}
+        additionalClassName="comment"
+      />
+    );
+  }, [markdown, rendererOptions]);
 
 
   const rootClassName = getRootClassName(comment);
   const rootClassName = getRootClassName(comment);
   const revHref = `?revisionId=${comment.revision}`;
   const revHref = `?revisionId=${comment.revision}`;

+ 114 - 109
apps/app/src/features/comment/server/models/comment.ts

@@ -1,122 +1,127 @@
-module.exports = function(crowi) {
-  const debug = require('debug')('growi:models:comment');
-  const mongoose = require('mongoose');
-  const ObjectId = mongoose.Schema.Types.ObjectId;
-  const commentEvent = crowi.event('comment');
-
-  const commentSchema = new mongoose.Schema({
-    page: { type: ObjectId, ref: 'Page', index: true },
-    creator: { type: ObjectId, ref: 'User', index: true },
-    revision: { type: ObjectId, ref: 'Revision', index: true },
-    comment: { type: String, required: true },
-    commentPosition: { type: Number, default: -1 },
-    isMarkdown: { type: Boolean, default: false },
-    replyTo: { type: ObjectId },
-  }, {
-    timestamps: true,
-  });
-
-  commentSchema.statics.create = function(pageId, creatorId, revisionId, comment, position, isMarkdown, replyTo) {
-    const Comment = this;
-
-    return new Promise(((resolve, reject) => {
-      const newComment = new Comment();
-
-      newComment.page = pageId;
-      newComment.creator = creatorId;
-      newComment.revision = revisionId;
-      newComment.comment = comment;
-      newComment.commentPosition = position;
-      newComment.isMarkdown = isMarkdown || false;
-      newComment.replyTo = replyTo;
-
-      newComment.save((err, data) => {
-        if (err) {
-          debug('Error on saving comment.', err);
-          return reject(err);
-        }
-        debug('Comment saved.', data);
-        return resolve(data);
-      });
-    }));
-  };
-
-  commentSchema.statics.getCommentsByPageId = function(id) {
-    return this.find({ page: id }).sort({ createdAt: -1 });
-  };
-
-  commentSchema.statics.getCommentsByRevisionId = function(id) {
-    return this.find({ revision: id }).sort({ createdAt: -1 });
-  };
-
-
-  /**
-   * @return {object} key: page._id, value: comments
-   */
-  commentSchema.statics.getPageIdToCommentMap = async function(pageIds) {
-    const results = await this.aggregate()
-      .match({ page: { $in: pageIds } })
-      .group({ _id: '$page', comments: { $push: '$comment' } });
-
-    // convert to map
-    const idToCommentMap = {};
-    results.forEach((result, i) => {
-      idToCommentMap[result._id] = result.comments;
+import type { IUser } from '@growi/core/dist/interfaces';
+import {
+  Types, Document, Model, Schema,
+} from 'mongoose';
+
+import { IComment } from '~/interfaces/comment';
+import { getOrCreateModel } from '~/server/util/mongoose-utils';
+import loggerFactory from '~/utils/logger';
+
+const logger = loggerFactory('growi:models:comment');
+
+export interface CommentDocument extends IComment, Document {
+  removeWithReplies: () => Promise<void>
+  findCreatorsByPage: (pageId: Types.ObjectId) => Promise<CommentDocument[]>
+}
+
+export interface CommentModel extends Model<CommentDocument> {
+  add: (
+    pageId: Types.ObjectId,
+    creatorId: Types.ObjectId,
+    revisionId: Types.ObjectId,
+    comment: string,
+    commentPosition: number,
+    replyTo?: Types.ObjectId | null,
+  ) => Promise<void>
+
+  getCommentsByPageId: (pageId: Types.ObjectId) => Promise<CommentDocument[]>
+
+  getCommentsByRevisionId: (revisionId: Types.ObjectId) => Promise<CommentDocument[]>
+
+  getPageIdToCommentMap: (pageIds: Types.ObjectId[]) => Promise<Record<string, CommentDocument[]>>
+
+  findCreatorsByPage: (pageId: Types.ObjectId) => Promise<IUser[]>
+
+  countCommentByPageId: (pageId: Types.ObjectId) => Promise<number>
+}
+
+const commentSchema = new Schema<CommentDocument, CommentModel>({
+  page: { type: Schema.Types.ObjectId, ref: 'Page', index: true },
+  creator: { type: Schema.Types.ObjectId, ref: 'User', index: true },
+  revision: { type: Schema.Types.ObjectId, ref: 'Revision', index: true },
+  comment: { type: String, required: true },
+  commentPosition: { type: Number, default: -1 },
+  replyTo: { type: Schema.Types.ObjectId },
+}, {
+  timestamps: true,
+});
+
+/**
+ * post remove hook
+ */
+commentSchema.post('remove', async(savedComment) => {
+  await commentEvent.emit('delete', savedComment);
+});
+
+
+commentSchema.statics.add = async function(
+    pageId: Types.ObjectId,
+    creatorId: Types.ObjectId,
+    revisionId: Types.ObjectId,
+    comment: string,
+    commentPosition: number,
+    replyTo?: string,
+) {
+
+  try {
+    const data = await this.create({
+      page: pageId.toString(),
+      creator: creatorId.toString(),
+      revision: revisionId.toString(),
+      comment,
+      commentPosition,
+      replyTo,
     });
     });
+    logger.debug('Comment saved.', data);
+  }
+  catch (err) {
+    logger.debug('Error on saving comment.', err);
+    throw err;
+  }
+};
 
 
-    return idToCommentMap;
-  };
-
-  commentSchema.statics.countCommentByPageId = function(page) {
-    const self = this;
-
-    return new Promise(((resolve, reject) => {
-      self.count({ page }, (err, data) => {
-        if (err) {
-          return reject(err);
-        }
-
-        return resolve(data);
-      });
-    }));
-  };
-
-  commentSchema.statics.updateCommentsByPageId = async function(comment, isMarkdown, commentId) {
-    const Comment = this;
-
-    const commentData = await Comment.findOneAndUpdate(
-      { _id: commentId },
-      { $set: { comment, isMarkdown } },
-    );
+commentSchema.statics.getCommentsByPageId = function(id) {
+  return this.find({ page: id }).sort({ createdAt: -1 });
+};
 
 
-    await commentEvent.emit('update', commentData);
+commentSchema.statics.getCommentsByRevisionId = function(id) {
+  return this.find({ revision: id }).sort({ createdAt: -1 });
+};
 
 
-    return commentData;
-  };
 
 
+/**
+ * @return {object} key: page._id, value: comments
+ */
+commentSchema.statics.getPageIdToCommentMap = async function(pageIds) {
+  const results = await this.aggregate()
+    .match({ page: { $in: pageIds } })
+    .group({ _id: '$page', comments: { $push: '$comment' } });
 
 
-  /**
-   * post remove hook
-   */
-  commentSchema.post('reomove', async(savedComment) => {
-    await commentEvent.emit('delete', savedComment);
+  // convert to map
+  const idToCommentMap = {};
+  results.forEach((result, i) => {
+    idToCommentMap[result._id] = result.comments;
   });
   });
 
 
-  commentSchema.methods.removeWithReplies = async function(comment) {
-    const Comment = crowi.model('Comment');
+  return idToCommentMap;
+};
 
 
-    await Comment.remove({
-      $or: (
-        [{ replyTo: this._id }, { _id: this._id }]),
-    });
+commentSchema.statics.findCreatorsByPage = async function(page) {
+  return this.distinct('creator', { page }).exec();
+};
 
 
-    await commentEvent.emit('delete', comment);
-    return;
-  };
+commentSchema.statics.countCommentByPageId = async function(page) {
+  return this.count({ page });
+};
 
 
-  commentSchema.statics.findCreatorsByPage = async function(page) {
-    return this.distinct('creator', { page }).exec();
-  };
+commentSchema.methods.removeWithReplies = async function(comment) {
+  await this.remove({
+    $or: (
+      [{ replyTo: this._id }, { _id: this._id }]),
+  });
 
 
-  return mongoose.model('Comment', commentSchema);
+  await commentEvent.emit('delete', comment);
+  return;
 };
 };
+
+export default getOrCreateModel<CommentDocument, CommentModel>('Comment', commentSchema);

+ 5 - 6
apps/app/src/interfaces/comment.ts

@@ -1,18 +1,17 @@
 import type {
 import type {
-  Nullable, Ref, HasObjectId,
+  Ref, HasObjectId,
   IPage, IRevision, IUser,
   IPage, IRevision, IUser,
 } from '@growi/core';
 } from '@growi/core';
 
 
 export type IComment = {
 export type IComment = {
+  page: Ref<IPage>,
+  creator: Ref<IUser>,
+  revision: Ref<IRevision>,
   comment: string;
   comment: string;
   commentPosition: number,
   commentPosition: number,
-  isMarkdown: boolean,
-  replyTo: Nullable<string>,
+  replyTo?: string,
   createdAt: Date,
   createdAt: Date,
   updatedAt: Date,
   updatedAt: Date,
-  page: Ref<IPage>,
-  revision: Ref<IRevision>,
-  creator: IUser,
 };
 };
 
 
 export interface ICommentPostArgs {
 export interface ICommentPostArgs {

+ 2 - 4
apps/app/src/server/routes/comment.js

@@ -233,7 +233,6 @@ 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');
     const commentEvent = crowi.event('comment');
 
 
@@ -245,7 +244,7 @@ module.exports = function(crowi, app) {
 
 
     let createdComment;
     let createdComment;
     try {
     try {
-      createdComment = await Comment.create(pageId, req.user._id, revisionId, comment, position, isMarkdown, replyTo);
+      createdComment = await Comment.create(pageId, req.user._id, revisionId, comment, position, replyTo);
       commentEvent.emit('create', createdComment);
       commentEvent.emit('create', createdComment);
     }
     }
     catch (err) {
     catch (err) {
@@ -355,7 +354,6 @@ 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;
 
 
@@ -389,7 +387,7 @@ 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('update', updatedComment);
     }
     }

+ 0 - 2
apps/app/test/integration/service/v5.public-page.test.ts

@@ -739,7 +739,6 @@ describe('PageService page operations with only public pages', () => {
     await Comment.insertMany([
     await Comment.insertMany([
       {
       {
         commentPosition: -1,
         commentPosition: -1,
-        isMarkdown: true,
         page: pageIdForDuplicate11,
         page: pageIdForDuplicate11,
         creator: dummyUser1._id,
         creator: dummyUser1._id,
         revision: revisionIdForDuplicate10,
         revision: revisionIdForDuplicate10,
@@ -982,7 +981,6 @@ describe('PageService page operations with only public pages', () => {
     await Comment.insertMany([
     await Comment.insertMany([
       {
       {
         commentPosition: -1,
         commentPosition: -1,
-        isMarkdown: true,
         page: pageIdForDeleteCompletely2,
         page: pageIdForDeleteCompletely2,
         creator: dummyUser1._id,
         creator: dummyUser1._id,
         revision: revisionIdForDeleteCompletely4,
         revision: revisionIdForDeleteCompletely4,