page.ts 29 KB

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