Browse Source

Merge pull request #7529 from weseek/fix/119361-the-note-and-keep-commands-of-the-growi-bot-are-not-functioning

fix: The "note" and "keep" commands of the GROWI bot are not functioning
Yuki Takei 3 years ago
parent
commit
b0f19512c9

+ 5 - 5
packages/app/src/server/service/slack-command-handler/create-page-service.js

@@ -11,17 +11,17 @@ class CreatePageService {
     this.crowi = crowi;
     this.crowi = crowi;
   }
   }
 
 
-  async createPageInGrowi(interactionPayloadAccessor, path, contentsBody, respondUtil, userId) {
-    const Page = this.crowi.model('Page');
+  async createPageInGrowi(interactionPayloadAccessor, path, contentsBody, respondUtil, user) {
     const reshapedContentsBody = reshapeContentsBody(contentsBody);
     const reshapedContentsBody = reshapeContentsBody(contentsBody);
 
 
     // sanitize path
     // sanitize path
     const sanitizedPath = this.crowi.xss.process(path);
     const sanitizedPath = this.crowi.xss.process(path);
     const normalizedPath = pathUtils.normalizePath(sanitizedPath);
     const normalizedPath = pathUtils.normalizePath(sanitizedPath);
 
 
-    // generate a dummy id because Operation to create a page needs ObjectId
-    const dummyObjectIdOfUser = userId != null ? userId : new mongoose.Types.ObjectId();
-    const page = await this.crowi.pageService.create(normalizedPath, reshapedContentsBody, dummyObjectIdOfUser, {});
+    // Since an ObjectId is required for creating a page, if a user does not exist, a dummy user will be generated
+    const userOrDummyUser = user != null ? user : { _id: new mongoose.Types.ObjectId() };
+
+    const page = await this.crowi.pageService.create(normalizedPath, reshapedContentsBody, userOrDummyUser, {});
 
 
     // Send a message when page creation is complete
     // Send a message when page creation is complete
     const growiUri = this.crowi.appService.getSiteUrl();
     const growiUri = this.crowi.appService.getSiteUrl();

+ 3 - 4
packages/app/src/server/service/slack-command-handler/keep.js

@@ -35,7 +35,6 @@ module.exports = (crowi) => {
     const channelId = payload.channel.id; // this must exist since the type is always block_actions
     const channelId = payload.channel.id; // this must exist since the type is always block_actions
     const user = await User.findUserBySlackMemberId(payload.user.id);
     const user = await User.findUserBySlackMemberId(payload.user.id);
 
 
-    const userId = user != null ? user._id : null;
     // validate form
     // validate form
     const { path, oldest, newest } = await this.keepValidateForm(client, payload, interactionPayloadAccessor);
     const { path, oldest, newest } = await this.keepValidateForm(client, payload, interactionPayloadAccessor);
     // get messages
     // get messages
@@ -45,7 +44,7 @@ module.exports = (crowi) => {
 
 
     const contentsBody = cleanedContents.join('');
     const contentsBody = cleanedContents.join('');
     // create and send url message
     // create and send url message
-    await this.keepCreatePageAndSendPreview(client, interactionPayloadAccessor, path, userId, contentsBody, respondUtil);
+    await this.keepCreatePageAndSendPreview(client, interactionPayloadAccessor, path, user, contentsBody, respondUtil);
   };
   };
 
 
   handler.keepValidateForm = async function(client, payload, interactionPayloadAccessor) {
   handler.keepValidateForm = async function(client, payload, interactionPayloadAccessor) {
@@ -197,8 +196,8 @@ module.exports = (crowi) => {
     return cleanedContents;
     return cleanedContents;
   };
   };
 
 
-  handler.keepCreatePageAndSendPreview = async function(client, interactionPayloadAccessor, path, userId, contentsBody, respondUtil) {
-    await createPageService.createPageInGrowi(interactionPayloadAccessor, path, contentsBody, respondUtil, userId);
+  handler.keepCreatePageAndSendPreview = async function(client, interactionPayloadAccessor, path, user, contentsBody, respondUtil) {
+    await createPageService.createPageInGrowi(interactionPayloadAccessor, path, contentsBody, respondUtil, user);
 
 
     // TODO: contentsBody text characters must be less than 3001
     // TODO: contentsBody text characters must be less than 3001
     // send preview to dm
     // send preview to dm

+ 3 - 1
packages/app/src/server/service/slack-command-handler/note.js

@@ -17,6 +17,7 @@ module.exports = (crowi) => {
     type: 'conversations_select',
     type: 'conversations_select',
     default_to_current_conversation: true,
     default_to_current_conversation: true,
   };
   };
+  const { User } = crowi.models;
 
 
   handler.handleCommand = async(growiCommand, client, body, respondUtil) => {
   handler.handleCommand = async(growiCommand, client, body, respondUtil) => {
     await respondUtil.respond({
     await respondUtil.respond({
@@ -44,12 +45,13 @@ module.exports = (crowi) => {
   };
   };
 
 
   handler.createPage = async function(client, interactionPayload, interactionPayloadAccessor, respondUtil) {
   handler.createPage = async function(client, interactionPayload, interactionPayloadAccessor, respondUtil) {
+    const user = await User.findUserBySlackMemberId(interactionPayload.user.id);
     const path = interactionPayloadAccessor.getStateValues()?.path.path_input.value;
     const path = interactionPayloadAccessor.getStateValues()?.path.path_input.value;
     const contentsBody = interactionPayloadAccessor.getStateValues()?.contents.contents_input.value;
     const contentsBody = interactionPayloadAccessor.getStateValues()?.contents.contents_input.value;
     if (path == null || contentsBody == null) {
     if (path == null || contentsBody == null) {
       throw new SlackCommandHandlerError('All parameters are required.');
       throw new SlackCommandHandlerError('All parameters are required.');
     }
     }
-    await createPageService.createPageInGrowi(interactionPayloadAccessor, path, contentsBody, respondUtil);
+    await createPageService.createPageInGrowi(interactionPayloadAccessor, path, contentsBody, respondUtil, user);
     await respondUtil.deleteOriginal();
     await respondUtil.deleteOriginal();
   };
   };