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

implement isSupportedGrowiCommandForSingleUse

itizawa 4 лет назад
Родитель
Сommit
03a6464359

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

@@ -163,16 +163,20 @@ export class SlackCtrl {
     // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
     res.send();
 
-    // const syncedRelations = await this.relationsService.syncSupportedGrowiCommands(relations);
-
     const baseDate = new Date();
-    body.growiUrisForSingleUse = relations.filter((relation) => {
-      return this.relationsService.isSupportedGrowiCommandForSingleUse(relation, growiCommand.growiCommandType, baseDate);
-    }).map(relation => relation.growiUri);
 
-    // if (body.growiUrisForSingleUse.length > 0) {
-    //   return this.selectGrowiService.process(growiCommand, authorizeResult, body);
-    // }
+    const relationsForSingleUse:Relation[] = [];
+    await Promise.all(relations.map(async(relation) => {
+      const isSupported = await this.relationsService.isSupportedGrowiCommandForSingleUse(relation, growiCommand.growiCommandType, baseDate);
+      if (isSupported) {
+        relationsForSingleUse.push(relation);
+      }
+    }));
+
+    if (relationsForSingleUse.length > 0) {
+      body.growiUrisForSingleUse = relationsForSingleUse.map(v => v.growiUri);
+      return this.selectGrowiService.process(growiCommand, authorizeResult, body);
+    }
 
     // const relationsForBroadcastUse = syncedRelations.filter((relation) => {
     //   return this.relationsService.isSupportedGrowiCommandForBroadcastUse(relation, growiCommand.growiCommandType);

+ 26 - 18
packages/slackbot-proxy/src/services/RelationsService.ts

@@ -5,6 +5,10 @@ import { addHours } from 'date-fns';
 import { Relation } from '~/entities/relation';
 import { RelationRepository } from '~/repositories/relation';
 
+import loggerFactory from '~/utils/logger';
+
+const logger = loggerFactory('slackbot-proxy:services:RelationsService');
+
 @Service()
 export class RelationsService {
 
@@ -21,34 +25,38 @@ export class RelationsService {
     });
   }
 
-  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(new Date(), 48);
-
-      return this.relationRepository.save(relation);
-    }));
+  async syncSupportedGrowiCommands(relation:Relation): Promise<Relation> {
+    const res = await this.getSupportedGrowiCommands(relation);
+    const { supportedCommandsForBroadcastUse, supportedCommandsForSingleUse } = res.data;
+    relation.supportedCommandsForBroadcastUse = supportedCommandsForBroadcastUse;
+    relation.supportedCommandsForSingleUse = supportedCommandsForSingleUse;
+    relation.expiredAtCommands = addHours(new Date(), 48);
 
-    return result;
+    return this.relationRepository.save(relation);
   }
 
-  isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string, baseDate:Date):boolean {
+  async isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string, baseDate:Date):Promise<boolean> {
     const distanceHoursToExpiredAt = relation.getDistanceInMillisecondsToExpiredAt(baseDate);
 
     if (distanceHoursToExpiredAt < 0) {
-      console.log('sync');
+      try {
+        const syncedRelation = await this.syncSupportedGrowiCommands(relation);
+        return syncedRelation.supportedCommandsForSingleUse.includes(growiCommandType);
+      }
+      catch (err) {
+        logger.error(err);
+        return false;
+      }
     }
 
     // 24 hours
     if (distanceHoursToExpiredAt < 1000 * 60 * 60 * 24) {
-      console.log('update');
+      try {
+        this.syncSupportedGrowiCommands(relation);
+      }
+      catch (err) {
+        logger.error(err);
+      }
     }
 
     return relation.supportedCommandsForSingleUse.includes(growiCommandType);