page.ts 31 KB

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