page.ts 32 KB

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