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

implement isSupportedGrowiCommandForBroadcastUse

itizawa 4 лет назад
Родитель
Сommit
01839d0292

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

@@ -178,14 +178,18 @@ export class SlackCtrl {
       return this.selectGrowiService.process(growiCommand, authorizeResult, body);
     }
 
-    // const relationsForBroadcastUse = syncedRelations.filter((relation) => {
-    //   return this.relationsService.isSupportedGrowiCommandForBroadcastUse(relation, growiCommand.growiCommandType);
-    // });
+    const relationsForBroadcastUse:Relation[] = [];
+    await Promise.all(relations.map(async(relation) => {
+      const isSupported = await this.relationsService.isSupportedGrowiCommandForBroadcastUse(relation, growiCommand.growiCommandType, baseDate);
+      if (isSupported) {
+        relationsForSingleUse.push(relation);
+      }
+    }));
 
     /*
      * forward to GROWI server
      */
-    // this.sendCommand(growiCommand, relationsForBroadcastUse, body);
+    this.sendCommand(growiCommand, relationsForBroadcastUse, body);
   }
 
   @Post('/interactions')

+ 10 - 6
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -35,13 +35,13 @@ export class RelationsService {
     return this.relationRepository.save(relation);
   }
 
-  async isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string, baseDate:Date):Promise<boolean> {
+  async syncAndReturnBool(relation:Relation, baseDate:Date, discriminantWhetherRelationIsSupported:(relation:Relation)=>boolean):Promise<boolean> {
     const distanceHoursToExpiredAt = relation.getDistanceInMillisecondsToExpiredAt(baseDate);
 
     if (distanceHoursToExpiredAt < 0) {
       try {
         const syncedRelation = await this.syncSupportedGrowiCommands(relation);
-        return syncedRelation.supportedCommandsForSingleUse.includes(growiCommandType);
+        return discriminantWhetherRelationIsSupported(syncedRelation);
       }
       catch (err) {
         logger.error(err);
@@ -59,11 +59,15 @@ export class RelationsService {
       }
     }
 
-    return relation.supportedCommandsForSingleUse.includes(growiCommandType);
+    return discriminantWhetherRelationIsSupported(relation);
   }
 
-  // isSupportedGrowiCommandForBroadcastUse(relation:Relation, growiCommandType:string):boolean {
-  //   return relation.supportedCommandsForBroadcastUse.includes(growiCommandType);
-  // }
+  async isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string, baseDate:Date):Promise<boolean> {
+    return this.syncAndReturnBool(relation, baseDate, relation => relation.supportedCommandsForSingleUse.includes(growiCommandType));
+  }
+
+  async isSupportedGrowiCommandForBroadcastUse(relation:Relation, growiCommandType:string, baseDate:Date):Promise<boolean> {
+    return this.syncAndReturnBool(relation, baseDate, relation => relation.supportedCommandsForBroadcastUse.includes(growiCommandType));
+  }
 
 }