瀏覽代碼

Improved code

Taichi Masuyama 4 年之前
父節點
當前提交
751fe00eb7
共有 1 個文件被更改,包括 23 次插入13 次删除
  1. 23 13
      packages/app/src/server/models/page.ts

+ 23 - 13
packages/app/src/server/models/page.ts

@@ -369,8 +369,8 @@ export default (crowi: Crowi): any => {
     }
     }
 
 
     const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
     const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
+    // v4 compatible process
     if (!isV5Compatible) {
     if (!isV5Compatible) {
-      // v4 compatible process
       return this.createV4(path, body, user, options);
       return this.createV4(path, body, user, options);
     }
     }
 
 
@@ -379,28 +379,27 @@ export default (crowi: Crowi): any => {
     const {
     const {
       format = 'markdown', redirectTo, grantedUserIds = [user._id], grantUserGroupId,
       format = 'markdown', redirectTo, grantedUserIds = [user._id], grantUserGroupId,
     } = options;
     } = options;
+    let grant = options.grant;
 
 
     // sanitize path
     // sanitize path
     path = crowi.xss.process(path); // eslint-disable-line no-param-reassign
     path = crowi.xss.process(path); // eslint-disable-line no-param-reassign
-
-    let grant = options.grant;
-    // force public
-    if (isTopPage(path)) {
-      grant = GRANT_PUBLIC;
-    }
-
+    // throw if exists
     const isExist = (await this.count({ path, isEmpty: false })) > 0; // not validate empty page
     const isExist = (await this.count({ path, isEmpty: false })) > 0; // not validate empty page
     if (isExist) {
     if (isExist) {
       throw new Error('Cannot create new page to existed path');
       throw new Error('Cannot create new page to existed path');
     }
     }
+    // force public
+    if (isTopPage(path)) {
+      grant = GRANT_PUBLIC;
+    }
 
 
-    // find existing empty page at target path
+    // find an existing empty page
     const emptyPage = await Page.findOne({ path, isEmpty: true });
     const emptyPage = await Page.findOne({ path, isEmpty: true });
 
 
+    /*
+     * UserGroup & Owner validation
+     */
     if (grant !== GRANT_RESTRICTED) {
     if (grant !== GRANT_RESTRICTED) {
-      /*
-       * UserGroup & Owner validation
-       */
       let isGrantNormalized = false;
       let isGrantNormalized = false;
       try {
       try {
         // It must check descendants as well if emptyTarget is not null
         // It must check descendants as well if emptyTarget is not null
@@ -441,11 +440,22 @@ export default (crowi: Crowi): any => {
     page.lastUpdateUser = user;
     page.lastUpdateUser = user;
     page.redirectTo = redirectTo;
     page.redirectTo = redirectTo;
     page.status = STATUS_PUBLISHED;
     page.status = STATUS_PUBLISHED;
-    page.parent = options.grant === GRANT_RESTRICTED ? null : parentId;
+
+    // set parent to null when GRANT_RESTRICTED
+    if (grant === GRANT_RESTRICTED) {
+      page.parent = null;
+    }
+    else {
+      page.parent = parentId;
+    }
 
 
     page.applyScope(user, grant, grantUserGroupId);
     page.applyScope(user, grant, grantUserGroupId);
 
 
     let savedPage = await page.save();
     let savedPage = await page.save();
+
+    /*
+     * After save
+     */
     const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });
     const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });
     const revision = await pushRevision(savedPage, newRevision, user);
     const revision = await pushRevision(savedPage, newRevision, user);
     savedPage = await this.findByPath(revision.path);
     savedPage = await this.findByPath(revision.path);