page.ts 29 KB

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