itizawa 4 лет назад
Родитель
Сommit
6cc02530ff

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

@@ -163,24 +163,25 @@ 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 syncedRelations = await this.relationsService.syncSupportedGrowiCommands(relations);
 
-    body.growiUrisForSingleUse = syncedRelations.filter((relation) => {
-      return this.relationsService.isSupportedGrowiCommandForSingleUse(relation, growiCommand.growiCommandType);
+    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);
-    }
+    // if (body.growiUrisForSingleUse.length > 0) {
+    //   return this.selectGrowiService.process(growiCommand, authorizeResult, body);
+    // }
 
-    const relationsForBroadcastUse = syncedRelations.filter((relation) => {
-      return this.relationsService.isSupportedGrowiCommandForBroadcastUse(relation, growiCommand.growiCommandType);
-    });
+    // const relationsForBroadcastUse = syncedRelations.filter((relation) => {
+    //   return this.relationsService.isSupportedGrowiCommandForBroadcastUse(relation, growiCommand.growiCommandType);
+    // });
 
     /*
      * forward to GROWI server
      */
-    this.sendCommand(growiCommand, relationsForBroadcastUse, body);
+    // this.sendCommand(growiCommand, relationsForBroadcastUse, body);
   }
 
   @Post('/interactions')

+ 5 - 0
packages/slackbot-proxy/src/entities/relation.ts

@@ -1,6 +1,7 @@
 import {
   Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, Index,
 } from 'typeorm';
+import { differenceInMilliseconds } from 'date-fns';
 import { Installation } from './installation';
 
 @Entity()
@@ -44,4 +45,8 @@ export class Relation {
     return this.expiredAtCommands.getTime() < now;
   }
 
+  getDistanceInMillisecondsToExpiredAt(baseDate:Date):number {
+    return differenceInMilliseconds(this.expiredAtCommands, baseDate);
+  }
+
 }

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

@@ -39,12 +39,23 @@ export class RelationsService {
     return result;
   }
 
-  isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string):boolean {
+  isSupportedGrowiCommandForSingleUse(relation:Relation, growiCommandType:string, baseDate:Date):boolean {
+    const distanceHoursToExpiredAt = relation.getDistanceInMillisecondsToExpiredAt(baseDate);
+
+    if (distanceHoursToExpiredAt < 0) {
+      console.log('sync');
+    }
+
+    // 24 hours
+    if (distanceHoursToExpiredAt < 1000 * 60 * 60 * 24) {
+      console.log('update');
+    }
+
     return relation.supportedCommandsForSingleUse.includes(growiCommandType);
   }
 
-  isSupportedGrowiCommandForBroadcastUse(relation:Relation, growiCommandType:string):boolean {
-    return relation.supportedCommandsForBroadcastUse.includes(growiCommandType);
-  }
+  // isSupportedGrowiCommandForBroadcastUse(relation:Relation, growiCommandType:string):boolean {
+  //   return relation.supportedCommandsForBroadcastUse.includes(growiCommandType);
+  // }
 
 }