create-page-service.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { markdownSectionBlock } from '@growi/slack/dist/utils/block-kit-builder';
  2. import { reshapeContentsBody } from '@growi/slack/dist/utils/reshape-contents-body';
  3. import { generalXssFilter } from '~/services/general-xss-filter';
  4. import loggerFactory from '~/utils/logger';
  5. // eslint-disable-next-line no-unused-vars
  6. const logger = loggerFactory('growi:service:CreatePageService');
  7. const { pathUtils } = require('@growi/core/dist/utils');
  8. const mongoose = require('mongoose');
  9. class CreatePageService {
  10. constructor(crowi) {
  11. this.crowi = crowi;
  12. }
  13. async createPageInGrowi(interactionPayloadAccessor, path, contentsBody, respondUtil, user) {
  14. const reshapedContentsBody = reshapeContentsBody(contentsBody);
  15. // sanitize path
  16. const sanitizedPath = generalXssFilter.process(path);
  17. const normalizedPath = pathUtils.normalizePath(sanitizedPath);
  18. // Since an ObjectId is required for creating a page, if a user does not exist, a dummy user will be generated
  19. const userOrDummyUser = user != null ? user : { _id: new mongoose.Types.ObjectId() };
  20. const page = await this.crowi.pageService.create(normalizedPath, reshapedContentsBody, userOrDummyUser, {});
  21. // Send a message when page creation is complete
  22. const growiUri = this.crowi.appService.getSiteUrl();
  23. await respondUtil.respond({
  24. text: 'Page has been created',
  25. blocks: [
  26. markdownSectionBlock(`The page <${decodeURI(`${growiUri}/${page._id} | ${decodeURI(growiUri + normalizedPath)}`)}> has been created.`),
  27. ],
  28. });
  29. }
  30. }
  31. module.exports = CreatePageService;