| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- const loggerFactory = require('@alias/logger');
- const logger = loggerFactory('growi:routes:apiv3:pages'); // eslint-disable-line no-unused-vars
- const express = require('express');
- const pathUtils = require('growi-commons').pathUtils;
- const router = express.Router();
- /**
- * @swagger
- * tags:
- * name: Pages
- */
- module.exports = (crowi) => {
- const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
- const loginRequired = require('../../middlewares/login-required')(crowi, true);
- const loginRequiredStrictly = require('../../middlewares/login-required')(crowi, true);
- const adminRequired = require('../../middlewares/admin-required')(crowi);
- const csrf = require('../../middlewares/csrf')(crowi);
- const Page = crowi.model('Page');
- const PageTagRelation = crowi.model('PageTagRelation');
- const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');
- const pageService = crowi.pageService;
- const globalNotificationService = crowi.getGlobalNotificationService();
- // TODO swagger and validation
- router.post('/', accessTokenParser, loginRequiredStrictly, csrf, async(req, res) => {
- const {
- body, grant, grantUserGroupId, overwriteScopesOfDescendants, isSlackEnabled, slackChannels, socketClientId, pageTags,
- } = req.body;
- let pagePath = req.body.path;
- // check whether path starts slash
- pagePath = pathUtils.addHeadingSlash(pagePath);
- // check page existence
- const isExist = await Page.count({ path: pagePath }) > 0;
- if (isExist) {
- return res.apiv3Err(err, 409);
- }
- const options = { socketClientId };
- if (grant != null) {
- options.grant = grant;
- options.grantUserGroupId = grantUserGroupId;
- }
- const createdPage = await Page.create(pagePath, body, req.user, options);
- let savedTags;
- if (pageTags != null) {
- await PageTagRelation.updatePageTags(createdPage.id, pageTags);
- savedTags = await PageTagRelation.listTagNamesByPage(createdPage.id);
- }
- const result = { page: pageService.serializeToObj(createdPage), tags: savedTags };
- // update scopes for descendants
- if (overwriteScopesOfDescendants) {
- Page.applyScopesToDescendantsAsyncronously(createdPage, req.user);
- }
- // global notification
- try {
- await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_CREATE, createdPage, req.user);
- }
- catch (err) {
- logger.error('Create notification failed', err);
- }
- // user notification
- if (isSlackEnabled) {
- await notifyToSlackByUser(createdPage, req.user, slackChannels, 'create', false);
- }
- return res.apiv3(result);
- });
- /**
- * @swagger
- *
- * /pages/recent:
- * get:
- * tags: [Pages]
- * description: Get recently updated pages
- * responses:
- * 200:
- * description: Return pages recently updated
- *
- */
- router.get('/recent', loginRequired, async(req, res) => {
- const limit = 20;
- const offset = parseInt(req.query.offset) || 0;
- const queryOptions = {
- offset,
- limit,
- includeTrashed: false,
- isRegExpEscapedFromPath: true,
- sort: 'updatedAt',
- desc: -1,
- };
- try {
- const result = await Page.findListWithDescendants('/', req.user, queryOptions);
- if (result.pages.length > limit) {
- result.pages.pop();
- }
- return res.apiv3(result);
- }
- catch (err) {
- res.code = 'unknown';
- logger.error('Failed to get recent pages', err);
- return res.apiv3Err(err, 500);
- }
- });
- /**
- * @swagger
- *
- * /pages/empty-trash:
- * delete:
- * tags: [Pages]
- * description: empty trash
- * responses:
- * 200:
- * description: Succeeded to remove all trash pages
- */
- router.delete('/empty-trash', loginRequired, adminRequired, csrf, async(req, res) => {
- try {
- const pages = await Page.completelyDeletePageRecursively('/trash', req.user);
- return res.apiv3({ pages });
- }
- catch (err) {
- res.code = 'unknown';
- logger.error('Failed to delete trash pages', err);
- return res.apiv3Err(err, 500);
- }
- });
- router.get('/subordinated-list', accessTokenParser, loginRequired, async(req, res) => {
- const { path } = req.query;
- try {
- const pageData = await Page.findByPath(path);
- const result = await Page.findManageableListWithDescendants(pageData, req.user);
- const resultPaths = result.map(element => element.path);
- return res.apiv3({ resultPaths });
- }
- catch (err) {
- res.code = 'unknown';
- logger.error('Failed to find the path', err);
- return res.apiv3Err(err, 500);
- }
- });
- return router;
- };
|