|
|
@@ -1,5 +1,4 @@
|
|
|
import express, { Request, Router } from 'express';
|
|
|
-import { pagePathUtils } from '@growi/core';
|
|
|
import { query, oneOf } from 'express-validator';
|
|
|
|
|
|
import { PageDocument, PageModel } from '../../models/page';
|
|
|
@@ -8,8 +7,6 @@ import loggerFactory from '../../../utils/logger';
|
|
|
import Crowi from '../../crowi';
|
|
|
import { ApiV3Response } from './interfaces/apiv3-response';
|
|
|
|
|
|
-const { isTopPage } = pagePathUtils;
|
|
|
-
|
|
|
const logger = loggerFactory('growi:routes:apiv3:page-tree');
|
|
|
|
|
|
/*
|
|
|
@@ -23,8 +20,7 @@ interface AuthorizedRequest extends Request {
|
|
|
* Validators
|
|
|
*/
|
|
|
const validator = {
|
|
|
- pageIdAndPathRequired: [
|
|
|
- query('id').isMongoId().withMessage('id is required'),
|
|
|
+ pagePathRequired: [
|
|
|
query('path').isString().withMessage('path is required'),
|
|
|
],
|
|
|
pageIdOrPathRequired: oneOf([
|
|
|
@@ -46,56 +42,14 @@ export default (crowi: Crowi): Router => {
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
- router.get('/siblings', accessTokenParser, loginRequiredStrictly, ...validator.pageIdAndPathRequired, apiV3FormValidator, async(req: AuthorizedRequest, res: ApiV3Response): Promise<any> => {
|
|
|
- const { id, path } = req.query;
|
|
|
-
|
|
|
- const Page: PageModel = crowi.model('Page');
|
|
|
-
|
|
|
- let siblings: PageDocument[];
|
|
|
- let target: PageDocument;
|
|
|
- try {
|
|
|
- siblings = await Page.findSiblingsByPathAndViewer(path as string, req.user);
|
|
|
-
|
|
|
- target = siblings.filter(page => page._id.toString() === id)?.[0];
|
|
|
- if (target == null) {
|
|
|
- throw Error('Target must exist.');
|
|
|
- }
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- logger.error('Error occurred while finding pages.', err);
|
|
|
- return res.apiv3Err(new ErrorV3('Error occurred while finding pages.'));
|
|
|
- }
|
|
|
-
|
|
|
- if (isTopPage(path as string)) {
|
|
|
- siblings = siblings.filter(page => !isTopPage(page.path));
|
|
|
- }
|
|
|
-
|
|
|
- return res.apiv3({ target, siblings });
|
|
|
- });
|
|
|
-
|
|
|
- /*
|
|
|
- * In most cases, using path should be prioritized
|
|
|
- */
|
|
|
- // eslint-disable-next-line max-len
|
|
|
- router.get('/ancestors', accessTokenParser, loginRequiredStrictly, validator.pageIdOrPathRequired, apiV3FormValidator, async(req: AuthorizedRequest, res: ApiV3Response): Promise<any> => {
|
|
|
- const { id, path } = req.query;
|
|
|
+ router.get('/ancestors-children', accessTokenParser, loginRequiredStrictly, ...validator.pagePathRequired, apiV3FormValidator, async(req: AuthorizedRequest, res: ApiV3Response): Promise<any> => {
|
|
|
+ const { path } = req.query;
|
|
|
|
|
|
const Page: PageModel = crowi.model('Page');
|
|
|
|
|
|
- let ancestors: PageDocument[];
|
|
|
- try {
|
|
|
- ancestors = await Page.findAncestorsByPathOrId((path || id) as string);
|
|
|
-
|
|
|
- if (ancestors.length === 0 && !isTopPage(path as string)) {
|
|
|
- throw Error('Ancestors must have at least one page.');
|
|
|
- }
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- logger.error('Error occurred while finding pages.', err);
|
|
|
- return res.apiv3Err(new ErrorV3('Error occurred while finding pages.'));
|
|
|
- }
|
|
|
+ const ancestorsChildren: Record<string, PageDocument[]> = await Page.findAncestorsChildrenByPathAndViewer(path as string, req.user);
|
|
|
|
|
|
- return res.apiv3({ ancestors });
|
|
|
+ return res.apiv3({ ancestorsChildren });
|
|
|
});
|
|
|
|
|
|
/*
|