page.ts 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. import nodePath from 'path';
  3. import { getOrCreateModel, pagePathUtils, pathUtils } from '@growi/core';
  4. import escapeStringRegexp from 'escape-string-regexp';
  5. import mongoose, {
  6. Schema, Model, Document, AnyObject,
  7. } from 'mongoose';
  8. import mongoosePaginate from 'mongoose-paginate-v2';
  9. import uniqueValidator from 'mongoose-unique-validator';
  10. import { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
  11. import { IPage } from '../../interfaces/page';
  12. import loggerFactory from '../../utils/logger';
  13. import Crowi from '../crowi';
  14. import { getPageSchema, extractToAncestorsPaths, populateDataToShowRevision } from './obsolete-page';
  15. import { PageRedirectModel } from './page-redirect';
  16. const { addTrailingSlash } = pathUtils;
  17. const { isTopPage, collectAncestorPaths } = pagePathUtils;
  18. const logger = loggerFactory('growi:models:page');
  19. /*
  20. * define schema
  21. */
  22. const GRANT_PUBLIC = 1;
  23. const GRANT_RESTRICTED = 2;
  24. const GRANT_SPECIFIED = 3; // DEPRECATED
  25. const GRANT_OWNER = 4;
  26. const GRANT_USER_GROUP = 5;
  27. const PAGE_GRANT_ERROR = 1;
  28. const STATUS_PUBLISHED = 'published';
  29. const STATUS_DELETED = 'deleted';
  30. export interface PageDocument extends IPage, Document {}
  31. type TargetAndAncestorsResult = {
  32. targetAndAncestors: PageDocument[]
  33. rootPage: PageDocument
  34. }
  35. export type CreateMethod = (path: string, body: string, user, options) => Promise<PageDocument & { _id: any }>
  36. export interface PageModel extends Model<PageDocument> {
  37. [x: string]: any; // for obsolete methods
  38. createEmptyPagesByPaths(paths: string[], user: any | null, onlyMigratedAsExistingPages?: boolean, andFilter?): Promise<void>
  39. getParentAndFillAncestors(path: string, user): Promise<PageDocument & { _id: any }>
  40. findByIdsAndViewer(pageIds: ObjectIdLike[], user, userGroups?, includeEmpty?: boolean): Promise<PageDocument[]>
  41. findByPathAndViewer(path: string | null, user, userGroups?, useFindOne?: boolean, includeEmpty?: boolean): Promise<PageDocument[]>
  42. findTargetAndAncestorsByPathOrId(pathOrId: string): Promise<TargetAndAncestorsResult>
  43. findChildrenByParentPathOrIdAndViewer(parentPathOrId: string, user, userGroups?): Promise<PageDocument[]>
  44. findAncestorsChildrenByPathAndViewer(path: string, user, userGroups?): Promise<Record<string, PageDocument[]>>
  45. generateGrantCondition(
  46. user, userGroups, showAnyoneKnowsLink?: boolean, showPagesRestrictedByOwner?: boolean, showPagesRestrictedByGroup?: boolean,
  47. ): { $or: any[] }
  48. PageQueryBuilder: typeof PageQueryBuilder
  49. GRANT_PUBLIC
  50. GRANT_RESTRICTED
  51. GRANT_SPECIFIED
  52. GRANT_OWNER
  53. GRANT_USER_GROUP
  54. PAGE_GRANT_ERROR
  55. STATUS_PUBLISHED
  56. STATUS_DELETED
  57. }
  58. type IObjectId = mongoose.Types.ObjectId;
  59. const ObjectId = mongoose.Schema.Types.ObjectId;
  60. const schema = new Schema<PageDocument, PageModel>({
  61. parent: {
  62. type: ObjectId, ref: 'Page', index: true, default: null,
  63. },
  64. descendantCount: { type: Number, default: 0 },
  65. isEmpty: { type: Boolean, default: false },
  66. path: {
  67. type: String, required: true, index: true,
  68. },
  69. revision: { type: ObjectId, ref: 'Revision' },
  70. status: { type: String, default: STATUS_PUBLISHED, index: true },
  71. grant: { type: Number, default: GRANT_PUBLIC, index: true },
  72. grantedUsers: [{ type: ObjectId, ref: 'User' }],
  73. grantedGroup: { type: ObjectId, ref: 'UserGroup', index: true },
  74. creator: { type: ObjectId, ref: 'User', index: true },
  75. lastUpdateUser: { type: ObjectId, ref: 'User' },
  76. liker: [{ type: ObjectId, ref: 'User' }],
  77. seenUsers: [{ type: ObjectId, ref: 'User' }],
  78. commentCount: { type: Number, default: 0 },
  79. slackChannels: { type: String },
  80. pageIdOnHackmd: { type: String },
  81. revisionHackmdSynced: { type: ObjectId, ref: 'Revision' }, // the revision that is synced to HackMD
  82. hasDraftOnHackmd: { type: Boolean }, // set true if revision and revisionHackmdSynced are same but HackMD document has modified
  83. createdAt: { type: Date, default: new Date() },
  84. updatedAt: { type: Date, default: new Date() },
  85. deleteUser: { type: ObjectId, ref: 'User' },
  86. deletedAt: { type: Date },
  87. }, {
  88. toJSON: { getters: true },
  89. toObject: { getters: true },
  90. });
  91. // apply plugins
  92. schema.plugin(mongoosePaginate);
  93. schema.plugin(uniqueValidator);
  94. const hasSlash = (str: string): boolean => {
  95. return str.includes('/');
  96. };
  97. /*
  98. * Generate RegExp instance for one level lower path
  99. */
  100. const generateChildrenRegExp = (path: string): RegExp => {
  101. // https://regex101.com/r/laJGzj/1
  102. // ex. /any_level1
  103. if (isTopPage(path)) return new RegExp(/^\/[^/]+$/);
  104. // https://regex101.com/r/mrDJrx/1
  105. // ex. /parent/any_child OR /any_level1
  106. return new RegExp(`^${path}(\\/[^/]+)\\/?$`);
  107. };
  108. class PageQueryBuilder {
  109. query: any;
  110. constructor(query, includeEmpty = false) {
  111. this.query = query;
  112. if (!includeEmpty) {
  113. this.query = this.query
  114. .and({
  115. $or: [
  116. { isEmpty: false },
  117. { isEmpty: null }, // for v4 compatibility
  118. ],
  119. });
  120. }
  121. }
  122. /**
  123. * Used for filtering the pages at specified paths not to include unintentional pages.
  124. * @param pathsToFilter The paths to have additional filters as to be applicable
  125. * @returns PageQueryBuilder
  126. */
  127. addConditionToFilterByApplicableAncestors(pathsToFilter: string[]) {
  128. this.query = this.query
  129. .and(
  130. {
  131. $or: [
  132. { path: '/' },
  133. { path: { $in: pathsToFilter }, grant: GRANT_PUBLIC, status: STATUS_PUBLISHED },
  134. { path: { $in: pathsToFilter }, parent: { $ne: null }, status: STATUS_PUBLISHED },
  135. { path: { $nin: pathsToFilter }, status: STATUS_PUBLISHED },
  136. ],
  137. },
  138. );
  139. return this;
  140. }
  141. addConditionToExcludeTrashed() {
  142. this.query = this.query
  143. .and({
  144. $or: [
  145. { status: null },
  146. { status: STATUS_PUBLISHED },
  147. ],
  148. });
  149. return this;
  150. }
  151. /**
  152. * generate the query to find the pages '{path}/*' and '{path}' self.
  153. * If top page, return without doing anything.
  154. */
  155. addConditionToListWithDescendants(path: string, option?) {
  156. // No request is set for the top page
  157. if (isTopPage(path)) {
  158. return this;
  159. }
  160. const pathNormalized = pathUtils.normalizePath(path);
  161. const pathWithTrailingSlash = addTrailingSlash(path);
  162. const startsPattern = escapeStringRegexp(pathWithTrailingSlash);
  163. this.query = this.query
  164. .and({
  165. $or: [
  166. { path: pathNormalized },
  167. { path: new RegExp(`^${startsPattern}`) },
  168. ],
  169. });
  170. return this;
  171. }
  172. /**
  173. * generate the query to find the pages '{path}/*' (exclude '{path}' self).
  174. * If top page, return without doing anything.
  175. */
  176. addConditionToListOnlyDescendants(path, option) {
  177. // No request is set for the top page
  178. if (isTopPage(path)) {
  179. return this;
  180. }
  181. const pathWithTrailingSlash = addTrailingSlash(path);
  182. const startsPattern = escapeStringRegexp(pathWithTrailingSlash);
  183. this.query = this.query
  184. .and({ path: new RegExp(`^${startsPattern}`) });
  185. return this;
  186. }
  187. addConditionToListOnlyAncestors(path) {
  188. const pathNormalized = pathUtils.normalizePath(path);
  189. const ancestorsPaths = extractToAncestorsPaths(pathNormalized);
  190. this.query = this.query
  191. .and({
  192. path: {
  193. $in: ancestorsPaths,
  194. },
  195. });
  196. return this;
  197. }
  198. /**
  199. * generate the query to find pages that start with `path`
  200. *
  201. * In normal case, returns '{path}/*' and '{path}' self.
  202. * If top page, return without doing anything.
  203. *
  204. * *option*
  205. * Left for backward compatibility
  206. */
  207. addConditionToListByStartWith(path, option?) {
  208. // No request is set for the top page
  209. if (isTopPage(path)) {
  210. return this;
  211. }
  212. const startsPattern = escapeStringRegexp(path);
  213. this.query = this.query
  214. .and({ path: new RegExp(`^${startsPattern}`) });
  215. return this;
  216. }
  217. async addConditionForParentNormalization(user) {
  218. // determine UserGroup condition
  219. let userGroups;
  220. if (user != null) {
  221. const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO: Typescriptize model
  222. userGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
  223. }
  224. const grantConditions: any[] = [
  225. { grant: null },
  226. { grant: GRANT_PUBLIC },
  227. ];
  228. if (user != null) {
  229. grantConditions.push(
  230. { grant: GRANT_OWNER, grantedUsers: user._id },
  231. );
  232. }
  233. if (userGroups != null && userGroups.length > 0) {
  234. grantConditions.push(
  235. { grant: GRANT_USER_GROUP, grantedGroup: { $in: userGroups } },
  236. );
  237. }
  238. this.query = this.query
  239. .and({
  240. $or: grantConditions,
  241. });
  242. return this;
  243. }
  244. async addConditionAsMigratablePages(user) {
  245. this.query = this.query
  246. .and({
  247. $or: [
  248. { grant: { $ne: GRANT_RESTRICTED } },
  249. { grant: { $ne: GRANT_SPECIFIED } },
  250. ],
  251. });
  252. this.addConditionAsNotMigrated();
  253. this.addConditionAsNonRootPage();
  254. this.addConditionToExcludeTrashed();
  255. await this.addConditionForParentNormalization(user);
  256. return this;
  257. }
  258. addConditionToFilteringByViewer(user, userGroups, showAnyoneKnowsLink = false, showPagesRestrictedByOwner = false, showPagesRestrictedByGroup = false) {
  259. const condition = generateGrantCondition(user, userGroups, showAnyoneKnowsLink, showPagesRestrictedByOwner, showPagesRestrictedByGroup);
  260. this.query = this.query
  261. .and(condition);
  262. return this;
  263. }
  264. addConditionToPagenate(offset, limit, sortOpt?) {
  265. this.query = this.query
  266. .sort(sortOpt).skip(offset).limit(limit); // eslint-disable-line newline-per-chained-call
  267. return this;
  268. }
  269. addConditionAsNonRootPage() {
  270. this.query = this.query.and({ path: { $ne: '/' } });
  271. return this;
  272. }
  273. addConditionAsNotMigrated() {
  274. this.query = this.query
  275. .and({ parent: null });
  276. return this;
  277. }
  278. addConditionAsMigrated() {
  279. this.query = this.query
  280. .and(
  281. {
  282. $or: [
  283. { parent: { $ne: null } },
  284. { path: '/' },
  285. ],
  286. },
  287. );
  288. return this;
  289. }
  290. /*
  291. * Add this condition when get any ancestor pages including the target's parent
  292. */
  293. addConditionToSortPagesByDescPath() {
  294. this.query = this.query.sort('-path');
  295. return this;
  296. }
  297. addConditionToSortPagesByAscPath() {
  298. this.query = this.query.sort('path');
  299. return this;
  300. }
  301. addConditionToMinimizeDataForRendering() {
  302. this.query = this.query.select('_id path isEmpty grant revision descendantCount');
  303. return this;
  304. }
  305. addConditionToListByPathsArray(paths) {
  306. this.query = this.query
  307. .and({
  308. path: {
  309. $in: paths,
  310. },
  311. });
  312. return this;
  313. }
  314. addConditionToListByPageIdsArray(pageIds) {
  315. this.query = this.query
  316. .and({
  317. _id: {
  318. $in: pageIds,
  319. },
  320. });
  321. return this;
  322. }
  323. addConditionToExcludeByPageIdsArray(pageIds) {
  324. this.query = this.query
  325. .and({
  326. _id: {
  327. $nin: pageIds,
  328. },
  329. });
  330. return this;
  331. }
  332. populateDataToList(userPublicFields) {
  333. this.query = this.query
  334. .populate({
  335. path: 'lastUpdateUser',
  336. select: userPublicFields,
  337. });
  338. return this;
  339. }
  340. populateDataToShowRevision(userPublicFields) {
  341. this.query = populateDataToShowRevision(this.query, userPublicFields);
  342. return this;
  343. }
  344. addConditionToFilteringByParentId(parentId) {
  345. this.query = this.query.and({ parent: parentId });
  346. return this;
  347. }
  348. }
  349. /**
  350. * Create empty pages if the page in paths didn't exist
  351. * @param onlyMigratedAsExistingPages Determine whether to include non-migrated pages as existing pages. If a page exists,
  352. * an empty page will not be created at that page's path.
  353. */
  354. schema.statics.createEmptyPagesByPaths = async function(paths: string[], user: any | null, onlyMigratedAsExistingPages = true, filter?): Promise<void> {
  355. const aggregationPipeline: any[] = [];
  356. // 1. Filter by paths
  357. aggregationPipeline.push({ $match: { path: { $in: paths } } });
  358. // 2. Normalized condition
  359. if (onlyMigratedAsExistingPages) {
  360. aggregationPipeline.push({
  361. $match: {
  362. $or: [
  363. { parent: { $ne: null } },
  364. { path: '/' },
  365. ],
  366. },
  367. });
  368. }
  369. // 3. Add custom pipeline
  370. if (filter != null) {
  371. aggregationPipeline.push({ $match: filter });
  372. }
  373. // 4. Add grant conditions
  374. let userGroups = null;
  375. if (user != null) {
  376. const UserGroupRelation = mongoose.model('UserGroupRelation') as any;
  377. userGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
  378. }
  379. const grantCondition = this.generateGrantCondition(user, userGroups);
  380. aggregationPipeline.push({ $match: grantCondition });
  381. // Run aggregation
  382. const existingPages = await this.aggregate(aggregationPipeline);
  383. const existingPagePaths = existingPages.map(page => page.path);
  384. // paths to create empty pages
  385. const notExistingPagePaths = paths.filter(path => !existingPagePaths.includes(path));
  386. // insertMany empty pages
  387. try {
  388. await this.insertMany(notExistingPagePaths.map(path => ({ path, isEmpty: true })));
  389. }
  390. catch (err) {
  391. logger.error('Failed to insert empty pages.', err);
  392. throw err;
  393. }
  394. };
  395. schema.statics.createEmptyPage = async function(
  396. path: string, parent: any, descendantCount = 0, // TODO: improve type including IPage at https://redmine.weseek.co.jp/issues/86506
  397. ): Promise<PageDocument & { _id: any }> {
  398. if (parent == null) {
  399. throw Error('parent must not be null');
  400. }
  401. const Page = this;
  402. const page = new Page();
  403. page.path = path;
  404. page.isEmpty = true;
  405. page.parent = parent;
  406. page.descendantCount = descendantCount;
  407. return page.save();
  408. };
  409. /**
  410. * Replace an existing page with an empty page.
  411. * It updates the children's parent to the new empty page's _id.
  412. * @param exPage a page document to be replaced
  413. * @returns Promise<void>
  414. */
  415. schema.statics.replaceTargetWithPage = async function(exPage, pageToReplaceWith?, deleteExPageIfEmpty = false) {
  416. // find parent
  417. const parent = await this.findOne({ _id: exPage.parent });
  418. if (parent == null) {
  419. throw Error('parent to update does not exist. Prepare parent first.');
  420. }
  421. // create empty page at path
  422. const newTarget = pageToReplaceWith == null ? await this.createEmptyPage(exPage.path, parent, exPage.descendantCount) : pageToReplaceWith;
  423. // find children by ex-page _id
  424. const children = await this.find({ parent: exPage._id });
  425. // bulkWrite
  426. const operationForNewTarget = {
  427. updateOne: {
  428. filter: { _id: newTarget._id },
  429. update: {
  430. parent: parent._id,
  431. },
  432. },
  433. };
  434. const operationsForChildren = {
  435. updateMany: {
  436. filter: {
  437. _id: { $in: children.map(d => d._id) },
  438. },
  439. update: {
  440. parent: newTarget._id,
  441. },
  442. },
  443. };
  444. await this.bulkWrite([operationForNewTarget, operationsForChildren]);
  445. const isExPageEmpty = exPage.isEmpty;
  446. if (deleteExPageIfEmpty && isExPageEmpty) {
  447. await this.deleteOne({ _id: exPage._id });
  448. logger.warn('Deleted empty page since it was replaced with another page.');
  449. }
  450. return this.findById(newTarget._id);
  451. };
  452. /**
  453. * Find parent or create parent if not exists.
  454. * It also updates parent of ancestors
  455. * @param path string
  456. * @returns Promise<PageDocument>
  457. */
  458. schema.statics.getParentAndFillAncestors = async function(path: string, user): Promise<PageDocument> {
  459. const parentPath = nodePath.dirname(path);
  460. const builder1 = new PageQueryBuilder(this.find({ path: parentPath }), true);
  461. const pagesCanBeParent = await builder1
  462. .addConditionAsMigrated()
  463. .query
  464. .exec();
  465. if (pagesCanBeParent.length >= 1) {
  466. return pagesCanBeParent[0]; // the earliest page will be the result
  467. }
  468. /*
  469. * Fill parents if parent is null
  470. */
  471. const ancestorPaths = collectAncestorPaths(path); // paths of parents need to be created
  472. // just create ancestors with empty pages
  473. await this.createEmptyPagesByPaths(ancestorPaths, user);
  474. // find ancestors
  475. const builder2 = new PageQueryBuilder(this.find(), true);
  476. // avoid including not normalized pages
  477. builder2.addConditionToFilterByApplicableAncestors(ancestorPaths);
  478. const ancestors = await builder2
  479. .addConditionToListByPathsArray(ancestorPaths)
  480. .addConditionToSortPagesByDescPath()
  481. .query
  482. .exec();
  483. const ancestorsMap = new Map(); // Map<path, page>
  484. ancestors.forEach(page => !ancestorsMap.has(page.path) && ancestorsMap.set(page.path, page)); // the earlier element should be the true ancestor
  485. // bulkWrite to update ancestors
  486. const nonRootAncestors = ancestors.filter(page => !isTopPage(page.path));
  487. const operations = nonRootAncestors.map((page) => {
  488. const parentPath = nodePath.dirname(page.path);
  489. return {
  490. updateOne: {
  491. filter: {
  492. _id: page._id,
  493. },
  494. update: {
  495. parent: ancestorsMap.get(parentPath)._id,
  496. },
  497. },
  498. };
  499. });
  500. await this.bulkWrite(operations);
  501. const parentId = ancestorsMap.get(parentPath)._id; // get parent page id to fetch updated parent parent
  502. const createdParent = await this.findOne({ _id: parentId });
  503. if (createdParent == null) {
  504. throw Error('updated parent not Found');
  505. }
  506. return createdParent;
  507. };
  508. // Utility function to add viewer condition to PageQueryBuilder instance
  509. const addViewerCondition = async(queryBuilder: PageQueryBuilder, user, userGroups = null): Promise<void> => {
  510. let relatedUserGroups = userGroups;
  511. if (user != null && relatedUserGroups == null) {
  512. const UserGroupRelation: any = mongoose.model('UserGroupRelation');
  513. relatedUserGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
  514. }
  515. queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, false);
  516. };
  517. /*
  518. * Find pages by ID and viewer.
  519. */
  520. schema.statics.findByIdsAndViewer = async function(pageIds: string[], user, userGroups?, includeEmpty?: boolean): Promise<PageDocument[]> {
  521. const baseQuery = this.find({ _id: { $in: pageIds } });
  522. const queryBuilder = new PageQueryBuilder(baseQuery, includeEmpty);
  523. await addViewerCondition(queryBuilder, user, userGroups);
  524. return queryBuilder.query.exec();
  525. };
  526. /*
  527. * Find a page by path and viewer. Pass false to useFindOne to use findOne method.
  528. */
  529. schema.statics.findByPathAndViewer = async function(
  530. path: string | null, user, userGroups = null, useFindOne = true, includeEmpty = false,
  531. ): Promise<PageDocument | PageDocument[] | null> {
  532. if (path == null) {
  533. throw new Error('path is required.');
  534. }
  535. const baseQuery = useFindOne ? this.findOne({ path }) : this.find({ path });
  536. const queryBuilder = new PageQueryBuilder(baseQuery, includeEmpty);
  537. await addViewerCondition(queryBuilder, user, userGroups);
  538. return queryBuilder.query.exec();
  539. };
  540. /*
  541. * Find all ancestor pages by path. When duplicate pages found, it uses the oldest page as a result
  542. * The result will include the target as well
  543. */
  544. schema.statics.findTargetAndAncestorsByPathOrId = async function(pathOrId: string, user, userGroups): Promise<TargetAndAncestorsResult> {
  545. let path;
  546. if (!hasSlash(pathOrId)) {
  547. const _id = pathOrId;
  548. const page = await this.findOne({ _id });
  549. path = page == null ? '/' : page.path;
  550. }
  551. else {
  552. path = pathOrId;
  553. }
  554. const ancestorPaths = collectAncestorPaths(path);
  555. ancestorPaths.push(path); // include target
  556. // Do not populate
  557. const queryBuilder = new PageQueryBuilder(this.find(), true);
  558. await addViewerCondition(queryBuilder, user, userGroups);
  559. const _targetAndAncestors: PageDocument[] = await queryBuilder
  560. .addConditionAsMigrated()
  561. .addConditionToListByPathsArray(ancestorPaths)
  562. .addConditionToMinimizeDataForRendering()
  563. .addConditionToSortPagesByDescPath()
  564. .query
  565. .lean()
  566. .exec();
  567. // no same path pages
  568. const ancestorsMap = new Map<string, PageDocument>();
  569. _targetAndAncestors.forEach(page => ancestorsMap.set(page.path, page));
  570. const targetAndAncestors = Array.from(ancestorsMap.values());
  571. const rootPage = targetAndAncestors[targetAndAncestors.length - 1];
  572. return { targetAndAncestors, rootPage };
  573. };
  574. /*
  575. * Find all children by parent's path or id. Using id should be prioritized
  576. */
  577. schema.statics.findChildrenByParentPathOrIdAndViewer = async function(parentPathOrId: string, user, userGroups = null): Promise<PageDocument[]> {
  578. let queryBuilder: PageQueryBuilder;
  579. if (hasSlash(parentPathOrId)) {
  580. const path = parentPathOrId;
  581. const regexp = generateChildrenRegExp(path);
  582. queryBuilder = new PageQueryBuilder(this.find({ path: { $regex: regexp } }), true);
  583. }
  584. else {
  585. const parentId = parentPathOrId;
  586. queryBuilder = new PageQueryBuilder(this.find({ parent: parentId } as any), true); // TODO: improve type
  587. }
  588. await addViewerCondition(queryBuilder, user, userGroups);
  589. return queryBuilder
  590. .addConditionToSortPagesByAscPath()
  591. .query
  592. .lean()
  593. .exec();
  594. };
  595. schema.statics.findAncestorsChildrenByPathAndViewer = async function(path: string, user, userGroups = null): Promise<Record<string, PageDocument[]>> {
  596. const ancestorPaths = isTopPage(path) ? ['/'] : collectAncestorPaths(path); // root path is necessary for rendering
  597. const regexps = ancestorPaths.map(path => new RegExp(generateChildrenRegExp(path))); // cannot use re2
  598. // get pages at once
  599. const queryBuilder = new PageQueryBuilder(this.find({ path: { $in: regexps } }), true);
  600. await addViewerCondition(queryBuilder, user, userGroups);
  601. const _pages = await queryBuilder
  602. .addConditionAsMigrated()
  603. .addConditionToMinimizeDataForRendering()
  604. .addConditionToSortPagesByAscPath()
  605. .query
  606. .lean()
  607. .exec();
  608. // mark target
  609. const pages = _pages.map((page: PageDocument & {isTarget?: boolean}) => {
  610. if (page.path === path) {
  611. page.isTarget = true;
  612. }
  613. return page;
  614. });
  615. /*
  616. * If any non-migrated page is found during creating the pathToChildren map, it will stop incrementing at that moment
  617. */
  618. const pathToChildren: Record<string, PageDocument[]> = {};
  619. const sortedPaths = ancestorPaths.sort((a, b) => a.length - b.length); // sort paths by path.length
  620. sortedPaths.every((path) => {
  621. const children = pages.filter(page => nodePath.dirname(page.path) === path);
  622. if (children.length === 0) {
  623. return false; // break when children do not exist
  624. }
  625. pathToChildren[path] = children;
  626. return true;
  627. });
  628. return pathToChildren;
  629. };
  630. /*
  631. * Utils from obsolete-page.js
  632. */
  633. async function pushRevision(pageData, newRevision, user) {
  634. await newRevision.save();
  635. pageData.revision = newRevision;
  636. pageData.lastUpdateUser = user;
  637. pageData.updatedAt = Date.now();
  638. return pageData.save();
  639. }
  640. /**
  641. * add/subtract descendantCount of pages with provided paths by increment.
  642. * increment can be negative number
  643. */
  644. schema.statics.incrementDescendantCountOfPageIds = async function(pageIds: ObjectIdLike[], increment: number): Promise<void> {
  645. await this.updateMany({ _id: { $in: pageIds } }, { $inc: { descendantCount: increment } });
  646. };
  647. /**
  648. * recount descendantCount of a page with the provided id and return it
  649. */
  650. schema.statics.recountDescendantCount = async function(id: ObjectIdLike):Promise<number> {
  651. const res = await this.aggregate(
  652. [
  653. {
  654. $match: {
  655. parent: id,
  656. },
  657. },
  658. {
  659. $project: {
  660. parent: 1,
  661. isEmpty: 1,
  662. descendantCount: 1,
  663. },
  664. },
  665. {
  666. $group: {
  667. _id: '$parent',
  668. sumOfDescendantCount: {
  669. $sum: '$descendantCount',
  670. },
  671. sumOfDocsCount: {
  672. $sum: {
  673. $cond: { if: { $eq: ['$isEmpty', true] }, then: 0, else: 1 }, // exclude isEmpty true page from sumOfDocsCount
  674. },
  675. },
  676. },
  677. },
  678. {
  679. $set: {
  680. descendantCount: {
  681. $sum: ['$sumOfDescendantCount', '$sumOfDocsCount'],
  682. },
  683. },
  684. },
  685. ],
  686. );
  687. return res.length === 0 ? 0 : res[0].descendantCount;
  688. };
  689. schema.statics.findAncestorsUsingParentRecursively = async function(pageId: ObjectIdLike, shouldIncludeTarget: boolean) {
  690. const self = this;
  691. const target = await this.findById(pageId);
  692. if (target == null) {
  693. throw Error('Target not found');
  694. }
  695. async function findAncestorsRecursively(target, ancestors = shouldIncludeTarget ? [target] : []) {
  696. const parent = await self.findOne({ _id: target.parent });
  697. if (parent == null) {
  698. return ancestors;
  699. }
  700. return findAncestorsRecursively(parent, [...ancestors, parent]);
  701. }
  702. return findAncestorsRecursively(target);
  703. };
  704. // TODO: write test code
  705. /**
  706. * Recursively removes empty pages at leaf position.
  707. * @param pageId ObjectIdLike
  708. * @returns Promise<void>
  709. */
  710. schema.statics.removeLeafEmptyPagesRecursively = async function(pageId: ObjectIdLike): Promise<void> {
  711. const self = this;
  712. const initialPage = await this.findById(pageId);
  713. if (initialPage == null) {
  714. return;
  715. }
  716. if (!initialPage.isEmpty) {
  717. return;
  718. }
  719. async function generatePageIdsToRemove(childPage, page, pageIds: ObjectIdLike[] = []): Promise<ObjectIdLike[]> {
  720. if (!page.isEmpty) {
  721. return pageIds;
  722. }
  723. const isChildrenOtherThanTargetExist = await self.exists({ _id: { $ne: childPage?._id }, parent: page._id });
  724. if (isChildrenOtherThanTargetExist) {
  725. return pageIds;
  726. }
  727. pageIds.push(page._id);
  728. const nextPage = await self.findById(page.parent);
  729. if (nextPage == null) {
  730. return pageIds;
  731. }
  732. return generatePageIdsToRemove(page, nextPage, pageIds);
  733. }
  734. const pageIdsToRemove = await generatePageIdsToRemove(null, initialPage);
  735. await this.deleteMany({ _id: { $in: pageIdsToRemove } });
  736. };
  737. schema.statics.normalizeDescendantCountById = async function(pageId) {
  738. const children = await this.find({ parent: pageId });
  739. const sumChildrenDescendantCount = children.map(d => d.descendantCount).reduce((c1, c2) => c1 + c2);
  740. const sumChildPages = children.filter(p => !p.isEmpty).length;
  741. return this.updateOne({ _id: pageId }, { $set: { descendantCount: sumChildrenDescendantCount + sumChildPages } }, { new: true });
  742. };
  743. schema.statics.takeOffFromTree = async function(pageId: ObjectIdLike) {
  744. return this.findByIdAndUpdate(pageId, { $set: { parent: null } });
  745. };
  746. schema.statics.removeEmptyPages = async function(pageIdsToNotRemove: ObjectIdLike[], paths: string[]): Promise<void> {
  747. await this.deleteMany({
  748. _id: {
  749. $nin: pageIdsToNotRemove,
  750. },
  751. path: {
  752. $in: paths,
  753. },
  754. isEmpty: true,
  755. });
  756. };
  757. schema.statics.PageQueryBuilder = PageQueryBuilder as any; // mongoose does not support constructor type as statics attrs type
  758. export function generateGrantCondition(
  759. user, userGroups, showAnyoneKnowsLink = false, showPagesRestrictedByOwner = false, showPagesRestrictedByGroup = false,
  760. ): { $or: any[] } {
  761. const grantConditions: AnyObject[] = [
  762. { grant: null },
  763. { grant: GRANT_PUBLIC },
  764. ];
  765. if (showAnyoneKnowsLink) {
  766. grantConditions.push({ grant: GRANT_RESTRICTED });
  767. }
  768. if (showPagesRestrictedByOwner) {
  769. grantConditions.push(
  770. { grant: GRANT_SPECIFIED },
  771. { grant: GRANT_OWNER },
  772. );
  773. }
  774. else if (user != null) {
  775. grantConditions.push(
  776. { grant: GRANT_SPECIFIED, grantedUsers: user._id },
  777. { grant: GRANT_OWNER, grantedUsers: user._id },
  778. );
  779. }
  780. if (showPagesRestrictedByGroup) {
  781. grantConditions.push(
  782. { grant: GRANT_USER_GROUP },
  783. );
  784. }
  785. else if (userGroups != null && userGroups.length > 0) {
  786. grantConditions.push(
  787. { grant: GRANT_USER_GROUP, grantedGroup: { $in: userGroups } },
  788. );
  789. }
  790. return {
  791. $or: grantConditions,
  792. };
  793. }
  794. schema.statics.generateGrantCondition = generateGrantCondition;
  795. export type PageCreateOptions = {
  796. format?: string
  797. grantUserGroupId?: ObjectIdLike
  798. grant?: number
  799. }
  800. /*
  801. * Merge obsolete page model methods and define new methods which depend on crowi instance
  802. */
  803. export default (crowi: Crowi): any => {
  804. let pageEvent;
  805. if (crowi != null) {
  806. pageEvent = crowi.event('page');
  807. }
  808. schema.statics.create = async function(path: string, body: string, user, options: PageCreateOptions = {}) {
  809. if (crowi.pageGrantService == null || crowi.configManager == null || crowi.pageService == null || crowi.pageOperationService == null) {
  810. throw Error('Crowi is not setup');
  811. }
  812. const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
  813. // v4 compatible process
  814. if (!isV5Compatible) {
  815. return this.createV4(path, body, user, options);
  816. }
  817. const canOperate = await crowi.pageOperationService.canOperate(false, null, path);
  818. if (!canOperate) {
  819. throw Error(`Cannot operate create to path "${path}" right now.`);
  820. }
  821. const Page = this;
  822. const Revision = crowi.model('Revision');
  823. const {
  824. format = 'markdown', grantUserGroupId,
  825. } = options;
  826. let grant = options.grant;
  827. // sanitize path
  828. path = crowi.xss.process(path); // eslint-disable-line no-param-reassign
  829. // throw if exists
  830. const isExist = (await this.count({ path, isEmpty: false })) > 0; // not validate empty page
  831. if (isExist) {
  832. throw new Error('Cannot create new page to existed path');
  833. }
  834. // force public
  835. if (isTopPage(path)) {
  836. grant = GRANT_PUBLIC;
  837. }
  838. // find an existing empty page
  839. const emptyPage = await Page.findOne({ path, isEmpty: true });
  840. /*
  841. * UserGroup & Owner validation
  842. */
  843. if (grant !== GRANT_RESTRICTED) {
  844. let isGrantNormalized = false;
  845. try {
  846. // It must check descendants as well if emptyTarget is not null
  847. const shouldCheckDescendants = emptyPage != null;
  848. const newGrantedUserIds = grant === GRANT_OWNER ? [user._id] as IObjectId[] : undefined;
  849. isGrantNormalized = await crowi.pageGrantService.isGrantNormalized(user, path, grant, newGrantedUserIds, grantUserGroupId, shouldCheckDescendants);
  850. }
  851. catch (err) {
  852. logger.error(`Failed to validate grant of page at "${path}" of grant ${grant}:`, err);
  853. throw err;
  854. }
  855. if (!isGrantNormalized) {
  856. throw Error('The selected grant or grantedGroup is not assignable to this page.');
  857. }
  858. }
  859. /*
  860. * update empty page if exists, if not, create a new page
  861. */
  862. let page;
  863. if (emptyPage != null && grant !== GRANT_RESTRICTED) {
  864. page = emptyPage;
  865. const descendantCount = await this.recountDescendantCount(page._id);
  866. page.descendantCount = descendantCount;
  867. page.isEmpty = false;
  868. }
  869. else {
  870. page = new Page();
  871. }
  872. page.path = path;
  873. page.creator = user;
  874. page.lastUpdateUser = user;
  875. page.status = STATUS_PUBLISHED;
  876. // set parent to null when GRANT_RESTRICTED
  877. const isGrantRestricted = grant === GRANT_RESTRICTED;
  878. if (isTopPage(path) || isGrantRestricted) {
  879. page.parent = null;
  880. }
  881. else {
  882. const parent = await Page.getParentAndFillAncestors(path, user);
  883. page.parent = parent._id;
  884. }
  885. page.applyScope(user, grant, grantUserGroupId);
  886. let savedPage = await page.save();
  887. /*
  888. * After save
  889. */
  890. // Delete PageRedirect if exists
  891. const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
  892. try {
  893. await PageRedirect.deleteOne({ fromPath: path });
  894. logger.warn(`Deleted page redirect after creating a new page at path "${path}".`);
  895. }
  896. catch (err) {
  897. // no throw
  898. logger.error('Failed to delete PageRedirect');
  899. }
  900. const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });
  901. savedPage = await pushRevision(savedPage, newRevision, user);
  902. await savedPage.populateDataToShowRevision();
  903. pageEvent.emit('create', savedPage, user);
  904. // update descendantCount asynchronously
  905. await crowi.pageService.updateDescendantCountOfAncestors(savedPage._id, 1, false);
  906. return savedPage;
  907. };
  908. const shouldUseUpdatePageV4 = (grant:number, isV5Compatible:boolean, isOnTree:boolean): boolean => {
  909. const isRestricted = grant === GRANT_RESTRICTED;
  910. return !isRestricted && (!isV5Compatible || !isOnTree);
  911. };
  912. schema.statics.updatePage = async function(pageData, body, previousBody, user, options = {}) {
  913. if (crowi.configManager == null || crowi.pageGrantService == null || crowi.pageService == null) {
  914. throw Error('Crowi is not set up');
  915. }
  916. const wasOnTree = pageData.parent != null || isTopPage(pageData.path);
  917. const exParent = pageData.parent;
  918. const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
  919. const shouldUseV4Process = shouldUseUpdatePageV4(pageData.grant, isV5Compatible, wasOnTree);
  920. if (shouldUseV4Process) {
  921. // v4 compatible process
  922. return this.updatePageV4(pageData, body, previousBody, user, options);
  923. }
  924. const Revision = mongoose.model('Revision') as any; // TODO: Typescriptize model
  925. const grant = options.grant || pageData.grant; // use the previous data if absence
  926. const grantUserGroupId: undefined | ObjectIdLike = options.grantUserGroupId ?? pageData.grantedGroup?._id.toString();
  927. const isSyncRevisionToHackmd = options.isSyncRevisionToHackmd;
  928. const grantedUserIds = pageData.grantedUserIds || [user._id];
  929. const shouldBeOnTree = grant !== GRANT_RESTRICTED;
  930. const isChildrenExist = await this.count({ path: new RegExp(`^${escapeStringRegexp(addTrailingSlash(pageData.path))}`), parent: { $ne: null } });
  931. const newPageData = pageData;
  932. if (shouldBeOnTree) {
  933. let isGrantNormalized = false;
  934. try {
  935. isGrantNormalized = await crowi.pageGrantService.isGrantNormalized(user, pageData.path, grant, grantedUserIds, grantUserGroupId, true);
  936. }
  937. catch (err) {
  938. logger.error(`Failed to validate grant of page at "${pageData.path}" of grant ${grant}:`, err);
  939. throw err;
  940. }
  941. if (!isGrantNormalized) {
  942. throw Error('The selected grant or grantedGroup is not assignable to this page.');
  943. }
  944. if (!wasOnTree) {
  945. const newParent = await this.getParentAndFillAncestors(newPageData.path, user);
  946. newPageData.parent = newParent._id;
  947. }
  948. }
  949. else {
  950. if (wasOnTree && isChildrenExist) {
  951. // Update children's parent with new parent
  952. const newParentForChildren = await this.createEmptyPage(pageData.path, pageData.parent, pageData.descendantCount);
  953. await this.updateMany(
  954. { parent: pageData._id },
  955. { parent: newParentForChildren._id },
  956. );
  957. }
  958. newPageData.parent = null;
  959. newPageData.descendantCount = 0;
  960. }
  961. newPageData.applyScope(user, grant, grantUserGroupId);
  962. // update existing page
  963. let savedPage = await newPageData.save();
  964. const newRevision = await Revision.prepareRevision(newPageData, body, previousBody, user);
  965. savedPage = await pushRevision(savedPage, newRevision, user);
  966. await savedPage.populateDataToShowRevision();
  967. if (isSyncRevisionToHackmd) {
  968. savedPage = await this.syncRevisionToHackmd(savedPage);
  969. }
  970. pageEvent.emit('update', savedPage, user);
  971. // Update ex children's parent
  972. if (!wasOnTree && shouldBeOnTree) {
  973. const emptyPageAtSamePath = await this.findOne({ path: pageData.path, isEmpty: true }); // this page is necessary to find children
  974. if (isChildrenExist) {
  975. if (emptyPageAtSamePath != null) {
  976. // Update children's parent with new parent
  977. await this.updateMany(
  978. { parent: emptyPageAtSamePath._id },
  979. { parent: savedPage._id },
  980. );
  981. }
  982. }
  983. await this.findOneAndDelete({ path: pageData.path, isEmpty: true }); // delete here
  984. }
  985. // Sub operation
  986. // 1. Update descendantCount
  987. const shouldPlusDescCount = !wasOnTree && shouldBeOnTree;
  988. const shouldMinusDescCount = wasOnTree && !shouldBeOnTree;
  989. if (shouldPlusDescCount) {
  990. await crowi.pageService.updateDescendantCountOfAncestors(newPageData._id, 1, false);
  991. const newDescendantCount = await this.recountDescendantCount(newPageData._id);
  992. await this.updateOne({ _id: newPageData._id }, { descendantCount: newDescendantCount });
  993. }
  994. else if (shouldMinusDescCount) {
  995. // Update from parent. Parent is null if newPageData.grant is RESTRECTED.
  996. if (newPageData.grant === GRANT_RESTRICTED) {
  997. await crowi.pageService.updateDescendantCountOfAncestors(exParent, -1, true);
  998. }
  999. }
  1000. // 2. Delete unnecessary empty pages
  1001. const shouldRemoveLeafEmpPages = wasOnTree && !isChildrenExist;
  1002. if (shouldRemoveLeafEmpPages) {
  1003. await this.removeLeafEmptyPagesRecursively(exParent);
  1004. }
  1005. return savedPage;
  1006. };
  1007. // add old page schema methods
  1008. const pageSchema = getPageSchema(crowi);
  1009. schema.methods = { ...pageSchema.methods, ...schema.methods };
  1010. schema.statics = { ...pageSchema.statics, ...schema.statics };
  1011. return getOrCreateModel<PageDocument, PageModel>('Page', schema as any); // TODO: improve type
  1012. };