page.ts 32 KB

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