| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566 |
- import {
- getIdForRef,
- getIdStringForRef,
- PageStatus,
- YDocStatus,
- } from '@growi/core';
- import type {
- HasObjectId,
- IGrantedGroup,
- IPage,
- IPageInfoBasic,
- IPageInfoBasicForEmpty,
- IPageInfoBasicForEntity,
- IRevisionHasId,
- IUser,
- IUserHasId,
- Ref,
- } from '@growi/core/dist/interfaces';
- import { PageGrant } from '@growi/core/dist/interfaces';
- import { pagePathUtils, pathUtils } from '@growi/core/dist/utils';
- import escapeStringRegexp from 'escape-string-regexp';
- import type EventEmitter from 'events';
- import type { Cursor, HydratedDocument } from 'mongoose';
- import mongoose from 'mongoose';
- import pathlib from 'path';
- import { Readable, Writable } from 'stream';
- import { pipeline } from 'stream/promises';
- import { Comment } from '~/features/comment/server';
- import type { ExternalUserGroupDocument } from '~/features/external-user-group/server/models/external-user-group';
- import ExternalUserGroupRelation from '~/features/external-user-group/server/models/external-user-group-relation';
- import { isAiEnabled } from '~/features/openai/server/services';
- import { SupportedAction } from '~/interfaces/activity';
- import { V5ConversionErrCode } from '~/interfaces/errors/v5-conversion-error';
- import type { IOptionsForCreate, IOptionsForUpdate } from '~/interfaces/page';
- import type { IPageDeleteConfigValueToProcessValidation } from '~/interfaces/page-delete-config';
- import {
- PageDeleteConfigValue,
- PageSingleDeleteCompConfigValue,
- } from '~/interfaces/page-delete-config';
- import type { PopulatedGrantedGroup } from '~/interfaces/page-grant';
- import { PageActionStage, PageActionType } from '~/interfaces/page-operation';
- import { PageActionOnGroupDelete } from '~/interfaces/user-group';
- import {
- type PageMigrationErrorData,
- SocketEventName,
- type UpdateDescCountRawData,
- } from '~/interfaces/websocket';
- import type { CurrentPageYjsData } from '~/interfaces/yjs';
- import type Crowi from '~/server/crowi';
- import type { CreateMethod } from '~/server/models/page';
- import {
- type PageDocument,
- type PageModel,
- PageQueryBuilder,
- pushRevision,
- } from '~/server/models/page';
- import type { PageTagRelationDocument } from '~/server/models/page-tag-relation';
- import PageTagRelation from '~/server/models/page-tag-relation';
- import type { UserGroupDocument } from '~/server/models/user-group';
- import { createBatchStream } from '~/server/util/batch-stream';
- import { collectAncestorPaths } from '~/server/util/collect-ancestor-paths';
- import { generalXssFilter } from '~/services/general-xss-filter';
- import loggerFactory from '~/utils/logger';
- import { prepareDeleteConfigValuesForCalc } from '~/utils/page-delete-config';
- import type { ObjectIdLike } from '../../interfaces/mongoose-utils';
- import { Attachment } from '../../models/attachment';
- import { PathAlreadyExistsError } from '../../models/errors';
- import type { PageOperationDocument } from '../../models/page-operation';
- import PageOperation from '../../models/page-operation';
- import PageRedirect from '../../models/page-redirect';
- import type { IRevisionDocument } from '../../models/revision';
- import { Revision } from '../../models/revision';
- import { serializePageSecurely } from '../../models/serializers/page-serializer';
- import ShareLink from '../../models/share-link';
- import Subscription from '../../models/subscription';
- import UserGroupRelation from '../../models/user-group-relation';
- import { V5ConversionError } from '../../models/vo/v5-conversion-error';
- import { divideByType } from '../../util/granted-group';
- import { configManager } from '../config-manager';
- import type { IPageGrantService } from '../page-grant';
- import { preNotifyService } from '../pre-notify';
- import { getYjsService } from '../yjs';
- import { BULK_REINDEX_SIZE, LIMIT_FOR_MULTIPLE_PAGE_OP } from './consts';
- import { onSeen } from './events/seen';
- import type { IPageService } from './page-service';
- import { shouldUseV4Process } from './should-use-v4-process';
- export * from './page-service';
- const logger = loggerFactory('growi:services:page');
- const {
- isTrashPage,
- isTopPage,
- omitDuplicateAreaPageFromPages,
- getUsernameByPath,
- isUsersTopPage,
- isMovablePage,
- isUsersHomepage,
- } = pagePathUtils;
- const { addTrailingSlash } = pathUtils;
- // TODO: improve type
- class PageCursorsForDescendantsFactory {
- private user: any; // TODO: Typescriptize model
- private rootPage: any; // TODO: wait for mongoose update
- private shouldIncludeEmpty: boolean;
- private initialCursor: Cursor<any> | never[]; // TODO: wait for mongoose update
- constructor(user: any, rootPage: any, shouldIncludeEmpty: boolean) {
- this.user = user;
- this.rootPage = rootPage;
- this.shouldIncludeEmpty = shouldIncludeEmpty;
- }
- // 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: Cursor<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<Cursor<any> | never[]> {
- if (page == null) {
- return [];
- }
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- const { PageQueryBuilder } = Page;
- const builder = new PageQueryBuilder(Page.find(), this.shouldIncludeEmpty);
- builder.addConditionToFilteringByParentId(page._id);
- const cursor = builder.query
- .lean()
- .cursor({ batchSize: BULK_REINDEX_SIZE }) as Cursor<any>;
- return cursor;
- }
- private isNeverArray(val: Cursor<any> | never[]): val is never[] {
- return 'length' in val && val.length === 0;
- }
- }
- class PageService implements IPageService {
- crowi: Crowi;
- pageEvent: EventEmitter & {
- onCreate;
- onCreateMany;
- onAddSeenUsers;
- };
- tagEvent: any;
- activityEvent: any;
- pageGrantService: IPageGrantService;
- constructor(crowi: Crowi) {
- this.crowi = crowi;
- this.pageEvent = crowi.events.page;
- this.tagEvent = crowi.events.tag;
- this.activityEvent = crowi.events.activity;
- this.pageGrantService = crowi.pageGrantService;
- // init
- this.initPageEvent();
- this.canDeleteCompletely = this.canDeleteCompletely.bind(this);
- this.canDelete = this.canDelete.bind(this);
- }
- 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);
- // seen - mark page as seen by user
- this.pageEvent.on('seen', onSeen);
- }
- getEventEmitter(): EventEmitter {
- return this.pageEvent;
- }
- /**
- * Check if page can be deleted completely.
- * Use the following methods before execution of canDeleteCompletely to get params.
- * - pageService.getCreatorIdForCanDelete: creatorId
- * - pageGrantService.getUserRelatedGroups: userRelatedGroups
- * Do NOT make this method async as for now, because canDeleteCompletely is called in /page-listing/info in a for loop,
- * and /page-listing/info should not be an execution heavy API.
- */
- canDeleteCompletely(
- page: PageDocument,
- creatorId: ObjectIdLike | null,
- operator: any | null,
- isRecursively: boolean,
- userRelatedGroups: PopulatedGrantedGroup[],
- ): boolean {
- if (operator == null || isTopPage(page.path) || isUsersTopPage(page.path))
- return false;
- const pageCompleteDeletionAuthority = configManager.getConfig(
- 'security:pageCompleteDeletionAuthority',
- );
- const pageRecursiveCompleteDeletionAuthority = configManager.getConfig(
- 'security:pageRecursiveCompleteDeletionAuthority',
- );
- if (
- !this.canDeleteCompletelyAsMultiGroupGrantedPage(
- page,
- creatorId,
- operator,
- userRelatedGroups,
- )
- )
- return false;
- const [singleAuthority, recursiveAuthority] =
- prepareDeleteConfigValuesForCalc(
- pageCompleteDeletionAuthority,
- pageRecursiveCompleteDeletionAuthority,
- );
- return this.canDeleteLogic(
- creatorId,
- operator,
- isRecursively,
- singleAuthority,
- recursiveAuthority,
- );
- }
- /**
- * If page is multi-group granted, check if operator is allowed to completely delete the page.
- * see: https://dev.growi.org/656745fa52eafe1cf1879508#%E5%AE%8C%E5%85%A8%E3%81%AB%E5%89%8A%E9%99%A4%E3%81%99%E3%82%8B%E6%93%8D%E4%BD%9C
- * creatorId must be obtained by getCreatorIdForCanDelete
- */
- canDeleteCompletelyAsMultiGroupGrantedPage(
- page: PageDocument,
- creatorId: ObjectIdLike | null,
- operator: any | null,
- userRelatedGroups: PopulatedGrantedGroup[],
- ): boolean {
- const pageCompleteDeletionAuthority = configManager.getConfig(
- 'security:pageCompleteDeletionAuthority',
- );
- const isAllGroupMembershipRequiredForPageCompleteDeletion =
- configManager.getConfig(
- 'security:isAllGroupMembershipRequiredForPageCompleteDeletion',
- );
- const isAdmin = operator?.admin ?? false;
- const isAuthor =
- operator?._id == null ? false : operator._id.equals(creatorId);
- const isAdminOrAuthor = isAdmin || isAuthor;
- if (
- page.grant === PageGrant.GRANT_USER_GROUP &&
- !isAdminOrAuthor &&
- pageCompleteDeletionAuthority ===
- PageSingleDeleteCompConfigValue.Anyone &&
- isAllGroupMembershipRequiredForPageCompleteDeletion
- ) {
- const userRelatedGrantedGroups =
- this.pageGrantService.getUserRelatedGrantedGroupsSyncronously(
- userRelatedGroups,
- page,
- );
- if (userRelatedGrantedGroups.length !== page.grantedGroups.length) {
- return false;
- }
- }
- return true;
- }
- // When page is empty, the 'canDelete' judgement should be done using the creator of the closest non-empty ancestor page.
- async getCreatorIdForCanDelete(
- page: PageDocument,
- ): Promise<ObjectIdLike | null> {
- if (page.isEmpty) {
- const Page = mongoose.model<IPage, PageModel>('Page');
- const notEmptyClosestAncestor = await Page.findNonEmptyClosestAncestor(
- page.path,
- );
- return notEmptyClosestAncestor?.creator == null
- ? null
- : getIdForRef(notEmptyClosestAncestor.creator);
- }
- return page.creator == null ? null : getIdForRef(page.creator);
- }
- // Use getCreatorIdForCanDelete before execution of canDelete to get creatorId.
- canDelete(
- page: PageDocument,
- creatorId: ObjectIdLike | null,
- operator: any | null,
- isRecursively: boolean,
- ): boolean {
- if (operator == null || isTopPage(page.path) || isUsersTopPage(page.path))
- return false;
- const pageDeletionAuthority = configManager.getConfig(
- 'security:pageDeletionAuthority',
- );
- const pageRecursiveDeletionAuthority = configManager.getConfig(
- 'security:pageRecursiveDeletionAuthority',
- );
- const [singleAuthority, recursiveAuthority] =
- prepareDeleteConfigValuesForCalc(
- pageDeletionAuthority,
- pageRecursiveDeletionAuthority,
- );
- return this.canDeleteLogic(
- creatorId,
- operator,
- isRecursively,
- singleAuthority,
- recursiveAuthority,
- );
- }
- canDeleteUserHomepageByConfig(): boolean {
- return (
- configManager.getConfig('security:user-homepage-deletion:isEnabled') ??
- false
- );
- }
- async isUsersHomepageOwnerAbsent(path: string): Promise<boolean> {
- const User = mongoose.model('User');
- const username = getUsernameByPath(path);
- if (username == null) {
- throw new Error('Cannot found username by path');
- }
- const ownerExists = await User.exists({ username }).exec();
- return ownerExists == null;
- }
- private canDeleteLogic(
- creatorId: ObjectIdLike | null,
- operator,
- isRecursively: boolean,
- authority: IPageDeleteConfigValueToProcessValidation | null,
- recursiveAuthority: IPageDeleteConfigValueToProcessValidation | null,
- ): boolean {
- const isAdmin = operator?.admin ?? false;
- const isAuthor =
- operator?._id == null ? false : operator._id.equals(creatorId);
- if (isRecursively) {
- return this.compareDeleteConfig(isAdmin, isAuthor, recursiveAuthority);
- }
- return this.compareDeleteConfig(isAdmin, isAuthor, authority);
- }
- private compareDeleteConfig(
- isAdmin: boolean,
- isAuthor: boolean,
- authority: IPageDeleteConfigValueToProcessValidation | null,
- ): boolean {
- if (isAdmin) {
- return true;
- }
- if (authority === PageDeleteConfigValue.Anyone || authority == null) {
- return true;
- }
- if (authority === PageDeleteConfigValue.AdminAndAuthor && isAuthor) {
- return true;
- }
- return false;
- }
- private async getAbsenseUserHomeList(
- pages: PageDocument[],
- ): Promise<string[]> {
- const userHomepages = pages.filter((p) => isUsersHomepage(p.path));
- const User = mongoose.model<IUser>('User');
- const usernames = userHomepages
- .map((page) => getUsernameByPath(page.path))
- // see: https://zenn.dev/kimuson/articles/filter_safety_type_guard
- .filter(
- (username): username is Exclude<typeof username, null> =>
- username !== null,
- );
- const existingUsernames = await User.distinct<string>('username', {
- username: { $in: usernames },
- });
- return userHomepages
- .filter((page) => {
- const username = getUsernameByPath(page.path);
- if (username == null) {
- throw new Error('Cannot found username by path');
- }
- return !existingUsernames.includes(username);
- })
- .map((p) => p.path);
- }
- private async filterPages(
- pages: PageDocument[],
- user: IUserHasId,
- isRecursively: boolean,
- canDeleteFunction: (
- page: PageDocument,
- creatorId: ObjectIdLike | null,
- operator: any,
- isRecursively: boolean,
- userRelatedGroups: PopulatedGrantedGroup[],
- ) => boolean,
- ): Promise<PageDocument[]> {
- const userRelatedGroups =
- await this.pageGrantService.getUserRelatedGroups(user);
- const filteredPages = pages.filter(async (p) => {
- if (p.isEmpty) return true;
- const canDelete = canDeleteFunction(
- p,
- p.creator == null ? null : getIdForRef(p.creator),
- user,
- isRecursively,
- userRelatedGroups,
- );
- return canDelete;
- });
- if (!this.canDeleteUserHomepageByConfig()) {
- return filteredPages.filter((p) => !isUsersHomepage(p.path));
- }
- // Confirmation of deletion of user homepages is an asynchronous process,
- // so it is processed separately for performance optimization.
- const absenseUserHomeList =
- await this.getAbsenseUserHomeList(filteredPages);
- const excludeActiveUserHomepage = (path: string) => {
- if (!isUsersHomepage(path)) {
- return true;
- }
- return absenseUserHomeList.includes(path);
- };
- return filteredPages.filter((p) => excludeActiveUserHomepage(p.path));
- }
- async filterPagesByCanDeleteCompletely(
- pages: PageDocument[],
- user: IUserHasId,
- isRecursively: boolean,
- ): Promise<PageDocument[]> {
- return this.filterPages(
- pages,
- user,
- isRecursively,
- this.canDeleteCompletely,
- );
- }
- async filterPagesByCanDelete(
- pages: PageDocument[],
- user: IUserHasId,
- isRecursively: boolean,
- ): Promise<PageDocument[]> {
- return this.filterPages(pages, user, isRecursively, this.canDelete);
- }
- private shouldUseV4ProcessForRevert(page): boolean {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const isV5Compatible = configManager.getConfig('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 = mongoose.model<IPage, PageModel>('Page');
- const { PageQueryBuilder } = Page;
- const builder = new PageQueryBuilder(Page.find(), true)
- .addConditionAsRootOrNotOnTree() // to avoid affecting v5 pages
- .addConditionToListOnlyDescendants(targetPagePath);
- await Page.addConditionToFilteringByViewerToEdit(builder, userToOperate);
- return builder.query.lean().cursor({ batchSize: BULK_REINDEX_SIZE });
- }
- async renamePage(
- page: IPage,
- newPagePath,
- user,
- options,
- activityParameters,
- ): Promise<PageDocument | null> {
- /*
- * Common Operation
- */
- const Page = mongoose.model('Page') as unknown as PageModel;
- const parameters = {
- ip: activityParameters.ip,
- endpoint: activityParameters.endpoint,
- action:
- page.descendantCount > 0
- ? SupportedAction.ACTION_PAGE_RECURSIVELY_RENAME
- : SupportedAction.ACTION_PAGE_RENAME,
- user,
- targetModel: 'Page',
- target: page,
- snapshot: {
- username: user.username,
- },
- };
- const activity =
- await this.crowi.activityService.createActivity(parameters);
- const isExist = await Page.exists({ path: newPagePath, isEmpty: false });
- 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 isShouldUseV4Process = shouldUseV4Process(page);
- if (isShouldUseV4Process) {
- return this.renamePageV4(page, newPagePath, user, options);
- }
- 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: PageOperationDocument;
- 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;
- }
- let renamedPage: PageDocument | null = null;
- try {
- renamedPage = await this.renameMainOperation(
- page,
- newPagePath,
- user,
- options,
- pageOp._id,
- activity,
- );
- } catch (err) {
- logger.error('Error occurred while running renameMainOperation', err);
- // cleanup
- await PageOperation.deleteOne({ _id: pageOp._id });
- throw err;
- }
- if (page.descendantCount < 1) {
- const preNotify = preNotifyService.generatePreNotify(activity);
- this.activityEvent.emit('updated', activity, page, preNotify);
- }
- this.disableAncestorPagesTtl(newPagePath);
- return renamedPage;
- }
- async renameMainOperation(
- page,
- newPagePath: string,
- user,
- options,
- pageOpId: ObjectIdLike,
- activity?,
- ): Promise<PageDocument | null> {
- const Page = mongoose.model<IPage, PageModel>('Page');
- const updateMetadata = options.updateMetadata || false;
- // sanitize path
- const newPagePathSanitized = generalXssFilter.process(newPagePath);
- // UserGroup & Owner validation
- // use the parent's grant when target page is an empty page
- let grant: PageGrant;
- let grantedUserIds: ObjectIdLike[] | undefined;
- let grantedGroupIds: IGrantedGroup[];
- 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.map((u) => getIdForRef(u));
- grantedGroupIds = parent.grantedGroups;
- } else {
- grant = page.grant;
- grantedUserIds = page.grantedUsers;
- grantedGroupIds = page.grantedGroups;
- }
- if (grant !== Page.GRANT_RESTRICTED) {
- let isGrantNormalized = false;
- try {
- isGrantNormalized = await this.pageGrantService.isGrantNormalized(
- user,
- newPagePathSanitized,
- grant,
- grantedUserIds,
- grantedGroupIds,
- false,
- );
- } catch (err) {
- logger.error(
- `Failed to validate grant of page at "${newPagePathSanitized}" when renaming`,
- err,
- );
- throw err;
- }
- if (!isGrantNormalized) {
- throw Error(
- `This page cannot be renamed to "${newPagePathSanitized}" 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: PageDocument | undefined;
- // If renaming to under target, run getParentAndforceCreateEmptyTree to fill new ancestors
- if (this.isRenamingToUnderTarget(page.path, newPagePathSanitized)) {
- newParent = await this.getParentAndforceCreateEmptyTree(
- page,
- newPagePathSanitized,
- );
- } else {
- newParent = await this.getParentAndFillAncestorsByUser(
- user,
- newPagePathSanitized,
- );
- }
- // 3. Put back target page to tree (also update the other attrs)
- const update: Partial<IPage> = {};
- update.path = newPagePathSanitized;
- update.parent = newParent?._id;
- if (updateMetadata) {
- update.lastUpdateUser = user;
- update.updatedAt = new Date();
- }
- const renamedPage = await Page.findByIdAndUpdate(
- page._id,
- { $set: update },
- { new: true },
- );
- // 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) {
- await PageRedirect.create({
- fromPath: page.path,
- toPath: newPagePathSanitized,
- });
- }
- this.pageEvent.emit('rename');
- // 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,
- newPagePathSanitized,
- user,
- options,
- renamedPage,
- pageOp._id,
- activity,
- );
- return renamedPage;
- }
- async renameSubOperation(
- page,
- newPagePathSanitized: string,
- user,
- options,
- renamedPage,
- pageOpId: ObjectIdLike,
- activity?,
- ): 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
- const descendantsSubscribedSets = new Set();
- await this.renameDescendantsWithStream(
- page,
- newPagePathSanitized,
- user,
- options,
- false,
- descendantsSubscribedSets,
- );
- const descendantsSubscribedUsers = Array.from(
- descendantsSubscribedSets,
- ) as Ref<IUser>[];
- const preNotify = preNotifyService.generatePreNotify(
- activity,
- async () => {
- return descendantsSubscribedUsers;
- },
- );
- this.activityEvent.emit('updated', activity, page, preNotify);
- } 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, newPagePathSanitized)) {
- // remove empty pages at leaf position
- await Page.removeLeafEmptyPagesRecursively(page.parent);
- }
- await PageOperation.findByIdAndDelete(pageOpId);
- }
- async resumeRenameSubOperation(
- renamedPage: PageDocument,
- pageOp: PageOperationDocument,
- activity?,
- ): 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,
- activity,
- );
- }
- /**
- * 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,
- activity?,
- ): Promise<void> {
- await this.renameSubOperation(
- page,
- toPath,
- user,
- options,
- renamedPage,
- pageOpId,
- activity,
- );
- 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 = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- const {
- isRecursively = false,
- createRedirectPage = false,
- updateMetadata = false,
- } = options;
- // sanitize path
- const newPagePathSanitized = generalXssFilter.process(newPagePath);
- // create descendants first
- if (isRecursively) {
- await this.renameDescendantsWithStream(
- page,
- newPagePathSanitized,
- user,
- options,
- );
- }
- const update: any = {};
- // update Page
- update.path = newPagePathSanitized;
- if (updateMetadata) {
- update.lastUpdateUser = user;
- update.updatedAt = Date.now();
- }
- const renamedPage = await Page.findByIdAndUpdate(
- page._id,
- { $set: update },
- { new: true },
- );
- // update Rivisions
- if (renamedPage == null) {
- throw new Error('Failed to rename page');
- }
- await Revision.updateRevisionListByPageId(renamedPage._id, {
- pageId: renamedPage._id,
- });
- if (createRedirectPage) {
- await PageRedirect.create({
- fromPath: page.path,
- toPath: newPagePathSanitized,
- });
- }
- 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 { updateMetadata, createRedirectPage } = options;
- const updatePathOperations: any[] = [];
- const insertPageRedirectOperations: any[] = [];
- pages.forEach((page) => {
- const newPagePath = page.path.replace(
- oldPagePathPrefix,
- newPagePathPrefix,
- );
- // increment updatePathOperations
- const update =
- !page.isEmpty && updateMetadata
- ? {
- $set: {
- path: newPagePath,
- lastUpdateUser: user._id,
- updatedAt: new Date(),
- },
- }
- : {
- $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 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,
- newPagePathSanitized,
- user,
- options = {},
- shouldUseV4Process = true,
- descendantsSubscribedSets?,
- ) {
- // v4 compatible process
- if (shouldUseV4Process) {
- return this.renameDescendantsWithStreamV4(
- targetPage,
- newPagePathSanitized,
- user,
- options,
- );
- }
- const factory = new PageCursorsForDescendantsFactory(
- user,
- targetPage,
- true,
- );
- const readStream = await factory.generateReadable();
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- const newPagePathPrefix = newPagePathSanitized;
- 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,
- );
- const subscribedUsers = await Subscription.getSubscriptions(batch);
- subscribedUsers.forEach((eachUser) => {
- descendantsSubscribedSets.add(eachUser);
- });
- 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 = newPagePathSanitized;
- pageEvent.emit('syncDescendantsUpdate', targetPage, user);
- callback();
- },
- });
- await pipeline(readStream, batchStream, writeStream);
- }
- private async renameDescendantsWithStreamV4(
- targetPage,
- newPagePathSanitized,
- user,
- options = {},
- ) {
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(
- targetPage.path,
- user,
- );
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- const newPagePathPrefix = newPagePathSanitized;
- 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 = newPagePathSanitized;
- pageEvent.emit('syncDescendantsUpdate', targetPage, user);
- callback();
- },
- });
- await pipeline(readStream, batchStream, writeStream);
- }
- /*
- * Duplicate
- */
- async duplicate(
- page: PageDocument,
- newPagePath: string,
- user,
- isRecursively: boolean,
- onlyDuplicateUserRelatedResources: boolean,
- ) {
- /*
- * 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;
- if (!isRecursively && page.isEmpty) {
- throw Error('Page not found.');
- }
- const newPagePathSanitized = generalXssFilter.process(newPagePath);
- // 1. Separate v4 & v5 process
- const isShouldUseV4Process = shouldUseV4Process(page);
- if (isShouldUseV4Process) {
- return this.duplicateV4(
- page,
- newPagePathSanitized,
- user,
- isRecursively,
- onlyDuplicateUserRelatedResources,
- );
- }
- const canOperate = await this.crowi.pageOperationService.canOperate(
- isRecursively,
- page.path,
- newPagePathSanitized,
- );
- if (!canOperate) {
- throw Error(
- `Cannot operate duplicate to path "${newPagePathSanitized}" right now.`,
- );
- }
- // 2. UserGroup & Owner validation
- // use the parent's grant when target page is an empty page
- let grant: PageGrant;
- let grantedUserIds: ObjectIdLike[] | undefined;
- let grantedGroupIds: IGrantedGroup[];
- 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.map((u) => getIdForRef(u));
- grantedGroupIds = onlyDuplicateUserRelatedResources
- ? await this.pageGrantService.getUserRelatedGrantedGroups(parent, user)
- : parent.grantedGroups;
- } else {
- grant = page.grant;
- grantedUserIds = page.grantedUsers.map((u) => getIdForRef(u));
- grantedGroupIds = onlyDuplicateUserRelatedResources
- ? await this.pageGrantService.getUserRelatedGrantedGroups(page, user)
- : page.grantedGroups;
- }
- if (grant !== Page.GRANT_RESTRICTED) {
- let isGrantNormalized = false;
- try {
- isGrantNormalized = await this.pageGrantService.isGrantNormalized(
- user,
- newPagePathSanitized,
- grant,
- grantedUserIds,
- grantedGroupIds,
- false,
- );
- } catch (err) {
- logger.error(
- `Failed to validate grant of page at "${newPagePathSanitized}" when duplicating`,
- err,
- );
- throw err;
- }
- if (!isGrantNormalized) {
- throw Error(
- `This page cannot be duplicated to "${newPagePathSanitized}" 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: IOptionsForCreate = {
- grant,
- grantUserGroupIds: grantedGroupIds,
- };
- let duplicatedTarget: HydratedDocument<PageDocument>;
- if (page.isEmpty) {
- const parent = await this.getParentAndFillAncestorsByUser(
- user,
- newPagePath,
- );
- duplicatedTarget = await Page.createEmptyPage(newPagePath, parent);
- } else {
- const populatedPage = await page.populate<{
- revision: IRevisionHasId | null;
- }>({ path: 'revision', model: 'Revision', select: 'body' });
- duplicatedTarget = await (this.create as CreateMethod)(
- newPagePath,
- populatedPage?.revision?.body ?? '',
- user,
- options,
- );
- if (isAiEnabled()) {
- const { getOpenaiService } = await import(
- '~/features/openai/server/services/openai'
- );
- const openaiService = getOpenaiService();
- // Do not await because communication with OpenAI takes time
- openaiService?.createVectorStoreFileOnPageCreate([duplicatedTarget]);
- }
- }
- this.pageEvent.emit('duplicate', page, user);
- // 4. Take over tags
- const originTags = await page.findRelatedTagsById();
- let savedTags: PageTagRelationDocument[] = [];
- 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: PageOperationDocument;
- 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;
- }
- (async () => {
- try {
- await this.duplicateRecursivelyMainOperation(
- page,
- newPagePath,
- user,
- pageOp._id,
- onlyDuplicateUserRelatedResources,
- );
- } catch (err) {
- logger.error(
- 'Error occurred while running duplicateRecursivelyMainOperation.',
- err,
- );
- // cleanup
- await PageOperation.deleteOne({ _id: pageOp._id });
- throw err;
- }
- })();
- }
- const result = serializePageSecurely(duplicatedTarget);
- result.tags = savedTags;
- return result;
- }
- async duplicateRecursivelyMainOperation(
- page: PageDocument,
- newPagePath: string,
- user,
- pageOpId: ObjectIdLike,
- onlyDuplicateUserRelatedResources: boolean,
- ): Promise<void> {
- const nDuplicatedPages = await this.duplicateDescendantsWithStream(
- page,
- newPagePath,
- user,
- onlyDuplicateUserRelatedResources,
- false,
- );
- // normalize parent of descendant pages
- const shouldNormalize = this.shouldNormalizeParent(page);
- if (shouldNormalize) {
- try {
- await this.normalizeParentAndDescendantCountOfDescendants(
- newPagePath,
- user,
- true,
- );
- 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,
- onlyDuplicateUserRelatedResources: boolean,
- ) {
- // populate
- await page.populate({
- path: 'revision',
- model: 'Revision',
- select: 'body',
- });
- // create option
- const options: any = { page };
- options.grant = page.grant;
- options.grantUserGroupIds = page.grantedGroups;
- options.grantedUserIds = page.grantedUsers;
- const newPagePathSanitized = generalXssFilter.process(newPagePath);
- const createdPage = await this.create(
- newPagePathSanitized,
- page.revision.body,
- user,
- options,
- );
- this.pageEvent.emit('duplicate', page, user);
- if (isRecursively) {
- this.duplicateDescendantsWithStream(
- page,
- newPagePathSanitized,
- user,
- onlyDuplicateUserRelatedResources,
- );
- }
- // take over tags
- const originTags = await page.findRelatedTagsById();
- let savedTags: PageTagRelationDocument[] = [];
- 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) {
- // 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,
- onlyDuplicateUserRelatedResources: boolean,
- shouldUseV4Process = true,
- ) {
- if (shouldUseV4Process) {
- return this.duplicateDescendantsV4(
- pages,
- user,
- oldPagePathPrefix,
- newPagePathPrefix,
- );
- }
- const Page = mongoose.model<PageDocument, PageModel>('Page');
- 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: Record<string, IRevisionDocument> = {};
- revisions.forEach((revision) => {
- pageIdRevisionMapping[getIdStringForRef(revision.pageId)] = revision;
- });
- // key: oldPageId, value: newPageId
- const pageIdMapping = {};
- const newPages: any[] = [];
- const newRevisions: any[] = [];
- const userRelatedGroups =
- await this.pageGrantService.getUserRelatedGroups(user);
- // 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;
- const isDuplicateTarget =
- !page.isEmpty &&
- (!onlyDuplicateUserRelatedResources ||
- this.pageGrantService.isUserGrantedPageAccess(
- page,
- user,
- userRelatedGroups,
- ));
- if (isDuplicateTarget) {
- const grantedGroups = onlyDuplicateUserRelatedResources
- ? this.pageGrantService.getUserRelatedGrantedGroupsSyncronously(
- userRelatedGroups,
- page,
- )
- : page.grantedGroups;
- const newPage = {
- _id: newPageId,
- path: newPagePath,
- creator: user._id,
- grant: page.grant,
- grantedGroups,
- grantedUsers: page.grantedUsers,
- lastUpdateUser: user._id,
- revision: revisionId,
- };
- newRevisions.push({
- _id: revisionId,
- pageId: newPageId,
- body: pageIdRevisionMapping[page._id.toString()].body,
- author: user._id,
- format: 'markdown',
- });
- newPages.push(newPage);
- }
- });
- const duplicatedPages = await Page.insertMany(newPages, { ordered: false });
- const duplicatedPageIds = duplicatedPages.map(
- (duplicatedPage) => duplicatedPage._id,
- );
- await Revision.insertMany(newRevisions, { ordered: false });
- await this.duplicateTags(pageIdMapping);
- const duplicatedPagesWithPopulatedToShowRevision: HydratedDocument<PageDocument>[] =
- await Page.find({
- _id: { $in: duplicatedPageIds },
- grant: PageGrant.GRANT_PUBLIC,
- }).populate('revision');
- if (isAiEnabled()) {
- const { getOpenaiService } = await import(
- '~/features/openai/server/services/openai'
- );
- const openaiService = getOpenaiService();
- // Do not await because communication with OpenAI takes time
- openaiService?.createVectorStoreFileOnPageCreate(
- duplicatedPagesWithPopulatedToShowRevision,
- );
- }
- }
- private async duplicateDescendantsV4(
- pages,
- user,
- oldPagePathPrefix,
- newPagePathPrefix,
- ) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- 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: Record<string, IRevisionDocument> = {};
- revisions.forEach((revision) => {
- pageIdRevisionMapping[getIdStringForRef(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,
- grantedGroups: page.grantedGroups,
- grantedUsers: page.grantedUsers,
- lastUpdateUser: user._id,
- revision: revisionId,
- });
- newRevisions.push({
- _id: revisionId,
- pageId: newPageId,
- body: pageIdRevisionMapping[page._id.toString()].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,
- newPagePathSanitized,
- user,
- onlyDuplicateUserRelatedResources: boolean,
- shouldUseV4Process = true,
- ) {
- if (shouldUseV4Process) {
- return this.duplicateDescendantsWithStreamV4(
- page,
- newPagePathSanitized,
- user,
- onlyDuplicateUserRelatedResources,
- );
- }
- const iterableFactory = new PageCursorsForDescendantsFactory(
- user,
- page,
- true,
- );
- const readStream = await iterableFactory.generateReadable();
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- const newPagePathPrefix = newPagePathSanitized;
- 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,
- onlyDuplicateUserRelatedResources,
- 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 = newPagePathSanitized;
- pageEvent.emit('syncDescendantsUpdate', page, user);
- callback();
- },
- });
- await pipeline(readStream, batchStream, writeStream);
- return nNonEmptyDuplicatedPages;
- }
- private async duplicateDescendantsWithStreamV4(
- page,
- newPagePathSanitized,
- user,
- onlyDuplicateUserRelatedResources: boolean,
- ) {
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(
- page.path,
- user,
- );
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- const newPagePathPrefix = newPagePathSanitized;
- 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,
- onlyDuplicateUserRelatedResources,
- );
- 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 = newPagePathSanitized;
- pageEvent.emit('syncDescendantsUpdate', page, user);
- callback();
- },
- });
- await pipeline(readStream, batchStream, writeStream);
- return count;
- }
- /*
- * Delete
- */
- async deletePage(
- page,
- user,
- options = {},
- isRecursively = false,
- activityParameters,
- ) {
- /*
- * Common Operation
- */
- const Page = mongoose.model('Page') as PageModel;
- // Separate v4 & v5 process
- const isShouldUseV4Process = shouldUseV4Process(page);
- if (isShouldUseV4Process) {
- 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 (isTopPage(page.path) || isUsersTopPage(page.path)) {
- throw new Error('Page is not deletable.');
- }
- if (pagePathUtils.isUsersHomepage(page.path)) {
- if (!this.canDeleteUserHomepageByConfig()) {
- throw new Error('User Homepage is not deletable.');
- }
- if (!(await this.isUsersHomepageOwnerAbsent(page.path))) {
- throw new Error('User Homepage 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);
- }
- const parameters = {
- ip: activityParameters.ip,
- endpoint: activityParameters.endpoint,
- action:
- page.descendantCount > 0
- ? SupportedAction.ACTION_PAGE_RECURSIVELY_DELETE
- : SupportedAction.ACTION_PAGE_DELETE,
- user,
- target: page,
- targetModel: 'Page',
- snapshot: {
- username: user.username,
- },
- };
- const activity =
- await this.crowi.activityService.createActivity(parameters);
- // Delete target (only updating an existing document's properties )
- let deletedPage: PageDocument | null;
- 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: PageOperationDocument;
- 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
- */
- (async () => {
- try {
- await this.deleteRecursivelyMainOperation(
- page,
- user,
- pageOp._id,
- activity,
- );
- } catch (err) {
- logger.error(
- 'Error occurred while running deleteRecursivelyMainOperation.',
- err,
- );
- // cleanup
- await PageOperation.deleteOne({ _id: pageOp._id });
- throw err;
- } finally {
- this.pageEvent.emit('syncDescendantsUpdate', deletedPage, user);
- }
- })();
- } else {
- const preNotify = preNotifyService.generatePreNotify(activity);
- this.activityEvent.emit('updated', activity, page, preNotify);
- }
- return deletedPage;
- }
- private async deleteNonEmptyTarget(page, user) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- 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('delete', page, deletedPage, user);
- return deletedPage;
- }
- async deleteRecursivelyMainOperation(
- page,
- user,
- pageOpId: ObjectIdLike,
- activity?,
- ): Promise<void> {
- const descendantsSubscribedSets = new Set();
- await this.deleteDescendantsWithStream(
- page,
- user,
- false,
- descendantsSubscribedSets,
- );
- const descendantsSubscribedUsers = Array.from(
- descendantsSubscribedSets,
- ) as Ref<IUser>[];
- const preNotify = preNotifyService.generatePreNotify(activity, async () => {
- return descendantsSubscribedUsers;
- });
- this.activityEvent.emit('updated', activity, page, preNotify);
- await PageOperation.findByIdAndDelete(pageOpId);
- // no sub operation available
- }
- private async deletePageV4(
- page: HydratedDocument<PageDocument>,
- user,
- options = {},
- isRecursively = false,
- ) {
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- 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, deletedPage, user);
- return deletedPage;
- }
- private async deleteDescendants(pages, user) {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const deletePageOperations: any[] = [];
- const insertPageRedirectOperations: any[] = [];
- pages.forEach((page) => {
- const newPath = Page.getDeletedPageName(page.path);
- const operation = page.isEmpty
- ? {
- deleteOne: {
- filter: { _id: page._id },
- },
- }
- : {
- 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
- },
- },
- },
- };
- if (!page.isEmpty) {
- 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,
- descendantsSubscribedSets?,
- ): Promise<number> {
- const readStream = shouldUseV4Process
- ? await this.generateReadStreamToOperateOnlyDescendants(
- targetPage.path,
- user,
- )
- : await new PageCursorsForDescendantsFactory(
- user,
- targetPage,
- true,
- ).generateReadable();
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- 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);
- const subscribedUsers = await Subscription.getSubscriptions(batch);
- subscribedUsers.forEach((eachUser) => {
- descendantsSubscribedSets.add(eachUser);
- });
- 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();
- },
- });
- await pipeline(readStream, batchStream, writeStream);
- return nDeletedNonEmptyPages;
- }
- async deleteCompletelyOperation(pageIds, pagePaths): Promise<void> {
- // Delete Attachments, Revisions, Pages and emit delete
- const Page = mongoose.model('Page') as unknown as PageModel;
- const { attachmentService } = this.crowi;
- const attachments = await Attachment.find({ page: { $in: pageIds } });
- await Promise.all([
- 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),
- // Leave bookmarks without deleting -- 2024.05.17 Yuki Takei
- ]);
- if (isAiEnabled()) {
- const { getOpenaiService } = await import(
- '~/features/openai/server/services/openai'
- );
- const openaiService = getOpenaiService();
- await openaiService?.deleteVectorStoreFilesByPageIds(pageIds);
- }
- }
- // delete multiple pages
- async deleteMultipleCompletely(pages, user) {
- 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,
- activityParameters,
- ) {
- /*
- * 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 isShouldUseV4Process = shouldUseV4Process(page);
- if (isShouldUseV4Process) {
- 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);
- const parameters = {
- ip: activityParameters.ip,
- endpoint: activityParameters.endpoint,
- action:
- page.descendantCount > 0
- ? SupportedAction.ACTION_PAGE_RECURSIVELY_DELETE_COMPLETELY
- : SupportedAction.ACTION_PAGE_DELETE_COMPLETELY,
- user,
- target: page,
- targetModel: 'Page',
- snapshot: {
- username: user.username,
- },
- };
- const activity =
- await this.crowi.activityService.createActivity(parameters);
- // 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 (!page.isEmpty && !preventEmitting) {
- this.pageEvent.emit('deleteCompletely', page, user);
- }
- if (isRecursively) {
- let pageOp: PageOperationDocument;
- 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
- */
- (async () => {
- try {
- await this.deleteCompletelyRecursivelyMainOperation(
- page,
- user,
- options,
- pageOp._id,
- activity,
- );
- } catch (err) {
- logger.error(
- 'Error occurred while running deleteCompletelyRecursivelyMainOperation.',
- err,
- );
- // cleanup
- await PageOperation.deleteOne({ _id: pageOp._id });
- throw err;
- }
- })();
- } else {
- const preNotify = preNotifyService.generatePreNotify(activity);
- this.activityEvent.emit('updated', activity, page, preNotify);
- }
- return;
- }
- async deleteCompletelyRecursivelyMainOperation(
- page,
- user,
- options,
- pageOpId: ObjectIdLike,
- activity?,
- ): Promise<void> {
- const descendantsSubscribedSets = new Set();
- await this.deleteCompletelyDescendantsWithStream(
- page,
- user,
- options,
- false,
- descendantsSubscribedSets,
- );
- const descendantsSubscribedUsers = Array.from(
- descendantsSubscribedSets,
- ) as Ref<IUser>[];
- const preNotify = preNotifyService.generatePreNotify(activity, async () => {
- return descendantsSubscribedUsers;
- });
- this.activityEvent.emit('updated', activity, page, preNotify);
- 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 = {}, activityParameters) {
- const page = { path: '/trash' };
- const parameters = {
- ...activityParameters,
- action: SupportedAction.ACTION_PAGE_RECURSIVELY_DELETE_COMPLETELY,
- user,
- targetModel: 'Page',
- snapshot: {
- username: user.username,
- },
- };
- const activity =
- await this.crowi.activityService.createActivity(parameters);
- const descendantsSubscribedSets = new Set();
- const pages = await this.deleteCompletelyDescendantsWithStream(
- page,
- user,
- options,
- true,
- descendantsSubscribedSets,
- );
- const descendantsSubscribedUsers = Array.from(
- descendantsSubscribedSets,
- ) as Ref<IUser>[];
- const preNotify = preNotifyService.generatePreNotify(activity, async () => {
- return descendantsSubscribedUsers;
- });
- this.activityEvent.emit('updated', activity, page, preNotify);
- return pages;
- }
- /**
- * Create delete completely stream
- */
- private async deleteCompletelyDescendantsWithStream(
- targetPage,
- user,
- options = {},
- shouldUseV4Process = true,
- descendantsSubscribedSets?,
- ): Promise<number> {
- const readStream = shouldUseV4Process // pages don't have parents
- ? await this.generateReadStreamToOperateOnlyDescendants(
- targetPage.path,
- user,
- )
- : await new PageCursorsForDescendantsFactory(
- user,
- targetPage,
- true,
- ).generateReadable();
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- 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);
- const subscribedUsers = await Subscription.getSubscriptions(batch);
- subscribedUsers.forEach((eachUser) => {
- descendantsSubscribedSets.add(eachUser);
- });
- 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();
- },
- });
- await pipeline(readStream, batchStream, writeStream);
- return nDeletedNonEmptyPages;
- }
- // no need to separate Main Sub since it is devided into single page operations
- async deleteMultiplePages(
- pagesToDelete,
- user,
- options,
- activityParameters,
- ): 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,
- false,
- activityParameters,
- );
- }
- } else {
- for await (const page of pages) {
- await this.deletePage(
- page,
- user,
- {},
- isRecursively,
- activityParameters,
- );
- }
- }
- }
- // use the same process in both v4 and v5
- private async revertDeletedDescendants(pages, user) {
- const Page = mongoose.model<IPage, PageModel>('Page');
- 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,
- activityParameters?,
- ) {
- /*
- * Common Operation
- */
- const Page = mongoose.model<IPage, PageModel>('Page');
- const parameters = {
- ip: activityParameters.ip,
- endpoint: activityParameters.endpoint,
- action:
- page.descendantCount > 0
- ? SupportedAction.ACTION_PAGE_RECURSIVELY_REVERT
- : SupportedAction.ACTION_PAGE_REVERT,
- user,
- target: page,
- targetModel: 'Page',
- snapshot: {
- username: user.username,
- },
- };
- const activity =
- await this.crowi.activityService.createActivity(parameters);
- // 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 when recursively operation
- if (originPage != null && (!originPage.isEmpty || isRecursively)) {
- throw new PathAlreadyExistsError('already_exists', originPage.path);
- }
- // 2. Revert target
- const parent = await this.getParentAndFillAncestorsByUser(user, newPath);
- const shouldReplace = originPage != null && originPage.isEmpty;
- let updatedPage = await Page.findByIdAndUpdate(
- page._id,
- {
- $set: {
- path: newPath,
- status: Page.STATUS_PUBLISHED,
- lastUpdateUser: user._id,
- deleteUser: null,
- deletedAt: null,
- parent: parent._id,
- descendantCount: shouldReplace ? originPage.descendantCount : 0,
- },
- },
- { new: true },
- );
- if (shouldReplace) {
- updatedPage = await Page.replaceTargetWithPage(
- originPage,
- updatedPage,
- true,
- );
- }
- await PageTagRelation.updateMany(
- { relatedPage: page._id },
- { $set: { isPageTrashed: false } },
- );
- this.pageEvent.emit('revert', page, updatedPage, user);
- if (!isRecursively) {
- await this.updateDescendantCountOfAncestors(parent._id, 1, true);
- const preNotify = preNotifyService.generatePreNotify(activity);
- this.activityEvent.emit('updated', activity, page, preNotify);
- } else {
- let pageOp: PageOperationDocument;
- 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
- */
- (async () => {
- try {
- await this.revertRecursivelyMainOperation(
- page,
- user,
- options,
- pageOp._id,
- activity,
- );
- this.pageEvent.emit('syncDescendantsUpdate', updatedPage, user);
- } catch (err) {
- logger.error(
- 'Error occurred while running revertRecursivelyMainOperation.',
- err,
- );
- // cleanup
- await PageOperation.deleteOne({ _id: pageOp._id });
- throw err;
- }
- })();
- }
- return updatedPage;
- }
- async revertRecursivelyMainOperation(
- page,
- user,
- options,
- pageOpId: ObjectIdLike,
- activity?,
- ): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const descendantsSubscribedSets = new Set();
- await this.revertDeletedDescendantsWithStream(
- page,
- user,
- options,
- false,
- descendantsSubscribedSets,
- );
- const descendantsSubscribedUsers = Array.from(
- descendantsSubscribedSets,
- ) as Ref<IUser>[];
- const preNotify = preNotifyService.generatePreNotify(activity, async () => {
- return descendantsSubscribedUsers;
- });
- this.activityEvent.emit('updated', activity, page, preNotify);
- 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 = mongoose.model<IPage, PageModel>('Page');
- 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;
- logger.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, updatedPage, user);
- return updatedPage;
- }
- private async applyScopesToDescendantsWithStream(
- parentPage,
- user,
- isV4 = false,
- ) {
- const Page = mongoose.model<IPage, PageModel>('Page');
- const builder = new Page.PageQueryBuilder(Page.find());
- builder.addConditionToListOnlyDescendants(parentPage.path);
- if (isV4) {
- builder.addConditionAsRootOrNotOnTree();
- } else {
- builder.addConditionAsOnTree();
- }
- // add grant conditions
- await Page.addConditionToFilteringByViewerToEdit(builder, user);
- const grant = parentPage.grant;
- const userRelatedGroups =
- await this.pageGrantService.getUserRelatedGroups(user);
- const userRelatedParentGrantedGroups =
- this.pageGrantService.getUserRelatedGrantedGroupsSyncronously(
- userRelatedGroups,
- parentPage,
- );
- const childPagesReadableStream = builder.query.cursor({
- batchSize: BULK_REINDEX_SIZE,
- });
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- const childPagesWritable = new Writable({
- objectMode: true,
- write: async (batch, encoding, callback) => {
- await this.updateChildPagesGrant(
- batch,
- grant,
- user,
- userRelatedGroups,
- userRelatedParentGrantedGroups,
- );
- callback();
- },
- });
- await pipeline(childPagesReadableStream, batchStream, childPagesWritable);
- }
- async updateChildPagesGrant(
- pages: PageDocument[],
- grant: PageGrant,
- user,
- userRelatedGroups: PopulatedGrantedGroup[],
- userRelatedParentGrantedGroups: IGrantedGroup[],
- ): Promise<void> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const operations: any = [];
- pages.forEach((childPage) => {
- let newChildGrantedGroups: IGrantedGroup[] = [];
- if (grant === PageGrant.GRANT_USER_GROUP) {
- newChildGrantedGroups = this.getNewGrantedGroupsSyncronously(
- userRelatedGroups,
- userRelatedParentGrantedGroups,
- childPage,
- );
- }
- const canChangeGrant =
- this.pageGrantService.validateGrantChangeSyncronously(
- userRelatedGroups,
- childPage.grantedGroups,
- grant,
- newChildGrantedGroups,
- );
- if (canChangeGrant) {
- operations.push({
- updateOne: {
- filter: { _id: childPage._id },
- update: {
- $set: {
- grant,
- grantedUsers: grant === PageGrant.GRANT_OWNER ? [user._id] : [],
- grantedGroups: newChildGrantedGroups,
- },
- },
- },
- });
- }
- });
- await Page.bulkWrite(operations);
- }
- /**
- * Create revert stream
- */
- private async revertDeletedDescendantsWithStream(
- targetPage,
- user,
- options = {},
- shouldUseV4Process = true,
- descendantsSubscribedSets?,
- ): Promise<number> {
- if (shouldUseV4Process) {
- return this.revertDeletedDescendantsWithStreamV4(
- targetPage,
- user,
- options,
- );
- }
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(
- targetPage.path,
- user,
- );
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- 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);
- const subscribedUsers = await Subscription.getSubscriptions(batch);
- subscribedUsers.forEach((eachUser) => {
- descendantsSubscribedSets.add(eachUser);
- });
- 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();
- },
- });
- await pipeline(readStream, batchStream, writeStream);
- return count;
- }
- private async revertDeletedDescendantsWithStreamV4(
- targetPage,
- user,
- options = {},
- ) {
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(
- targetPage.path,
- user,
- );
- const batchStream = createBatchStream(BULK_REINDEX_SIZE);
- 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();
- },
- });
- await pipeline(readStream, batchStream, writeStream);
- return count;
- }
- async handlePrivatePagesForGroupsToDelete(
- groupsToDelete: UserGroupDocument[] | ExternalUserGroupDocument[],
- action: PageActionOnGroupDelete,
- transferToUserGroup: IGrantedGroup | undefined,
- user: IUser,
- ): Promise<void> {
- const Page = mongoose.model<IPage, PageModel>('Page');
- const pages = await Page.find({
- grantedGroups: { $elemMatch: { item: { $in: groupsToDelete } } },
- });
- switch (action) {
- case PageActionOnGroupDelete.publicize:
- await Page.removeGroupsToDeleteFromPages(pages, groupsToDelete);
- break;
- case PageActionOnGroupDelete.delete:
- return this.deleteMultipleCompletely(pages, user);
- case PageActionOnGroupDelete.transfer:
- await Page.transferPagesToGroup(pages, transferToUserGroup);
- 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: HydratedDocument<PageDocument>,
- isGuestUser?: boolean,
- ): IPageInfoBasic {
- const isMovable = isGuestUser ? false : isMovablePage(page.path);
- const pageId = page._id.toString();
- if (page.isEmpty) {
- return {
- emptyPageId: pageId,
- isNotFound: false,
- isV5Compatible: true,
- isEmpty: true,
- isMovable,
- isRevertible: false,
- } satisfies IPageInfoBasicForEmpty;
- }
- const likers = page.liker.slice(0, 15) as Ref<IUserHasId>[];
- const seenUsers = page.seenUsers.slice(0, 15) as Ref<IUserHasId>[];
- const infoForEntity = {
- isNotFound: false,
- 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,
- isRevertible: isTrashPage(page.path),
- contentAge: page.getContentAge(),
- descendantCount: page.descendantCount,
- commentCount: page.commentCount,
- // biome-ignore lint/style/noNonNullAssertion: the page must have a revision if it is not empty
- latestRevisionId: getIdStringForRef(page.revision!),
- } satisfies IPageInfoBasicForEntity;
- return infoForEntity;
- }
- async shortBodiesMapByPageIds(
- pageIds: ObjectIdLike[] = [],
- user?,
- ): Promise<Record<string, string | null>> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const MAX_LENGTH = 350;
- // aggregation options
- const userGroups =
- user != null
- ? [
- ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
- ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(
- user,
- )),
- ]
- : null;
- const viewerCondition = Page.generateGrantCondition(user, userGroups);
- const filterByIds = {
- _id: { $in: pageIds },
- };
- let pages: Array<{
- _id: string;
- revisionData?: Array<{ revision?: string }>;
- }>;
- 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<PageDocument, PageModel>('Page');
- const { PageQueryBuilder } = Page;
- // This validation is not 100% correct since it ignores user to count
- const builder = new PageQueryBuilder(Page.find());
- builder.addConditionAsRootOrNotOnTree();
- 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,
- );
- }
- const notEmptyParent =
- pages[0] == null
- ? await Page.findNotEmptyParentByPathRecursively(path)
- : null;
- const page =
- pages[0] ??
- (await this.forceCreateBySystem(path, '', {
- grant: notEmptyParent?.grant,
- grantUserIds: notEmptyParent?.grantedUsers.map((u) => getIdForRef(u)),
- grantUserGroupIds: notEmptyParent?.grantedGroups,
- }));
- const grant = page.grant;
- const grantedUserIds = page.grantedUsers.map((u) => getIdForRef(u));
- const grantedGroupIds = page.grantedGroups;
- /*
- * UserGroup & Owner validation
- */
- let isGrantNormalized = false;
- try {
- const shouldCheckDescendants = true;
- isGrantNormalized = await this.pageGrantService.isGrantNormalized(
- user,
- path,
- grant,
- grantedUserIds,
- grantedGroupIds,
- 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: PageOperationDocument;
- 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;
- }
- (async () => {
- try {
- await this.normalizeParentRecursivelyMainOperation(
- page,
- user,
- pageOp._id,
- );
- } catch (err) {
- logger.error(
- 'Error occurred while running normalizeParentRecursivelyMainOperation.',
- err,
- );
- // cleanup
- await PageOperation.deleteOne({ _id: pageOp._id });
- throw err;
- }
- })();
- }
- 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}.`,
- );
- }
- await 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,
- grantedGroups: grantedGroupIds,
- } = 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.pageGrantService.isGrantNormalized(
- user,
- path,
- grant,
- grantedUserIds,
- grantedGroupIds,
- 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: PageDocument | null;
- // 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;
- if (normalizedPage != null && normalizedPage.parent != null) {
- await this.updateDescendantCountOfAncestors(
- getIdForRef(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: PageDocument[];
- let nonNormalizablePages: PageDocument[];
- try {
- [normalizablePages, nonNormalizablePages] =
- await this.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: PageOperationDocument;
- 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.error(
- 'Failed to run normalizeParentRecursivelyMainOperation.',
- err,
- );
- // cleanup
- await PageOperation.deleteOne({ _id: pageOp._id });
- 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 = mongoose.model('Page') as unknown as PageModel;
- 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: /growi_check_is_path_index_unique/g },
- });
- }
- return isUnique;
- }
- async normalizeAllPublicPages(): Promise<void> {
- let isUnique: boolean;
- 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, false, 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 configManager.updateConfig('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,
- isDuplicateOperation = false,
- ): Promise<void> {
- await this.normalizeParentRecursively([path], user, isDuplicateOperation);
- // 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,
- isDuplicateOperation = false,
- 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
- const userGroups =
- user != null
- ? [
- ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
- ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(
- user,
- )),
- ]
- : null;
- const grantFiltersByUser: { $or: any[] } | null = !isDuplicateOperation
- ? Page.generateGrantCondition(user, userGroups)
- : null;
- return this._normalizeParentRecursively(
- pathAndRegExpsToNormalize,
- ancestorPaths,
- user,
- grantFiltersByUser,
- shouldEmitProgress,
- );
- }
- private buildFilterForNormalizeParentRecursively(
- pathOrRegExps: (RegExp | string)[],
- publicPathsToNormalize: string[],
- grantFiltersByUser?: { $or: any[] } | null,
- ) {
- 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 != null
- ? [grantFiltersByUser, ...andFilter.$and]
- : [...andFilter.$and],
- },
- { $or: orFilter.$or },
- ],
- };
- return mergedFilter;
- }
- private async _normalizeParentRecursively(
- pathOrRegExps: (RegExp | string)[],
- publicPathsToNormalize: string[],
- user,
- grantFiltersByUser: { $or: any[] } | null,
- 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;
- 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 builder2 = new PageQueryBuilder(Page.find(), true);
- if (grantFiltersByUser != null) {
- builder2.query = builder2.query.and(grantFiltersByUser);
- }
- 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,
- ],
- };
- if (grantFiltersByUser != null) {
- filter.$and.push(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();
- },
- });
- await pipeline(pagesStream, batchStream, migratePagesStream);
- if ((await Page.exists(matchFilter)) && shouldContinue) {
- return this._normalizeParentRecursively(
- pathOrRegExps,
- publicPathsToNormalize,
- user,
- grantFiltersByUser,
- 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 = mongoose.model<IPage, PageModel>('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 = mongoose.model<IPage, PageModel>('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: Cursor<any>,
- batchSize: number,
- ): Promise<void> {
- const Page = mongoose.model<IPage, PageModel>('Page');
- const batchStream = createBatchStream(batchSize);
- 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();
- },
- });
- await pipeline(pageCursor, batchStream, 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 = mongoose.model<IPage, PageModel>('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,
- );
- const userGroups =
- user != null
- ? [
- ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
- ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(
- user,
- )),
- ]
- : null;
- 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>
- for (const ancestor of ancestors) {
- if (!ancestorsMap.has(ancestor.path)) {
- ancestorsMap.set(ancestor.path, ancestor); // 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<HydratedDocument<PageDocument>> {
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- // 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<HydratedDocument<PageDocument>> {
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- // 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<HydratedDocument<PageDocument>> {
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- const emptyPage = await Page.findOne({ path, isEmpty: true });
- // Use empty page if exists, if not, create a new page
- let page: HydratedDocument<PageDocument>;
- 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 validateAppliedScope(
- user,
- grant,
- grantUserGroupIds: IGrantedGroup[],
- ) {
- if (grant === PageGrant.GRANT_USER_GROUP && grantUserGroupIds == null) {
- throw new Error('grantUserGroupIds is not specified');
- }
- if (grant === PageGrant.GRANT_USER_GROUP) {
- const {
- grantedUserGroups: grantedUserGroupIds,
- grantedExternalUserGroups: grantedExternalUserGroupIds,
- } = divideByType(grantUserGroupIds);
- const count =
- (await UserGroupRelation.countByGroupIdsAndUser(
- grantedUserGroupIds,
- user,
- )) +
- (await ExternalUserGroupRelation.countByGroupIdsAndUser(
- grantedExternalUserGroupIds,
- user,
- ));
- if (count === 0) {
- throw new Error('no relations were exist for group and user.');
- }
- }
- }
- private async canProcessCreate(
- path: string,
- grantData: {
- grant?: PageGrant;
- grantUserIds?: ObjectIdLike[];
- grantUserGroupIds?: IGrantedGroup[];
- },
- shouldValidateGrant: boolean,
- user?,
- options?: IOptionsForCreate,
- ): 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, grantUserIds, grantUserGroupIds } = 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 && !options?.overwriteScopesOfDescendants;
- isGrantNormalized = await this.pageGrantService.isGrantNormalized(
- user,
- path,
- grant,
- grantUserIds,
- grantUserGroupIds,
- 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.',
- );
- }
- if (options?.overwriteScopesOfDescendants) {
- const updateGrantInfo =
- await this.pageGrantService.generateUpdateGrantInfoToOverwriteDescendants(
- user,
- grant,
- options.grantUserGroupIds,
- );
- const canOverwriteDescendants =
- await this.pageGrantService.canOverwriteDescendants(
- path,
- user,
- updateGrantInfo,
- );
- if (!canOverwriteDescendants) {
- throw Error('Cannot overwrite scopes of descendants.');
- }
- }
- }
- return true;
- }
- /**
- * Create a page
- * Set options.isSynchronously to true to await all process when you want to run this method multiple times at short intervals.
- */
- async create(
- _path: string,
- body: string,
- user: HasObjectId,
- options: IOptionsForCreate = {},
- ): Promise<HydratedDocument<PageDocument>> {
- // Switch method
- const isV5Compatible = configManager.getConfig('app:isV5Compatible');
- if (!isV5Compatible) {
- return this.createV4(_path, body, user, options);
- }
- // Values
- const path: string = generalXssFilter.process(_path); // sanitize path
- // Retrieve closest ancestor document
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- const closestAncestor = await Page.findNonEmptyClosestAncestor(path);
- // Determine grantData
- const grant =
- options.grant ?? closestAncestor?.grant ?? PageGrant.GRANT_PUBLIC;
- const grantUserIds =
- grant === PageGrant.GRANT_OWNER ? [user._id] : undefined;
- const getGrantedGroupsFromClosestAncestor = async () => {
- if (closestAncestor == null) return undefined;
- if (options.onlyInheritUserRelatedGrantedGroups) {
- return this.pageGrantService.getUserRelatedGrantedGroups(
- closestAncestor,
- user,
- );
- }
- return closestAncestor.grantedGroups;
- };
- const grantUserGroupIds =
- options.grantUserGroupIds ??
- (await getGrantedGroupsFromClosestAncestor());
- const grantData = {
- grant,
- grantUserIds,
- grantUserGroupIds,
- };
- const isGrantRestricted = grant === PageGrant.GRANT_RESTRICTED;
- // Validate
- const shouldValidateGrant = !isGrantRestricted;
- const canProcessCreate = await this.canProcessCreate(
- path,
- grantData,
- shouldValidateGrant,
- user,
- options,
- );
- if (!canProcessCreate) {
- throw Error('Cannot 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, grantUserGroupIds);
- // 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;
- }
- // Make WIP
- if (options.wip) {
- const hasChildren = await Page.exists({ parent: page._id });
- page.makeWip(hasChildren != null); // disableTtl = hasChildren != null
- }
- // Save
- let savedPage = await page.save();
- // Create revision
- const newRevision = Revision.prepareRevision(
- savedPage,
- body,
- null,
- user,
- options.origin,
- );
- savedPage = await pushRevision(savedPage, newRevision, user);
- await savedPage.populateDataToShowRevision();
- // Emit create event
- this.pageEvent.emit('create', savedPage, user);
- // Directly run sub operation for now since it might be complex to handle main operation for creating pages -- Taichi Masuyama 2022.11.08
- let pageOp: PageOperationDocument;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.Create,
- actionStage: PageActionStage.Sub,
- page: savedPage,
- user,
- fromPath: path,
- options,
- });
- } catch (err) {
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- this.createSubOperation(savedPage, user, options, pageOp._id);
- return savedPage;
- }
- /**
- * Used to run sub operation in create method
- */
- async createSubOperation(
- page,
- user,
- options: IOptionsForCreate,
- pageOpId: ObjectIdLike,
- ): Promise<void> {
- await this.disableAncestorPagesTtl(page.path);
- // Update descendantCount
- await this.updateDescendantCountOfAncestors(page._id, 1, false);
- // Delete PageRedirect if exists
- try {
- await PageRedirect.deleteOne({ fromPath: page.path });
- logger.warn(
- `Deleted page redirect after creating a new page at path "${page.path}".`,
- );
- } catch (err) {
- // no throw
- logger.error('Failed to delete PageRedirect');
- }
- // update scopes for descendants
- if (options.overwriteScopesOfDescendants) {
- await this.applyScopesToDescendantsWithStream(page, user);
- }
- await PageOperation.findByIdAndDelete(pageOpId);
- }
- /**
- * V4 compatible create method
- */
- private async createV4(path, body, user, options: any = {}) {
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- const format = options.format || 'markdown';
- const grantUserGroupIds = options.grantUserGroupIds || null;
- const expandContentWidth = configManager.getConfig(
- 'customize:isContainerFluid',
- );
- // sanitize path
- const pathSanitized = generalXssFilter.process(path);
- let grant = options.grant;
- // force public
- if (isTopPage(pathSanitized)) {
- grant = PageGrant.GRANT_PUBLIC;
- }
- const isExist = await Page.count({ path: pathSanitized });
- if (isExist) {
- throw new Error('Cannot create new page to existed path');
- }
- const page = new Page();
- page.path = pathSanitized;
- page.creator = user;
- page.lastUpdateUser = user;
- page.status = PageStatus.STATUS_PUBLISHED;
- if (expandContentWidth != null) {
- page.expandContentWidth = expandContentWidth;
- }
- await this.validateAppliedScope(user, grant, grantUserGroupIds);
- page.applyScope(user, grant, grantUserGroupIds);
- let savedPage = await page.save();
- const newRevision = Revision.prepareRevision(
- savedPage,
- body,
- null,
- user,
- undefined,
- { format },
- );
- savedPage = await pushRevision(savedPage, newRevision, user);
- await savedPage.populateDataToShowRevision();
- this.pageEvent.emit('create', savedPage, user);
- // update scopes for descendants
- if (options.overwriteScopesOfDescendants) {
- this.applyScopesToDescendantsWithStream(savedPage, user, true);
- }
- return savedPage;
- }
- private async canProcessForceCreateBySystem(
- path: string,
- grantData: {
- grant: PageGrant;
- grantUserIds?: ObjectIdLike[];
- grantUserGroupId?: ObjectIdLike;
- },
- ): Promise<boolean> {
- return this.canProcessCreate(path, grantData, false);
- }
- private async disableAncestorPagesTtl(path: string): Promise<void> {
- const Page = mongoose.model<PageDocument, PageModel>('Page');
- const ancestorPaths = collectAncestorPaths(path);
- const ancestorPageIds = await Page.aggregate([
- { $match: { path: { $in: ancestorPaths, $nin: ['/'] }, isEmpty: false } },
- { $project: { _id: 1 } },
- ]);
- await Page.updateMany(
- { _id: { $in: ancestorPageIds } },
- { $unset: { ttlTimestamp: true } },
- );
- }
- /**
- * @private
- * This method receives the same arguments as the PageService.create method does except for the added type '{ grantUserIds?: 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.
- */
- async forceCreateBySystem(
- path: string,
- body: string,
- options: IOptionsForCreate & { grantUserIds?: ObjectIdLike[] },
- ): Promise<PageDocument> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- const isV5Compatible = configManager.getConfig('app:isV5Compatible');
- if (!isV5Compatible) {
- throw Error('This method is available only when v5 compatible');
- }
- // Values
- const pathSanitized = generalXssFilter.process(path);
- const { grantUserGroupIds, grantUserIds } = options;
- const grant = isTopPage(pathSanitized) ? Page.GRANT_PUBLIC : options.grant;
- const isGrantRestricted = grant === Page.GRANT_RESTRICTED;
- const isGrantOwner = grant === Page.GRANT_OWNER;
- const grantData = {
- grant,
- grantUserIds: isGrantOwner ? grantUserIds : undefined,
- grantUserGroupIds,
- };
- // Validate
- if (isGrantOwner && grantUserIds?.length !== 1) {
- throw Error('grantedUser must exist when grant is GRANT_OWNER');
- }
- const canProcessForceCreateBySystem =
- await this.canProcessForceCreateBySystem(pathSanitized, grantData);
- if (!canProcessForceCreateBySystem) {
- throw Error('Cannot process forceCreateBySystem');
- }
- // Prepare a page document
- const shouldNew = isGrantRestricted;
- const page = await this.preparePageDocumentToCreate(
- pathSanitized,
- shouldNew,
- );
- // Set field
- this.setFieldExceptForGrantRevisionParent(page, pathSanitized);
- // Apply scope
- page.applyScope({ _id: grantUserIds?.[0] }, grant, grantUserGroupIds);
- // Set parent
- if (isTopPage(pathSanitized) || isGrantRestricted) {
- // set parent to null when GRANT_RESTRICTED
- page.parent = null;
- } else {
- const parent =
- await this.getParentAndFillAncestorsBySystem(pathSanitized);
- page.parent = parent._id;
- }
- // Save
- let savedPage = await page.save();
- // Create revision
- const dummyUser: HasObjectId = {
- _id: new mongoose.Types.ObjectId().toString(),
- };
- const newRevision = Revision.prepareRevision(
- savedPage,
- body,
- null,
- dummyUser,
- );
- savedPage = await pushRevision(savedPage, newRevision, dummyUser);
- if (savedPage._id == null) {
- throw new Error('Something went wrong: _id is null');
- }
- // Update descendantCount
- await this.updateDescendantCountOfAncestors(savedPage._id, 1, false);
- // Emit create event
- this.pageEvent.emit('create', savedPage, dummyUser);
- return savedPage;
- }
- private shouldUseUpdatePageV4(
- grant: number,
- isV5Compatible: boolean,
- isOnTree: boolean,
- ): boolean {
- const isRestricted = grant === PageGrant.GRANT_RESTRICTED;
- return !isRestricted && (!isV5Compatible || !isOnTree);
- }
- /**
- * A wrapper method of updatePage for updating grant only.
- */
- async updateGrant(
- page: HydratedDocument<PageDocument>,
- user: IUserHasId,
- grantData: { grant: PageGrant; userRelatedGrantedGroups: IGrantedGroup[] },
- ): Promise<PageDocument> {
- const { grant, userRelatedGrantedGroups } = grantData;
- const options: IOptionsForUpdate = {
- grant,
- userRelatedGrantUserGroupIds: userRelatedGrantedGroups,
- };
- return this.updatePage(page, null, null, user, options);
- }
- async updatePageSubOperation(
- page,
- user,
- exPage,
- options: IOptionsForUpdate,
- pageOpId: ObjectIdLike,
- ): Promise<void> {
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- const currentPage = page;
- const exParent = exPage.parent;
- const wasOnTree = exPage.parent != null || isTopPage(exPage.path);
- const shouldBeOnTree = currentPage.grant !== PageGrant.GRANT_RESTRICTED;
- const isChildrenExist = await Page.count({
- path: new RegExp(
- `^${escapeStringRegexp(addTrailingSlash(currentPage.path))}`,
- ),
- parent: { $ne: null },
- });
- // 1. Update descendantCount
- const shouldPlusDescCount = !wasOnTree && shouldBeOnTree;
- const shouldMinusDescCount = wasOnTree && !shouldBeOnTree;
- if (shouldPlusDescCount) {
- await this.updateDescendantCountOfAncestors(currentPage._id, 1, false);
- const newDescendantCount = await Page.recountDescendantCount(
- currentPage._id,
- );
- await Page.updateOne(
- { _id: currentPage._id },
- { descendantCount: newDescendantCount },
- );
- } else if (shouldMinusDescCount) {
- // Update from parent. Parent is null if currentPage.grant is RESTRECTED.
- if (currentPage.grant === PageGrant.GRANT_RESTRICTED) {
- await this.updateDescendantCountOfAncestors(exParent, -1, true);
- }
- }
- // 2. Delete unnecessary empty pages
- const shouldRemoveLeafEmpPages = wasOnTree && !isChildrenExist;
- if (shouldRemoveLeafEmpPages) {
- await Page.removeLeafEmptyPagesRecursively(exParent);
- }
- // 3. Update scopes for descendants
- if (options.overwriteScopesOfDescendants && shouldBeOnTree) {
- await this.applyScopesToDescendantsWithStream(currentPage, user);
- }
- await PageOperation.findByIdAndDelete(pageOpId);
- }
- /**
- * Get the new GrantedGroups for the page going through an update operation.
- * It will include the groups specified by the operator, and groups which the user does not belong to, but was related to the page before the update.
- * @param userRelatedGrantedGroups The groups specified by the operator
- * @param page The page going through an update operation
- * @param user The operator
- * @returns The new GrantedGroups array to be set to the page
- */
- async getNewGrantedGroups(
- userRelatedGrantedGroups: IGrantedGroup[],
- page: PageDocument,
- user,
- ): Promise<IGrantedGroup[]> {
- const userRelatedGroups =
- await this.pageGrantService.getUserRelatedGroups(user);
- return this.getNewGrantedGroupsSyncronously(
- userRelatedGroups,
- userRelatedGrantedGroups,
- page,
- );
- }
- /**
- * Use when you do not want to use getNewGrantedGroups with async/await (e.g inside loops that process a large amount of pages)
- * Specification of userRelatedGroups is necessary to avoid the cost of fetching userRelatedGroups from DB every time.
- */
- getNewGrantedGroupsSyncronously(
- userRelatedGroups: PopulatedGrantedGroup[],
- userRelatedGrantedGroups: IGrantedGroup[],
- page: PageDocument,
- ): IGrantedGroup[] {
- const previousGrantedGroups = page.grantedGroups;
- const userRelatedPreviousGrantedGroups = this.pageGrantService
- .getUserRelatedGrantedGroupsSyncronously(userRelatedGroups, page)
- .map((g) => getIdForRef(g.item));
- const userUnrelatedPreviousGrantedGroups = previousGrantedGroups.filter(
- (g) => !userRelatedPreviousGrantedGroups.includes(getIdForRef(g.item)),
- );
- return [...userUnrelatedPreviousGrantedGroups, ...userRelatedGrantedGroups];
- }
- // There are cases where "revisionId" is not required for revision updates
- // See: https://dev.growi.org/651a6f4a008fee2f99187431#origin-%E3%81%AE%E5%BC%B7%E5%BC%B1
- async updatePage(
- pageData: HydratedDocument<PageDocument>,
- body: string | null,
- previousBody: string | null,
- user: IUserHasId,
- options: IOptionsForUpdate = {},
- ): Promise<HydratedDocument<PageDocument>> {
- const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>(
- 'Page',
- );
- const wasOnTree = pageData.parent != null || isTopPage(pageData.path);
- const isV5Compatible = configManager.getConfig('app:isV5Compatible');
- const shouldUseV4Process = this.shouldUseUpdatePageV4(
- pageData.grant,
- isV5Compatible,
- wasOnTree,
- );
- if (shouldUseV4Process) {
- // v4 compatible process
- return this.updatePageV4(pageData, body, previousBody, user, options);
- }
- // Clone page document
- const clonedPageData = Page.hydrate(pageData.toObject());
- const newPageData = pageData;
- // Once updated it's exempt from automatic deletion
- if (options.wip == null) {
- newPageData.ttlTimestamp = undefined;
- } else if (options.wip) {
- newPageData.unpublish();
- } else {
- newPageData.publish();
- }
- // use the previous data if absent
- const grant = options.grant ?? clonedPageData.grant;
- const grantUserGroupIds =
- options.userRelatedGrantUserGroupIds != null
- ? await this.getNewGrantedGroups(
- options.userRelatedGrantUserGroupIds,
- clonedPageData,
- user,
- )
- : clonedPageData.grantedGroups;
- const grantedUserIds = clonedPageData.grantedUserIds || [user._id];
- const shouldBeOnTree = grant !== PageGrant.GRANT_RESTRICTED;
- const isChildrenExist = await Page.count({
- path: new RegExp(
- `^${escapeStringRegexp(addTrailingSlash(clonedPageData.path))}`,
- ),
- parent: { $ne: null },
- });
- const isGrantChangeable = await this.pageGrantService.validateGrantChange(
- user,
- pageData.grantedGroups,
- grant,
- grantUserGroupIds,
- );
- if (!isGrantChangeable) {
- throw Error(
- 'The selected grant or grantedGroup is not assignable to this page.',
- );
- }
- if (shouldBeOnTree) {
- let isGrantNormalized = false;
- try {
- const shouldCheckDescendants = !options.overwriteScopesOfDescendants;
- isGrantNormalized = await this.pageGrantService.isGrantNormalized(
- user,
- clonedPageData.path,
- grant,
- grantedUserIds,
- grantUserGroupIds,
- shouldCheckDescendants,
- false,
- );
- } catch (err) {
- logger.error(
- `Failed to validate grant of page at "${clonedPageData.path}" of grant ${grant}:`,
- err,
- );
- throw err;
- }
- if (!isGrantNormalized) {
- throw Error(
- 'The selected grant or grantedGroup is not assignable to this page.',
- );
- }
- if (options.overwriteScopesOfDescendants) {
- const updateGrantInfo =
- await this.pageGrantService.generateUpdateGrantInfoToOverwriteDescendants(
- user,
- grant,
- options.userRelatedGrantUserGroupIds,
- );
- const canOverwriteDescendants =
- await this.pageGrantService.canOverwriteDescendants(
- clonedPageData.path,
- user,
- updateGrantInfo,
- );
- if (!canOverwriteDescendants) {
- throw Error('Cannot overwrite scopes of descendants.');
- }
- }
- if (!wasOnTree) {
- const newParent = await this.getParentAndFillAncestorsByUser(
- user,
- newPageData.path,
- );
- newPageData.parent = newParent._id;
- }
- } else {
- if (wasOnTree && isChildrenExist) {
- // Update children's parent with new parent
- const newParentForChildren = await Page.createEmptyPage(
- clonedPageData.path,
- clonedPageData.parent,
- clonedPageData.descendantCount,
- );
- await Page.updateMany(
- { parent: clonedPageData._id },
- { parent: newParentForChildren._id },
- );
- }
- newPageData.parent = null;
- newPageData.descendantCount = 0;
- }
- newPageData.applyScope(user, grant, grantUserGroupIds);
- // update existing page
- let savedPage = await newPageData.save();
- // Update body
- const isBodyPresent = body != null;
- const shouldUpdateBody = isBodyPresent;
- if (shouldUpdateBody) {
- const origin = options.origin;
- const newRevision = await Revision.prepareRevision(
- newPageData,
- body,
- previousBody,
- user,
- origin,
- );
- savedPage = await pushRevision(savedPage, newRevision, user);
- await savedPage.populateDataToShowRevision();
- }
- this.pageEvent.emit('update', savedPage, user);
- // Update ex children's parent
- if (!wasOnTree && shouldBeOnTree) {
- const emptyPageAtSamePath = await Page.findOne({
- path: clonedPageData.path,
- isEmpty: true,
- }); // this page is necessary to find children
- if (isChildrenExist) {
- if (emptyPageAtSamePath != null) {
- // Update children's parent with new parent
- await Page.updateMany(
- { parent: emptyPageAtSamePath._id },
- { parent: savedPage._id },
- );
- }
- }
- await Page.findOneAndDelete({ path: clonedPageData.path, isEmpty: true }); // delete here
- }
- // Directly run sub operation for now since it might be complex to handle main operation for updating pages -- Taichi Masuyama 2022.11.08
- let pageOp: PageOperationDocument;
- try {
- pageOp = await PageOperation.create({
- actionType: PageActionType.Update,
- actionStage: PageActionStage.Sub,
- page: savedPage,
- exPage: clonedPageData,
- user,
- fromPath: clonedPageData.path,
- options,
- });
- } catch (err) {
- logger.error('Failed to create PageOperation document.', err);
- throw err;
- }
- this.updatePageSubOperation(
- savedPage,
- user,
- clonedPageData,
- options,
- pageOp._id,
- );
- return savedPage;
- }
- // There are cases where "revisionId" is not required for revision updates
- // See: https://dev.growi.org/651a6f4a008fee2f99187431#origin-%E3%81%AE%E5%BC%B7%E5%BC%B1
- async updatePageV4(
- pageData: HydratedDocument<PageDocument>,
- body,
- previousBody,
- user,
- options: IOptionsForUpdate = {},
- ): Promise<HydratedDocument<PageDocument>> {
- // use the previous data if absent
- const grant = options.grant || pageData.grant;
- const grantUserGroupIds =
- options.userRelatedGrantUserGroupIds != null
- ? await this.getNewGrantedGroups(
- options.userRelatedGrantUserGroupIds,
- pageData,
- user,
- )
- : pageData.grantedGroups;
- // validate multiple group grant before save using pageData and options
- await this.pageGrantService.validateGrantChange(
- user,
- pageData.grantedGroups,
- grant,
- grantUserGroupIds,
- );
- await this.validateAppliedScope(user, grant, grantUserGroupIds);
- pageData.applyScope(user, grant, grantUserGroupIds);
- // update existing page
- let savedPage = await pageData.save();
- // Update revision
- const isBodyPresent = body != null;
- const shouldUpdateBody = isBodyPresent;
- if (shouldUpdateBody) {
- const newRevision = await Revision.prepareRevision(
- pageData,
- body,
- previousBody,
- user,
- options.origin,
- );
- savedPage = await pushRevision(savedPage, newRevision, user);
- await savedPage.populateDataToShowRevision();
- }
- // update scopes for descendants
- if (options.overwriteScopesOfDescendants) {
- this.applyScopesToDescendantsWithStream(savedPage, user, true);
- }
- this.pageEvent.emit('update', savedPage, user);
- return savedPage;
- }
- /**
- * Find all pages in trash page
- */
- async findAllTrashPages(
- user: IUserHasId,
- userGroups = null,
- ): Promise<HydratedDocument<IPage>[]> {
- const Page = mongoose.model('Page') as unknown as PageModel;
- // https://regex101.com/r/KYZWls/1
- // ex. /trash/.*
- const regexp = /^\/trash\/.*$/;
- const queryBuilder = new PageQueryBuilder(
- Page.find({ path: { $regex: regexp } }),
- true,
- );
- await queryBuilder.addViewerCondition(user, userGroups);
- const pages: HydratedDocument<IPage>[] = await queryBuilder
- .addConditionToSortPagesByAscPath()
- .query.lean()
- .exec();
- return pages;
- }
- async getYjsData(pageId: string): Promise<CurrentPageYjsData> {
- const yjsService = getYjsService();
- const currentYdoc = yjsService.getCurrentYdoc(pageId);
- const ydocStatus = await yjsService.getYDocStatus(pageId);
- const hasYdocsNewerThanLatestRevision = ydocStatus === YDocStatus.DRAFT;
- return {
- hasYdocsNewerThanLatestRevision,
- awarenessStateSize: currentYdoc?.awareness.states.size,
- };
- }
- async createTtlIndex(): Promise<void> {
- const wipPageExpirationSeconds =
- configManager.getConfig('app:wipPageExpirationSeconds') ?? 172800;
- const collection = mongoose.connection.collection('pages');
- try {
- const targetField = 'ttlTimestamp_1';
- const indexes = await collection.indexes();
- const foundTargetField = indexes.find((i) => i.name === targetField);
- const isNotSpec =
- foundTargetField?.expireAfterSeconds == null ||
- foundTargetField?.expireAfterSeconds !== wipPageExpirationSeconds;
- const shoudDropIndex = foundTargetField != null && isNotSpec;
- const shoudCreateIndex = foundTargetField == null || shoudDropIndex;
- if (shoudDropIndex) {
- await collection.dropIndex(targetField);
- }
- if (shoudCreateIndex) {
- await collection.createIndex(
- { ttlTimestamp: 1 },
- { expireAfterSeconds: wipPageExpirationSeconds },
- );
- }
- } catch (err) {
- logger.error('Failed to create TTL Index', err);
- throw err;
- }
- }
- }
- export default PageService;
|