Yuki Takei 1 год назад
Родитель
Сommit
8159da17f5
2 измененных файлов с 28 добавлено и 19 удалено
  1. 14 14
      apps/app/src/server/models/page.ts
  2. 14 5
      apps/app/src/server/models/revision.ts

+ 14 - 14
apps/app/src/server/models/page.ts

@@ -11,10 +11,11 @@ import { getIdForRef, isPopulated } from '@growi/core/dist/interfaces';
 import { isTopPage, hasSlash } from '@growi/core/dist/utils/page-path-utils';
 import { isTopPage, hasSlash } from '@growi/core/dist/utils/page-path-utils';
 import { addTrailingSlash, normalizePath } from '@growi/core/dist/utils/path-utils';
 import { addTrailingSlash, normalizePath } from '@growi/core/dist/utils/path-utils';
 import escapeStringRegexp from 'escape-string-regexp';
 import escapeStringRegexp from 'escape-string-regexp';
-import type { Model, Document, AnyObject } from 'mongoose';
-import mongoose, {
-  Schema,
+import type {
+  Model, Document, AnyObject,
+  Types,
 } from 'mongoose';
 } from 'mongoose';
+import mongoose, { Schema } from 'mongoose';
 import mongoosePaginate from 'mongoose-paginate-v2';
 import mongoosePaginate from 'mongoose-paginate-v2';
 import uniqueValidator from 'mongoose-unique-validator';
 import uniqueValidator from 'mongoose-unique-validator';
 
 
@@ -44,7 +45,7 @@ const PAGE_GRANT_ERROR = 1;
 const STATUS_PUBLISHED = 'published';
 const STATUS_PUBLISHED = 'published';
 const STATUS_DELETED = 'deleted';
 const STATUS_DELETED = 'deleted';
 
 
-export interface PageDocument extends IPage, Document {
+export interface PageDocument extends IPage, Document<Types.ObjectId> {
   [x:string]: any // for obsolete methods
   [x:string]: any // for obsolete methods
   getLatestRevisionBodyLength(): Promise<number | null | undefined>
   getLatestRevisionBodyLength(): Promise<number | null | undefined>
   calculateAndUpdateLatestRevisionBodyLength(this: PageDocument): Promise<void>
   calculateAndUpdateLatestRevisionBodyLength(this: PageDocument): Promise<void>
@@ -101,22 +102,21 @@ export interface PageModel extends Model<PageDocument> {
   STATUS_DELETED
   STATUS_DELETED
 }
 }
 
 
-const ObjectId = mongoose.Schema.Types.ObjectId;
 
 
 const schema = new Schema<PageDocument, PageModel>({
 const schema = new Schema<PageDocument, PageModel>({
   parent: {
   parent: {
-    type: ObjectId, ref: 'Page', index: true, default: null,
+    type: Schema.Types.ObjectId, ref: 'Page', index: true, default: null,
   },
   },
   descendantCount: { type: Number, default: 0 },
   descendantCount: { type: Number, default: 0 },
   isEmpty: { type: Boolean, default: false },
   isEmpty: { type: Boolean, default: false },
   path: {
   path: {
     type: String, required: true, index: true,
     type: String, required: true, index: true,
   },
   },
-  revision: { type: ObjectId, ref: 'Revision' },
+  revision: { type: Schema.Types.ObjectId, ref: 'Revision' },
   latestRevisionBodyLength: { type: Number },
   latestRevisionBodyLength: { type: Number },
   status: { type: String, default: STATUS_PUBLISHED, index: true },
   status: { type: String, default: STATUS_PUBLISHED, index: true },
   grant: { type: Number, default: GRANT_PUBLIC, index: true },
   grant: { type: Number, default: GRANT_PUBLIC, index: true },
-  grantedUsers: [{ type: ObjectId, ref: 'User' }],
+  grantedUsers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
   grantedGroups: {
   grantedGroups: {
     type: [{
     type: [{
       type: {
       type: {
@@ -126,7 +126,7 @@ const schema = new Schema<PageDocument, PageModel>({
         default: 'UserGroup',
         default: 'UserGroup',
       },
       },
       item: {
       item: {
-        type: ObjectId,
+        type: Schema.Types.ObjectId,
         refPath: 'grantedGroups.type',
         refPath: 'grantedGroups.type',
         required: true,
         required: true,
         index: true,
         index: true,
@@ -140,16 +140,16 @@ const schema = new Schema<PageDocument, PageModel>({
     default: [],
     default: [],
     required: true,
     required: true,
   },
   },
-  creator: { type: ObjectId, ref: 'User', index: true },
-  lastUpdateUser: { type: ObjectId, ref: 'User' },
-  liker: [{ type: ObjectId, ref: 'User' }],
-  seenUsers: [{ type: ObjectId, ref: 'User' }],
+  creator: { type: Schema.Types.ObjectId, ref: 'User', index: true },
+  lastUpdateUser: { type: Schema.Types.ObjectId, ref: 'User' },
+  liker: [{ type: Schema.Types.ObjectId, ref: 'User' }],
+  seenUsers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
   commentCount: { type: Number, default: 0 },
   commentCount: { type: Number, default: 0 },
   expandContentWidth: { type: Boolean },
   expandContentWidth: { type: Boolean },
   wip: { type: Boolean },
   wip: { type: Boolean },
   ttlTimestamp: { type: Date },
   ttlTimestamp: { type: Date },
   updatedAt: { type: Date, default: Date.now }, // Do not use timetamps for updatedAt because it breaks 'updateMetadata: false' option
   updatedAt: { type: Date, default: Date.now }, // Do not use timetamps for updatedAt because it breaks 'updateMetadata: false' option
-  deleteUser: { type: ObjectId, ref: 'User' },
+  deleteUser: { type: Schema.Types.ObjectId, ref: 'User' },
   deletedAt: { type: Date },
   deletedAt: { type: Date },
 }, {
 }, {
   timestamps: { createdAt: true, updatedAt: false },
   timestamps: { createdAt: true, updatedAt: false },

+ 14 - 5
apps/app/src/server/models/revision.ts

@@ -4,8 +4,9 @@ import type {
   Origin,
   Origin,
 } from '@growi/core';
 } from '@growi/core';
 import { allOrigin } from '@growi/core';
 import { allOrigin } from '@growi/core';
+import type { Types } from 'mongoose';
 import {
 import {
-  Schema, Types, type Document, type Model,
+  Schema, type Document, type Model,
 } from 'mongoose';
 } from 'mongoose';
 import mongoosePaginate from 'mongoose-paginate-v2';
 import mongoosePaginate from 'mongoose-paginate-v2';
 
 
@@ -17,10 +18,11 @@ import type { PageDocument } from './page';
 
 
 const logger = loggerFactory('growi:models:revision');
 const logger = loggerFactory('growi:models:revision');
 
 
+
 export interface IRevisionDocument extends IRevision, Document {
 export interface IRevisionDocument extends IRevision, Document {
 }
 }
 
 
-type UpdateRevisionListByPageId = (pageId: string, updateData: Partial<IRevision>) => Promise<void>;
+type UpdateRevisionListByPageId = (pageId: Types.ObjectId, updateData: Partial<IRevision>) => Promise<void>;
 type PrepareRevision = (
 type PrepareRevision = (
   pageData: PageDocument, body: string, previousBody: string | null, user: HasObjectId, origin?: Origin, options?: { format: string }
   pageData: PageDocument, body: string, previousBody: string | null, user: HasObjectId, origin?: Origin, options?: { format: string }
 ) => IRevisionDocument;
 ) => IRevisionDocument;
@@ -58,17 +60,24 @@ const revisionSchema = new Schema<IRevisionDocument, IRevisionModel>({
 revisionSchema.plugin(mongoosePaginate);
 revisionSchema.plugin(mongoosePaginate);
 
 
 const updateRevisionListByPageId: UpdateRevisionListByPageId = async function(this: IRevisionModel, pageId, updateData) {
 const updateRevisionListByPageId: UpdateRevisionListByPageId = async function(this: IRevisionModel, pageId, updateData) {
+  // Check pageId for safety
+  if (pageId == null) {
+    throw new Error('Error: pageId is required');
+  }
   await this.updateMany({ pageId }, { $set: updateData });
   await this.updateMany({ pageId }, { $set: updateData });
 };
 };
 revisionSchema.statics.updateRevisionListByPageId = updateRevisionListByPageId;
 revisionSchema.statics.updateRevisionListByPageId = updateRevisionListByPageId;
 
 
 const prepareRevision: PrepareRevision = function(this: IRevisionModel, pageData, body, previousBody, user, origin, options = { format: 'markdown' }) {
 const prepareRevision: PrepareRevision = function(this: IRevisionModel, pageData, body, previousBody, user, origin, options = { format: 'markdown' }) {
-  if (!user._id) {
-    throw new Error('Error: user should have _id');
+  if (user._id == null) {
+    throw new Error('user should have _id');
+  }
+  if (pageData._id == null) {
+    throw new Error('pageData should have _id');
   }
   }
 
 
   const newRevision = new this();
   const newRevision = new this();
-  newRevision.pageId = pageData._id;
+  newRevision.pageId = pageData._id.toString();
   newRevision.body = body;
   newRevision.body = body;
   newRevision.format = options.format;
   newRevision.format = options.format;
   newRevision.author = user._id;
   newRevision.author = user._id;