page.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. const { serializePageSecurely } = require('../models/serializers/page-serializer');
  2. const { serializeRevisionSecurely } = require('../models/serializers/revision-serializer');
  3. const { serializeUserSecurely } = require('../models/serializers/user-serializer');
  4. /**
  5. * @swagger
  6. * tags:
  7. * name: Pages
  8. */
  9. /**
  10. * @swagger
  11. *
  12. * components:
  13. * schemas:
  14. * Page:
  15. * description: Page
  16. * type: object
  17. * properties:
  18. * _id:
  19. * type: string
  20. * description: page ID
  21. * example: 5e07345972560e001761fa63
  22. * __v:
  23. * type: number
  24. * description: DB record version
  25. * example: 0
  26. * commentCount:
  27. * type: number
  28. * description: count of comments
  29. * example: 3
  30. * createdAt:
  31. * type: string
  32. * description: date created at
  33. * example: 2010-01-01T00:00:00.000Z
  34. * creator:
  35. * $ref: '#/components/schemas/User'
  36. * extended:
  37. * type: object
  38. * description: extend data
  39. * example: {}
  40. * grant:
  41. * type: number
  42. * description: grant
  43. * example: 1
  44. * grantedUsers:
  45. * type: array
  46. * description: granted users
  47. * items:
  48. * type: string
  49. * description: user ID
  50. * example: ["5ae5fccfc5577b0004dbd8ab"]
  51. * lastUpdateUser:
  52. * $ref: '#/components/schemas/User'
  53. * liker:
  54. * type: array
  55. * description: granted users
  56. * items:
  57. * type: string
  58. * description: user ID
  59. * example: []
  60. * path:
  61. * type: string
  62. * description: page path
  63. * example: /
  64. * redirectTo:
  65. * type: string
  66. * description: redirect path
  67. * example: ""
  68. * revision:
  69. * $ref: '#/components/schemas/Revision'
  70. * status:
  71. * type: string
  72. * description: status
  73. * enum:
  74. * - 'wip'
  75. * - 'published'
  76. * - 'deleted'
  77. * - 'deprecated'
  78. * example: published
  79. * updatedAt:
  80. * type: string
  81. * description: date updated at
  82. * example: 2010-01-01T00:00:00.000Z
  83. *
  84. * UpdatePost:
  85. * description: UpdatePost
  86. * type: object
  87. * properties:
  88. * _id:
  89. * type: string
  90. * description: update post ID
  91. * example: 5e0734e472560e001761fa68
  92. * __v:
  93. * type: number
  94. * description: DB record version
  95. * example: 0
  96. * pathPattern:
  97. * type: string
  98. * description: path pattern
  99. * example: /test
  100. * patternPrefix:
  101. * type: string
  102. * description: patternPrefix prefix
  103. * example: /
  104. * patternPrefix2:
  105. * type: string
  106. * description: path
  107. * example: test
  108. * channel:
  109. * type: string
  110. * description: channel
  111. * example: general
  112. * provider:
  113. * type: string
  114. * description: provider
  115. * enum:
  116. * - slack
  117. * example: slack
  118. * creator:
  119. * $ref: '#/components/schemas/User'
  120. * createdAt:
  121. * type: string
  122. * description: date created at
  123. * example: 2010-01-01T00:00:00.000Z
  124. */
  125. /* eslint-disable no-use-before-define */
  126. module.exports = function(crowi, app) {
  127. const debug = require('debug')('growi:routes:page');
  128. const logger = require('@alias/logger')('growi:routes:page');
  129. const swig = require('swig-templates');
  130. const pathUtils = require('growi-commons').pathUtils;
  131. const Page = crowi.model('Page');
  132. const User = crowi.model('User');
  133. const PageTagRelation = crowi.model('PageTagRelation');
  134. const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');
  135. const ShareLink = crowi.model('ShareLink');
  136. const ApiResponse = require('../util/apiResponse');
  137. const getToday = require('../util/getToday');
  138. const { configManager, xssService } = crowi;
  139. const interceptorManager = crowi.getInterceptorManager();
  140. const globalNotificationService = crowi.getGlobalNotificationService();
  141. const userNotificationService = crowi.getUserNotificationService();
  142. const XssOption = require('../../lib/service/xss/xssOption');
  143. const Xss = require('../../lib/service/xss/index');
  144. const initializedConfig = {
  145. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  146. tagWhiteList: xssService.getTagWhiteList(),
  147. attrWhiteList: xssService.getAttrWhiteList(),
  148. };
  149. const xssOption = new XssOption(initializedConfig);
  150. const xss = new Xss(xssOption);
  151. const actions = {};
  152. function getPathFromRequest(req) {
  153. return pathUtils.normalizePath(req.params[0] || '');
  154. }
  155. function isUserPage(path) {
  156. if (path.match(/^\/user\/[^/]+\/?$/)) {
  157. return true;
  158. }
  159. return false;
  160. }
  161. function generatePager(offset, limit, totalCount) {
  162. let prev = null;
  163. if (offset > 0) {
  164. prev = offset - limit;
  165. if (prev < 0) {
  166. prev = 0;
  167. }
  168. }
  169. let next = offset + limit;
  170. if (totalCount < next) {
  171. next = null;
  172. }
  173. return {
  174. prev,
  175. next,
  176. offset,
  177. };
  178. }
  179. function addRenderVarsForPage(renderVars, page) {
  180. renderVars.page = page;
  181. renderVars.revision = page.revision;
  182. renderVars.pageIdOnHackmd = page.pageIdOnHackmd;
  183. renderVars.revisionHackmdSynced = page.revisionHackmdSynced;
  184. renderVars.hasDraftOnHackmd = page.hasDraftOnHackmd;
  185. if (page.creator != null) {
  186. renderVars.page.creator = renderVars.page.creator.toObject();
  187. }
  188. if (page.revision.author != null) {
  189. renderVars.revision.author = renderVars.revision.author.toObject();
  190. }
  191. if (page.deleteUser != null) {
  192. renderVars.page.deleteUser = renderVars.page.deleteUser.toObject();
  193. }
  194. }
  195. function addRenderVarsForPresentation(renderVars, page) {
  196. // sanitize page.revision.body
  197. if (crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention')) {
  198. const preventXssRevision = xss.process(page.revision.body);
  199. page.revision.body = preventXssRevision;
  200. }
  201. renderVars.page = page;
  202. renderVars.revision = page.revision;
  203. }
  204. async function addRenderVarsForUserPage(renderVars, page) {
  205. const userData = await User.findUserByUsername(User.getUsernameByPath(page.path));
  206. if (userData != null) {
  207. renderVars.pageUser = serializeUserSecurely(userData);
  208. }
  209. }
  210. function addRenderVarsForScope(renderVars, page) {
  211. renderVars.grant = page.grant;
  212. renderVars.grantedGroupId = page.grantedGroup ? page.grantedGroup.id : null;
  213. renderVars.grantedGroupName = page.grantedGroup ? page.grantedGroup.name : null;
  214. }
  215. async function addRenderVarsForDescendants(renderVars, path, requestUser, offset, limit, isRegExpEscapedFromPath) {
  216. const SEENER_THRESHOLD = 10;
  217. const queryOptions = {
  218. offset,
  219. limit,
  220. includeTrashed: path.startsWith('/trash/'),
  221. isRegExpEscapedFromPath,
  222. };
  223. const result = await Page.findListWithDescendants(path, requestUser, queryOptions);
  224. if (result.pages.length > limit) {
  225. result.pages.pop();
  226. }
  227. renderVars.viewConfig = {
  228. seener_threshold: SEENER_THRESHOLD,
  229. };
  230. renderVars.pager = generatePager(result.offset, result.limit, result.totalCount);
  231. renderVars.pages = result.pages;
  232. }
  233. function replacePlaceholdersOfTemplate(template, req) {
  234. if (req.user == null) {
  235. return '';
  236. }
  237. const definitions = {
  238. pagepath: getPathFromRequest(req),
  239. username: req.user.name,
  240. today: getToday(),
  241. };
  242. const compiledTemplate = swig.compile(template);
  243. return compiledTemplate(definitions);
  244. }
  245. async function showPageForPresentation(req, res, next) {
  246. const path = getPathFromRequest(req);
  247. const { revisionId } = req.query;
  248. let page = await Page.findByPathAndViewer(path, req.user);
  249. if (page == null) {
  250. next();
  251. }
  252. const renderVars = {};
  253. // populate
  254. page = await page.populateDataToMakePresentation(revisionId);
  255. if (page != null) {
  256. addRenderVarsForPresentation(renderVars, page);
  257. }
  258. return res.render('page_presentation', renderVars);
  259. }
  260. async function showTopPage(req, res, next) {
  261. const portalPath = req.path;
  262. const revisionId = req.query.revision;
  263. const view = 'layout-growi/page_list';
  264. const renderVars = { path: portalPath };
  265. let portalPage = await Page.findByPathAndViewer(portalPath, req.user);
  266. portalPage.initLatestRevisionField(revisionId);
  267. // add user to seen users
  268. if (req.user != null) {
  269. portalPage = await portalPage.seen(req.user);
  270. }
  271. // populate
  272. portalPage = await portalPage.populateDataToShowRevision();
  273. addRenderVarsForPage(renderVars, portalPage);
  274. const sharelinksNumber = await ShareLink.countDocuments({ relatedPage: portalPage._id });
  275. renderVars.sharelinksNumber = sharelinksNumber;
  276. const limit = 50;
  277. const offset = parseInt(req.query.offset) || 0;
  278. await addRenderVarsForDescendants(renderVars, portalPath, req.user, offset, limit);
  279. await interceptorManager.process('beforeRenderPage', req, res, renderVars);
  280. return res.render(view, renderVars);
  281. }
  282. async function showPageForGrowiBehavior(req, res, next) {
  283. const path = getPathFromRequest(req);
  284. const revisionId = req.query.revision;
  285. let page = await Page.findByPathAndViewer(path, req.user);
  286. if (page == null) {
  287. // check the page is forbidden or just does not exist.
  288. req.isForbidden = await Page.count({ path }) > 0;
  289. return next();
  290. }
  291. if (page.redirectTo) {
  292. debug(`Redirect to '${page.redirectTo}'`);
  293. return res.redirect(`${encodeURI(page.redirectTo)}?redirectFrom=${encodeURIComponent(path)}`);
  294. }
  295. logger.debug('Page is found when processing pageShowForGrowiBehavior', page._id, page.path);
  296. const limit = 50;
  297. const offset = parseInt(req.query.offset) || 0;
  298. const renderVars = {};
  299. let view = 'layout-growi/page';
  300. page.initLatestRevisionField(revisionId);
  301. // add user to seen users
  302. if (req.user != null) {
  303. page = await page.seen(req.user);
  304. }
  305. // populate
  306. page = await page.populateDataToShowRevision();
  307. addRenderVarsForPage(renderVars, page);
  308. addRenderVarsForScope(renderVars, page);
  309. await addRenderVarsForDescendants(renderVars, path, req.user, offset, limit, true);
  310. const sharelinksNumber = await ShareLink.countDocuments({ relatedPage: page._id });
  311. renderVars.sharelinksNumber = sharelinksNumber;
  312. if (isUserPage(page.path)) {
  313. // change template
  314. view = 'layout-growi/user_page';
  315. await addRenderVarsForUserPage(renderVars, page);
  316. }
  317. await interceptorManager.process('beforeRenderPage', req, res, renderVars);
  318. return res.render(view, renderVars);
  319. }
  320. actions.showTopPage = function(req, res) {
  321. return showTopPage(req, res);
  322. };
  323. /**
  324. * Redirect to the page without trailing slash
  325. */
  326. actions.showPageWithEndOfSlash = function(req, res, next) {
  327. return res.redirect(pathUtils.removeTrailingSlash(req.path));
  328. };
  329. /**
  330. * switch action
  331. * - presentation mode
  332. * - by behaviorType
  333. */
  334. actions.showPage = async function(req, res, next) {
  335. // presentation mode
  336. if (req.query.presentation) {
  337. return showPageForPresentation(req, res, next);
  338. }
  339. // delegate to showPageForGrowiBehavior
  340. return showPageForGrowiBehavior(req, res, next);
  341. };
  342. actions.showSharedPage = async function(req, res, next) {
  343. const { linkId } = req.params;
  344. const revisionId = req.query.revision;
  345. const shareLink = await ShareLink.findOne({ _id: linkId }).populate('relatedPage');
  346. if (shareLink == null || shareLink.relatedPage == null) {
  347. // page or sharelink are not found
  348. return res.render('layout-growi/not_found_shared_page');
  349. }
  350. const renderVars = {};
  351. renderVars.sharelink = shareLink;
  352. // check if share link is expired
  353. if (shareLink.isExpired()) {
  354. // page is not found
  355. return res.render('layout-growi/expired_shared_page', renderVars);
  356. }
  357. let page = shareLink.relatedPage;
  358. // presentation mode
  359. if (req.query.presentation) {
  360. page = await page.populateDataToMakePresentation(revisionId);
  361. // populate
  362. addRenderVarsForPage(renderVars, page);
  363. return res.render('page_presentation', renderVars);
  364. }
  365. page.initLatestRevisionField(revisionId);
  366. // populate
  367. page = await page.populateDataToShowRevision();
  368. addRenderVarsForPage(renderVars, page);
  369. addRenderVarsForScope(renderVars, page);
  370. await interceptorManager.process('beforeRenderPage', req, res, renderVars);
  371. return res.render('layout-growi/shared_page', renderVars);
  372. };
  373. /**
  374. * switch action by behaviorType
  375. */
  376. /* eslint-disable no-else-return */
  377. actions.trashPageListShowWrapper = function(req, res) {
  378. // redirect to '/trash'
  379. return res.redirect('/trash');
  380. };
  381. /* eslint-enable no-else-return */
  382. /**
  383. * switch action by behaviorType
  384. */
  385. /* eslint-disable no-else-return */
  386. actions.trashPageShowWrapper = function(req, res) {
  387. // Crowi behavior for '/trash/*'
  388. return actions.deletedPageListShow(req, res);
  389. };
  390. /* eslint-enable no-else-return */
  391. /**
  392. * switch action by behaviorType
  393. */
  394. /* eslint-disable no-else-return */
  395. actions.deletedPageListShowWrapper = function(req, res) {
  396. const path = `/trash${getPathFromRequest(req)}`;
  397. return res.redirect(path);
  398. };
  399. /* eslint-enable no-else-return */
  400. actions.notFound = async function(req, res) {
  401. const path = getPathFromRequest(req);
  402. const isCreatable = Page.isCreatableName(path);
  403. let view;
  404. const renderVars = { path };
  405. if (!isCreatable) {
  406. view = 'layout-growi/not_creatable';
  407. }
  408. else if (req.isForbidden) {
  409. view = 'layout-growi/forbidden';
  410. }
  411. else {
  412. view = 'layout-growi/not_found';
  413. // retrieve templates
  414. if (req.user != null) {
  415. const template = await Page.findTemplate(path);
  416. if (template.templateBody) {
  417. const body = replacePlaceholdersOfTemplate(template.templateBody, req);
  418. const tags = template.templateTags;
  419. renderVars.template = body;
  420. renderVars.templateTags = tags;
  421. }
  422. }
  423. // add scope variables by ancestor page
  424. const ancestor = await Page.findAncestorByPathAndViewer(path, req.user);
  425. if (ancestor != null) {
  426. await ancestor.populate('grantedGroup').execPopulate();
  427. addRenderVarsForScope(renderVars, ancestor);
  428. }
  429. }
  430. const limit = 50;
  431. const offset = parseInt(req.query.offset) || 0;
  432. await addRenderVarsForDescendants(renderVars, path, req.user, offset, limit, true);
  433. return res.render(view, renderVars);
  434. };
  435. actions.deletedPageListShow = async function(req, res) {
  436. // normalizePath makes '/trash/' -> '/trash'
  437. const path = pathUtils.normalizePath(`/trash${getPathFromRequest(req)}`);
  438. const limit = 50;
  439. const offset = parseInt(req.query.offset) || 0;
  440. const queryOptions = {
  441. offset,
  442. limit,
  443. includeTrashed: true,
  444. };
  445. const renderVars = {
  446. page: null,
  447. path,
  448. pages: [],
  449. };
  450. const result = await Page.findListWithDescendants(path, req.user, queryOptions);
  451. if (result.pages.length > limit) {
  452. result.pages.pop();
  453. }
  454. renderVars.pager = generatePager(result.offset, result.limit, result.totalCount);
  455. renderVars.pages = result.pages;
  456. res.render('layout-growi/page_list', renderVars);
  457. };
  458. /**
  459. * redirector
  460. */
  461. actions.redirector = async function(req, res) {
  462. const id = req.params.id;
  463. const page = await Page.findByIdAndViewer(id, req.user);
  464. if (page != null) {
  465. return res.redirect(encodeURI(page.path));
  466. }
  467. return res.redirect('/');
  468. };
  469. const api = {};
  470. actions.api = api;
  471. /**
  472. * @swagger
  473. *
  474. * /pages.list:
  475. * get:
  476. * tags: [Pages, CrowiCompatibles]
  477. * operationId: listPages
  478. * summary: /pages.list
  479. * description: Get list of pages
  480. * parameters:
  481. * - in: query
  482. * name: path
  483. * schema:
  484. * $ref: '#/components/schemas/Page/properties/path'
  485. * - in: query
  486. * name: user
  487. * schema:
  488. * $ref: '#/components/schemas/User/properties/username'
  489. * - in: query
  490. * name: limit
  491. * schema:
  492. * $ref: '#/components/schemas/V1PaginateResult/properties/meta/properties/limit'
  493. * - in: query
  494. * name: offset
  495. * schema:
  496. * $ref: '#/components/schemas/V1PaginateResult/properties/meta/properties/offset'
  497. * responses:
  498. * 200:
  499. * description: Succeeded to get list of pages.
  500. * content:
  501. * application/json:
  502. * schema:
  503. * properties:
  504. * ok:
  505. * $ref: '#/components/schemas/V1Response/properties/ok'
  506. * pages:
  507. * type: array
  508. * items:
  509. * $ref: '#/components/schemas/Page'
  510. * description: page list
  511. * 403:
  512. * $ref: '#/components/responses/403'
  513. * 500:
  514. * $ref: '#/components/responses/500'
  515. */
  516. /**
  517. * @api {get} /pages.list List pages by user
  518. * @apiName ListPage
  519. * @apiGroup Page
  520. *
  521. * @apiParam {String} path
  522. * @apiParam {String} user
  523. */
  524. api.list = async function(req, res) {
  525. const username = req.query.user || null;
  526. const path = req.query.path || null;
  527. const limit = +req.query.limit || 50;
  528. const offset = parseInt(req.query.offset) || 0;
  529. const queryOptions = { offset, limit: limit + 1 };
  530. // Accepts only one of these
  531. if (username === null && path === null) {
  532. return res.json(ApiResponse.error('Parameter user or path is required.'));
  533. }
  534. if (username !== null && path !== null) {
  535. return res.json(ApiResponse.error('Parameter user or path is required.'));
  536. }
  537. try {
  538. let result = null;
  539. if (path == null) {
  540. const user = await User.findUserByUsername(username);
  541. if (user === null) {
  542. throw new Error('The user not found.');
  543. }
  544. result = await Page.findListByCreator(user, req.user, queryOptions);
  545. }
  546. else {
  547. result = await Page.findListByStartWith(path, req.user, queryOptions);
  548. }
  549. if (result.pages.length > limit) {
  550. result.pages.pop();
  551. }
  552. result.pages.forEach((page) => {
  553. if (page.lastUpdateUser != null && page.lastUpdateUser instanceof User) {
  554. page.lastUpdateUser = serializeUserSecurely(page.lastUpdateUser);
  555. }
  556. });
  557. return res.json(ApiResponse.success(result));
  558. }
  559. catch (err) {
  560. return res.json(ApiResponse.error(err));
  561. }
  562. };
  563. // TODO If everything that depends on this route, delete it too
  564. api.create = async function(req, res) {
  565. const body = req.body.body || null;
  566. let pagePath = req.body.path || null;
  567. const grant = req.body.grant || null;
  568. const grantUserGroupId = req.body.grantUserGroupId || null;
  569. const overwriteScopesOfDescendants = req.body.overwriteScopesOfDescendants || null;
  570. const isSlackEnabled = !!req.body.isSlackEnabled; // cast to boolean
  571. const slackChannels = req.body.slackChannels || null;
  572. const socketClientId = req.body.socketClientId || undefined;
  573. const pageTags = req.body.pageTags || undefined;
  574. if (body === null || pagePath === null) {
  575. return res.json(ApiResponse.error('Parameters body and path are required.'));
  576. }
  577. // check whether path starts slash
  578. pagePath = pathUtils.addHeadingSlash(pagePath);
  579. // check page existence
  580. const isExist = await Page.count({ path: pagePath }) > 0;
  581. if (isExist) {
  582. return res.json(ApiResponse.error('Page exists', 'already_exists'));
  583. }
  584. const options = { socketClientId };
  585. if (grant != null) {
  586. options.grant = grant;
  587. options.grantUserGroupId = grantUserGroupId;
  588. }
  589. const createdPage = await Page.create(pagePath, body, req.user, options);
  590. let savedTags;
  591. if (pageTags != null) {
  592. await PageTagRelation.updatePageTags(createdPage.id, pageTags);
  593. savedTags = await PageTagRelation.listTagNamesByPage(createdPage.id);
  594. }
  595. const result = {
  596. page: serializePageSecurely(createdPage),
  597. revision: serializeRevisionSecurely(createdPage.revision),
  598. tags: savedTags,
  599. };
  600. res.json(ApiResponse.success(result));
  601. // update scopes for descendants
  602. if (overwriteScopesOfDescendants) {
  603. Page.applyScopesToDescendantsAsyncronously(createdPage, req.user);
  604. }
  605. // global notification
  606. try {
  607. await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_CREATE, createdPage, req.user);
  608. }
  609. catch (err) {
  610. logger.error('Create notification failed', err);
  611. }
  612. // user notification
  613. if (isSlackEnabled) {
  614. try {
  615. const results = await userNotificationService.fire(createdPage, req.user, slackChannels, 'create');
  616. results.forEach((result) => {
  617. if (result.status === 'rejected') {
  618. logger.error('Create user notification failed', result.reason);
  619. }
  620. });
  621. }
  622. catch (err) {
  623. logger.error('Create user notification failed', err);
  624. }
  625. }
  626. };
  627. /**
  628. * @swagger
  629. *
  630. * /pages.update:
  631. * post:
  632. * tags: [Pages, CrowiCompatibles]
  633. * operationId: updatePage
  634. * summary: /pages.update
  635. * description: Update page
  636. * requestBody:
  637. * content:
  638. * application/json:
  639. * schema:
  640. * properties:
  641. * body:
  642. * $ref: '#/components/schemas/Revision/properties/body'
  643. * page_id:
  644. * $ref: '#/components/schemas/Page/properties/_id'
  645. * revision_id:
  646. * $ref: '#/components/schemas/Revision/properties/_id'
  647. * grant:
  648. * $ref: '#/components/schemas/Page/properties/grant'
  649. * required:
  650. * - body
  651. * - page_id
  652. * - revision_id
  653. * responses:
  654. * 200:
  655. * description: Succeeded to update page.
  656. * content:
  657. * application/json:
  658. * schema:
  659. * properties:
  660. * ok:
  661. * $ref: '#/components/schemas/V1Response/properties/ok'
  662. * page:
  663. * $ref: '#/components/schemas/Page'
  664. * revision:
  665. * $ref: '#/components/schemas/Revision'
  666. * 403:
  667. * $ref: '#/components/responses/403'
  668. * 500:
  669. * $ref: '#/components/responses/500'
  670. */
  671. /**
  672. * @api {post} /pages.update Update page
  673. * @apiName UpdatePage
  674. * @apiGroup Page
  675. *
  676. * @apiParam {String} body
  677. * @apiParam {String} page_id
  678. * @apiParam {String} revision_id
  679. * @apiParam {String} grant
  680. *
  681. * In the case of the page exists:
  682. * - If revision_id is specified => update the page,
  683. * - If revision_id is not specified => force update by the new contents.
  684. */
  685. api.update = async function(req, res) {
  686. const pageBody = req.body.body || null;
  687. const pageId = req.body.page_id || null;
  688. const revisionId = req.body.revision_id || null;
  689. const grant = req.body.grant || null;
  690. const grantUserGroupId = req.body.grantUserGroupId || null;
  691. const overwriteScopesOfDescendants = req.body.overwriteScopesOfDescendants || null;
  692. const isSlackEnabled = !!req.body.isSlackEnabled; // cast to boolean
  693. const slackChannels = req.body.slackChannels || null;
  694. const isSyncRevisionToHackmd = !!req.body.isSyncRevisionToHackmd; // cast to boolean
  695. const socketClientId = req.body.socketClientId || undefined;
  696. const pageTags = req.body.pageTags || undefined;
  697. if (pageId === null || pageBody === null || revisionId === null) {
  698. return res.json(ApiResponse.error('page_id, body and revision_id are required.'));
  699. }
  700. // check page existence
  701. const isExist = await Page.count({ _id: pageId }) > 0;
  702. if (!isExist) {
  703. return res.json(ApiResponse.error(`Page('${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
  704. }
  705. // check revision
  706. let page = await Page.findByIdAndViewer(pageId, req.user);
  707. if (page != null && revisionId != null && !page.isUpdatable(revisionId)) {
  708. return res.json(ApiResponse.error('Posted param "revisionId" is outdated.', 'outdated'));
  709. }
  710. const options = { isSyncRevisionToHackmd, socketClientId };
  711. if (grant != null) {
  712. options.grant = grant;
  713. options.grantUserGroupId = grantUserGroupId;
  714. }
  715. const Revision = crowi.model('Revision');
  716. const previousRevision = await Revision.findById(revisionId);
  717. try {
  718. page = await Page.updatePage(page, pageBody, previousRevision.body, req.user, options);
  719. }
  720. catch (err) {
  721. logger.error('error on _api/pages.update', err);
  722. return res.json(ApiResponse.error(err));
  723. }
  724. let savedTags;
  725. if (pageTags != null) {
  726. await PageTagRelation.updatePageTags(pageId, pageTags);
  727. savedTags = await PageTagRelation.listTagNamesByPage(pageId);
  728. }
  729. const result = {
  730. page: serializePageSecurely(page),
  731. revision: serializeRevisionSecurely(page.revision),
  732. tags: savedTags,
  733. };
  734. res.json(ApiResponse.success(result));
  735. // update scopes for descendants
  736. if (overwriteScopesOfDescendants) {
  737. Page.applyScopesToDescendantsAsyncronously(page, req.user);
  738. }
  739. // global notification
  740. try {
  741. await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_EDIT, page, req.user);
  742. }
  743. catch (err) {
  744. logger.error('Edit notification failed', err);
  745. }
  746. // user notification
  747. if (isSlackEnabled) {
  748. try {
  749. const results = await userNotificationService.fire(page, req.user, slackChannels, 'update', { previousRevision });
  750. results.forEach((result) => {
  751. if (result.status === 'rejected') {
  752. logger.error('Create user notification failed', result.reason);
  753. }
  754. });
  755. }
  756. catch (err) {
  757. logger.error('Create user notification failed', err);
  758. }
  759. }
  760. };
  761. /**
  762. * @swagger
  763. *
  764. * /pages.get:
  765. * get:
  766. * tags: [Pages, CrowiCompatibles]
  767. * operationId: getPage
  768. * summary: /pages.get
  769. * description: Get page data
  770. * parameters:
  771. * - in: query
  772. * name: page_id
  773. * schema:
  774. * $ref: '#/components/schemas/Page/properties/_id'
  775. * - in: query
  776. * name: path
  777. * schema:
  778. * $ref: '#/components/schemas/Page/properties/path'
  779. * - in: query
  780. * name: revision_id
  781. * schema:
  782. * $ref: '#/components/schemas/Revision/properties/_id'
  783. * responses:
  784. * 200:
  785. * description: Succeeded to get page data.
  786. * content:
  787. * application/json:
  788. * schema:
  789. * properties:
  790. * ok:
  791. * $ref: '#/components/schemas/V1Response/properties/ok'
  792. * page:
  793. * $ref: '#/components/schemas/Page'
  794. * 403:
  795. * $ref: '#/components/responses/403'
  796. * 500:
  797. * $ref: '#/components/responses/500'
  798. */
  799. /**
  800. * @api {get} /pages.get Get page data
  801. * @apiName GetPage
  802. * @apiGroup Page
  803. *
  804. * @apiParam {String} page_id
  805. * @apiParam {String} path
  806. * @apiParam {String} revision_id
  807. */
  808. api.get = async function(req, res) {
  809. const pagePath = req.query.path || null;
  810. const pageId = req.query.page_id || null; // TODO: handling
  811. if (!pageId && !pagePath) {
  812. return res.json(ApiResponse.error(new Error('Parameter path or page_id is required.')));
  813. }
  814. let page;
  815. try {
  816. if (pageId) { // prioritized
  817. page = await Page.findByIdAndViewer(pageId, req.user);
  818. }
  819. else if (pagePath) {
  820. page = await Page.findByPathAndViewer(pagePath, req.user);
  821. }
  822. if (page == null) {
  823. throw new Error(`Page '${pageId || pagePath}' is not found or forbidden`, 'notfound_or_forbidden');
  824. }
  825. page.initLatestRevisionField();
  826. // populate
  827. page = await page.populateDataToShowRevision();
  828. }
  829. catch (err) {
  830. return res.json(ApiResponse.error(err));
  831. }
  832. const result = {};
  833. result.page = page; // TODO consider to use serializePageSecurely method -- 2018.08.06 Yuki Takei
  834. return res.json(ApiResponse.success(result));
  835. };
  836. /**
  837. * @swagger
  838. *
  839. * /pages.exist:
  840. * get:
  841. * tags: [Pages]
  842. * operationId: getPageExistence
  843. * summary: /pages.exist
  844. * description: Get page existence
  845. * parameters:
  846. * - in: query
  847. * name: pagePaths
  848. * schema:
  849. * type: string
  850. * description: Page path list in JSON Array format
  851. * example: '["/", "/user/unknown"]'
  852. * responses:
  853. * 200:
  854. * description: Succeeded to get page existence.
  855. * content:
  856. * application/json:
  857. * schema:
  858. * properties:
  859. * ok:
  860. * $ref: '#/components/schemas/V1Response/properties/ok'
  861. * pages:
  862. * type: string
  863. * description: Properties of page path and existence
  864. * example: '{"/": true, "/user/unknown": false}'
  865. * 403:
  866. * $ref: '#/components/responses/403'
  867. * 500:
  868. * $ref: '#/components/responses/500'
  869. */
  870. /**
  871. * @api {get} /pages.exist Get if page exists
  872. * @apiName GetPage
  873. * @apiGroup Page
  874. *
  875. * @apiParam {String} pages (stringified JSON)
  876. */
  877. api.exist = async function(req, res) {
  878. const pagePaths = JSON.parse(req.query.pagePaths || '[]');
  879. const pages = {};
  880. await Promise.all(pagePaths.map(async(path) => {
  881. // check page existence
  882. const isExist = await Page.count({ path }) > 0;
  883. pages[path] = isExist;
  884. return;
  885. }));
  886. const result = { pages };
  887. return res.json(ApiResponse.success(result));
  888. };
  889. /**
  890. * @swagger
  891. *
  892. * /pages.getPageTag:
  893. * get:
  894. * tags: [Pages]
  895. * operationId: getPageTag
  896. * summary: /pages.getPageTag
  897. * description: Get page tag
  898. * parameters:
  899. * - in: query
  900. * name: pageId
  901. * schema:
  902. * $ref: '#/components/schemas/Page/properties/_id'
  903. * responses:
  904. * 200:
  905. * description: Succeeded to get page tags.
  906. * content:
  907. * application/json:
  908. * schema:
  909. * properties:
  910. * ok:
  911. * $ref: '#/components/schemas/V1Response/properties/ok'
  912. * tags:
  913. * $ref: '#/components/schemas/Tags'
  914. * 403:
  915. * $ref: '#/components/responses/403'
  916. * 500:
  917. * $ref: '#/components/responses/500'
  918. */
  919. /**
  920. * @api {get} /pages.getPageTag get page tags
  921. * @apiName GetPageTag
  922. * @apiGroup Page
  923. *
  924. * @apiParam {String} pageId
  925. */
  926. api.getPageTag = async function(req, res) {
  927. const result = {};
  928. try {
  929. result.tags = await PageTagRelation.listTagNamesByPage(req.query.pageId);
  930. }
  931. catch (err) {
  932. return res.json(ApiResponse.error(err));
  933. }
  934. return res.json(ApiResponse.success(result));
  935. };
  936. /**
  937. * @swagger
  938. *
  939. * /pages.updatePost:
  940. * get:
  941. * tags: [Pages, CrowiCompatibles]
  942. * operationId: getUpdatePostPage
  943. * summary: /pages.updatePost
  944. * description: Get UpdatePost setting list
  945. * parameters:
  946. * - in: query
  947. * name: path
  948. * schema:
  949. * $ref: '#/components/schemas/Page/properties/path'
  950. * responses:
  951. * 200:
  952. * description: Succeeded to get UpdatePost setting list.
  953. * content:
  954. * application/json:
  955. * schema:
  956. * properties:
  957. * ok:
  958. * $ref: '#/components/schemas/V1Response/properties/ok'
  959. * updatePost:
  960. * $ref: '#/components/schemas/UpdatePost'
  961. * 403:
  962. * $ref: '#/components/responses/403'
  963. * 500:
  964. * $ref: '#/components/responses/500'
  965. */
  966. /**
  967. * @api {get} /pages.updatePost
  968. * @apiName Get UpdatePost setting list
  969. * @apiGroup Page
  970. *
  971. * @apiParam {String} path
  972. */
  973. api.getUpdatePost = function(req, res) {
  974. const path = req.query.path;
  975. const UpdatePost = crowi.model('UpdatePost');
  976. if (!path) {
  977. return res.json(ApiResponse.error({}));
  978. }
  979. UpdatePost.findSettingsByPath(path)
  980. .then((data) => {
  981. // eslint-disable-next-line no-param-reassign
  982. data = data.map((e) => {
  983. return e.channel;
  984. });
  985. debug('Found updatePost data', data);
  986. const result = { updatePost: data };
  987. return res.json(ApiResponse.success(result));
  988. })
  989. .catch((err) => {
  990. debug('Error occured while get setting', err);
  991. return res.json(ApiResponse.error({}));
  992. });
  993. };
  994. /**
  995. * @api {post} /pages.remove Remove page
  996. * @apiName RemovePage
  997. * @apiGroup Page
  998. *
  999. * @apiParam {String} page_id Page Id.
  1000. * @apiParam {String} revision_id
  1001. */
  1002. api.remove = async function(req, res) {
  1003. const pageId = req.body.page_id;
  1004. const previousRevision = req.body.revision_id || null;
  1005. const socketClientId = req.body.socketClientId || undefined;
  1006. // get completely flag
  1007. const isCompletely = (req.body.completely != null);
  1008. // get recursively flag
  1009. const isRecursively = (req.body.recursively != null);
  1010. const options = { socketClientId };
  1011. const page = await Page.findByIdAndViewer(pageId, req.user);
  1012. if (page == null) {
  1013. return res.json(ApiResponse.error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
  1014. }
  1015. debug('Delete page', page._id, page.path);
  1016. try {
  1017. if (isCompletely) {
  1018. if (!req.user.canDeleteCompletely(page.creator)) {
  1019. return res.json(ApiResponse.error('You can not delete completely', 'user_not_admin'));
  1020. }
  1021. await crowi.pageService.deleteCompletely(page, req.user, options, isRecursively);
  1022. }
  1023. else {
  1024. if (!page.isUpdatable(previousRevision)) {
  1025. return res.json(ApiResponse.error('Someone could update this page, so couldn\'t delete.', 'outdated'));
  1026. }
  1027. await crowi.pageService.deletePage(page, req.user, options, isRecursively);
  1028. }
  1029. }
  1030. catch (err) {
  1031. logger.error('Error occured while get setting', err);
  1032. return res.json(ApiResponse.error('Failed to delete page.', err.message));
  1033. }
  1034. debug('Page deleted', page.path);
  1035. const result = {};
  1036. result.page = page; // TODO consider to use serializePageSecurely method -- 2018.08.06 Yuki Takei
  1037. res.json(ApiResponse.success(result));
  1038. try {
  1039. // global notification
  1040. await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_DELETE, page, req.user);
  1041. }
  1042. catch (err) {
  1043. logger.error('Delete notification failed', err);
  1044. }
  1045. };
  1046. /**
  1047. * @api {post} /pages.revertRemove Revert removed page
  1048. * @apiName RevertRemovePage
  1049. * @apiGroup Page
  1050. *
  1051. * @apiParam {String} page_id Page Id.
  1052. */
  1053. api.revertRemove = async function(req, res, options) {
  1054. const pageId = req.body.page_id;
  1055. const socketClientId = req.body.socketClientId || undefined;
  1056. // get recursively flag
  1057. const isRecursively = (req.body.recursively != null);
  1058. let page;
  1059. try {
  1060. page = await Page.findByIdAndViewer(pageId, req.user);
  1061. if (page == null) {
  1062. throw new Error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden');
  1063. }
  1064. page = await crowi.pageService.revertDeletedPage(page, req.user, { socketClientId }, isRecursively);
  1065. }
  1066. catch (err) {
  1067. logger.error('Error occured while get setting', err);
  1068. return res.json(ApiResponse.error('Failed to revert deleted page.'));
  1069. }
  1070. const result = {};
  1071. result.page = page; // TODO consider to use serializePageSecurely method -- 2018.08.06 Yuki Takei
  1072. return res.json(ApiResponse.success(result));
  1073. };
  1074. /**
  1075. * @swagger
  1076. *
  1077. * /pages.rename:
  1078. * post:
  1079. * tags: [Pages, CrowiCompatibles]
  1080. * operationId: renamePage
  1081. * summary: /pages.rename
  1082. * description: Rename page
  1083. * requestBody:
  1084. * content:
  1085. * application/json:
  1086. * schema:
  1087. * properties:
  1088. * page_id:
  1089. * $ref: '#/components/schemas/Page/properties/_id'
  1090. * path:
  1091. * $ref: '#/components/schemas/Page/properties/path'
  1092. * revision_id:
  1093. * $ref: '#/components/schemas/Revision/properties/_id'
  1094. * new_path:
  1095. * type: string
  1096. * description: new path
  1097. * example: /user/alice/new_test
  1098. * create_redirect:
  1099. * type: boolean
  1100. * description: whether redirect page
  1101. * required:
  1102. * - page_id
  1103. * responses:
  1104. * 200:
  1105. * description: Succeeded to rename page.
  1106. * content:
  1107. * application/json:
  1108. * schema:
  1109. * properties:
  1110. * ok:
  1111. * $ref: '#/components/schemas/V1Response/properties/ok'
  1112. * page:
  1113. * $ref: '#/components/schemas/Page'
  1114. * 403:
  1115. * $ref: '#/components/responses/403'
  1116. * 500:
  1117. * $ref: '#/components/responses/500'
  1118. */
  1119. /**
  1120. * @api {post} /pages.rename Rename page
  1121. * @apiName RenamePage
  1122. * @apiGroup Page
  1123. *
  1124. * @apiParam {String} page_id Page Id.
  1125. * @apiParam {String} path
  1126. * @apiParam {String} revision_id
  1127. * @apiParam {String} new_path New path name.
  1128. * @apiParam {Bool} create_redirect
  1129. */
  1130. api.rename = async function(req, res) {
  1131. const pageId = req.body.page_id;
  1132. const previousRevision = req.body.revision_id || null;
  1133. let newPagePath = pathUtils.normalizePath(req.body.new_path);
  1134. const options = {
  1135. createRedirectPage: (req.body.create_redirect != null),
  1136. updateMetadata: (req.body.remain_metadata == null),
  1137. socketClientId: +req.body.socketClientId || undefined,
  1138. };
  1139. const isRecursively = (req.body.recursively != null);
  1140. if (!Page.isCreatableName(newPagePath)) {
  1141. return res.json(ApiResponse.error(`Could not use the path '${newPagePath})'`, 'invalid_path'));
  1142. }
  1143. // check whether path starts slash
  1144. newPagePath = pathUtils.addHeadingSlash(newPagePath);
  1145. const isExist = await Page.count({ path: newPagePath }) > 0;
  1146. if (isExist) {
  1147. // if page found, cannot cannot rename to that path
  1148. return res.json(ApiResponse.error(`'new_path=${newPagePath}' already exists`, 'already_exists'));
  1149. }
  1150. let page;
  1151. try {
  1152. page = await Page.findByIdAndViewer(pageId, req.user);
  1153. if (page == null) {
  1154. return res.json(ApiResponse.error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
  1155. }
  1156. if (!page.isUpdatable(previousRevision)) {
  1157. return res.json(ApiResponse.error('Someone could update this page, so couldn\'t delete.', 'outdated'));
  1158. }
  1159. page = await crowi.pageService.renamePage(page, newPagePath, req.user, options, isRecursively);
  1160. }
  1161. catch (err) {
  1162. logger.error(err);
  1163. return res.json(ApiResponse.error('Failed to update page.', 'unknown'));
  1164. }
  1165. const result = {};
  1166. result.page = page; // TODO consider to use serializePageSecurely method -- 2018.08.06 Yuki Takei
  1167. res.json(ApiResponse.success(result));
  1168. try {
  1169. // global notification
  1170. await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_MOVE, page, req.user, {
  1171. oldPath: req.body.path,
  1172. });
  1173. }
  1174. catch (err) {
  1175. logger.error('Move notification failed', err);
  1176. }
  1177. return page;
  1178. };
  1179. /**
  1180. * @swagger
  1181. *
  1182. * /pages.duplicate:
  1183. * post:
  1184. * tags: [Pages]
  1185. * operationId: duplicatePage
  1186. * summary: /pages.duplicate
  1187. * description: Duplicate page
  1188. * requestBody:
  1189. * content:
  1190. * application/json:
  1191. * schema:
  1192. * properties:
  1193. * page_id:
  1194. * $ref: '#/components/schemas/Page/properties/_id'
  1195. * new_path:
  1196. * $ref: '#/components/schemas/Page/properties/path'
  1197. * required:
  1198. * - page_id
  1199. * responses:
  1200. * 200:
  1201. * description: Succeeded to duplicate page.
  1202. * content:
  1203. * application/json:
  1204. * schema:
  1205. * properties:
  1206. * ok:
  1207. * $ref: '#/components/schemas/V1Response/properties/ok'
  1208. * page:
  1209. * $ref: '#/components/schemas/Page'
  1210. * tags:
  1211. * $ref: '#/components/schemas/Tags'
  1212. * 403:
  1213. * $ref: '#/components/responses/403'
  1214. * 500:
  1215. * $ref: '#/components/responses/500'
  1216. */
  1217. /**
  1218. * @api {post} /pages.duplicate Duplicate page
  1219. * @apiName DuplicatePage
  1220. * @apiGroup Page
  1221. *
  1222. * @apiParam {String} page_id Page Id.
  1223. * @apiParam {String} new_path New path name.
  1224. */
  1225. api.duplicate = async function(req, res) {
  1226. const pageId = req.body.page_id;
  1227. let newPagePath = pathUtils.normalizePath(req.body.new_path);
  1228. const page = await Page.findByIdAndViewer(pageId, req.user);
  1229. if (page == null) {
  1230. return res.json(ApiResponse.error(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden'));
  1231. }
  1232. // check whether path starts slash
  1233. newPagePath = pathUtils.addHeadingSlash(newPagePath);
  1234. await page.populateDataToShowRevision();
  1235. const originTags = await page.findRelatedTagsById();
  1236. req.body.path = newPagePath;
  1237. req.body.body = page.revision.body;
  1238. req.body.grant = page.grant;
  1239. req.body.grantedUsers = page.grantedUsers;
  1240. req.body.grantUserGroupId = page.grantedGroup;
  1241. req.body.pageTags = originTags;
  1242. return api.create(req, res);
  1243. };
  1244. /**
  1245. * @api {post} /pages.unlink Remove the redirecting page
  1246. * @apiName UnlinkPage
  1247. * @apiGroup Page
  1248. *
  1249. * @apiParam {String} page_id Page Id.
  1250. * @apiParam {String} revision_id
  1251. */
  1252. api.unlink = async function(req, res) {
  1253. const path = req.body.path;
  1254. try {
  1255. await Page.removeRedirectOriginPageByPath(path);
  1256. logger.debug('Redirect Page deleted', path);
  1257. }
  1258. catch (err) {
  1259. logger.error('Error occured while get setting', err);
  1260. return res.json(ApiResponse.error('Failed to delete redirect page.'));
  1261. }
  1262. const result = { path };
  1263. return res.json(ApiResponse.success(result));
  1264. };
  1265. return actions;
  1266. };