page.ts 30 KB

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