Browse Source

implement syncSupportedGrowiCommands

itizawa 4 years ago
parent
commit
ac0dcfce99

+ 4 - 4
packages/slackbot-proxy/src/controllers/slack.ts

@@ -163,8 +163,9 @@ export class SlackCtrl {
     // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
     // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
     res.send();
     res.send();
 
 
-    body.growiUrisForSingleUse = relations.filter((relation) => {
-      // TODO GW-6845 retrieve commands if it has expired
+    const syncedRelations = await this.relationsService.syncSupportedGrowiCommands(relations);
+
+    body.growiUrisForSingleUse = syncedRelations.filter((relation) => {
       return this.relationsService.isSupportedGrowiCommandForSingleUse(relation, growiCommand.growiCommandType);
       return this.relationsService.isSupportedGrowiCommandForSingleUse(relation, growiCommand.growiCommandType);
     }).map(relation => relation.growiUri);
     }).map(relation => relation.growiUri);
 
 
@@ -172,8 +173,7 @@ export class SlackCtrl {
       return this.selectGrowiService.process(growiCommand, authorizeResult, body);
       return this.selectGrowiService.process(growiCommand, authorizeResult, body);
     }
     }
 
 
-    const relationsForBroadcastUse = relations.filter((relation) => {
-      // TODO GW-6845 retrieve commands if it has expired
+    const relationsForBroadcastUse = syncedRelations.filter((relation) => {
       return this.relationsService.isSupportedGrowiCommandForBroadcastUse(relation, growiCommand.growiCommandType);
       return this.relationsService.isSupportedGrowiCommandForBroadcastUse(relation, growiCommand.growiCommandType);
     });
     });
 
 

+ 38 - 3
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -1,15 +1,50 @@
-import { Service } from '@tsed/di';
+import { Inject, Service } from '@tsed/di';
+import axios from 'axios';
+import { addHours } from 'date-fns';
+
 import { Relation } from '~/entities/relation';
 import { Relation } from '~/entities/relation';
+import { RelationRepository } from '~/repositories/relation';
 
 
 @Service()
 @Service()
 export class RelationsService {
 export class RelationsService {
 
 
+  @Inject()
+  relationRepository: RelationRepository;
+
+  async getSupportedGrowiCommands(relation:Relation):Promise<any> {
+    // generate API URL
+    const url = new URL('/_api/v3/slack-integration/supported-commands', relation.growiUri);
+    return axios.get(url.toString(), {
+      headers: {
+        'x-growi-ptog-tokens': relation.tokenPtoG,
+      },
+    });
+  }
+
+  async syncSupportedGrowiCommands(relations:Relation[]): Promise<Relation[]> {
+    const result = await Promise.all(relations.map(async(relation) => {
+      if (!relation.isExpiredCommands()) {
+        return relation;
+      }
+
+      const res = await this.getSupportedGrowiCommands(relation);
+      const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
+      relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
+      relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
+      relation.expiredAtCommands = addHours(supportedCommandsForSingleUse.expiredAtCommands, 48);
+
+      return this.relationRepository.save(relation);
+    }));
+
+    return result;
+  }
+
   isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string):boolean {
   isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string):boolean {
-    return !relation.isExpiredCommands() && relation.supportedCommandsForSingleUse.includes(growiCommandType);
+    return relation.supportedCommandsForSingleUse.includes(growiCommandType);
   }
   }
 
 
   isSupportedGrowiCommandForBroadcastUse(relation:Relation, growiCommandType:string):boolean {
   isSupportedGrowiCommandForBroadcastUse(relation:Relation, growiCommandType:string):boolean {
-    return !relation.isExpiredCommands() && relation.supportedCommandsForBroadcastUse.includes(growiCommandType);
+    return relation.supportedCommandsForBroadcastUse.includes(growiCommandType);
   }
   }
 
 
 }
 }

+ 7 - 0
src/server/routes/apiv3/slack-integration.js

@@ -225,5 +225,12 @@ module.exports = (crowi) => {
     return handleInteractions(req, res);
     return handleInteractions(req, res);
   });
   });
 
 
+  router.get('/supported-commands', verifyAccessTokenFromProxy, async(req, res) => {
+    const tokenPtoG = req.headers['x-growi-ptog-tokens'];
+    const slackAppIntegration = await SlackAppIntegration.findOne({ tokenPtoG });
+
+    return res.send(slackAppIntegration);
+  });
+
   return router;
   return router;
 };
 };