page.ts 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. import nodePath from 'path';
  3. import { HasObjectId, 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 & 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. 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[]): PageQueryBuilder {
  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(): PageQueryBuilder {
  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?): PageQueryBuilder {
  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): PageQueryBuilder {
  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): PageQueryBuilder {
  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): Promise<PageQueryBuilder> {
  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): Promise<PageQueryBuilder> {
  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, includeAnyoneWithTheLink = false): 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, includeAnyoneWithTheLink);
  293. return this;
  294. }
  295. addConditionToFilteringByViewer(
  296. user, userGroups, includeAnyoneWithTheLink = false, showPagesRestrictedByOwner = false, showPagesRestrictedByGroup = false,
  297. ): PageQueryBuilder {
  298. const condition = generateGrantCondition(user, userGroups, includeAnyoneWithTheLink, showPagesRestrictedByOwner, showPagesRestrictedByGroup);
  299. this.query = this.query
  300. .and(condition);
  301. return this;
  302. }
  303. addConditionToPagenate(offset, limit, sortOpt?): PageQueryBuilder {
  304. this.query = this.query
  305. .sort(sortOpt).skip(offset).limit(limit); // eslint-disable-line newline-per-chained-call
  306. return this;
  307. }
  308. addConditionAsNonRootPage(): PageQueryBuilder {
  309. this.query = this.query.and({ path: { $ne: '/' } });
  310. return this;
  311. }
  312. addConditionAsNotMigrated(): PageQueryBuilder {
  313. this.query = this.query
  314. .and({ parent: null });
  315. return this;
  316. }
  317. addConditionAsOnTree(): PageQueryBuilder {
  318. this.query = this.query
  319. .and(
  320. {
  321. $or: [
  322. { parent: { $ne: null } },
  323. { path: '/' },
  324. ],
  325. },
  326. );
  327. return this;
  328. }
  329. /*
  330. * Add this condition when get any ancestor pages including the target's parent
  331. */
  332. addConditionToSortPagesByDescPath(): PageQueryBuilder {
  333. this.query = this.query.sort('-path');
  334. return this;
  335. }
  336. addConditionToSortPagesByAscPath(): PageQueryBuilder {
  337. this.query = this.query.sort('path');
  338. return this;
  339. }
  340. addConditionToMinimizeDataForRendering(): PageQueryBuilder {
  341. this.query = this.query.select('_id path isEmpty grant revision descendantCount');
  342. return this;
  343. }
  344. addConditionToListByPathsArray(paths): PageQueryBuilder {
  345. this.query = this.query
  346. .and({
  347. path: {
  348. $in: paths,
  349. },
  350. });
  351. return this;
  352. }
  353. addConditionToListByPageIdsArray(pageIds): PageQueryBuilder {
  354. this.query = this.query
  355. .and({
  356. _id: {
  357. $in: pageIds,
  358. },
  359. });
  360. return this;
  361. }
  362. addConditionToExcludeByPageIdsArray(pageIds): PageQueryBuilder {
  363. this.query = this.query
  364. .and({
  365. _id: {
  366. $nin: pageIds,
  367. },
  368. });
  369. return this;
  370. }
  371. populateDataToList(userPublicFields): PageQueryBuilder {
  372. this.query = this.query
  373. .populate({
  374. path: 'lastUpdateUser',
  375. select: userPublicFields,
  376. });
  377. return this;
  378. }
  379. populateDataToShowRevision(userPublicFields): PageQueryBuilder {
  380. this.query = populateDataToShowRevision(this.query, userPublicFields);
  381. return this;
  382. }
  383. addConditionToFilteringByParentId(parentId): PageQueryBuilder {
  384. this.query = this.query.and({ parent: parentId });
  385. return this;
  386. }
  387. }
  388. schema.statics.createEmptyPage = async function(
  389. path: string, parent: any, descendantCount = 0, // TODO: improve type including IPage at https://redmine.weseek.co.jp/issues/86506
  390. ): Promise<PageDocument & { _id: any }> {
  391. if (parent == null) {
  392. throw Error('parent must not be null');
  393. }
  394. const Page = this;
  395. const page = new Page();
  396. page.path = path;
  397. page.isEmpty = true;
  398. page.parent = parent;
  399. page.descendantCount = descendantCount;
  400. return page.save();
  401. };
  402. /**
  403. * Replace an existing page with an empty page.
  404. * It updates the children's parent to the new empty page's _id.
  405. * @param exPage a page document to be replaced
  406. * @returns Promise<void>
  407. */
  408. schema.statics.replaceTargetWithPage = async function(exPage, pageToReplaceWith?, deleteExPageIfEmpty = false) {
  409. // find parent
  410. const parent = await this.findOne({ _id: exPage.parent });
  411. if (parent == null) {
  412. throw Error('parent to update does not exist. Prepare parent first.');
  413. }
  414. // create empty page at path
  415. const newTarget = pageToReplaceWith == null ? await this.createEmptyPage(exPage.path, parent, exPage.descendantCount) : pageToReplaceWith;
  416. // find children by ex-page _id
  417. const children = await this.find({ parent: exPage._id });
  418. // bulkWrite
  419. const operationForNewTarget = {
  420. updateOne: {
  421. filter: { _id: newTarget._id },
  422. update: {
  423. parent: parent._id,
  424. },
  425. },
  426. };
  427. const operationsForChildren = {
  428. updateMany: {
  429. filter: {
  430. _id: { $in: children.map(d => d._id) },
  431. },
  432. update: {
  433. parent: newTarget._id,
  434. },
  435. },
  436. };
  437. await this.bulkWrite([operationForNewTarget, operationsForChildren]);
  438. const isExPageEmpty = exPage.isEmpty;
  439. if (deleteExPageIfEmpty && isExPageEmpty) {
  440. await this.deleteOne({ _id: exPage._id });
  441. logger.warn('Deleted empty page since it was replaced with another page.');
  442. }
  443. return this.findById(newTarget._id);
  444. };
  445. /*
  446. * Find pages by ID and viewer.
  447. */
  448. schema.statics.findByIdsAndViewer = async function(pageIds: string[], user, userGroups?, includeEmpty?: boolean): Promise<PageDocument[]> {
  449. const baseQuery = this.find({ _id: { $in: pageIds } });
  450. const queryBuilder = new PageQueryBuilder(baseQuery, includeEmpty);
  451. await queryBuilder.addViewerCondition(user, userGroups);
  452. return queryBuilder.query.exec();
  453. };
  454. /*
  455. * Find a page by path and viewer. Pass false to useFindOne to use findOne method.
  456. */
  457. schema.statics.findByPathAndViewer = async function(
  458. path: string | null, user, userGroups = null, useFindOne = false, includeEmpty = false,
  459. ): Promise<(PageDocument | PageDocument[]) & HasObjectId | null> {
  460. if (path == null) {
  461. throw new Error('path is required.');
  462. }
  463. const baseQuery = useFindOne ? this.findOne({ path }) : this.find({ path });
  464. const includeAnyoneWithTheLink = useFindOne;
  465. const queryBuilder = new PageQueryBuilder(baseQuery, includeEmpty);
  466. await queryBuilder.addViewerCondition(user, userGroups, includeAnyoneWithTheLink);
  467. return queryBuilder.query.exec();
  468. };
  469. schema.statics.countByPathAndViewer = async function(path: string | null, user, userGroups = null, includeEmpty = false): Promise<number> {
  470. if (path == null) {
  471. throw new Error('path is required.');
  472. }
  473. const baseQuery = this.count({ path });
  474. const queryBuilder = new PageQueryBuilder(baseQuery, includeEmpty);
  475. await queryBuilder.addViewerCondition(user, userGroups);
  476. return queryBuilder.query.exec();
  477. };
  478. schema.statics.findRecentUpdatedPages = async function(
  479. path: string, user, options, includeEmpty = false,
  480. ): Promise<PaginatedPages> {
  481. const sortOpt = {};
  482. sortOpt[options.sort] = options.desc;
  483. const Page = this;
  484. const User = mongoose.model('User') as any;
  485. if (path == null) {
  486. throw new Error('path is required.');
  487. }
  488. const baseQuery = this.find({});
  489. const queryBuilder = new PageQueryBuilder(baseQuery, includeEmpty);
  490. if (!options.includeTrashed) {
  491. queryBuilder.addConditionToExcludeTrashed();
  492. }
  493. queryBuilder.addConditionToListWithDescendants(path, options);
  494. queryBuilder.populateDataToList(User.USER_FIELDS_EXCEPT_CONFIDENTIAL);
  495. await queryBuilder.addViewerCondition(user);
  496. const pages = await Page.paginate(queryBuilder.query.clone(), {
  497. lean: true, sort: sortOpt, offset: options.offset, limit: options.limit,
  498. });
  499. const results = {
  500. pages: pages.docs, totalCount: pages.totalDocs, offset: options.offset, limit: options.limit,
  501. };
  502. return results;
  503. };
  504. /*
  505. * Find all ancestor pages by path. When duplicate pages found, it uses the oldest page as a result
  506. * The result will include the target as well
  507. */
  508. schema.statics.findTargetAndAncestorsByPathOrId = async function(pathOrId: string, user, userGroups): Promise<TargetAndAncestorsResult> {
  509. let path;
  510. if (!hasSlash(pathOrId)) {
  511. const _id = pathOrId;
  512. const page = await this.findOne({ _id });
  513. path = page == null ? '/' : page.path;
  514. }
  515. else {
  516. path = pathOrId;
  517. }
  518. const ancestorPaths = collectAncestorPaths(path);
  519. ancestorPaths.push(path); // include target
  520. // Do not populate
  521. const queryBuilder = new PageQueryBuilder(this.find(), true);
  522. await queryBuilder.addViewerCondition(user, userGroups);
  523. const _targetAndAncestors: PageDocument[] = await queryBuilder
  524. .addConditionAsOnTree()
  525. .addConditionToListByPathsArray(ancestorPaths)
  526. .addConditionToMinimizeDataForRendering()
  527. .addConditionToSortPagesByDescPath()
  528. .query
  529. .lean()
  530. .exec();
  531. // no same path pages
  532. const ancestorsMap = new Map<string, PageDocument>();
  533. _targetAndAncestors.forEach(page => ancestorsMap.set(page.path, page));
  534. const targetAndAncestors = Array.from(ancestorsMap.values());
  535. const rootPage = targetAndAncestors[targetAndAncestors.length - 1];
  536. return { targetAndAncestors, rootPage };
  537. };
  538. /**
  539. * Create empty pages at paths at which no pages exist
  540. * @param paths Page paths
  541. * @param aggrPipelineForExistingPages AggregationPipeline object to find existing pages at paths
  542. */
  543. schema.statics.createEmptyPagesByPaths = async function(paths: string[], aggrPipelineForExistingPages: any[]): Promise<void> {
  544. const existingPages = await this.aggregate(aggrPipelineForExistingPages);
  545. const existingPagePaths = existingPages.map(page => page.path);
  546. const notExistingPagePaths = paths.filter(path => !existingPagePaths.includes(path));
  547. await this.insertMany(notExistingPagePaths.map(path => ({ path, isEmpty: true })));
  548. };
  549. /**
  550. * Find a parent page by path
  551. * @param {string} path
  552. * @returns {Promise<PageDocument | null>}
  553. */
  554. schema.statics.findParentByPath = async function(path: string): Promise<PageDocument | null> {
  555. const parentPath = nodePath.dirname(path);
  556. const builder = new PageQueryBuilder(this.find({ path: parentPath }), true);
  557. const pagesCanBeParent = await builder
  558. .addConditionAsOnTree()
  559. .query
  560. .exec();
  561. if (pagesCanBeParent.length >= 1) {
  562. return pagesCanBeParent[0]; // the earliest page will be the result
  563. }
  564. return null;
  565. };
  566. /*
  567. * Utils from obsolete-page.js
  568. */
  569. export async function pushRevision(pageData, newRevision, user) {
  570. await newRevision.save();
  571. pageData.revision = newRevision;
  572. pageData.lastUpdateUser = user?._id ?? user;
  573. pageData.updatedAt = Date.now();
  574. return pageData.save();
  575. }
  576. /**
  577. * add/subtract descendantCount of pages with provided paths by increment.
  578. * increment can be negative number
  579. */
  580. schema.statics.incrementDescendantCountOfPageIds = async function(pageIds: ObjectIdLike[], increment: number): Promise<void> {
  581. await this.updateMany({ _id: { $in: pageIds } }, { $inc: { descendantCount: increment } });
  582. };
  583. /**
  584. * recount descendantCount of a page with the provided id and return it
  585. */
  586. schema.statics.recountDescendantCount = async function(id: ObjectIdLike): Promise<number> {
  587. const res = await this.aggregate(
  588. [
  589. {
  590. $match: {
  591. parent: id,
  592. },
  593. },
  594. {
  595. $project: {
  596. parent: 1,
  597. isEmpty: 1,
  598. descendantCount: 1,
  599. },
  600. },
  601. {
  602. $group: {
  603. _id: '$parent',
  604. sumOfDescendantCount: {
  605. $sum: '$descendantCount',
  606. },
  607. sumOfDocsCount: {
  608. $sum: {
  609. $cond: { if: { $eq: ['$isEmpty', true] }, then: 0, else: 1 }, // exclude isEmpty true page from sumOfDocsCount
  610. },
  611. },
  612. },
  613. },
  614. {
  615. $set: {
  616. descendantCount: {
  617. $sum: ['$sumOfDescendantCount', '$sumOfDocsCount'],
  618. },
  619. },
  620. },
  621. ],
  622. );
  623. return res.length === 0 ? 0 : res[0].descendantCount;
  624. };
  625. schema.statics.findAncestorsUsingParentRecursively = async function(pageId: ObjectIdLike, shouldIncludeTarget: boolean) {
  626. const self = this;
  627. const target = await this.findById(pageId);
  628. if (target == null) {
  629. throw Error('Target not found');
  630. }
  631. async function findAncestorsRecursively(target, ancestors = shouldIncludeTarget ? [target] : []) {
  632. const parent = await self.findOne({ _id: target.parent });
  633. if (parent == null) {
  634. return ancestors;
  635. }
  636. return findAncestorsRecursively(parent, [...ancestors, parent]);
  637. }
  638. return findAncestorsRecursively(target);
  639. };
  640. // TODO: write test code
  641. /**
  642. * Recursively removes empty pages at leaf position.
  643. * @param pageId ObjectIdLike
  644. * @returns Promise<void>
  645. */
  646. schema.statics.removeLeafEmptyPagesRecursively = async function(pageId: ObjectIdLike): Promise<void> {
  647. const self = this;
  648. const initialPage = await this.findById(pageId);
  649. if (initialPage == null) {
  650. return;
  651. }
  652. if (!initialPage.isEmpty) {
  653. return;
  654. }
  655. async function generatePageIdsToRemove(childPage, page, pageIds: ObjectIdLike[] = []): Promise<ObjectIdLike[]> {
  656. if (!page.isEmpty) {
  657. return pageIds;
  658. }
  659. const isChildrenOtherThanTargetExist = await self.exists({ _id: { $ne: childPage?._id }, parent: page._id });
  660. if (isChildrenOtherThanTargetExist) {
  661. return pageIds;
  662. }
  663. pageIds.push(page._id);
  664. const nextPage = await self.findById(page.parent);
  665. if (nextPage == null) {
  666. return pageIds;
  667. }
  668. return generatePageIdsToRemove(page, nextPage, pageIds);
  669. }
  670. const pageIdsToRemove = await generatePageIdsToRemove(null, initialPage);
  671. await this.deleteMany({ _id: { $in: pageIdsToRemove } });
  672. };
  673. schema.statics.normalizeDescendantCountById = async function(pageId) {
  674. const children = await this.find({ parent: pageId });
  675. const sumChildrenDescendantCount = children.map(d => d.descendantCount).reduce((c1, c2) => c1 + c2);
  676. const sumChildPages = children.filter(p => !p.isEmpty).length;
  677. return this.updateOne({ _id: pageId }, { $set: { descendantCount: sumChildrenDescendantCount + sumChildPages } }, { new: true });
  678. };
  679. schema.statics.takeOffFromTree = async function(pageId: ObjectIdLike) {
  680. return this.findByIdAndUpdate(pageId, { $set: { parent: null } });
  681. };
  682. schema.statics.removeEmptyPages = async function(pageIdsToNotRemove: ObjectIdLike[], paths: string[]): Promise<void> {
  683. await this.deleteMany({
  684. _id: {
  685. $nin: pageIdsToNotRemove,
  686. },
  687. path: {
  688. $in: paths,
  689. },
  690. isEmpty: true,
  691. });
  692. };
  693. /**
  694. * Find a not empty parent recursively.
  695. * @param {string} path
  696. * @returns {Promise<PageDocument | null>}
  697. */
  698. schema.statics.findNotEmptyParentByPathRecursively = async function(path: string): Promise<PageDocument | null> {
  699. const parent = await this.findParentByPath(path);
  700. if (parent == null) {
  701. return null;
  702. }
  703. const recursive = async(page: PageDocument): Promise<PageDocument> => {
  704. if (!page.isEmpty) {
  705. return page;
  706. }
  707. const next = await this.findById(page.parent);
  708. if (next == null || isTopPage(next.path)) {
  709. return page;
  710. }
  711. return recursive(next);
  712. };
  713. const notEmptyParent = await recursive(parent);
  714. return notEmptyParent;
  715. };
  716. schema.statics.findParent = async function(pageId): Promise<PageDocument | null> {
  717. return this.findOne({ _id: pageId });
  718. };
  719. schema.statics.PageQueryBuilder = PageQueryBuilder as any; // mongoose does not support constructor type as statics attrs type
  720. export function generateGrantCondition(
  721. user, userGroups, includeAnyoneWithTheLink = false, showPagesRestrictedByOwner = false, showPagesRestrictedByGroup = false,
  722. ): { $or: any[] } {
  723. const grantConditions: AnyObject[] = [
  724. { grant: null },
  725. { grant: GRANT_PUBLIC },
  726. ];
  727. if (includeAnyoneWithTheLink) {
  728. grantConditions.push({ grant: GRANT_RESTRICTED });
  729. }
  730. if (showPagesRestrictedByOwner) {
  731. grantConditions.push(
  732. { grant: GRANT_SPECIFIED },
  733. { grant: GRANT_OWNER },
  734. );
  735. }
  736. else if (user != null) {
  737. grantConditions.push(
  738. { grant: GRANT_SPECIFIED, grantedUsers: user._id },
  739. { grant: GRANT_OWNER, grantedUsers: user._id },
  740. );
  741. }
  742. if (showPagesRestrictedByGroup) {
  743. grantConditions.push(
  744. { grant: GRANT_USER_GROUP },
  745. );
  746. }
  747. else if (userGroups != null && userGroups.length > 0) {
  748. grantConditions.push(
  749. { grant: GRANT_USER_GROUP, grantedGroup: { $in: userGroups } },
  750. );
  751. }
  752. return {
  753. $or: grantConditions,
  754. };
  755. }
  756. schema.statics.generateGrantCondition = generateGrantCondition;
  757. // find ancestor page with isEmpty: false. If parameter path is '/', return undefined
  758. schema.statics.findNonEmptyClosestAncestor = async function(path: string): Promise<PageDocument | undefined> {
  759. if (path === '/') {
  760. return;
  761. }
  762. const builderForAncestors = new PageQueryBuilder(this.find(), false); // empty page not included
  763. const ancestors = await builderForAncestors
  764. .addConditionToListOnlyAncestors(path) // only ancestor paths
  765. .addConditionToSortPagesByDescPath() // sort by path in Desc. Long to Short.
  766. .query
  767. .exec();
  768. return ancestors[0];
  769. };
  770. export type PageCreateOptions = {
  771. format?: string
  772. grantUserGroupId?: ObjectIdLike
  773. grant?: number
  774. }
  775. /*
  776. * Merge obsolete page model methods and define new methods which depend on crowi instance
  777. */
  778. // remove type for crowi to prevent 'import/no-cycle'
  779. // eslint-disable-next-line import/no-anonymous-default-export
  780. export default (crowi): any => {
  781. let pageEvent;
  782. if (crowi != null) {
  783. pageEvent = crowi.event('page');
  784. }
  785. const shouldUseUpdatePageV4 = (grant: number, isV5Compatible: boolean, isOnTree: boolean): boolean => {
  786. const isRestricted = grant === GRANT_RESTRICTED;
  787. return !isRestricted && (!isV5Compatible || !isOnTree);
  788. };
  789. schema.statics.emitPageEventUpdate = (page: IPageHasId, user: IUserHasId): void => {
  790. pageEvent.emit('update', page, user);
  791. };
  792. /**
  793. * A wrapper method of schema.statics.updatePage for updating grant only.
  794. * @param {PageDocument} page
  795. * @param {UserDocument} user
  796. * @param options
  797. */
  798. schema.statics.updateGrant = async function(page, user, grantData: {grant: PageGrant, grantedGroup: ObjectIdLike}) {
  799. const { grant, grantedGroup } = grantData;
  800. const options = {
  801. grant,
  802. grantUserGroupId: grantedGroup,
  803. isSyncRevisionToHackmd: false,
  804. };
  805. return this.updatePage(page, null, null, user, options);
  806. };
  807. schema.statics.updatePage = async function(
  808. pageData,
  809. body: string | null,
  810. previousBody: string | null,
  811. user,
  812. options: {grant?: PageGrant, grantUserGroupId?: ObjectIdLike, isSyncRevisionToHackmd?: boolean} = {},
  813. ) {
  814. if (crowi.configManager == null || crowi.pageGrantService == null || crowi.pageService == null) {
  815. throw Error('Crowi is not set up');
  816. }
  817. const wasOnTree = pageData.parent != null || isTopPage(pageData.path);
  818. const exParent = pageData.parent;
  819. const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
  820. const shouldUseV4Process = shouldUseUpdatePageV4(pageData.grant, isV5Compatible, wasOnTree);
  821. if (shouldUseV4Process) {
  822. // v4 compatible process
  823. return this.updatePageV4(pageData, body, previousBody, user, options);
  824. }
  825. const grant = options.grant ?? pageData.grant; // use the previous data if absence
  826. const grantUserGroupId: undefined | ObjectIdLike = options.grantUserGroupId ?? pageData.grantedGroup?._id.toString();
  827. const grantedUserIds = pageData.grantedUserIds || [user._id];
  828. const shouldBeOnTree = grant !== GRANT_RESTRICTED;
  829. const isChildrenExist = await this.count({ path: new RegExp(`^${escapeStringRegexp(addTrailingSlash(pageData.path))}`), parent: { $ne: null } });
  830. const newPageData = pageData;
  831. if (shouldBeOnTree) {
  832. let isGrantNormalized = false;
  833. try {
  834. isGrantNormalized = await crowi.pageGrantService.isGrantNormalized(user, pageData.path, grant, grantedUserIds, grantUserGroupId, true);
  835. }
  836. catch (err) {
  837. logger.error(`Failed to validate grant of page at "${pageData.path}" of grant ${grant}:`, err);
  838. throw err;
  839. }
  840. if (!isGrantNormalized) {
  841. throw Error('The selected grant or grantedGroup is not assignable to this page.');
  842. }
  843. if (!wasOnTree) {
  844. const newParent = await crowi.pageService.getParentAndFillAncestorsByUser(user, newPageData.path);
  845. newPageData.parent = newParent._id;
  846. }
  847. }
  848. else {
  849. if (wasOnTree && isChildrenExist) {
  850. // Update children's parent with new parent
  851. const newParentForChildren = await this.createEmptyPage(pageData.path, pageData.parent, pageData.descendantCount);
  852. await this.updateMany(
  853. { parent: pageData._id },
  854. { parent: newParentForChildren._id },
  855. );
  856. }
  857. newPageData.parent = null;
  858. newPageData.descendantCount = 0;
  859. }
  860. newPageData.applyScope(user, grant, grantUserGroupId);
  861. // update existing page
  862. let savedPage = await newPageData.save();
  863. // Update body
  864. const Revision = mongoose.model('Revision') as any; // TODO: Typescriptize model
  865. const isSyncRevisionToHackmd = options.isSyncRevisionToHackmd;
  866. const isBodyPresent = body != null && previousBody != null;
  867. const shouldUpdateBody = isBodyPresent;
  868. if (shouldUpdateBody) {
  869. const newRevision = await Revision.prepareRevision(newPageData, body, previousBody, user);
  870. savedPage = await pushRevision(savedPage, newRevision, user);
  871. await savedPage.populateDataToShowRevision();
  872. if (isSyncRevisionToHackmd) {
  873. savedPage = await this.syncRevisionToHackmd(savedPage);
  874. }
  875. }
  876. this.emitPageEventUpdate(savedPage, user);
  877. // Update ex children's parent
  878. if (!wasOnTree && shouldBeOnTree) {
  879. const emptyPageAtSamePath = await this.findOne({ path: pageData.path, isEmpty: true }); // this page is necessary to find children
  880. if (isChildrenExist) {
  881. if (emptyPageAtSamePath != null) {
  882. // Update children's parent with new parent
  883. await this.updateMany(
  884. { parent: emptyPageAtSamePath._id },
  885. { parent: savedPage._id },
  886. );
  887. }
  888. }
  889. await this.findOneAndDelete({ path: pageData.path, isEmpty: true }); // delete here
  890. }
  891. // Sub operation
  892. // 1. Update descendantCount
  893. const shouldPlusDescCount = !wasOnTree && shouldBeOnTree;
  894. const shouldMinusDescCount = wasOnTree && !shouldBeOnTree;
  895. if (shouldPlusDescCount) {
  896. await crowi.pageService.updateDescendantCountOfAncestors(newPageData._id, 1, false);
  897. const newDescendantCount = await this.recountDescendantCount(newPageData._id);
  898. await this.updateOne({ _id: newPageData._id }, { descendantCount: newDescendantCount });
  899. }
  900. else if (shouldMinusDescCount) {
  901. // Update from parent. Parent is null if newPageData.grant is RESTRECTED.
  902. if (newPageData.grant === GRANT_RESTRICTED) {
  903. await crowi.pageService.updateDescendantCountOfAncestors(exParent, -1, true);
  904. }
  905. }
  906. // 2. Delete unnecessary empty pages
  907. const shouldRemoveLeafEmpPages = wasOnTree && !isChildrenExist;
  908. if (shouldRemoveLeafEmpPages) {
  909. await this.removeLeafEmptyPagesRecursively(exParent);
  910. }
  911. return savedPage;
  912. };
  913. // add old page schema methods
  914. const pageSchema = getPageSchema(crowi);
  915. schema.methods = { ...pageSchema.methods, ...schema.methods };
  916. schema.statics = { ...pageSchema.statics, ...schema.statics };
  917. return getOrCreateModel<PageDocument, PageModel>('Page', schema as any); // TODO: improve type
  918. };