Răsfoiți Sursa

refactor comment model

Yuki Takei 2 ani în urmă
părinte
comite
e60932797f

+ 36 - 34
apps/app/src/features/comment/server/models/comment.ts

@@ -1,6 +1,6 @@
 import type { IUser } from '@growi/core/dist/interfaces';
 import {
-  Types, Document, Model, Schema,
+  Types, Document, Model, Schema, Query,
 } from 'mongoose';
 
 import { IComment } from '~/interfaces/comment';
@@ -14,25 +14,28 @@ export interface CommentDocument extends IComment, Document {
   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<CommentDocument>
-
-  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[]>
+type Add = (
+  pageId: Types.ObjectId,
+  creatorId: Types.ObjectId,
+  revisionId: Types.ObjectId,
+  comment: string,
+  commentPosition: number,
+  replyTo?: Types.ObjectId | null,
+) => Promise<CommentDocument>;
+type FindCommentsByPageId = (pageId: Types.ObjectId) => Query<CommentDocument[], CommentDocument>;
+type FindCommentsByRevisionId = (revisionId: Types.ObjectId) => Query<CommentDocument[], CommentDocument>;
+type FindCreatorsByPage = (pageId: Types.ObjectId) => Promise<IUser[]>
+type GetPageIdToCommentMap = (pageIds: Types.ObjectId[]) => Promise<Record<string, CommentDocument[]>>
+type CountCommentByPageId = (pageId: Types.ObjectId) => Promise<number>
 
-  countCommentByPageId: (pageId: Types.ObjectId) => Promise<number>
+export interface CommentModel extends Model<CommentDocument> {
+  add: Add
+  findCommentsByPageId: FindCommentsByPageId
+  findCommentsByRevisionId: FindCommentsByRevisionId
+  findCreatorsByPage: FindCreatorsByPage
+  getPageIdToCommentMap: GetPageIdToCommentMap
+  countCommentByPageId: CountCommentByPageId
 }
 
 const commentSchema = new Schema<CommentDocument, CommentModel>({
@@ -46,16 +49,15 @@ const commentSchema = new Schema<CommentDocument, CommentModel>({
   timestamps: true,
 });
 
-
-commentSchema.statics.add = async function(
-    pageId: Types.ObjectId,
-    creatorId: Types.ObjectId,
-    revisionId: Types.ObjectId,
-    comment: string,
-    commentPosition: number,
-    replyTo?: string,
-) {
-
+const add: Add = async function(
+    this: CommentModel,
+    pageId,
+    creatorId,
+    revisionId,
+    comment,
+    commentPosition,
+    replyTo?,
+): Promise<CommentDocument> {
   try {
     const data = await this.create({
       page: pageId.toString(),
@@ -74,15 +76,19 @@ commentSchema.statics.add = async function(
     throw err;
   }
 };
+commentSchema.statics.add = add;
 
-commentSchema.statics.getCommentsByPageId = function(id) {
+commentSchema.statics.findCommentsByPageId = function(id) {
   return this.find({ page: id }).sort({ createdAt: -1 });
 };
 
-commentSchema.statics.getCommentsByRevisionId = function(id) {
+commentSchema.statics.findCommentsByRevisionId = function(id) {
   return this.find({ revision: id }).sort({ createdAt: -1 });
 };
 
+commentSchema.statics.findCreatorsByPage = async function(page) {
+  return this.distinct('creator', { page }).exec();
+};
 
 /**
  * @return {object} key: page._id, value: comments
@@ -101,10 +107,6 @@ commentSchema.statics.getPageIdToCommentMap = async function(pageIds) {
   return idToCommentMap;
 };
 
-commentSchema.statics.findCreatorsByPage = async function(page) {
-  return this.distinct('creator', { page }).exec();
-};
-
 commentSchema.statics.countCommentByPageId = async function(page) {
   return this.count({ page });
 };

+ 0 - 1
apps/app/src/server/models/obsolete-page.js

@@ -702,7 +702,6 @@ export const getPageSchema = (crowi) => {
   };
 
   pageSchema.methods.getNotificationTargetUsers = async function() {
-    const Comment = mongoose.model('Comment');
     const Revision = mongoose.model('Revision');
 
     const [commentCreators, revisionAuthors] = await Promise.all([Comment.findCreatorsByPage(this), Revision.findAuthorsByPage(this)]);

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

@@ -124,21 +124,21 @@ module.exports = function(crowi, app) {
       return res.json(ApiResponse.error('Current user is not accessible to this page.'));
     }
 
-    let fetcher = null;
+    let query = null;
 
     try {
       if (revisionId) {
-        fetcher = Comment.getCommentsByRevisionId(revisionId);
+        query = Comment.findCommentsByRevisionId(revisionId);
       }
       else {
-        fetcher = Comment.getCommentsByPageId(pageId);
+        query = Comment.findCommentsByPageId(pageId);
       }
     }
     catch (err) {
       return res.json(ApiResponse.error(err));
     }
 
-    const comments = await fetcher.populate('creator');
+    const comments = await query.populate('creator');
     comments.forEach((comment) => {
       if (comment.creator != null && comment.creator instanceof User) {
         comment.creator = serializeUserSecurely(comment.creator);

+ 1 - 1
apps/app/src/server/service/search-delegator/elasticsearch.ts

@@ -5,6 +5,7 @@ import gc from 'expose-gc/function';
 import mongoose from 'mongoose';
 import streamToPromise from 'stream-to-promise';
 
+import { Comment } from '~/features/comment/server';
 import { SearchDelegatorName } from '~/interfaces/named-query';
 import {
   ISearchResult, ISearchResultData, SORT_AXIS, SORT_ORDER,
@@ -458,7 +459,6 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
     const Page = mongoose.model('Page') as unknown as PageModel;
     const { PageQueryBuilder } = Page;
     const Bookmark = mongoose.model('Bookmark') as any; // TODO: typescriptize model
-    const Comment = mongoose.model('Comment') as any; // TODO: typescriptize model
     const PageTagRelation = mongoose.model('PageTagRelation') as any; // TODO: typescriptize model
 
     const socket = shouldEmitProgress ? this.socketIoService.getAdminSocket() : null;