create-page-service.js 1.5 KB

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