Просмотр исходного кода

Moved createPageInGrowi to util

hakumizuki 4 лет назад
Родитель
Сommit
3292fa96ef

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

@@ -1,9 +1,8 @@
 const { markdownSectionBlock, inputSectionBlock } = require('@growi/slack');
 const { markdownSectionBlock, inputSectionBlock } = require('@growi/slack');
 const logger = require('@alias/logger')('growi:service:SlackCommandHandler:create');
 const logger = require('@alias/logger')('growi:service:SlackCommandHandler:create');
-const { reshapeContentsBody } = require('@growi/slack');
-const mongoose = require('mongoose');
 
 
 module.exports = (crowi) => {
 module.exports = (crowi) => {
+  const createPageInGrowi = require('../../util/createPageInGrowi')(crowi);
   const BaseSlackCommandHandler = require('./slack-command-handler');
   const BaseSlackCommandHandler = require('./slack-command-handler');
   const handler = new BaseSlackCommandHandler();
   const handler = new BaseSlackCommandHandler();
 
 
@@ -58,39 +57,7 @@ module.exports = (crowi) => {
     const path = payload.view.state.values.path.path_input.value;
     const path = payload.view.state.values.path.path_input.value;
     const channelId = JSON.parse(payload.view.private_metadata).channelId;
     const channelId = JSON.parse(payload.view.private_metadata).channelId;
     const contentsBody = payload.view.state.values.contents.contents_input.value;
     const contentsBody = payload.view.state.values.contents.contents_input.value;
-    await this.MUSTMOVETOUTILcreatePage(client, payload, path, channelId, contentsBody);
-  };
-
-  handler.MUSTMOVETOUTILcreatePage = async function(client, payload, path, channelId, contentsBody) {
-    const Page = crowi.model('Page');
-    const pathUtils = require('growi-commons').pathUtils;
-    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;
-    }
+    await createPageInGrowi(client, payload, path, channelId, contentsBody);
   };
   };
 
 
   return handler;
   return handler;

+ 5 - 36
src/server/service/slack-command-handler/togetter.js

@@ -3,11 +3,10 @@ const {
 } = require('@growi/slack');
 } = require('@growi/slack');
 const { parse, format } = require('date-fns');
 const { parse, format } = require('date-fns');
 const axios = require('axios');
 const axios = require('axios');
-const logger = require('@alias/logger')('growi:service:SlackBotService');
-const mongoose = require('mongoose');
-const { reshapeContentsBody } = require('@growi/slack');
+const logger = require('@alias/logger')('growi:service:SlackBotService:togetter');
 
 
 module.exports = (crowi) => {
 module.exports = (crowi) => {
+  const createPageInGrowi = require('../../util/createPageInGrowi')(crowi);
   const BaseSlackCommandHandler = require('./slack-command-handler');
   const BaseSlackCommandHandler = require('./slack-command-handler');
   const handler = new BaseSlackCommandHandler();
   const handler = new BaseSlackCommandHandler();
 
 
@@ -54,6 +53,8 @@ module.exports = (crowi) => {
       await this.togetterCreatePageAndSendPreview(client, payload, path, channel, contentsBody);
       await this.togetterCreatePageAndSendPreview(client, payload, path, channel, contentsBody);
     }
     }
     catch (err) {
     catch (err) {
+      logger.error(err);
+      // upcoming GW-6853 will change: just throw Error() here and handle in slackbot.js
       await client.chat.postMessage({
       await client.chat.postMessage({
         channel: payload.user.id,
         channel: payload.user.id,
         text: err.message,
         text: err.message,
@@ -143,7 +144,7 @@ module.exports = (crowi) => {
 
 
   handler.togetterCreatePageAndSendPreview = async function(client, payload, path, channel, contentsBody) {
   handler.togetterCreatePageAndSendPreview = async function(client, payload, path, channel, contentsBody) {
     try {
     try {
-      await this.MUSTMOVETOUTILcreatePage(client, payload, path, channel, contentsBody);
+      await createPageInGrowi(client, payload, path, channel, contentsBody);
       // send preview to dm
       // send preview to dm
       await client.chat.postMessage({
       await client.chat.postMessage({
         channel: payload.user.id,
         channel: payload.user.id,
@@ -166,38 +167,6 @@ module.exports = (crowi) => {
     }
     }
   };
   };
 
 
-  handler.MUSTMOVETOUTILcreatePage = async function(client, payload, path, channelId, contentsBody) {
-    const Page = crowi.model('Page');
-    const pathUtils = require('growi-commons').pathUtils;
-    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;
-    }
-  };
-
   handler.togetterMessageBlocks = function(messages, body, args, limit) {
   handler.togetterMessageBlocks = function(messages, body, args, limit) {
     return [
     return [
       markdownSectionBlock('Select the oldest and latest datetime of the messages to use.'),
       markdownSectionBlock('Select the oldest and latest datetime of the messages to use.'),

+ 36 - 0
src/server/util/createPageInGrowi.js

@@ -0,0 +1,36 @@
+const { markdownSectionBlock } = require('@growi/slack');
+const logger = require('@alias/logger')('growi:util:createPageInGrowi');
+const { reshapeContentsBody } = require('@growi/slack');
+const mongoose = require('mongoose');
+
+module.exports = crowi => async(client, payload, path, channelId, contentsBody) => {
+  const Page = crowi.model('Page');
+  const pathUtils = require('growi-commons').pathUtils;
+  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;
+  }
+};