| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549 |
- import pathlib from 'path';
- import { Readable, Writable } from 'stream';
- import { pagePathUtils, pathUtils } from '@growi/core';
- import escapeStringRegexp from 'escape-string-regexp';
- import mongoose, { ObjectId, QueryCursor } from 'mongoose';
- import streamToPromise from 'stream-to-promise';
- import { Ref } from '~/interfaces/common';
- import { V5ConversionErrCode } from '~/interfaces/errors/v5-conversion-error';
- import { HasObjectId } from '~/interfaces/has-object-id';
- import {
- IPage, IPageInfo, IPageInfoForEntity, IPageWithMeta,
- } from '~/interfaces/page';
- import {
- PageDeleteConfigValue, IPageDeleteConfigValueToProcessValidation,
- } from '~/interfaces/page-delete-config';
- import { IPageOperationProcessInfo, IPageOperationProcessData } from '~/interfaces/page-operation';
- import { IUserHasId } from '~/interfaces/user';
- import { PageMigrationErrorData, SocketEventName, UpdateDescCountRawData } from '~/interfaces/websocket';
- import {
- CreateMethod, PageCreateOptions, PageModel, PageDocument, pushRevision, PageQueryBuilder,
- } from '~/server/models/page';
- import { createBatchStream } from '~/server/util/batch-stream';
- import loggerFactory from '~/utils/logger';
- import { prepareDeleteConfigValuesForCalc } from '~/utils/page-delete-config';
- import PageEvent from '../events/page';
- import { ObjectIdLike } from '../interfaces/mongoose-utils';
- import { PathAlreadyExistsError } from '../models/errors';
- import PageOperation, { PageActionStage, PageActionType, PageOperationDocument } from '../models/page-operation';
- import { PageRedirectModel } from '../models/page-redirect';
- import { serializePageSecurely } from '../models/serializers/page-serializer';
- import Subscription from '../models/subscription';
- import { V5ConversionError } from '../models/vo/v5-conversion-error';
- const debug = require('debug')('growi:services:page');
- const logger = loggerFactory('growi:services:page');
- const {
- isTrashPage, isTopPage, omitDuplicateAreaPageFromPages,
- collectAncestorPaths, isMovablePage, canMoveByPath, isUsersProtectedPages, hasSlash, generateChildrenRegExp,
- } = pagePathUtils;
- const { addTrailingSlash } = pathUtils;
- const BULK_REINDEX_SIZE = 100;
- const LIMIT_FOR_MULTIPLE_PAGE_OP = 20;
- // TODO: improve type
- class PageCursorsForDescendantsFactory {
- private user: any; // TODO: Typescriptize model
- private rootPage: any; // TODO: wait for mongoose update
- private shouldIncludeEmpty: boolean;
- private initialCursor: QueryCursor<any> | never[]; // TODO: wait for mongoose update
- private Page: PageModel;
- constructor(user: any, rootPage: any, shouldIncludeEmpty: boolean) {
- this.user = user;
- this.rootPage = rootPage;
- this.shouldIncludeEmpty = shouldIncludeEmpty;
- this.Page = mongoose.model('Page') as unknown as PageModel;
- }
- // prepare initial cursor
- private async init() {
- const initialCursor = await this.generateCursorToFindChildren(this.rootPage);
- this.initialCursor = initialCursor;
- }
- /**
- * Returns Iterable that yields only descendant pages unorderedly
- * @returns Promise<AsyncGenerator>
- */
- async generateIterable(): Promise<AsyncGenerator | never[]> {
- // initialize cursor
- await this.init();
- return this.isNeverArray(this.initialCursor) ? [] : this.generateOnlyDescendants(this.initialCursor);
- }
- /**
- * Returns Readable that produces only descendant pages unorderedly
- * @returns Promise<Readable>
- */
- async generateReadable(): Promise<Readable> {
- return Readable.from(await this.generateIterable());
- }
- /**
- * Generator that unorderedly yields descendant pages
- */
- private async* generateOnlyDescendants(cursor: QueryCursor<any>) {
- for await (const page of cursor) {
- const nextCursor = await this.generateCursorToFindChildren(page);
- if (!this.isNeverArray(nextCursor)) {
- yield* this.generateOnlyDescendants(nextCursor); // recursively yield
- }
- yield page;
- }
- }
- private async generateCursorToFindChildren(page: any): Promise<QueryCursor<any> | never[]> {
- if (page == null) {
- return [];
- }
- const { PageQueryBuilder } = this.Page;
- const builder = new PageQueryBuilder(this.Page.find(), this.shouldIncludeEmpty);
- builder.addConditionToFilteringByParentId(page._id);
- const cursor = builder.query.lean().cursor({ batchSize: BULK_REINDEX_SIZE }) as QueryCursor<any>;
- return cursor;
- }
- private isNeverArray(val: QueryCursor<any> | never[]): val is never[] {
- return 'length' in val && val.length === 0;
- }
- }
- class PageService {
- crowi: any;
- pageEvent: any;
- tagEvent: any;
- constructor(crowi) {
- this.crowi = crowi;
- this.pageEvent = crowi.event('page');
- this.tagEvent = crowi.event('tag');
- // init
- this.initPageEvent();
- }
- private initPageEvent() {
- // create
- this.pageEvent.on('create', this.pageEvent.onCreate);
- // createMany
- this.pageEvent.on('createMany', this.pageEvent.onCreateMany);
- this.pageEvent.on('addSeenUsers', this.pageEvent.onAddSeenUsers);
- }
- canDeleteCompletely(path: string, creatorId: ObjectIdLike, operator: any | null, isRecursively: boolean): boolean {
- if (operator == null || isTopPage(path) || isUsersProtectedPages(path)) return false;
- const pageCompleteDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority');
- const pageRecursiveCompleteDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageRecursiveCompleteDeletionAuthority');
- const [singleAuthority, recursiveAuthority] = prepareDeleteConfigValuesForCalc(pageCompleteDeletionAuthority, pageRecursiveCompleteDeletionAuthority);
- return this.canDeleteLogic(creatorId, operator, isRecursively, singleAuthority, recursiveAuthority);
- }
- canDelete(path: string, creatorId: ObjectIdLike, operator: any | null, isRecursively: boolean): boolean {
- if (operator == null || isUsersProtectedPages(path) || isTopPage(path)) return false;
- const pageDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageDeletionAuthority');
- const pageRecursiveDeletionAuthority = this.crowi.configManager.getConfig('crowi', 'security:pageRecursiveDeletionAuthority');
- const [singleAuthority, recursiveAuthority] = prepareDeleteConfigValuesForCalc(pageDeletionAuthority, pageRecursiveDeletionAuthority);
- return this.canDeleteLogic(creatorId, operator, isRecursively, singleAuthority, recursiveAuthority);
- }
- private canDeleteLogic(
- creatorId: ObjectIdLike,
- operator,
- isRecursively: boolean,
- authority: IPageDeleteConfigValueToProcessValidation | null,
- recursiveAuthority: IPageDeleteConfigValueToProcessValidation | null,
- ): boolean {
- const isAdmin = operator?.admin ?? false;
- const isOperator = operator?._id == null ? false : operator._id.equals(creatorId);
- if (isRecursively) {
- return this.compareDeleteConfig(isAdmin, isOperator, recursiveAuthority);
- }
- return this.compareDeleteConfig(isAdmin, isOperator, authority);
- }
- private compareDeleteConfig(isAdmin: boolean, isOperator: boolean, authority: IPageDeleteConfigValueToProcessValidation | null): boolean {
- if (isAdmin) {
- return true;
- }
- if (authority === PageDeleteConfigValue.Anyone || authority == null) {
- return true;
- }
- if (authority === PageDeleteConfigValue.AdminAndAuthor && isOperator) {
- return true;
- }
- return false;
- }
- filterPagesByCanDeleteCompletely(pages, user, isRecursively: boolean) {
- return pages.filter(p => p.isEmpty || this.canDeleteCompletely(p.path, p.creator, user, isRecursively));
- }
- filterPagesByCanDelete(pages, user, isRecursively: boolean) {
- return pages.filter(p => p.isEmpty || this.canDelete(p.path, p.creator, user, isRecursively));
- }
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
- async findPageAndMetaDataByViewer(pageId: string, path: string, user: IUserHasId, includeEmpty = false, isSharedPage = false): Promise<IPageWithMeta|null> {
- const Page = this.crowi.model('Page');
- let page: PageModel & PageDocument & HasObjectId;
- if (pageId != null) { // prioritized
- page = await Page.findByIdAndViewer(pageId, user, null, includeEmpty);
- }
- else {
- page = await Page.findByPathAndViewer(path, user, null, includeEmpty);
- }
- if (page == null) {
- return null;
- }
- if (isSharedPage) {
- return {
- data: page,
- meta: {
- isV5Compatible: isTopPage(page.path) || page.parent != null,
- isEmpty: page.isEmpty,
- isMovable: false,
- isDeletable: false,
- isAbleToDeleteCompletely: false,
- isRevertible: false,
- },
- };
- }
- const isGuestUser = user == null;
- const pageInfo = this.constructBasicPageInfo(page, isGuestUser);
- const Bookmark = this.crowi.model('Bookmark');
- const bookmarkCount = await Bookmark.countByPageId(pageId);
- const metadataForGuest = {
- ...pageInfo,
- bookmarkCount,
- };
- if (isGuestUser) {
- return {
- data: page,
- meta: metadataForGuest,
- };
- }
- const isBookmarked: boolean = (await Bookmark.findByPageIdAndUserId(pageId, user._id)) != null;
- const isLiked: boolean = page.isLiked(user);
- const subscription = await Subscription.findByUserIdAndTargetId(user._id, pageId);
- let creatorId = page.creator;
- if (page.isEmpty) {
- // Need non-empty ancestor page to get its creatorId because empty page does NOT have it.
- // Use creatorId of ancestor page to determine whether the empty page is deletable
- const notEmptyClosestAncestor = await Page.findNonEmptyClosestAncestor(page.path);
- creatorId = notEmptyClosestAncestor.creator;
- }
- const isDeletable = this.canDelete(page.path, creatorId, user, false);
- const isAbleToDeleteCompletely = this.canDeleteCompletely(page.path, creatorId, user, false); // use normal delete config
- return {
- data: page,
- meta: {
- ...metadataForGuest,
- isDeletable,
- isAbleToDeleteCompletely,
- isBookmarked,
- isLiked,
- subscriptionStatus: subscription?.status,
- },
- };
- }
- private shouldUseV4Process(page): boolean {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const isTrashPage = page.status === Page.STATUS_DELETED;
- const isPageMigrated = page.parent != null;
- const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
- const isRoot = isTopPage(page.path);
- const isPageRestricted = page.grant === Page.GRANT_RESTRICTED;
- const shouldUseV4Process = !isRoot && (!isV5Compatible || !isPageMigrated || isTrashPage || isPageRestricted);
- return shouldUseV4Process;
- }
- private shouldUseV4ProcessForRevert(page): boolean {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
- const isPageRestricted = page.grant === Page.GRANT_RESTRICTED;
- const shouldUseV4Process = !isV5Compatible || isPageRestricted;
- return shouldUseV4Process;
- }
- private shouldNormalizeParent(page): boolean {
- const Page = mongoose.model('Page') as unknown as PageModel;
- return page.grant !== Page.GRANT_RESTRICTED && page.grant !== Page.GRANT_SPECIFIED;
- }
- /**
- * Generate read stream to operate descendants of the specified page path
- * @param {string} targetPagePath
- * @param {User} viewer
- */
- private async generateReadStreamToOperateOnlyDescendants(targetPagePath, userToOperate) {
- const Page = this.crowi.model('Page');
- const { PageQueryBuilder } = Page;
- const builder = new PageQueryBuilder(Page.find(), true)
- .addConditionAsNotMigrated() // to avoid affecting v5 pages
- .addConditionToListOnlyDescendants(targetPagePath);
- await Page.addConditionToFilteringByViewerToEdit(builder, userToOperate);
- return builder
- .query
- .lean()
- .cursor({ batchSize: BULK_REINDEX_SIZE });
- }
- async renamePage(page, newPagePath, user, options) {
- /*
- * Common Operation
- */
- const Page = mongoose.model('Page') as unknown as PageModel;
- const isExist = await Page.exists({ path: newPagePath });
- if (isExist) {
- throw Error(`Page already exists at ${newPagePath}`);
- }
- if (isTopPage(page.path)) {
- throw Error('It is forbidden to rename the top page');
- }
- // Separate v4 & v5 process
- const shouldUseV4Process = this.shouldUseV4Process(page);
- if (shouldUseV4Process) {
- return this.renamePageV4(page, newPagePath, user, options);
- }
- if (options.isMoveMode) {
- const fromPath = page.path;
- const toPath = newPagePath;
- const canMove = canMoveByPath(fromPath, toPath) && await Page.exists({ path: newPagePath });
- if (!canMove) {
- throw Error('Cannot move to this path.');
- }
- }
- const canOperate = await this.crowi.pageOperationService.canOperate(true, page.path, newPagePath);
- if (!canOperate) {
- throw Error(`Cannot operate rename to path "${newPagePath}" right now.`);
- }
- /*
- * Resumable Operation
- */
- let pageOp;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.Rename,
- actionStage: PageActionStage.Main,
- page,
- user,
- fromPath: page.path,
- toPath: newPagePath,
- options,
- });
- }
- catch (err) {
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- const renamedPage = await this.renameMainOperation(page, newPagePath, user, options, pageOp._id);
- return renamedPage;
- }
- async renameMainOperation(page, newPagePath: string, user, options, pageOpId: ObjectIdLike) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const updateMetadata = options.updateMetadata || false;
- // sanitize path
- newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
- // UserGroup & Owner validation
- // use the parent's grant when target page is an empty page
- let grant;
- let grantedUserIds;
- let grantedGroupId;
- if (page.isEmpty) {
- const parent = await Page.findOne({ _id: page.parent });
- if (parent == null) {
- throw Error('parent not found');
- }
- grant = parent.grant;
- grantedUserIds = parent.grantedUsers;
- grantedGroupId = parent.grantedGroup;
- }
- else {
- grant = page.grant;
- grantedUserIds = page.grantedUsers;
- grantedGroupId = page.grantedGroup;
- }
- if (grant !== Page.GRANT_RESTRICTED) {
- let isGrantNormalized = false;
- try {
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, newPagePath, grant, grantedUserIds, grantedGroupId, false);
- }
- catch (err) {
- logger.error(`Failed to validate grant of page at "${newPagePath}" when renaming`, err);
- throw err;
- }
- if (!isGrantNormalized) {
- throw Error(`This page cannot be renamed to "${newPagePath}" since the selected grant or grantedGroup is not assignable to this page.`);
- }
- }
- // 1. Take target off from tree
- await Page.takeOffFromTree(page._id);
- // 2. Find new parent
- let newParent;
- // If renaming to under target, run getParentAndforceCreateEmptyTree to fill new ancestors
- if (this.isRenamingToUnderTarget(page.path, newPagePath)) {
- newParent = await this.getParentAndforceCreateEmptyTree(page, newPagePath);
- }
- else {
- newParent = await this.getParentAndFillAncestorsByUser(user, newPagePath);
- }
- // 3. Put back target page to tree (also update the other attrs)
- const update: Partial<IPage> = {};
- update.path = newPagePath;
- update.parent = newParent._id;
- if (updateMetadata) {
- update.lastUpdateUser = user;
- update.updatedAt = new Date();
- }
- const renamedPage = await Page.findByIdAndUpdate(page._id, { $set: update }, { new: true });
- this.pageEvent.emit('rename', page, user);
- // 5.increase parent's descendantCount.
- // see: https://dev.growi.org/62149d019311629d4ecd91cf#Handling%20of%20descendantCount%20in%20case%20of%20unexpected%20process%20interruption
- const nToIncreaseForOperationInterruption = 1;
- await Page.incrementDescendantCountOfPageIds([newParent._id], nToIncreaseForOperationInterruption);
- // create page redirect
- if (options.createRedirectPage) {
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- await PageRedirect.create({ fromPath: page.path, toPath: newPagePath });
- }
- // Set to Sub
- const pageOp = await PageOperation.findByIdAndUpdatePageActionStage(pageOpId, PageActionStage.Sub);
- if (pageOp == null) {
- throw Error('PageOperation document not found');
- }
- /*
- * Sub Operation
- */
- this.renameSubOperation(page, newPagePath, user, options, renamedPage, pageOp._id);
- return renamedPage;
- }
- async renameSubOperation(page, newPagePath: string, user, options, renamedPage, pageOpId: ObjectIdLike): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const exParentId = page.parent;
- const timerObj = this.crowi.pageOperationService.autoUpdateExpiryDate(pageOpId);
- try {
- // update descendants first
- await this.renameDescendantsWithStream(page, newPagePath, user, options, false);
- }
- catch (err) {
- logger.warn(err);
- throw Error(err);
- }
- finally {
- this.crowi.pageOperationService.clearAutoUpdateInterval(timerObj);
- }
- // reduce parent's descendantCount
- // see: https://dev.growi.org/62149d019311629d4ecd91cf#Handling%20of%20descendantCount%20in%20case%20of%20unexpected%20process%20interruption
- const nToReduceForOperationInterruption = -1;
- await Page.incrementDescendantCountOfPageIds([renamedPage.parent], nToReduceForOperationInterruption);
- const nToReduce = -1 * ((page.isEmpty ? 0 : 1) + page.descendantCount);
- await this.updateDescendantCountOfAncestors(exParentId, nToReduce, true);
- // increase ancestore's descendantCount
- const nToIncrease = (renamedPage.isEmpty ? 0 : 1) + page.descendantCount;
- await this.updateDescendantCountOfAncestors(renamedPage._id, nToIncrease, false);
- // Remove leaf empty pages if not moving to under the ex-target position
- if (!this.isRenamingToUnderTarget(page.path, newPagePath)) {
- // remove empty pages at leaf position
- await Page.removeLeafEmptyPagesRecursively(page.parent);
- }
- await PageOperation.findByIdAndDelete(pageOpId);
- }
- async resumeRenameSubOperation(renamedPage: PageDocument, pageOp: PageOperationDocument): Promise<void> {
- const isProcessable = pageOp.isProcessable();
- if (!isProcessable) {
- throw Error('This page operation is currently being processed');
- }
- if (pageOp.toPath == null) {
- throw Error(`Property toPath is missing which is needed to resume rename operation(${pageOp._id})`);
- }
- const {
- page, fromPath, toPath, options, user,
- } = pageOp;
- this.fixPathsAndDescendantCountOfAncestors(page, user, options, renamedPage, pageOp._id, fromPath, toPath);
- }
- /**
- * Renaming paths and fixing descendantCount of ancestors. It shoud be run synchronously.
- * `renameSubOperation` to restart rename operation
- * `updateDescendantCountOfPagesWithPaths` to fix descendantCount of ancestors
- */
- private async fixPathsAndDescendantCountOfAncestors(page, user, options, renamedPage, pageOpId, fromPath, toPath): Promise<void> {
- await this.renameSubOperation(page, toPath, user, options, renamedPage, pageOpId);
- const ancestorsPaths = this.crowi.pageOperationService.getAncestorsPathsByFromAndToPath(fromPath, toPath);
- await this.updateDescendantCountOfPagesWithPaths(ancestorsPaths);
- }
- private isRenamingToUnderTarget(fromPath: string, toPath: string): boolean {
- const pathToTest = escapeStringRegexp(addTrailingSlash(fromPath));
- const pathToBeTested = toPath;
- return (new RegExp(`^${pathToTest}`, 'i')).test(pathToBeTested);
- }
- private async getParentAndforceCreateEmptyTree(originalPage, toPath: string) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const fromPath = originalPage.path;
- const newParentPath = pathlib.dirname(toPath);
- // local util
- const collectAncestorPathsUntilFromPath = (path: string, paths: string[] = []): string[] => {
- if (path === fromPath) return paths;
- const parentPath = pathlib.dirname(path);
- paths.push(parentPath);
- return collectAncestorPathsUntilFromPath(parentPath, paths);
- };
- const pathsToInsert = collectAncestorPathsUntilFromPath(toPath);
- const originalParent = await Page.findById(originalPage.parent);
- if (originalParent == null) {
- throw Error('Original parent not found');
- }
- const insertedPages = await Page.insertMany(pathsToInsert.map((path) => {
- return {
- path,
- isEmpty: true,
- };
- }));
- const pages = [...insertedPages, originalParent];
- const ancestorsMap = new Map<string, PageDocument & {_id: any}>(pages.map(p => [p.path, p]));
- // bulkWrite to update ancestors
- const operations = insertedPages.map((page) => {
- const parentPath = pathlib.dirname(page.path);
- const op = {
- updateOne: {
- filter: {
- _id: page._id,
- },
- update: {
- $set: {
- parent: ancestorsMap.get(parentPath)?._id,
- descedantCount: originalParent.descendantCount,
- },
- },
- },
- };
- return op;
- });
- await Page.bulkWrite(operations);
- const newParent = ancestorsMap.get(newParentPath);
- return newParent;
- }
- private async renamePageV4(page, newPagePath, user, options) {
- const Page = this.crowi.model('Page');
- const Revision = this.crowi.model('Revision');
- const {
- isRecursively = false,
- createRedirectPage = false,
- updateMetadata = false,
- } = options;
- // sanitize path
- newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
- // create descendants first
- if (isRecursively) {
- await this.renameDescendantsWithStream(page, newPagePath, user, options);
- }
- const update: any = {};
- // update Page
- update.path = newPagePath;
- if (updateMetadata) {
- update.lastUpdateUser = user;
- update.updatedAt = Date.now();
- }
- const renamedPage = await Page.findByIdAndUpdate(page._id, { $set: update }, { new: true });
- // update Rivisions
- await Revision.updateRevisionListByPageId(renamedPage._id, { pageId: renamedPage._id });
- if (createRedirectPage) {
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- await PageRedirect.create({ fromPath: page.path, toPath: newPagePath });
- }
- this.pageEvent.emit('rename');
- return renamedPage;
- }
- private async renameDescendants(pages, user, options, oldPagePathPrefix, newPagePathPrefix, shouldUseV4Process = true) {
- // v4 compatible process
- if (shouldUseV4Process) {
- return this.renameDescendantsV4(pages, user, options, oldPagePathPrefix, newPagePathPrefix);
- }
- const Page = mongoose.model('Page') as unknown as PageModel;
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- const { updateMetadata, createRedirectPage } = options;
- const updatePathOperations: any[] = [];
- const insertPageRedirectOperations: any[] = [];
- pages.forEach((page) => {
- const newPagePath = page.path.replace(oldPagePathPrefix, newPagePathPrefix);
- // increment updatePathOperations
- let update;
- if (!page.isEmpty && updateMetadata) {
- update = {
- $set: { path: newPagePath, lastUpdateUser: user._id, updatedAt: new Date() },
- };
- }
- else {
- update = {
- $set: { path: newPagePath },
- };
- }
- if (!page.isEmpty && createRedirectPage) {
- // insert PageRedirect
- insertPageRedirectOperations.push({
- insertOne: {
- document: {
- fromPath: page.path,
- toPath: newPagePath,
- },
- },
- });
- }
- updatePathOperations.push({
- updateOne: {
- filter: {
- _id: page._id,
- },
- update,
- },
- });
- });
- try {
- await Page.bulkWrite(updatePathOperations);
- }
- catch (err) {
- if (err.code !== 11000) {
- throw new Error(`Failed to rename pages: ${err}`);
- }
- }
- try {
- await PageRedirect.bulkWrite(insertPageRedirectOperations);
- }
- catch (err) {
- if (err.code !== 11000) {
- throw Error(`Failed to create PageRedirect documents: ${err}`);
- }
- }
- this.pageEvent.emit('updateMany', pages, user);
- }
- private async renameDescendantsV4(pages, user, options, oldPagePathPrefix, newPagePathPrefix) {
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- const pageCollection = mongoose.connection.collection('pages');
- const { updateMetadata, createRedirectPage } = options;
- const unorderedBulkOp = pageCollection.initializeUnorderedBulkOp();
- const insertPageRedirectOperations: any[] = [];
- pages.forEach((page) => {
- const newPagePath = page.path.replace(oldPagePathPrefix, newPagePathPrefix);
- if (updateMetadata) {
- unorderedBulkOp
- .find({ _id: page._id })
- .update({ $set: { path: newPagePath, lastUpdateUser: user._id, updatedAt: new Date() } });
- }
- else {
- unorderedBulkOp.find({ _id: page._id }).update({ $set: { path: newPagePath } });
- }
- // insert PageRedirect
- if (!page.isEmpty && createRedirectPage) {
- insertPageRedirectOperations.push({
- insertOne: {
- document: {
- fromPath: page.path,
- toPath: newPagePath,
- },
- },
- });
- }
- });
- try {
- await unorderedBulkOp.execute();
- }
- catch (err) {
- if (err.code !== 11000) {
- throw new Error(`Failed to rename pages: ${err}`);
- }
- }
- try {
- await PageRedirect.bulkWrite(insertPageRedirectOperations);
- }
- catch (err) {
- if (err.code !== 11000) {
- throw Error(`Failed to create PageRedirect documents: ${err}`);
- }
- }
- this.pageEvent.emit('updateMany', pages, user);
- }
- private async renameDescendantsWithStream(targetPage, newPagePath, user, options = {}, shouldUseV4Process = true) {
- // v4 compatible process
- if (shouldUseV4Process) {
- return this.renameDescendantsWithStreamV4(targetPage, newPagePath, user, options);
- }
- const factory = new PageCursorsForDescendantsFactory(user, targetPage, true);
- const readStream = await factory.generateReadable();
- const newPagePathPrefix = newPagePath;
- const pathRegExp = new RegExp(`^${escapeStringRegexp(targetPage.path)}`, 'i');
- const renameDescendants = this.renameDescendants.bind(this);
- const pageEvent = this.pageEvent;
- let count = 0;
- const writeStream = new Writable({
- objectMode: true,
- async write(batch, encoding, callback) {
- try {
- count += batch.length;
- await renameDescendants(
- batch, user, options, pathRegExp, newPagePathPrefix, shouldUseV4Process,
- );
- logger.debug(`Renaming pages progressing: (count=${count})`);
- }
- catch (err) {
- logger.error('Renaming error on add anyway: ', err);
- }
- callback();
- },
- async final(callback) {
- logger.debug(`Renaming pages has completed: (totalCount=${count})`);
- // update path
- targetPage.path = newPagePath;
- pageEvent.emit('syncDescendantsUpdate', targetPage, user);
- callback();
- },
- });
- readStream
- .pipe(createBatchStream(BULK_REINDEX_SIZE))
- .pipe(writeStream);
- await streamToPromise(writeStream);
- }
- private async renameDescendantsWithStreamV4(targetPage, newPagePath, user, options = {}) {
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
- const newPagePathPrefix = newPagePath;
- const pathRegExp = new RegExp(`^${escapeStringRegexp(targetPage.path)}`, 'i');
- const renameDescendants = this.renameDescendants.bind(this);
- const pageEvent = this.pageEvent;
- let count = 0;
- const writeStream = new Writable({
- objectMode: true,
- async write(batch, encoding, callback) {
- try {
- count += batch.length;
- await renameDescendants(batch, user, options, pathRegExp, newPagePathPrefix);
- logger.debug(`Renaming pages progressing: (count=${count})`);
- }
- catch (err) {
- logger.error('renameDescendants error on add anyway: ', err);
- }
- callback();
- },
- final(callback) {
- logger.debug(`Renaming pages has completed: (totalCount=${count})`);
- // update path
- targetPage.path = newPagePath;
- pageEvent.emit('syncDescendantsUpdate', targetPage, user);
- callback();
- },
- });
- readStream
- .pipe(createBatchStream(BULK_REINDEX_SIZE))
- .pipe(writeStream);
- await streamToPromise(writeStream);
- }
- /*
- * Duplicate
- */
- async duplicate(page, newPagePath, user, isRecursively) {
- /*
- * Common Operation
- */
- const isEmptyAndNotRecursively = page?.isEmpty && !isRecursively;
- if (page == null || isEmptyAndNotRecursively) {
- throw new Error('Cannot find or duplicate the empty page');
- }
- const Page = mongoose.model('Page') as unknown as PageModel;
- const PageTagRelation = mongoose.model('PageTagRelation') as any; // TODO: Typescriptize model
- if (!isRecursively && page.isEmpty) {
- throw Error('Page not found.');
- }
- newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
- // 1. Separate v4 & v5 process
- const shouldUseV4Process = this.shouldUseV4Process(page);
- if (shouldUseV4Process) {
- return this.duplicateV4(page, newPagePath, user, isRecursively);
- }
- const canOperate = await this.crowi.pageOperationService.canOperate(isRecursively, page.path, newPagePath);
- if (!canOperate) {
- throw Error(`Cannot operate duplicate to path "${newPagePath}" right now.`);
- }
- // 2. UserGroup & Owner validation
- // use the parent's grant when target page is an empty page
- let grant;
- let grantedUserIds;
- let grantedGroupId;
- if (page.isEmpty) {
- const parent = await Page.findOne({ _id: page.parent });
- if (parent == null) {
- throw Error('parent not found');
- }
- grant = parent.grant;
- grantedUserIds = parent.grantedUsers;
- grantedGroupId = parent.grantedGroup;
- }
- else {
- grant = page.grant;
- grantedUserIds = page.grantedUsers;
- grantedGroupId = page.grantedGroup;
- }
- if (grant !== Page.GRANT_RESTRICTED) {
- let isGrantNormalized = false;
- try {
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, newPagePath, grant, grantedUserIds, grantedGroupId, false);
- }
- catch (err) {
- logger.error(`Failed to validate grant of page at "${newPagePath}" when duplicating`, err);
- throw err;
- }
- if (!isGrantNormalized) {
- throw Error(`This page cannot be duplicated to "${newPagePath}" since the selected grant or grantedGroup is not assignable to this page.`);
- }
- }
- // copy & populate (reason why copy: SubOperation only allows non-populated page document)
- const copyPage = { ...page };
- // 3. Duplicate target
- const options: PageCreateOptions = {
- grant: page.grant,
- grantUserGroupId: page.grantedGroup,
- };
- let duplicatedTarget;
- if (page.isEmpty) {
- const parent = await this.getParentAndFillAncestorsByUser(user, newPagePath);
- duplicatedTarget = await Page.createEmptyPage(newPagePath, parent);
- }
- else {
- await page.populate({ path: 'revision', model: 'Revision', select: 'body' });
- duplicatedTarget = await (this.create as CreateMethod)(
- newPagePath, page.revision.body, user, options,
- );
- }
- this.pageEvent.emit('duplicate', page, user);
- // 4. Take over tags
- const originTags = await page.findRelatedTagsById();
- let savedTags = [];
- if (originTags.length !== 0) {
- await PageTagRelation.updatePageTags(duplicatedTarget._id, originTags);
- savedTags = await PageTagRelation.listTagNamesByPage(duplicatedTarget._id);
- this.tagEvent.emit('update', duplicatedTarget, savedTags);
- }
- if (isRecursively) {
- /*
- * Resumable Operation
- */
- let pageOp;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.Duplicate,
- actionStage: PageActionStage.Main,
- page: copyPage,
- user,
- fromPath: page.path,
- toPath: newPagePath,
- });
- }
- catch (err) {
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- this.duplicateRecursivelyMainOperation(page, newPagePath, user, pageOp._id);
- }
- const result = serializePageSecurely(duplicatedTarget);
- result.tags = savedTags;
- return result;
- }
- async duplicateRecursivelyMainOperation(page, newPagePath: string, user, pageOpId: ObjectIdLike): Promise<void> {
- const nDuplicatedPages = await this.duplicateDescendantsWithStream(page, newPagePath, user, false);
- // normalize parent of descendant pages
- const shouldNormalize = this.shouldNormalizeParent(page);
- if (shouldNormalize) {
- try {
- await this.normalizeParentAndDescendantCountOfDescendants(newPagePath, user);
- logger.info(`Successfully normalized duplicated descendant pages under "${newPagePath}"`);
- }
- catch (err) {
- logger.error('Failed to normalize descendants afrer duplicate:', err);
- throw err;
- }
- }
- // Set to Sub
- const pageOp = await PageOperation.findByIdAndUpdatePageActionStage(pageOpId, PageActionStage.Sub);
- if (pageOp == null) {
- throw Error('PageOperation document not found');
- }
- /*
- * Sub Operation
- */
- await this.duplicateRecursivelySubOperation(newPagePath, nDuplicatedPages, pageOp._id);
- }
- async duplicateRecursivelySubOperation(newPagePath: string, nDuplicatedPages: number, pageOpId: ObjectIdLike): Promise<void> {
- const Page = mongoose.model('Page');
- const newTarget = await Page.findOne({ path: newPagePath }); // only one page will be found since duplicating to existing path is forbidden
- if (newTarget == null) {
- throw Error('No duplicated page found. Something might have gone wrong in duplicateRecursivelyMainOperation.');
- }
- await this.updateDescendantCountOfAncestors(newTarget._id, nDuplicatedPages, false);
- await PageOperation.findByIdAndDelete(pageOpId);
- }
- async duplicateV4(page, newPagePath, user, isRecursively) {
- const PageTagRelation = mongoose.model('PageTagRelation') as any; // TODO: Typescriptize model
- // populate
- await page.populate({ path: 'revision', model: 'Revision', select: 'body' });
- // create option
- const options: any = { page };
- options.grant = page.grant;
- options.grantUserGroupId = page.grantedGroup;
- options.grantedUserIds = page.grantedUsers;
- newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
- const createdPage = await this.crowi.pageService.create(
- newPagePath, page.revision.body, user, options,
- );
- this.pageEvent.emit('duplicate', page, user);
- if (isRecursively) {
- this.duplicateDescendantsWithStream(page, newPagePath, user);
- }
- // take over tags
- const originTags = await page.findRelatedTagsById();
- let savedTags = [];
- if (originTags != null) {
- await PageTagRelation.updatePageTags(createdPage.id, originTags);
- savedTags = await PageTagRelation.listTagNamesByPage(createdPage.id);
- this.tagEvent.emit('update', createdPage, savedTags);
- }
- const result = serializePageSecurely(createdPage);
- result.tags = savedTags;
- return result;
- }
- /**
- * Receive the object with oldPageId and newPageId and duplicate the tags from oldPage to newPage
- * @param {Object} pageIdMapping e.g. key: oldPageId, value: newPageId
- */
- private async duplicateTags(pageIdMapping) {
- const PageTagRelation = mongoose.model('PageTagRelation');
- // convert pageId from string to ObjectId
- const pageIds = Object.keys(pageIdMapping);
- const stage = { $or: pageIds.map((pageId) => { return { relatedPage: new mongoose.Types.ObjectId(pageId) } }) };
- const pagesAssociatedWithTag = await PageTagRelation.aggregate([
- {
- $match: stage,
- },
- {
- $group: {
- _id: '$relatedTag',
- relatedPages: { $push: '$relatedPage' },
- },
- },
- ]);
- const newPageTagRelation: any[] = [];
- pagesAssociatedWithTag.forEach(({ _id, relatedPages }) => {
- // relatedPages
- relatedPages.forEach((pageId) => {
- newPageTagRelation.push({
- relatedPage: pageIdMapping[pageId], // newPageId
- relatedTag: _id,
- });
- });
- });
- return PageTagRelation.insertMany(newPageTagRelation, { ordered: false });
- }
- private async duplicateDescendants(pages, user, oldPagePathPrefix, newPagePathPrefix, shouldUseV4Process = true) {
- if (shouldUseV4Process) {
- return this.duplicateDescendantsV4(pages, user, oldPagePathPrefix, newPagePathPrefix);
- }
- const Page = this.crowi.model('Page');
- const Revision = this.crowi.model('Revision');
- const pageIds = pages.map(page => page._id);
- const revisions = await Revision.find({ pageId: { $in: pageIds } });
- // Mapping to set to the body of the new revision
- const pageIdRevisionMapping = {};
- revisions.forEach((revision) => {
- pageIdRevisionMapping[revision.pageId] = revision;
- });
- // key: oldPageId, value: newPageId
- const pageIdMapping = {};
- const newPages: any[] = [];
- const newRevisions: any[] = [];
- // no need to save parent here
- pages.forEach((page) => {
- const newPageId = new mongoose.Types.ObjectId();
- const newPagePath = page.path.replace(oldPagePathPrefix, newPagePathPrefix);
- const revisionId = new mongoose.Types.ObjectId();
- pageIdMapping[page._id] = newPageId;
- let newPage;
- if (!page.isEmpty) {
- newPage = {
- _id: newPageId,
- path: newPagePath,
- creator: user._id,
- grant: page.grant,
- grantedGroup: page.grantedGroup,
- grantedUsers: page.grantedUsers,
- lastUpdateUser: user._id,
- revision: revisionId,
- };
- newRevisions.push({
- _id: revisionId, pageId: newPageId, body: pageIdRevisionMapping[page._id].body, author: user._id, format: 'markdown',
- });
- }
- newPages.push(newPage);
- });
- await Page.insertMany(newPages, { ordered: false });
- await Revision.insertMany(newRevisions, { ordered: false });
- await this.duplicateTags(pageIdMapping);
- }
- private async duplicateDescendantsV4(pages, user, oldPagePathPrefix, newPagePathPrefix) {
- const Page = this.crowi.model('Page');
- const Revision = this.crowi.model('Revision');
- const pageIds = pages.map(page => page._id);
- const revisions = await Revision.find({ pageId: { $in: pageIds } });
- // Mapping to set to the body of the new revision
- const pageIdRevisionMapping = {};
- revisions.forEach((revision) => {
- pageIdRevisionMapping[revision.pageId] = revision;
- });
- // key: oldPageId, value: newPageId
- const pageIdMapping = {};
- const newPages: any[] = [];
- const newRevisions: any[] = [];
- pages.forEach((page) => {
- const newPageId = new mongoose.Types.ObjectId();
- const newPagePath = page.path.replace(oldPagePathPrefix, newPagePathPrefix);
- const revisionId = new mongoose.Types.ObjectId();
- pageIdMapping[page._id] = newPageId;
- newPages.push({
- _id: newPageId,
- path: newPagePath,
- creator: user._id,
- grant: page.grant,
- grantedGroup: page.grantedGroup,
- grantedUsers: page.grantedUsers,
- lastUpdateUser: user._id,
- revision: revisionId,
- });
- newRevisions.push({
- _id: revisionId, pageId: newPageId, body: pageIdRevisionMapping[page._id].body, author: user._id, format: 'markdown',
- });
- });
- await Page.insertMany(newPages, { ordered: false });
- await Revision.insertMany(newRevisions, { ordered: false });
- await this.duplicateTags(pageIdMapping);
- }
- private async duplicateDescendantsWithStream(page, newPagePath, user, shouldUseV4Process = true) {
- if (shouldUseV4Process) {
- return this.duplicateDescendantsWithStreamV4(page, newPagePath, user);
- }
- const iterableFactory = new PageCursorsForDescendantsFactory(user, page, true);
- const readStream = await iterableFactory.generateReadable();
- const newPagePathPrefix = newPagePath;
- const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
- const duplicateDescendants = this.duplicateDescendants.bind(this);
- const pageEvent = this.pageEvent;
- let count = 0;
- let nNonEmptyDuplicatedPages = 0;
- const writeStream = new Writable({
- objectMode: true,
- async write(batch, encoding, callback) {
- try {
- count += batch.length;
- nNonEmptyDuplicatedPages += batch.filter(page => !page.isEmpty).length;
- await duplicateDescendants(batch, user, pathRegExp, newPagePathPrefix, shouldUseV4Process);
- logger.debug(`Adding pages progressing: (count=${count})`);
- }
- catch (err) {
- logger.error('addAllPages error on add anyway: ', err);
- }
- callback();
- },
- async final(callback) {
- logger.debug(`Adding pages has completed: (totalCount=${count})`);
- // update path
- page.path = newPagePath;
- pageEvent.emit('syncDescendantsUpdate', page, user);
- callback();
- },
- });
- readStream
- .pipe(createBatchStream(BULK_REINDEX_SIZE))
- .pipe(writeStream);
- await streamToPromise(writeStream);
- return nNonEmptyDuplicatedPages;
- }
- private async duplicateDescendantsWithStreamV4(page, newPagePath, user) {
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(page.path, user);
- const newPagePathPrefix = newPagePath;
- const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
- const duplicateDescendants = this.duplicateDescendants.bind(this);
- const pageEvent = this.pageEvent;
- let count = 0;
- const writeStream = new Writable({
- objectMode: true,
- async write(batch, encoding, callback) {
- try {
- count += batch.length;
- await duplicateDescendants(batch, user, pathRegExp, newPagePathPrefix);
- logger.debug(`Adding pages progressing: (count=${count})`);
- }
- catch (err) {
- logger.error('addAllPages error on add anyway: ', err);
- }
- callback();
- },
- final(callback) {
- logger.debug(`Adding pages has completed: (totalCount=${count})`);
- // update path
- page.path = newPagePath;
- pageEvent.emit('syncDescendantsUpdate', page, user);
- callback();
- },
- });
- readStream
- .pipe(createBatchStream(BULK_REINDEX_SIZE))
- .pipe(writeStream);
- await streamToPromise(writeStream);
- return count;
- }
- /*
- * Delete
- */
- async deletePage(page, user, options = {}, isRecursively = false) {
- /*
- * Common Operation
- */
- const Page = mongoose.model('Page') as PageModel;
- // Separate v4 & v5 process
- const shouldUseV4Process = this.shouldUseV4Process(page);
- if (shouldUseV4Process) {
- return this.deletePageV4(page, user, options, isRecursively);
- }
- // Validate
- if (page.isEmpty && !isRecursively) {
- throw Error('Page not found.');
- }
- const isTrashed = isTrashPage(page.path);
- if (isTrashed) {
- throw new Error('This method does NOT support deleting trashed pages.');
- }
- if (!isMovablePage(page.path)) {
- throw new Error('Page is not deletable.');
- }
- const newPath = Page.getDeletedPageName(page.path);
- const canOperate = await this.crowi.pageOperationService.canOperate(isRecursively, page.path, newPath);
- if (!canOperate) {
- throw Error(`Cannot operate delete to path "${newPath}" right now.`);
- }
- // Replace with an empty page
- const isChildrenExist = await Page.exists({ parent: page._id });
- const shouldReplace = !isRecursively && isChildrenExist;
- if (shouldReplace) {
- await Page.replaceTargetWithPage(page, null, true);
- }
- // Delete target (only updating an existing document's properties )
- let deletedPage;
- if (!page.isEmpty) {
- deletedPage = await this.deleteNonEmptyTarget(page, user);
- }
- else { // always recursive
- deletedPage = page;
- await Page.deleteOne({ _id: page._id, isEmpty: true });
- }
- // 1. Update descendantCount
- if (isRecursively) {
- const inc = page.isEmpty ? -page.descendantCount : -(page.descendantCount + 1);
- await this.updateDescendantCountOfAncestors(page.parent, inc, true);
- }
- else {
- // update descendantCount of ancestors'
- await this.updateDescendantCountOfAncestors(page.parent, -1, true);
- }
- // 2. Delete leaf empty pages
- await Page.removeLeafEmptyPagesRecursively(page.parent);
- if (isRecursively) {
- let pageOp;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.Delete,
- actionStage: PageActionStage.Main,
- page,
- user,
- fromPath: page.path,
- toPath: newPath,
- });
- }
- catch (err) {
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- /*
- * Resumable Operation
- */
- this.deleteRecursivelyMainOperation(page, user, pageOp._id);
- }
- else {
- this.pageEvent.emit('delete', page, user);
- }
- return deletedPage;
- }
- private async deleteNonEmptyTarget(page, user) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const PageTagRelation = mongoose.model('PageTagRelation') as any; // TODO: Typescriptize model
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- const newPath = Page.getDeletedPageName(page.path);
- const deletedPage = await Page.findByIdAndUpdate(page._id, {
- $set: {
- path: newPath, status: Page.STATUS_DELETED, deleteUser: user._id, deletedAt: Date.now(), parent: null, descendantCount: 0, // set parent as null
- },
- }, { new: true });
- await PageTagRelation.updateMany({ relatedPage: page._id }, { $set: { isPageTrashed: true } });
- try {
- await PageRedirect.create({ fromPath: page.path, toPath: newPath });
- }
- catch (err) {
- if (err.code !== 11000) {
- throw err;
- }
- }
- this.pageEvent.emit('create', deletedPage, user);
- return deletedPage;
- }
- async deleteRecursivelyMainOperation(page, user, pageOpId: ObjectIdLike): Promise<void> {
- await this.deleteDescendantsWithStream(page, user, false);
- await PageOperation.findByIdAndDelete(pageOpId);
- // no sub operation available
- }
- private async deletePageV4(page, user, options = {}, isRecursively = false) {
- const Page = mongoose.model('Page') as PageModel;
- const PageTagRelation = mongoose.model('PageTagRelation') as any; // TODO: Typescriptize model
- const Revision = mongoose.model('Revision') as any; // TODO: Typescriptize model
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- const newPath = Page.getDeletedPageName(page.path);
- const isTrashed = isTrashPage(page.path);
- if (isTrashed) {
- throw new Error('This method does NOT support deleting trashed pages.');
- }
- if (!isMovablePage(page.path)) {
- throw new Error('Page is not deletable.');
- }
- if (isRecursively) {
- this.deleteDescendantsWithStream(page, user);
- }
- // update Revisions
- await Revision.updateRevisionListByPageId(page._id, { pageId: page._id });
- const deletedPage = await Page.findByIdAndUpdate(page._id, {
- $set: {
- path: newPath, status: Page.STATUS_DELETED, deleteUser: user._id, deletedAt: Date.now(),
- },
- }, { new: true });
- await PageTagRelation.updateMany({ relatedPage: page._id }, { $set: { isPageTrashed: true } });
- try {
- await PageRedirect.create({ fromPath: page.path, toPath: newPath });
- }
- catch (err) {
- if (err.code !== 11000) {
- throw err;
- }
- }
- this.pageEvent.emit('delete', page, user);
- this.pageEvent.emit('create', deletedPage, user);
- return deletedPage;
- }
- private async deleteDescendants(pages, user) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- const deletePageOperations: any[] = [];
- const insertPageRedirectOperations: any[] = [];
- pages.forEach((page) => {
- const newPath = Page.getDeletedPageName(page.path);
- let operation;
- // if empty, delete completely
- if (page.isEmpty) {
- operation = {
- deleteOne: {
- filter: { _id: page._id },
- },
- };
- }
- // if not empty, set parent to null and update to trash
- else {
- operation = {
- updateOne: {
- filter: { _id: page._id },
- update: {
- $set: {
- path: newPath, status: Page.STATUS_DELETED, deleteUser: user._id, deletedAt: Date.now(), parent: null, descendantCount: 0, // set parent as null
- },
- },
- },
- };
- insertPageRedirectOperations.push({
- insertOne: {
- document: {
- fromPath: page.path,
- toPath: newPath,
- },
- },
- });
- }
- deletePageOperations.push(operation);
- });
- try {
- await Page.bulkWrite(deletePageOperations);
- }
- catch (err) {
- if (err.code !== 11000) {
- throw new Error(`Failed to delete pages: ${err}`);
- }
- }
- finally {
- this.pageEvent.emit('syncDescendantsDelete', pages, user);
- }
- try {
- await PageRedirect.bulkWrite(insertPageRedirectOperations);
- }
- catch (err) {
- if (err.code !== 11000) {
- throw Error(`Failed to create PageRedirect documents: ${err}`);
- }
- }
- }
- /**
- * Create delete stream and return deleted document count
- */
- private async deleteDescendantsWithStream(targetPage, user, shouldUseV4Process = true): Promise<number> {
- let readStream;
- if (shouldUseV4Process) {
- readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
- }
- else {
- const factory = new PageCursorsForDescendantsFactory(user, targetPage, true);
- readStream = await factory.generateReadable();
- }
- const deleteDescendants = this.deleteDescendants.bind(this);
- let count = 0;
- let nDeletedNonEmptyPages = 0; // used for updating descendantCount
- const writeStream = new Writable({
- objectMode: true,
- async write(batch, encoding, callback) {
- nDeletedNonEmptyPages += batch.filter(d => !d.isEmpty).length;
- try {
- count += batch.length;
- await deleteDescendants(batch, user);
- logger.debug(`Deleting pages progressing: (count=${count})`);
- }
- catch (err) {
- logger.error('deleteDescendants error on add anyway: ', err);
- }
- callback();
- },
- final(callback) {
- logger.debug(`Deleting pages has completed: (totalCount=${count})`);
- callback();
- },
- });
- readStream
- .pipe(createBatchStream(BULK_REINDEX_SIZE))
- .pipe(writeStream);
- await streamToPromise(writeStream);
- return nDeletedNonEmptyPages;
- }
- private async deleteCompletelyOperation(pageIds, pagePaths) {
- // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
- const Bookmark = this.crowi.model('Bookmark');
- const Comment = this.crowi.model('Comment');
- const Page = this.crowi.model('Page');
- const PageTagRelation = this.crowi.model('PageTagRelation');
- const ShareLink = this.crowi.model('ShareLink');
- const Revision = this.crowi.model('Revision');
- const Attachment = this.crowi.model('Attachment');
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- const { attachmentService } = this.crowi;
- const attachments = await Attachment.find({ page: { $in: pageIds } });
- return Promise.all([
- Bookmark.deleteMany({ page: { $in: pageIds } }),
- Comment.deleteMany({ page: { $in: pageIds } }),
- PageTagRelation.deleteMany({ relatedPage: { $in: pageIds } }),
- ShareLink.deleteMany({ relatedPage: { $in: pageIds } }),
- Revision.deleteMany({ pageId: { $in: pageIds } }),
- Page.deleteMany({ _id: { $in: pageIds } }),
- PageRedirect.deleteMany({ $or: [{ fromPath: { $in: pagePaths } }, { toPath: { $in: pagePaths } }] }),
- attachmentService.removeAllAttachments(attachments),
- ]);
- }
- // delete multiple pages
- private async deleteMultipleCompletely(pages, user, options = {}) {
- const ids = pages.map(page => (page._id));
- const paths = pages.map(page => (page.path));
- logger.debug('Deleting completely', paths);
- await this.deleteCompletelyOperation(ids, paths);
- this.pageEvent.emit('syncDescendantsDelete', pages, user); // update as renamed page
- return;
- }
- async deleteCompletely(page, user, options = {}, isRecursively = false, preventEmitting = false) {
- /*
- * Common Operation
- */
- const Page = mongoose.model('Page') as PageModel;
- if (isTopPage(page.path)) {
- throw Error('It is forbidden to delete the top page');
- }
- if (page.isEmpty && !isRecursively) {
- throw Error('Page not found.');
- }
- // v4 compatible process
- const shouldUseV4Process = this.shouldUseV4Process(page);
- if (shouldUseV4Process) {
- return this.deleteCompletelyV4(page, user, options, isRecursively, preventEmitting);
- }
- const canOperate = await this.crowi.pageOperationService.canOperate(isRecursively, page.path, null);
- if (!canOperate) {
- throw Error(`Cannot operate deleteCompletely from path "${page.path}" right now.`);
- }
- const ids = [page._id];
- const paths = [page.path];
- logger.debug('Deleting completely', paths);
- // 1. update descendantCount
- if (isRecursively) {
- const inc = page.isEmpty ? -page.descendantCount : -(page.descendantCount + 1);
- await this.updateDescendantCountOfAncestors(page.parent, inc, true);
- }
- else {
- // replace with an empty page
- const shouldReplace = await Page.exists({ parent: page._id });
- let pageToUpdateDescendantCount = page;
- if (shouldReplace) {
- pageToUpdateDescendantCount = await Page.replaceTargetWithPage(page);
- }
- await this.updateDescendantCountOfAncestors(pageToUpdateDescendantCount.parent, -1, true);
- }
- // 2. then delete target completely
- await this.deleteCompletelyOperation(ids, paths);
- // delete leaf empty pages
- await Page.removeLeafEmptyPagesRecursively(page.parent);
- if (isRecursively) {
- let pageOp;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.DeleteCompletely,
- actionStage: PageActionStage.Main,
- page,
- user,
- fromPath: page.path,
- options,
- });
- }
- catch (err) {
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- /*
- * Main Operation
- */
- this.deleteCompletelyRecursivelyMainOperation(page, user, options, pageOp._id);
- }
- else if (!page.isEmpty && !preventEmitting) {
- this.pageEvent.emit('deleteCompletely', page, user);
- }
- return;
- }
- async deleteCompletelyRecursivelyMainOperation(page, user, options, pageOpId: ObjectIdLike): Promise<void> {
- await this.deleteCompletelyDescendantsWithStream(page, user, options, false);
- await PageOperation.findByIdAndDelete(pageOpId);
- // no sub operation available
- }
- private async deleteCompletelyV4(page, user, options = {}, isRecursively = false, preventEmitting = false) {
- const ids = [page._id];
- const paths = [page.path];
- logger.debug('Deleting completely', paths);
- await this.deleteCompletelyOperation(ids, paths);
- if (isRecursively) {
- this.deleteCompletelyDescendantsWithStream(page, user, options);
- }
- if (!page.isEmpty && !preventEmitting) {
- this.pageEvent.emit('deleteCompletely', page, user);
- }
- return;
- }
- async emptyTrashPage(user, options = {}) {
- return this.deleteCompletelyDescendantsWithStream({ path: '/trash' }, user, options);
- }
- /**
- * Create delete completely stream
- */
- private async deleteCompletelyDescendantsWithStream(targetPage, user, options = {}, shouldUseV4Process = true): Promise<number> {
- let readStream;
- if (shouldUseV4Process) { // pages don't have parents
- readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
- }
- else {
- const factory = new PageCursorsForDescendantsFactory(user, targetPage, true);
- readStream = await factory.generateReadable();
- }
- let count = 0;
- let nDeletedNonEmptyPages = 0; // used for updating descendantCount
- const deleteMultipleCompletely = this.deleteMultipleCompletely.bind(this);
- const writeStream = new Writable({
- objectMode: true,
- async write(batch, encoding, callback) {
- nDeletedNonEmptyPages += batch.filter(d => !d.isEmpty).length;
- try {
- count += batch.length;
- await deleteMultipleCompletely(batch, user, options);
- logger.debug(`Adding pages progressing: (count=${count})`);
- }
- catch (err) {
- logger.error('addAllPages error on add anyway: ', err);
- }
- callback();
- },
- final(callback) {
- logger.debug(`Adding pages has completed: (totalCount=${count})`);
- callback();
- },
- });
- readStream
- .pipe(createBatchStream(BULK_REINDEX_SIZE))
- .pipe(writeStream);
- await streamToPromise(writeStream);
- return nDeletedNonEmptyPages;
- }
- // no need to separate Main Sub since it is devided into single page operations
- async deleteMultiplePages(pagesToDelete, user, options): Promise<void> {
- const { isRecursively, isCompletely } = options;
- if (pagesToDelete.length > LIMIT_FOR_MULTIPLE_PAGE_OP) {
- throw Error(`The maximum number of pages is ${LIMIT_FOR_MULTIPLE_PAGE_OP}.`);
- }
- // omit duplicate paths if isRecursively true, omit empty pages if isRecursively false
- const pages = isRecursively ? omitDuplicateAreaPageFromPages(pagesToDelete) : pagesToDelete.filter(p => !p.isEmpty);
- if (isCompletely) {
- for await (const page of pages) {
- await this.deleteCompletely(page, user, {}, isRecursively);
- }
- }
- else {
- for await (const page of pages) {
- await this.deletePage(page, user, {}, isRecursively);
- }
- }
- }
- // use the same process in both v4 and v5
- private async revertDeletedDescendants(pages, user) {
- const Page = this.crowi.model('Page');
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- const revertPageOperations: any[] = [];
- const fromPathsToDelete: string[] = [];
- pages.forEach((page) => {
- // e.g. page.path = /trash/test, toPath = /test
- const toPath = Page.getRevertDeletedPageName(page.path);
- revertPageOperations.push({
- updateOne: {
- filter: { _id: page._id },
- update: {
- $set: {
- path: toPath, status: Page.STATUS_PUBLISHED, lastUpdateUser: user._id, deleteUser: null, deletedAt: null,
- },
- },
- },
- });
- fromPathsToDelete.push(page.path);
- });
- try {
- await Page.bulkWrite(revertPageOperations);
- await PageRedirect.deleteMany({ fromPath: { $in: fromPathsToDelete } });
- }
- catch (err) {
- if (err.code !== 11000) {
- throw new Error(`Failed to revert pages: ${err}`);
- }
- }
- }
- async revertDeletedPage(page, user, options = {}, isRecursively = false) {
- /*
- * Common Operation
- */
- const Page = this.crowi.model('Page');
- const PageTagRelation = this.crowi.model('PageTagRelation');
- // 1. Separate v4 & v5 process
- const shouldUseV4Process = this.shouldUseV4ProcessForRevert(page);
- if (shouldUseV4Process) {
- return this.revertDeletedPageV4(page, user, options, isRecursively);
- }
- const newPath = Page.getRevertDeletedPageName(page.path);
- const canOperate = await this.crowi.pageOperationService.canOperate(isRecursively, page.path, newPath);
- if (!canOperate) {
- throw Error(`Cannot operate revert from path "${page.path}" right now.`);
- }
- const includeEmpty = true;
- const originPage = await Page.findByPath(newPath, includeEmpty);
- // throw if any page already exists
- if (originPage != null) {
- throw new PathAlreadyExistsError('already_exists', originPage.path);
- }
- // 2. Revert target
- const parent = await this.getParentAndFillAncestorsByUser(user, newPath);
- const updatedPage = await Page.findByIdAndUpdate(page._id, {
- $set: {
- path: newPath, status: Page.STATUS_PUBLISHED, lastUpdateUser: user._id, deleteUser: null, deletedAt: null, parent: parent._id, descendantCount: 0,
- },
- }, { new: true });
- await PageTagRelation.updateMany({ relatedPage: page._id }, { $set: { isPageTrashed: false } });
- if (!isRecursively) {
- await this.updateDescendantCountOfAncestors(parent._id, 1, true);
- this.pageEvent.emit('revert', page, user);
- }
- else {
- let pageOp;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.Revert,
- actionStage: PageActionStage.Main,
- page,
- user,
- fromPath: page.path,
- toPath: newPath,
- options,
- });
- }
- catch (err) {
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- /*
- * Resumable Operation
- */
- this.revertRecursivelyMainOperation(page, user, options, pageOp._id);
- }
- return updatedPage;
- }
- async revertRecursivelyMainOperation(page, user, options, pageOpId: ObjectIdLike): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- await this.revertDeletedDescendantsWithStream(page, user, options, false);
- const newPath = Page.getRevertDeletedPageName(page.path);
- // normalize parent of descendant pages
- const shouldNormalize = this.shouldNormalizeParent(page);
- if (shouldNormalize) {
- try {
- await this.normalizeParentAndDescendantCountOfDescendants(newPath, user);
- logger.info(`Successfully normalized reverted descendant pages under "${newPath}"`);
- }
- catch (err) {
- logger.error('Failed to normalize descendants afrer revert:', err);
- throw err;
- }
- }
- // Set to Sub
- const pageOp = await PageOperation.findByIdAndUpdatePageActionStage(pageOpId, PageActionStage.Sub);
- if (pageOp == null) {
- throw Error('PageOperation document not found');
- }
- /*
- * Sub Operation
- */
- await this.revertRecursivelySubOperation(newPath, pageOp._id);
- }
- async revertRecursivelySubOperation(newPath: string, pageOpId: ObjectIdLike): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const newTarget = await Page.findOne({ path: newPath }); // only one page will be found since duplicating to existing path is forbidden
- if (newTarget == null) {
- throw Error('No reverted page found. Something might have gone wrong in revertRecursivelyMainOperation.');
- }
- // update descendantCount of ancestors'
- await this.updateDescendantCountOfAncestors(newTarget.parent as ObjectIdLike, newTarget.descendantCount + 1, true);
- await PageOperation.findByIdAndDelete(pageOpId);
- }
- private async revertDeletedPageV4(page, user, options = {}, isRecursively = false) {
- const Page = this.crowi.model('Page');
- const PageTagRelation = this.crowi.model('PageTagRelation');
- const newPath = Page.getRevertDeletedPageName(page.path);
- const originPage = await Page.findByPath(newPath);
- if (originPage != null) {
- throw new PathAlreadyExistsError('already_exists', originPage.path);
- }
- if (isRecursively) {
- this.revertDeletedDescendantsWithStream(page, user, options);
- }
- page.status = Page.STATUS_PUBLISHED;
- page.lastUpdateUser = user;
- debug('Revert deleted the page', page, newPath);
- const updatedPage = await Page.findByIdAndUpdate(page._id, {
- $set: {
- path: newPath, status: Page.STATUS_PUBLISHED, lastUpdateUser: user._id, deleteUser: null, deletedAt: null,
- },
- }, { new: true });
- await PageTagRelation.updateMany({ relatedPage: page._id }, { $set: { isPageTrashed: false } });
- this.pageEvent.emit('revert', page, user);
- return updatedPage;
- }
- /**
- * Create revert stream
- */
- private async revertDeletedDescendantsWithStream(targetPage, user, options = {}, shouldUseV4Process = true): Promise<number> {
- if (shouldUseV4Process) {
- return this.revertDeletedDescendantsWithStreamV4(targetPage, user, options);
- }
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
- const revertDeletedDescendants = this.revertDeletedDescendants.bind(this);
- let count = 0;
- const writeStream = new Writable({
- objectMode: true,
- async write(batch, encoding, callback) {
- try {
- count += batch.length;
- await revertDeletedDescendants(batch, user);
- logger.debug(`Reverting pages progressing: (count=${count})`);
- }
- catch (err) {
- logger.error('revertPages error on add anyway: ', err);
- }
- callback();
- },
- async final(callback) {
- logger.debug(`Reverting pages has completed: (totalCount=${count})`);
- callback();
- },
- });
- readStream
- .pipe(createBatchStream(BULK_REINDEX_SIZE))
- .pipe(writeStream);
- await streamToPromise(writeStream);
- return count;
- }
- private async revertDeletedDescendantsWithStreamV4(targetPage, user, options = {}) {
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
- const revertDeletedDescendants = this.revertDeletedDescendants.bind(this);
- let count = 0;
- const writeStream = new Writable({
- objectMode: true,
- async write(batch, encoding, callback) {
- try {
- count += batch.length;
- await revertDeletedDescendants(batch, user);
- logger.debug(`Reverting pages progressing: (count=${count})`);
- }
- catch (err) {
- logger.error('revertPages error on add anyway: ', err);
- }
- callback();
- },
- final(callback) {
- logger.debug(`Reverting pages has completed: (totalCount=${count})`);
- callback();
- },
- });
- readStream
- .pipe(createBatchStream(BULK_REINDEX_SIZE))
- .pipe(writeStream);
- await streamToPromise(readStream);
- return count;
- }
- async handlePrivatePagesForGroupsToDelete(groupsToDelete, action, transferToUserGroupId, user) {
- const Page = this.crowi.model('Page');
- const pages = await Page.find({ grantedGroup: { $in: groupsToDelete } });
- switch (action) {
- case 'public':
- await Page.publicizePages(pages);
- break;
- case 'delete':
- return this.deleteMultipleCompletely(pages, user);
- case 'transfer':
- await Page.transferPagesToGroup(pages, transferToUserGroupId);
- break;
- default:
- throw new Error('Unknown action for private pages');
- }
- }
- private extractStringIds(refs: Ref<HasObjectId>[]) {
- return refs.map((ref: Ref<HasObjectId>) => {
- return (typeof ref === 'string') ? ref : ref._id.toString();
- });
- }
- constructBasicPageInfo(page: IPage, isGuestUser?: boolean): IPageInfo | IPageInfoForEntity {
- const isMovable = isGuestUser ? false : isMovablePage(page.path);
- if (page.isEmpty) {
- return {
- isV5Compatible: true,
- isEmpty: true,
- isMovable,
- isDeletable: false,
- isAbleToDeleteCompletely: false,
- isRevertible: false,
- };
- }
- const likers = page.liker.slice(0, 15) as Ref<IUserHasId>[];
- const seenUsers = page.seenUsers.slice(0, 15) as Ref<IUserHasId>[];
- return {
- isV5Compatible: isTopPage(page.path) || page.parent != null,
- isEmpty: false,
- sumOfLikers: page.liker.length,
- likerIds: this.extractStringIds(likers),
- seenUserIds: this.extractStringIds(seenUsers),
- sumOfSeenUsers: page.seenUsers.length,
- isMovable,
- isDeletable: isMovable,
- isAbleToDeleteCompletely: false,
- isRevertible: isTrashPage(page.path),
- };
- }
- async shortBodiesMapByPageIds(pageIds: ObjectId[] = [], user): Promise<Record<string, string | null>> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const MAX_LENGTH = 350;
- // aggregation options
- let userGroups;
- if (user != null && userGroups == null) {
- const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // Typescriptize model
- userGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
- }
- const viewerCondition = Page.generateGrantCondition(user, userGroups);
- const filterByIds = {
- _id: { $in: pageIds },
- };
- let pages;
- try {
- pages = await Page
- .aggregate([
- // filter by pageIds
- {
- $match: filterByIds,
- },
- // filter by viewer
- {
- $match: viewerCondition,
- },
- // lookup: https://docs.mongodb.com/v4.4/reference/operator/aggregation/lookup/
- {
- $lookup: {
- from: 'revisions',
- let: { localRevision: '$revision' },
- pipeline: [
- {
- $match: {
- $expr: {
- $eq: ['$_id', '$$localRevision'],
- },
- },
- },
- {
- $project: {
- // What is $substrCP?
- // see: https://stackoverflow.com/questions/43556024/mongodb-error-substrbytes-invalid-range-ending-index-is-in-the-middle-of-a-ut/43556249
- revision: { $substrCP: ['$body', 0, MAX_LENGTH] },
- },
- },
- ],
- as: 'revisionData',
- },
- },
- // projection
- {
- $project: {
- _id: 1,
- revisionData: 1,
- },
- },
- ]).exec();
- }
- catch (err) {
- logger.error('Error occurred while generating shortBodiesMap');
- throw err;
- }
- const shortBodiesMap = {};
- pages.forEach((page) => {
- shortBodiesMap[page._id] = page.revisionData?.[0]?.revision;
- });
- return shortBodiesMap;
- }
- async normalizeParentByPath(path: string, user): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const { PageQueryBuilder } = Page;
- // This validation is not 100% correct since it ignores user to count
- const builder = new PageQueryBuilder(Page.find());
- builder.addConditionAsNotMigrated();
- builder.addConditionToListWithDescendants(path);
- const nEstimatedNormalizationTarget: number = await builder.query.exec('count');
- if (nEstimatedNormalizationTarget === 0) {
- throw Error('No page is available for conversion');
- }
- const pages = await Page.findByPathAndViewer(path, user, null, false);
- if (pages == null || !Array.isArray(pages)) {
- throw Error('Something went wrong while converting pages.');
- }
- if (pages.length === 0) {
- const isForbidden = await Page.count({ path, isEmpty: false }) > 0;
- if (isForbidden) {
- throw new V5ConversionError('It is not allowed to convert this page.', V5ConversionErrCode.FORBIDDEN);
- }
- }
- if (pages.length > 1) {
- throw new V5ConversionError(
- `There are more than two pages at the path "${path}". Please rename or delete the page first.`,
- V5ConversionErrCode.DUPLICATE_PAGES_FOUND,
- );
- }
- let page;
- let systematicallyCreatedPage;
- const shouldCreateNewPage = pages[0] == null;
- if (shouldCreateNewPage) {
- const notEmptyParent = await Page.findNotEmptyParentByPathRecursively(path);
- const options: PageCreateOptions & { grantedUsers?: ObjectIdLike[] | undefined } = {
- grant: notEmptyParent.grant,
- grantUserGroupId: notEmptyParent.grantedGroup,
- grantedUsers: notEmptyParent.grantedUsers,
- };
- systematicallyCreatedPage = await this.forceCreateBySystem(
- path,
- '',
- options,
- );
- page = systematicallyCreatedPage;
- }
- else {
- page = pages[0];
- }
- const grant = page.grant;
- const grantedUserIds = page.grantedUsers;
- const grantedGroupId = page.grantedGroup;
- /*
- * UserGroup & Owner validation
- */
- let isGrantNormalized = false;
- try {
- const shouldCheckDescendants = true;
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
- }
- catch (err) {
- logger.error(`Failed to validate grant of page at "${path}"`, err);
- throw err;
- }
- if (!isGrantNormalized) {
- throw new V5ConversionError(
- 'This page cannot be migrated since the selected grant or grantedGroup is not assignable to this page.',
- V5ConversionErrCode.GRANT_INVALID,
- );
- }
- let pageOp;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.NormalizeParent,
- actionStage: PageActionStage.Main,
- page,
- user,
- fromPath: page.path,
- toPath: page.path,
- });
- }
- catch (err) {
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- this.normalizeParentRecursivelyMainOperation(page, user, pageOp._id);
- }
- async normalizeParentByPageIdsRecursively(pageIds: ObjectIdLike[], user): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const pages = await Page.findByIdsAndViewer(pageIds, user, null);
- if (pages == null || pages.length === 0) {
- throw Error('pageIds is null or 0 length.');
- }
- if (pages.length > LIMIT_FOR_MULTIPLE_PAGE_OP) {
- throw Error(`The maximum number of pageIds allowed is ${LIMIT_FOR_MULTIPLE_PAGE_OP}.`);
- }
- this.normalizeParentRecursivelyByPages(pages, user);
- return;
- }
- async normalizeParentByPageIds(pageIds: ObjectIdLike[], user): Promise<void> {
- const Page = await mongoose.model('Page') as unknown as PageModel;
- const socket = this.crowi.socketIoService.getDefaultSocket();
- for await (const pageId of pageIds) {
- const page = await Page.findById(pageId);
- if (page == null) {
- continue;
- }
- const errorData: PageMigrationErrorData = { paths: [page.path] };
- try {
- const canOperate = await this.crowi.pageOperationService.canOperate(false, page.path, page.path);
- if (!canOperate) {
- throw Error(`Cannot operate normalizeParent to path "${page.path}" right now.`);
- }
- const normalizedPage = await this.normalizeParentByPage(page, user);
- if (normalizedPage == null) {
- socket.emit(SocketEventName.PageMigrationError, errorData);
- logger.error(`Failed to update descendantCount of page of id: "${pageId}"`);
- }
- }
- catch (err) {
- socket.emit(SocketEventName.PageMigrationError, errorData);
- logger.error('Something went wrong while normalizing parent.', err);
- }
- }
- socket.emit(SocketEventName.PageMigrationSuccess);
- }
- private async normalizeParentByPage(page, user) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const {
- path, grant, grantedUsers: grantedUserIds, grantedGroup: grantedGroupId,
- } = page;
- // check if any page exists at target path already
- const existingPage = await Page.findOne({ path, parent: { $ne: null } });
- if (existingPage != null && !existingPage.isEmpty) {
- throw Error('Page already exists. Please rename the page to continue.');
- }
- /*
- * UserGroup & Owner validation
- */
- if (grant !== Page.GRANT_RESTRICTED) {
- let isGrantNormalized = false;
- try {
- const shouldCheckDescendants = true;
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantedGroupId, shouldCheckDescendants);
- }
- catch (err) {
- logger.error(`Failed to validate grant of page at "${path}"`, err);
- throw err;
- }
- if (!isGrantNormalized) {
- throw Error('This page cannot be migrated since the selected grant or grantedGroup is not assignable to this page.');
- }
- }
- else {
- throw Error('Restricted pages can not be migrated');
- }
- let normalizedPage;
- // replace if empty page exists
- if (existingPage != null && existingPage.isEmpty) {
- // Inherit descendantCount from the empty page
- const updatedPage = await Page.findOneAndUpdate({ _id: page._id }, { descendantCount: existingPage.descendantCount }, { new: true });
- await Page.replaceTargetWithPage(existingPage, updatedPage, true);
- normalizedPage = await Page.findById(page._id);
- }
- else {
- const parent = await this.getParentAndFillAncestorsByUser(user, page.path);
- normalizedPage = await Page.findOneAndUpdate({ _id: page._id }, { parent: parent._id }, { new: true });
- }
- // Update descendantCount
- const inc = 1;
- await this.updateDescendantCountOfAncestors(normalizedPage.parent, inc, true);
- return normalizedPage;
- }
- async normalizeParentRecursivelyByPages(pages, user): Promise<void> {
- /*
- * Main Operation
- */
- const socket = this.crowi.socketIoService.getDefaultSocket();
- const pagesToNormalize = omitDuplicateAreaPageFromPages(pages);
- let normalizablePages;
- let nonNormalizablePages;
- try {
- [normalizablePages, nonNormalizablePages] = await this.crowi.pageGrantService.separateNormalizableAndNotNormalizablePages(user, pagesToNormalize);
- }
- catch (err) {
- socket.emit(SocketEventName.PageMigrationError);
- throw err;
- }
- if (normalizablePages.length === 0) {
- socket.emit(SocketEventName.PageMigrationError);
- return;
- }
- if (nonNormalizablePages.length !== 0) {
- const nonNormalizablePagePaths: string[] = nonNormalizablePages.map(p => p.path);
- socket.emit(SocketEventName.PageMigrationError, { paths: nonNormalizablePagePaths });
- logger.debug('Some pages could not be converted.', nonNormalizablePagePaths);
- }
- /*
- * Main Operation (s)
- */
- const errorPagePaths: string[] = [];
- for await (const page of normalizablePages) {
- const canOperate = await this.crowi.pageOperationService.canOperate(true, page.path, page.path);
- if (!canOperate) {
- errorPagePaths.push(page.path);
- throw Error(`Cannot operate normalizeParentRecursiively to path "${page.path}" right now.`);
- }
- const Page = mongoose.model('Page') as unknown as PageModel;
- const { PageQueryBuilder } = Page;
- const builder = new PageQueryBuilder(Page.findOne());
- builder.addConditionAsOnTree();
- builder.addConditionToListByPathsArray([page.path]);
- const existingPage = await builder.query.exec();
- if (existingPage?.parent != null) {
- errorPagePaths.push(page.path);
- throw Error('This page has already converted.');
- }
- let pageOp;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.NormalizeParent,
- actionStage: PageActionStage.Main,
- page,
- user,
- fromPath: page.path,
- toPath: page.path,
- });
- }
- catch (err) {
- errorPagePaths.push(page.path);
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- try {
- await this.normalizeParentRecursivelyMainOperation(page, user, pageOp._id);
- }
- catch (err) {
- errorPagePaths.push(page.path);
- logger.err('Failed to run normalizeParentRecursivelyMainOperation.', err);
- throw err;
- }
- }
- if (errorPagePaths.length === 0) {
- socket.emit(SocketEventName.PageMigrationSuccess);
- }
- else {
- socket.emit(SocketEventName.PageMigrationError, { paths: errorPagePaths });
- }
- }
- async normalizeParentRecursivelyMainOperation(page, user, pageOpId: ObjectIdLike): Promise<number> {
- // Save prevDescendantCount for sub-operation
- const Page = mongoose.model('Page') as unknown as PageModel;
- const { PageQueryBuilder } = Page;
- const builder = new PageQueryBuilder(Page.findOne(), true);
- builder.addConditionAsOnTree();
- builder.addConditionToListByPathsArray([page.path]);
- const exPage = await builder.query.exec();
- const options = { prevDescendantCount: exPage?.descendantCount ?? 0 };
- let count: number;
- try {
- count = await this.normalizeParentRecursively([page.path], user);
- }
- catch (err) {
- logger.error('V5 initial miration failed.', err);
- // socket.emit('normalizeParentRecursivelyByPageIds', { error: err.message }); TODO: use socket to tell user
- throw err;
- }
- // Set to Sub
- const pageOp = await PageOperation.findByIdAndUpdatePageActionStage(pageOpId, PageActionStage.Sub);
- if (pageOp == null) {
- throw Error('PageOperation document not found');
- }
- await this.normalizeParentRecursivelySubOperation(page, user, pageOp._id, options);
- return count;
- }
- async normalizeParentRecursivelySubOperation(page, user, pageOpId: ObjectIdLike, options: {prevDescendantCount: number}): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- try {
- // update descendantCount of self and descendant pages first
- await this.updateDescendantCountOfSelfAndDescendants(page.path);
- // find pages again to get updated descendantCount
- // then calculate inc
- const pageAfterUpdatingDescendantCount = await Page.findByIdAndViewer(page._id, user);
- if (pageAfterUpdatingDescendantCount == null) {
- throw Error('Page not found after updating descendantCount');
- }
- const { prevDescendantCount } = options;
- const newDescendantCount = pageAfterUpdatingDescendantCount.descendantCount;
- let inc = newDescendantCount - prevDescendantCount;
- const isAlreadyConverted = page.parent != null;
- if (!isAlreadyConverted) {
- inc += 1;
- }
- await this.updateDescendantCountOfAncestors(page._id, inc, false);
- }
- catch (err) {
- logger.error('Failed to update descendantCount after normalizing parent:', err);
- throw Error(`Failed to update descendantCount after normalizing parent: ${err}`);
- }
- await PageOperation.findByIdAndDelete(pageOpId);
- }
- async _isPagePathIndexUnique() {
- const Page = this.crowi.model('Page');
- const now = (new Date()).toString();
- const path = `growi_check_is_path_index_unique_${now}`;
- let isUnique = false;
- try {
- await Page.insertMany([
- { path },
- { path },
- ]);
- }
- catch (err) {
- if (err?.code === 11000) { // Error code 11000 indicates the index is unique
- isUnique = true;
- logger.info('Page path index is unique.');
- }
- else {
- throw err;
- }
- }
- finally {
- await Page.deleteMany({ path: { $regex: new RegExp('growi_check_is_path_index_unique', 'g') } });
- }
- return isUnique;
- }
- async normalizeAllPublicPages() {
- let isUnique;
- try {
- isUnique = await this._isPagePathIndexUnique();
- }
- catch (err) {
- logger.error('Failed to check path index status', err);
- throw err;
- }
- // drop unique index first
- if (isUnique) {
- try {
- await this._v5NormalizeIndex();
- }
- catch (err) {
- logger.error('V5 index normalization failed.', err);
- throw err;
- }
- }
- // then migrate
- try {
- await this.normalizeParentRecursively(['/'], null, true);
- }
- catch (err) {
- logger.error('V5 initial miration failed.', err);
- throw err;
- }
- // update descendantCount of all public pages
- try {
- await this.updateDescendantCountOfSelfAndDescendants('/');
- logger.info('Successfully updated all descendantCount of public pages.');
- }
- catch (err) {
- logger.error('Failed updating descendantCount of public pages.', err);
- throw err;
- }
- await this._setIsV5CompatibleTrue();
- }
- private async _setIsV5CompatibleTrue() {
- try {
- await this.crowi.configManager.updateConfigsInTheSameNamespace('crowi', {
- 'app:isV5Compatible': true,
- });
- logger.info('Successfully migrated all public pages.');
- }
- catch (err) {
- logger.warn('Failed to update app:isV5Compatible to true.');
- throw err;
- }
- }
- private async normalizeParentAndDescendantCountOfDescendants(path: string, user): Promise<void> {
- await this.normalizeParentRecursively([path], user);
- // update descendantCount of descendant pages
- await this.updateDescendantCountOfSelfAndDescendants(path);
- }
- /**
- * Normalize parent attribute by passing paths and user.
- * @param paths Pages under this paths value will be updated.
- * @param user To be used to filter pages to update. If null, only public pages will be updated.
- * @returns Promise<void>
- */
- async normalizeParentRecursively(paths: string[], user: any | null, shouldEmitProgress = false): Promise<number> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const ancestorPaths = paths.flatMap(p => collectAncestorPaths(p, []));
- // targets' descendants
- const pathAndRegExpsToNormalize: (RegExp | string)[] = paths
- .map(p => new RegExp(`^${escapeStringRegexp(addTrailingSlash(p))}`, 'i'));
- // include targets' path
- pathAndRegExpsToNormalize.push(...paths);
- // determine UserGroup condition
- let userGroups = null;
- if (user != null) {
- const UserGroupRelation = mongoose.model('UserGroupRelation') as any; // TODO: Typescriptize model
- userGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
- }
- const grantFiltersByUser: { $or: any[] } = Page.generateGrantCondition(user, userGroups);
- return this._normalizeParentRecursively(pathAndRegExpsToNormalize, ancestorPaths, grantFiltersByUser, user, shouldEmitProgress);
- }
- private buildFilterForNormalizeParentRecursively(pathOrRegExps: (RegExp | string)[], publicPathsToNormalize: string[], grantFiltersByUser: { $or: any[] }) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const andFilter: any = {
- $and: [
- {
- parent: null,
- status: Page.STATUS_PUBLISHED,
- path: { $ne: '/' },
- },
- ],
- };
- const orFilter: any = { $or: [] };
- // specified pathOrRegExps
- if (pathOrRegExps.length > 0) {
- orFilter.$or.push(
- {
- path: { $in: pathOrRegExps },
- },
- );
- }
- // not specified but ancestors of specified pathOrRegExps
- if (publicPathsToNormalize.length > 0) {
- orFilter.$or.push(
- {
- path: { $in: publicPathsToNormalize },
- grant: Page.GRANT_PUBLIC, // use only public pages to complete the tree
- },
- );
- }
- // Merge filters
- const mergedFilter = {
- $and: [
- { $and: [grantFiltersByUser, ...andFilter.$and] },
- { $or: orFilter.$or },
- ],
- };
- return mergedFilter;
- }
- private async _normalizeParentRecursively(
- pathOrRegExps: (RegExp | string)[],
- publicPathsToNormalize: string[],
- grantFiltersByUser: { $or: any[] },
- user,
- shouldEmitProgress = false,
- count = 0,
- skiped = 0,
- isFirst = true,
- ): Promise<number> {
- const BATCH_SIZE = 100;
- const PAGES_LIMIT = 1000;
- const socket = shouldEmitProgress ? this.crowi.socketIoService.getAdminSocket() : null;
- const Page = mongoose.model('Page') as unknown as PageModel;
- const { PageQueryBuilder } = Page;
- // Build filter
- const matchFilter = this.buildFilterForNormalizeParentRecursively(pathOrRegExps, publicPathsToNormalize, grantFiltersByUser);
- let baseAggregation = Page
- .aggregate([
- { $match: matchFilter },
- {
- $project: { // minimize data to fetch
- _id: 1,
- path: 1,
- },
- },
- ]);
- // Limit pages to get
- const total = await Page.countDocuments(matchFilter);
- if (isFirst) {
- socket?.emit(SocketEventName.PMStarted, { total });
- }
- if (total > PAGES_LIMIT) {
- baseAggregation = baseAggregation.limit(Math.floor(total * 0.3));
- }
- const pagesStream = await baseAggregation.cursor({ batchSize: BATCH_SIZE });
- const batchStream = createBatchStream(BATCH_SIZE);
- let shouldContinue = true;
- let nextCount = count;
- let nextSkiped = skiped;
- // eslint-disable-next-line max-len
- const buildPipelineToCreateEmptyPagesByUser = this.buildPipelineToCreateEmptyPagesByUser.bind(this);
- const migratePagesStream = new Writable({
- objectMode: true,
- async write(pages, encoding, callback) {
- const parentPaths = Array.from(new Set<string>(pages.map(p => pathlib.dirname(p.path))));
- // 1. Remove unnecessary empty pages & reset parent for pages which had had those empty pages
- const pageIdsToNotDelete = pages.map(p => p._id);
- const emptyPagePathsToDelete = pages.map(p => p.path);
- const builder1 = new PageQueryBuilder(Page.find({ isEmpty: true }, { _id: 1 }), true);
- builder1.addConditionToListByPathsArray(emptyPagePathsToDelete);
- builder1.addConditionToExcludeByPageIdsArray(pageIdsToNotDelete);
- const emptyPagesToDelete = await builder1.query.lean().exec();
- const resetParentOperations = emptyPagesToDelete.map((p) => {
- return {
- updateOne: {
- filter: {
- parent: p._id,
- },
- update: {
- parent: null,
- },
- },
- };
- });
- await Page.bulkWrite(resetParentOperations);
- await Page.removeEmptyPages(pageIdsToNotDelete, emptyPagePathsToDelete);
- // 2. Create lacking parents as empty pages
- const orFilters = [
- { path: '/' },
- { path: { $in: publicPathsToNormalize }, grant: Page.GRANT_PUBLIC, status: Page.STATUS_PUBLISHED },
- { path: { $in: publicPathsToNormalize }, parent: { $ne: null }, status: Page.STATUS_PUBLISHED },
- { path: { $nin: publicPathsToNormalize }, status: Page.STATUS_PUBLISHED },
- ];
- const filterForApplicableAncestors = { $or: orFilters };
- const aggregationPipeline = await buildPipelineToCreateEmptyPagesByUser(user, parentPaths, false, filterForApplicableAncestors);
- await Page.createEmptyPagesByPaths(parentPaths, aggregationPipeline);
- // 3. Find parents
- const addGrantCondition = (builder) => {
- builder.query = builder.query.and(grantFiltersByUser);
- return builder;
- };
- const builder2 = new PageQueryBuilder(Page.find(), true);
- addGrantCondition(builder2);
- const parents = await builder2
- .addConditionToListByPathsArray(parentPaths)
- .addConditionToFilterByApplicableAncestors(publicPathsToNormalize)
- .query
- .lean()
- .exec();
- // Normalize all siblings for each page
- const updateManyOperations = parents.map((parent) => {
- const parentId = parent._id;
- // Build filter
- const parentPathEscaped = escapeStringRegexp(parent.path === '/' ? '' : parent.path); // adjust the path for RegExp
- const filter: any = {
- $and: [
- {
- path: { $regex: new RegExp(`^${parentPathEscaped}(\\/[^/]+)\\/?$`, 'i') }, // see: regexr.com/6889f (e.g. /parent/any_child or /any_level1)
- },
- {
- path: { $in: pathOrRegExps.concat(publicPathsToNormalize) },
- },
- filterForApplicableAncestors,
- grantFiltersByUser,
- ],
- };
- return {
- updateMany: {
- filter,
- update: {
- parent: parentId,
- },
- },
- };
- });
- try {
- const res = await Page.bulkWrite(updateManyOperations);
- nextCount += res.result.nModified;
- nextSkiped += res.result.writeErrors.length;
- logger.info(`Page migration processing: (migratedPages=${res.result.nModified})`);
- socket?.emit(SocketEventName.PMMigrating, { count: nextCount });
- socket?.emit(SocketEventName.PMErrorCount, { skip: nextSkiped });
- // Throw if any error is found
- if (res.result.writeErrors.length > 0) {
- logger.error('Failed to migrate some pages', res.result.writeErrors);
- socket?.emit(SocketEventName.PMEnded, { isSucceeded: false });
- throw Error('Failed to migrate some pages');
- }
- // Finish migration if no modification occurred
- if (res.result.nModified === 0 && res.result.nMatched === 0) {
- shouldContinue = false;
- logger.error('Migration is unable to continue', 'parentPaths:', parentPaths, 'bulkWriteResult:', res);
- socket?.emit(SocketEventName.PMEnded, { isSucceeded: false });
- }
- }
- catch (err) {
- logger.error('Failed to update page.parent.', err);
- throw err;
- }
- callback();
- },
- final(callback) {
- callback();
- },
- });
- pagesStream
- .pipe(batchStream)
- .pipe(migratePagesStream);
- await streamToPromise(migratePagesStream);
- if (await Page.exists(matchFilter) && shouldContinue) {
- return this._normalizeParentRecursively(
- pathOrRegExps,
- publicPathsToNormalize,
- grantFiltersByUser,
- user,
- shouldEmitProgress,
- nextCount,
- nextSkiped,
- false,
- );
- }
- // End
- socket?.emit(SocketEventName.PMEnded, { isSucceeded: true });
- return nextCount;
- }
- private async _v5NormalizeIndex() {
- const collection = mongoose.connection.collection('pages');
- try {
- // drop pages.path_1 indexes
- await collection.dropIndex('path_1');
- logger.info('Succeeded to drop unique indexes from pages.path.');
- }
- catch (err) {
- logger.warn('Failed to drop unique indexes from pages.path.', err);
- throw err;
- }
- try {
- // create indexes without
- await collection.createIndex({ path: 1 }, { unique: false });
- logger.info('Succeeded to create non-unique indexes on pages.path.');
- }
- catch (err) {
- logger.warn('Failed to create non-unique indexes on pages.path.', err);
- throw err;
- }
- }
- async countPagesCanNormalizeParentByUser(user): Promise<number> {
- if (user == null) {
- throw Error('user is required');
- }
- const Page = mongoose.model('Page') as unknown as PageModel;
- const { PageQueryBuilder } = Page;
- const builder = new PageQueryBuilder(Page.count(), false);
- await builder.addConditionAsMigratablePages(user);
- const nMigratablePages = await builder.query.exec();
- return nMigratablePages;
- }
- /**
- * update descendantCount of the following pages
- * - page that has the same path as the provided path
- * - pages that are descendants of the above page
- */
- async updateDescendantCountOfSelfAndDescendants(path: string): Promise<void> {
- const BATCH_SIZE = 200;
- const Page = this.crowi.model('Page');
- const { PageQueryBuilder } = Page;
- const builder = new PageQueryBuilder(Page.find(), true);
- builder.addConditionAsOnTree();
- builder.addConditionToListWithDescendants(path);
- builder.addConditionToSortPagesByDescPath();
- const aggregatedPages = await builder.query.lean().cursor({ batchSize: BATCH_SIZE });
- await this.recountAndUpdateDescendantCountOfPages(aggregatedPages, BATCH_SIZE);
- }
- /**
- * update descendantCount of the pages sequentially from longer path to shorter path
- */
- async updateDescendantCountOfPagesWithPaths(paths: string[]): Promise<void> {
- const BATCH_SIZE = 200;
- const Page = this.crowi.model('Page');
- const { PageQueryBuilder } = Page;
- const builder = new PageQueryBuilder(Page.find(), true);
- builder.addConditionToListByPathsArray(paths); // find by paths
- builder.addConditionToSortPagesByDescPath(); // sort in DESC
- const aggregatedPages = await builder.query.lean().cursor({ batchSize: BATCH_SIZE });
- await this.recountAndUpdateDescendantCountOfPages(aggregatedPages, BATCH_SIZE);
- }
- /**
- * Recount descendantCount of pages one by one
- */
- async recountAndUpdateDescendantCountOfPages(pageCursor: QueryCursor<any>, batchSize:number): Promise<void> {
- const Page = this.crowi.model('Page');
- const recountWriteStream = new Writable({
- objectMode: true,
- async write(pageDocuments, encoding, callback) {
- for await (const document of pageDocuments) {
- const descendantCount = await Page.recountDescendantCount(document._id);
- await Page.findByIdAndUpdate(document._id, { descendantCount });
- }
- callback();
- },
- final(callback) {
- callback();
- },
- });
- pageCursor
- .pipe(createBatchStream(batchSize))
- .pipe(recountWriteStream);
- await streamToPromise(recountWriteStream);
- }
- // update descendantCount of all pages that are ancestors of a provided pageId by count
- async updateDescendantCountOfAncestors(pageId: ObjectIdLike, inc: number, shouldIncludeTarget: boolean): Promise<void> {
- const Page = this.crowi.model('Page');
- const ancestors = await Page.findAncestorsUsingParentRecursively(pageId, shouldIncludeTarget);
- const ancestorPageIds = ancestors.map(p => p._id);
- await Page.incrementDescendantCountOfPageIds(ancestorPageIds, inc);
- const updateDescCountData: UpdateDescCountRawData = Object.fromEntries(ancestors.map(p => [p._id.toString(), p.descendantCount + inc]));
- this.emitUpdateDescCount(updateDescCountData);
- }
- private emitUpdateDescCount(data: UpdateDescCountRawData): void {
- const socket = this.crowi.socketIoService.getDefaultSocket();
- socket.emit(SocketEventName.UpdateDescCount, data);
- }
- /**
- * Build the base aggregation pipeline for fillAncestors--- methods
- * @param onlyMigratedAsExistingPages Determine whether to include non-migrated pages as existing pages. If a page exists,
- * an empty page will not be created at that page's path.
- */
- private buildBasePipelineToCreateEmptyPages(paths: string[], onlyMigratedAsExistingPages = true, andFilter?): any[] {
- const aggregationPipeline: any[] = [];
- const Page = mongoose.model('Page') as unknown as PageModel;
- // -- Filter by paths
- aggregationPipeline.push({ $match: { path: { $in: paths } } });
- // -- Normalized condition
- if (onlyMigratedAsExistingPages) {
- aggregationPipeline.push({
- $match: {
- $or: [
- { grant: Page.GRANT_PUBLIC },
- { parent: { $ne: null } },
- { path: '/' },
- ],
- },
- });
- }
- // -- Add custom pipeline
- if (andFilter != null) {
- aggregationPipeline.push({ $match: andFilter });
- }
- return aggregationPipeline;
- }
- private async buildPipelineToCreateEmptyPagesByUser(user, paths: string[], onlyMigratedAsExistingPages = true, andFilter?): Promise<any[]> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const pipeline = this.buildBasePipelineToCreateEmptyPages(paths, onlyMigratedAsExistingPages, andFilter);
- let userGroups = null;
- if (user != null) {
- const UserGroupRelation = mongoose.model('UserGroupRelation') as any;
- userGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user);
- }
- const grantCondition = Page.generateGrantCondition(user, userGroups);
- pipeline.push({ $match: grantCondition });
- return pipeline;
- }
- private buildPipelineToCreateEmptyPagesBySystem(paths: string[]): any[] {
- return this.buildBasePipelineToCreateEmptyPages(paths);
- }
- private async connectPageTree(path: string): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const { PageQueryBuilder } = Page;
- const ancestorPaths = collectAncestorPaths(path);
- // Find ancestors
- const builder = new PageQueryBuilder(Page.find(), true);
- builder.addConditionToFilterByApplicableAncestors(ancestorPaths); // avoid including not normalized pages
- const ancestors = await builder
- .addConditionToListByPathsArray(ancestorPaths)
- .addConditionToSortPagesByDescPath()
- .query
- .exec();
- // Update parent attrs
- const ancestorsMap = new Map(); // Map<path, page>
- ancestors.forEach(page => !ancestorsMap.has(page.path) && ancestorsMap.set(page.path, page)); // the earlier element should be the true ancestor
- const nonRootAncestors = ancestors.filter(page => !isTopPage(page.path));
- const operations = nonRootAncestors.map((page) => {
- const parentPath = pathlib.dirname(page.path);
- return {
- updateOne: {
- filter: {
- _id: page._id,
- },
- update: {
- parent: ancestorsMap.get(parentPath)._id,
- },
- },
- };
- });
- await Page.bulkWrite(operations);
- }
- /**
- * Find parent or create parent if not exists.
- * It also updates parent of ancestors
- * @param path string
- * @returns Promise<PageDocument>
- */
- async getParentAndFillAncestorsByUser(user, path: string): Promise<PageDocument> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- // Find parent
- const parent = await Page.findParentByPath(path);
- if (parent != null) {
- return parent;
- }
- const ancestorPaths = collectAncestorPaths(path);
- // Fill ancestors
- const aggregationPipeline: any[] = await this.buildPipelineToCreateEmptyPagesByUser(user, ancestorPaths);
- await Page.createEmptyPagesByPaths(ancestorPaths, aggregationPipeline);
- // Connect ancestors
- await this.connectPageTree(path);
- // Return the created parent
- const createdParent = await Page.findParentByPath(path);
- if (createdParent == null) {
- throw Error('Failed to find the created parent by getParentAndFillAncestorsByUser');
- }
- return createdParent;
- }
- async getParentAndFillAncestorsBySystem(path: string): Promise<PageDocument> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- // Find parent
- const parent = await Page.findParentByPath(path);
- if (parent != null) {
- return parent;
- }
- // Fill ancestors
- const ancestorPaths = collectAncestorPaths(path);
- const aggregationPipeline: any[] = this.buildPipelineToCreateEmptyPagesBySystem(ancestorPaths);
- await Page.createEmptyPagesByPaths(ancestorPaths, aggregationPipeline);
- // Connect ancestors
- await this.connectPageTree(path);
- // Return the created parent
- const createdParent = await Page.findParentByPath(path);
- if (createdParent == null) {
- throw Error('Failed to find the created parent by getParentAndFillAncestorsByUser');
- }
- return createdParent;
- }
- // --------- Create ---------
- private async preparePageDocumentToCreate(path: string, shouldNew: boolean): Promise<PageDocument> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const emptyPage = await Page.findOne({ path, isEmpty: true });
- // Use empty page if exists, if not, create a new page
- let page;
- if (shouldNew) {
- page = new Page();
- }
- else if (emptyPage != null) {
- page = emptyPage;
- const descendantCount = await Page.recountDescendantCount(page._id);
- page.descendantCount = descendantCount;
- page.isEmpty = false;
- }
- else {
- page = new Page();
- }
- return page;
- }
- private setFieldExceptForGrantRevisionParent(
- pageDocument: PageDocument,
- path: string,
- user?,
- ): void {
- const Page = mongoose.model('Page') as unknown as PageModel;
- pageDocument.path = path;
- pageDocument.creator = user;
- pageDocument.lastUpdateUser = user;
- pageDocument.status = Page.STATUS_PUBLISHED;
- }
- private async canProcessCreate(
- path: string,
- grantData: {
- grant: number,
- grantedUserIds?: ObjectIdLike[],
- grantUserGroupId?: ObjectIdLike,
- },
- shouldValidateGrant: boolean,
- user?,
- ): Promise<boolean> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- // Operatability validation
- const canOperate = await this.crowi.pageOperationService.canOperate(false, null, path);
- if (!canOperate) {
- logger.error(`Cannot operate create to path "${path}" right now.`);
- return false;
- }
- // Existance validation
- const isExist = (await Page.count({ path, isEmpty: false })) > 0; // not validate empty page
- if (isExist) {
- logger.error('Cannot create new page to existed path');
- return false;
- }
- // UserGroup & Owner validation
- const { grant, grantedUserIds, grantUserGroupId } = grantData;
- if (shouldValidateGrant) {
- if (user == null) {
- throw Error('user is required to validate grant');
- }
- let isGrantNormalized = false;
- try {
- // It must check descendants as well if emptyTarget is not null
- const isEmptyPageAlreadyExist = await Page.count({ path, isEmpty: true }) > 0;
- const shouldCheckDescendants = isEmptyPageAlreadyExist;
- isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(user, path, grant, grantedUserIds, grantUserGroupId, shouldCheckDescendants);
- }
- catch (err) {
- logger.error(`Failed to validate grant of page at "${path}" of grant ${grant}:`, err);
- throw err;
- }
- if (!isGrantNormalized) {
- throw Error('The selected grant or grantedGroup is not assignable to this page.');
- }
- }
- return true;
- }
- async create(path: string, body: string, user, options: PageCreateOptions = {}): Promise<PageDocument> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- // Switch method
- const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
- if (!isV5Compatible) {
- return Page.createV4(path, body, user, options);
- }
- // Values
- // eslint-disable-next-line no-param-reassign
- path = this.crowi.xss.process(path); // sanitize path
- const {
- format = 'markdown', grantUserGroupId,
- } = options;
- const grant = isTopPage(path) ? Page.GRANT_PUBLIC : options.grant;
- const grantData = {
- grant,
- grantedUserIds: grant === Page.GRANT_OWNER ? [user._id] : undefined,
- grantUserGroupId,
- };
- const isGrantRestricted = grant === Page.GRANT_RESTRICTED;
- // Validate
- const shouldValidateGrant = !isGrantRestricted;
- const canProcessCreate = await this.canProcessCreate(path, grantData, shouldValidateGrant, user);
- if (!canProcessCreate) {
- throw Error('Cannnot process create');
- }
- // Prepare a page document
- const shouldNew = isGrantRestricted;
- const page = await this.preparePageDocumentToCreate(path, shouldNew);
- // Set field
- this.setFieldExceptForGrantRevisionParent(page, path, user);
- // Apply scope
- page.applyScope(user, grant, grantUserGroupId);
- // Set parent
- if (isTopPage(path) || isGrantRestricted) { // set parent to null when GRANT_RESTRICTED
- page.parent = null;
- }
- else {
- const parent = await this.getParentAndFillAncestorsByUser(user, path);
- page.parent = parent._id;
- }
- // Save
- let savedPage = await page.save();
- // Create revision
- const Revision = mongoose.model('Revision') as any; // TODO: Typescriptize model
- const newRevision = Revision.prepareRevision(savedPage, body, null, user, { format });
- savedPage = await pushRevision(savedPage, newRevision, user);
- await savedPage.populateDataToShowRevision();
- // Update descendantCount
- await this.updateDescendantCountOfAncestors(savedPage._id, 1, false);
- // Emit create event
- this.pageEvent.emit('create', savedPage, user);
- // Delete PageRedirect if exists
- const PageRedirect = mongoose.model('PageRedirect') as unknown as PageRedirectModel;
- try {
- await PageRedirect.deleteOne({ fromPath: path });
- logger.warn(`Deleted page redirect after creating a new page at path "${path}".`);
- }
- catch (err) {
- // no throw
- logger.error('Failed to delete PageRedirect');
- }
- return savedPage;
- }
- private async canProcessForceCreateBySystem(
- path: string,
- grantData: {
- grant: number,
- grantedUserIds?: ObjectIdLike[],
- grantUserGroupId?: ObjectIdLike,
- },
- ): Promise<boolean> {
- return this.canProcessCreate(path, grantData, false);
- }
- /**
- * @private
- * This method receives the same arguments as the PageService.create method does except for the added type '{ grantedUsers?: ObjectIdLike[] }'.
- * This additional value is used to determine the grantedUser of the page to be created by system.
- * This method must not run isGrantNormalized method to validate grant. **If necessary, run it before use this method.**
- * -- Reason 1: This is because it is not expected to use this method when the grant validation is required.
- * -- Reason 2: This is because it is not expected to use this method when the program cannot determine the operator.
- */
- private async forceCreateBySystem(path: string, body: string, options: PageCreateOptions & { grantedUsers?: ObjectIdLike[] }): Promise<PageDocument> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
- if (!isV5Compatible) {
- throw Error('This method is available only when v5 compatible');
- }
- // Values
- // eslint-disable-next-line no-param-reassign
- path = this.crowi.xss.process(path); // sanitize path
- const {
- format = 'markdown', grantUserGroupId, grantedUsers,
- } = options;
- const grant = isTopPage(path) ? Page.GRANT_PUBLIC : options.grant;
- const isGrantRestricted = grant === Page.GRANT_RESTRICTED;
- const isGrantOwner = grant === Page.GRANT_OWNER;
- const grantData = {
- grant,
- grantedUserIds: isGrantOwner ? grantedUsers : undefined,
- grantUserGroupId,
- };
- // Validate
- if (isGrantOwner && grantedUsers?.length !== 1) {
- throw Error('grantedUser must exist when grant is GRANT_OWNER');
- }
- const canProcessForceCreateBySystem = await this.canProcessForceCreateBySystem(path, grantData);
- if (!canProcessForceCreateBySystem) {
- throw Error('Cannnot process forceCreateBySystem');
- }
- // Prepare a page document
- const shouldNew = isGrantRestricted;
- const page = await this.preparePageDocumentToCreate(path, shouldNew);
- // Set field
- this.setFieldExceptForGrantRevisionParent(page, path);
- // Apply scope
- page.applyScope({ _id: grantedUsers?.[0] }, grant, grantUserGroupId);
- // Set parent
- if (isTopPage(path) || isGrantRestricted) { // set parent to null when GRANT_RESTRICTED
- page.parent = null;
- }
- else {
- const parent = await this.getParentAndFillAncestorsBySystem(path);
- page.parent = parent._id;
- }
- // Save
- let savedPage = await page.save();
- // Create revision
- const Revision = mongoose.model('Revision') as any; // TODO: Typescriptize model
- const dummyUser = { _id: new mongoose.Types.ObjectId() };
- const newRevision = Revision.prepareRevision(savedPage, body, null, dummyUser, { format });
- savedPage = await pushRevision(savedPage, newRevision, dummyUser);
- // Update descendantCount
- await this.updateDescendantCountOfAncestors(savedPage._id, 1, false);
- // Emit create event
- this.pageEvent.emit('create', savedPage, dummyUser);
- return savedPage;
- }
- /*
- * Find all children by parent's path or id. Using id should be prioritized
- */
- async findChildrenByParentPathOrIdAndViewer(parentPathOrId: string, user, userGroups = null): Promise<PageDocument[]> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- let queryBuilder: PageQueryBuilder;
- if (hasSlash(parentPathOrId)) {
- const path = parentPathOrId;
- const regexp = generateChildrenRegExp(path);
- queryBuilder = new PageQueryBuilder(Page.find({ path: { $regex: regexp } }), true);
- }
- else {
- const parentId = parentPathOrId;
- // Use $eq for user-controlled sources. see: https://codeql.github.com/codeql-query-help/javascript/js-sql-injection/#recommendation
- queryBuilder = new PageQueryBuilder(Page.find({ parent: { $eq: parentId } } as any), true); // TODO: improve type
- }
- await queryBuilder.addViewerCondition(user, userGroups);
- const pages = await queryBuilder
- .addConditionToSortPagesByAscPath()
- .query
- .lean()
- .exec();
- await this.injectProcessDataIntoPagesByActionTypes(pages, [PageActionType.Rename]);
- return pages;
- }
- async findAncestorsChildrenByPathAndViewer(path: string, user, userGroups = null): Promise<Record<string, PageDocument[]>> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const ancestorPaths = isTopPage(path) ? ['/'] : collectAncestorPaths(path); // root path is necessary for rendering
- const regexps = ancestorPaths.map(path => new RegExp(generateChildrenRegExp(path))); // cannot use re2
- // get pages at once
- const queryBuilder = new PageQueryBuilder(Page.find({ path: { $in: regexps } }), true);
- await queryBuilder.addViewerCondition(user, userGroups);
- const pages = await queryBuilder
- .addConditionAsOnTree()
- .addConditionToMinimizeDataForRendering()
- .addConditionToSortPagesByAscPath()
- .query
- .lean()
- .exec();
- this.injectIsTargetIntoPages(pages, path);
- await this.injectProcessDataIntoPagesByActionTypes(pages, [PageActionType.Rename]);
- /*
- * If any non-migrated page is found during creating the pathToChildren map, it will stop incrementing at that moment
- */
- const pathToChildren: Record<string, PageDocument[]> = {};
- const sortedPaths = ancestorPaths.sort((a, b) => a.length - b.length); // sort paths by path.length
- sortedPaths.every((path) => {
- const children = pages.filter(page => pathlib.dirname(page.path) === path);
- if (children.length === 0) {
- return false; // break when children do not exist
- }
- pathToChildren[path] = children;
- return true;
- });
- return pathToChildren;
- }
- private injectIsTargetIntoPages(pages: (PageDocument & {isTarget?: boolean})[], path): void {
- pages.forEach((page) => {
- if (page.path === path) {
- page.isTarget = true;
- }
- });
- }
- /**
- * Inject processData into page docuements
- * The processData is a combination of actionType as a key and information on whether the action is processable as a value.
- */
- private async injectProcessDataIntoPagesByActionTypes(
- pages: (PageDocument & { processData?: IPageOperationProcessData })[],
- actionTypes: PageActionType[],
- ): Promise<void> {
- const pageOperations = await PageOperation.find({ actionType: { $in: actionTypes } });
- if (pageOperations == null || pageOperations.length === 0) {
- return;
- }
- const processInfo: IPageOperationProcessInfo = this.crowi.pageOperationService.generateProcessInfo(pageOperations);
- const operatingPageIds: string[] = Object.keys(processInfo);
- // inject processData into pages
- pages.forEach((page) => {
- const pageId = page._id.toString();
- if (operatingPageIds.includes(pageId)) {
- const processData: IPageOperationProcessData = processInfo[pageId];
- page.processData = processData;
- }
- });
- }
- }
- export default PageService;
|