page.ts 32 KB

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