create-page-service.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import loggerFactory from '~/utils/logger';
  2. const logger = loggerFactory('growi:service:CreatePageService');
  3. const { reshapeContentsBody, respond, markdownSectionBlock } = require('@growi/slack');
  4. const mongoose = require('mongoose');
  5. const { pathUtils } = require('@growi/core');
  6. class CreatePageService {
  7. constructor(crowi) {
  8. this.crowi = crowi;
  9. }
  10. async createPageInGrowi(interactionPayloadAccessor, path, contentsBody, respondUtil) {
  11. const Page = this.crowi.model('Page');
  12. const reshapedContentsBody = reshapeContentsBody(contentsBody);
  13. // sanitize path
  14. const sanitizedPath = this.crowi.xss.process(path);
  15. const normalizedPath = pathUtils.normalizePath(sanitizedPath);
  16. // generate a dummy id because Operation to create a page needs ObjectId
  17. const dummyObjectIdOfUser = new mongoose.Types.ObjectId();
  18. const page = await Page.create(normalizedPath, reshapedContentsBody, dummyObjectIdOfUser, {});
  19. // Send a message when page creation is complete
  20. const growiUri = this.crowi.appService.getSiteUrl();
  21. await respondUtil.respond({
  22. text: 'Page has been created',
  23. blocks: [
  24. markdownSectionBlock(`The page <${decodeURI(`${growiUri}/${page._id} | ${decodeURI(growiUri + normalizedPath)}`)}> has been created.`),
  25. ],
  26. });
  27. }
  28. }
  29. module.exports = CreatePageService;