page.ts 30 KB

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