Jelajahi Sumber

implement RelationsService

itizawa 4 tahun lalu
induk
melakukan
fb90f4f98e

+ 10 - 7
packages/slackbot-proxy/src/controllers/slack.ts

@@ -21,6 +21,7 @@ import { ExtractGrowiUriFromReq } from '~/middlewares/slack-to-growi/extract-gro
 import { InstallerService } from '~/services/InstallerService';
 import { SelectGrowiService } from '~/services/SelectGrowiService';
 import { RegisterService } from '~/services/RegisterService';
+import { RelationsService } from '~/services/RelationsService';
 import { UnregisterService } from '~/services/UnregisterService';
 import { InvalidUrlError } from '../models/errors';
 import loggerFactory from '~/utils/logger';
@@ -50,6 +51,9 @@ export class SlackCtrl {
   @Inject()
   registerService: RegisterService;
 
+  @Inject()
+  relationsService: RelationsService;
+
   @Inject()
   unregisterService: UnregisterService;
 
@@ -159,25 +163,24 @@ export class SlackCtrl {
     // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
     res.send();
 
-    body.growiUrisForSinglePost = relations.filter((relation) => {
+    body.growiUrisForSingleUse = relations.filter((relation) => {
       // TODO GW-6845 retrieve commands if it has expired
-      return !relation.isExpiredCommands() && relation.supportedCommandsForSingleUse.includes(growiCommand.growiCommandType);
+      return this.relationsService.isSupportedGrowiCommandForSingleUse(relation, growiCommand.growiCommandType);
     }).map(relation => relation.growiUri);
 
-
-    if (body.growiUrisForSinglePost.length > 0) {
+    if (body.growiUrisForSingleUse.length > 0) {
       return this.selectGrowiService.process(growiCommand, authorizeResult, body);
     }
 
-    const relationsForBroadcast = relations.filter((relation) => {
+    const relationsForBroadcastUse = relations.filter((relation) => {
       // TODO GW-6845 retrieve commands if it has expired
-      return !relation.isExpiredCommands() && relation.supportedCommandsForBroadcastUse.includes(growiCommand.growiCommandType);
+      return this.relationsService.isSupportedGrowiCommandForBroadcastUse(relation, growiCommand.growiCommandType);
     });
 
     /*
      * forward to GROWI server
      */
-    this.sendCommand(growiCommand, relationsForBroadcast, body);
+    this.sendCommand(growiCommand, relationsForBroadcastUse, body);
   }
 
   @Post('/interactions')

+ 15 - 0
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -0,0 +1,15 @@
+import { Service } from '@tsed/di';
+import { Relation } from '~/entities/relation';
+
+@Service()
+export class RelationsService {
+
+  isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string):boolean {
+    return !relation.isExpiredCommands() && relation.supportedCommandsForSingleUse.includes(growiCommandType);
+  }
+
+  isSupportedGrowiCommandForBroadcastUse(relation:Relation, growiCommandType:string):boolean {
+    return !relation.isExpiredCommands() && relation.supportedCommandsForBroadcastUse.includes(growiCommandType);
+  }
+
+}

+ 2 - 2
packages/slackbot-proxy/src/services/SelectGrowiService.ts

@@ -21,7 +21,7 @@ export class SelectGrowiService implements GrowiCommandProcessor {
   @Inject()
   relationRepository: RelationRepository;
 
-  async process(growiCommand: GrowiCommand, authorizeResult: AuthorizeResult, body: {[key:string]:string } & {growiUrisForSinglePost:string[]}): Promise<void> {
+  async process(growiCommand: GrowiCommand, authorizeResult: AuthorizeResult, body: {[key:string]:string } & {growiUrisForSingleUse:string[]}): Promise<void> {
     const { botToken } = authorizeResult;
 
     if (botToken == null) {
@@ -60,7 +60,7 @@ export class SelectGrowiService implements GrowiCommandProcessor {
             element: {
               type: 'static_select',
               action_id: 'growi_app',
-              options: body.growiUrisForSinglePost.map((growiUri) => {
+              options: body.growiUrisForSingleUse.map((growiUri) => {
                 return ({
                   text: {
                     type: 'plain_text',