hakumizuki il y a 4 ans
Parent
commit
dd33b9cdd3

+ 3 - 2
src/server/service/slack-command-handler/create.js

@@ -2,7 +2,8 @@ const { markdownSectionBlock, inputSectionBlock } = require('@growi/slack');
 const logger = require('@alias/logger')('growi:service:SlackCommandHandler:create');
 
 module.exports = (crowi) => {
-  const createPageInGrowi = require('./util/create-page-in-growi')(crowi);
+  const SlackCommandUtil = require('./slack-command-util');
+  const util = new SlackCommandUtil(crowi);
   const BaseSlackCommandHandler = require('./slack-command-handler');
   const handler = new BaseSlackCommandHandler();
 
@@ -57,7 +58,7 @@ module.exports = (crowi) => {
     const path = payload.view.state.values.path.path_input.value;
     const channelId = JSON.parse(payload.view.private_metadata).channelId;
     const contentsBody = payload.view.state.values.contents.contents_input.value;
-    await createPageInGrowi(client, payload, path, channelId, contentsBody);
+    await util.createPageInGrowi(client, payload, path, channelId, contentsBody);
   };
 
   return handler;

+ 46 - 0
src/server/service/slack-command-handler/slack-command-util.js

@@ -0,0 +1,46 @@
+const { markdownSectionBlock } = require('@growi/slack');
+const logger = require('@alias/logger')('growi:util:createPageInGrowi');
+const { reshapeContentsBody } = require('@growi/slack');
+const mongoose = require('mongoose');
+const pathUtils = require('growi-commons').pathUtils;
+
+class SlackCommandUtil {
+
+  constructor(crowi) {
+    this.crowi = crowi;
+  }
+
+  async createPageInGrowi(client, payload, path, channelId, contentsBody) {
+    const Page = this.crowi.model('Page');
+    const reshapedContentsBody = reshapeContentsBody(contentsBody);
+    try {
+      // sanitize path
+      const sanitizedPath = this.crowi.xss.process(path);
+      const normalizedPath = pathUtils.normalizePath(sanitizedPath);
+
+      // generate a dummy id because Operation to create a page needs ObjectId
+      const dummyObjectIdOfUser = new mongoose.Types.ObjectId();
+      const page = await Page.create(normalizedPath, reshapedContentsBody, dummyObjectIdOfUser, {});
+
+      // Send a message when page creation is complete
+      const growiUri = this.crowi.appService.getSiteUrl();
+      await client.chat.postEphemeral({
+        channel: channelId,
+        user: payload.user.id,
+        text: `The page <${decodeURI(`${growiUri}/${page._id} | ${decodeURI(growiUri + normalizedPath)}`)}> has been created.`,
+      });
+    }
+    catch (err) {
+      client.chat.postMessage({
+        channel: payload.user.id,
+        blocks: [
+          markdownSectionBlock(`Cannot create new page to existed path\n *Contents* :memo:\n ${reshapedContentsBody}`)],
+      });
+      logger.error('Failed to create page in GROWI.');
+      throw err;
+    }
+  }
+
+}
+
+module.exports = SlackCommandUtil;

+ 3 - 2
src/server/service/slack-command-handler/togetter.js

@@ -6,7 +6,8 @@ const axios = require('axios');
 const logger = require('@alias/logger')('growi:service:SlackBotService:togetter');
 
 module.exports = (crowi) => {
-  const createPageInGrowi = require('./util/create-page-in-growi')(crowi);
+  const SlackCommandUtil = require('./slack-command-util');
+  const util = new SlackCommandUtil(crowi);
   const BaseSlackCommandHandler = require('./slack-command-handler');
   const handler = new BaseSlackCommandHandler();
 
@@ -144,7 +145,7 @@ module.exports = (crowi) => {
 
   handler.togetterCreatePageAndSendPreview = async function(client, payload, path, channel, contentsBody) {
     try {
-      await createPageInGrowi(client, payload, path, channel, contentsBody);
+      await util.createPageInGrowi(client, payload, path, channel, contentsBody);
       // send preview to dm
       await client.chat.postMessage({
         channel: payload.user.id,

+ 0 - 36
src/server/service/slack-command-handler/util/create-page-in-growi.js

@@ -1,36 +0,0 @@
-const { markdownSectionBlock } = require('@growi/slack');
-const logger = require('@alias/logger')('growi:util:createPageInGrowi');
-const { reshapeContentsBody } = require('@growi/slack');
-const mongoose = require('mongoose');
-const pathUtils = require('growi-commons').pathUtils;
-
-module.exports = crowi => async(client, payload, path, channelId, contentsBody) => {
-  const Page = crowi.model('Page');
-  const reshapedContentsBody = reshapeContentsBody(contentsBody);
-  try {
-    // sanitize path
-    const sanitizedPath = crowi.xss.process(path);
-    const normalizedPath = pathUtils.normalizePath(sanitizedPath);
-
-    // generate a dummy id because Operation to create a page needs ObjectId
-    const dummyObjectIdOfUser = new mongoose.Types.ObjectId();
-    const page = await Page.create(normalizedPath, reshapedContentsBody, dummyObjectIdOfUser, {});
-
-    // Send a message when page creation is complete
-    const growiUri = crowi.appService.getSiteUrl();
-    await client.chat.postEphemeral({
-      channel: channelId,
-      user: payload.user.id,
-      text: `The page <${decodeURI(`${growiUri}/${page._id} | ${decodeURI(growiUri + normalizedPath)}`)}> has been created.`,
-    });
-  }
-  catch (err) {
-    client.chat.postMessage({
-      channel: payload.user.id,
-      blocks: [
-        markdownSectionBlock(`Cannot create new page to existed path\n *Contents* :memo:\n ${reshapedContentsBody}`)],
-    });
-    logger.error('Failed to create page in GROWI.');
-    throw err;
-  }
-};