create-page-service.js 1.4 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, user) {
  11. const reshapedContentsBody = reshapeContentsBody(contentsBody);
  12. // sanitize path
  13. const sanitizedPath = this.crowi.xss.process(path);
  14. const normalizedPath = pathUtils.normalizePath(sanitizedPath);
  15. // Since an ObjectId is required for creating a page, if a user does not exist, a dummy user will be generated
  16. const userOrDummyUser = user != null ? user : { _id: new mongoose.Types.ObjectId() };
  17. const page = await this.crowi.pageService.create(normalizedPath, reshapedContentsBody, userOrDummyUser, {});
  18. // Send a message when page creation is complete
  19. const growiUri = this.crowi.appService.getSiteUrl();
  20. await respondUtil.respond({
  21. text: 'Page has been created',
  22. blocks: [
  23. markdownSectionBlock(`The page <${decodeURI(`${growiUri}/${page._id} | ${decodeURI(growiUri + normalizedPath)}`)}> has been created.`),
  24. ],
  25. });
  26. }
  27. }
  28. module.exports = CreatePageService;