zahmis 4 лет назад
Родитель
Сommit
6519583834

+ 50 - 0
packages/app/src/server/models/slack-app-integration-mock.js

@@ -0,0 +1,50 @@
+const crypto = require('crypto');
+const mongoose = require('mongoose');
+
+const permittedChannelsSchema = new mongoose.Schema({
+  channelsObject: {},
+});
+
+
+const schema = new mongoose.Schema({
+  tokenGtoP: { type: String, required: true, unique: true },
+  tokenPtoG: { type: String, required: true, unique: true },
+  permittedChannels: permittedChannelsSchema,
+});
+
+class SlackAppIntegrationMock {
+
+  static generateAccessTokens() {
+    const now = new Date().getTime();
+    const hasher1 = crypto.createHash('sha512');
+    const hasher2 = crypto.createHash('sha512');
+    const tokenGtoP = hasher1.update(`gtop${now.toString()}${process.env.SALT_FOR_GTOP_TOKEN}`).digest('base64');
+    const tokenPtoG = hasher2.update(`ptog${now.toString()}${process.env.SALT_FOR_PTOG_TOKEN}`).digest('base64');
+    return [tokenGtoP, tokenPtoG];
+  }
+
+  static async generateUniqueAccessTokens() {
+    let duplicateTokens;
+    let tokenGtoP;
+    let tokenPtoG;
+    let generateTokens;
+
+    do {
+      generateTokens = this.generateAccessTokens();
+      tokenGtoP = generateTokens[0];
+      tokenPtoG = generateTokens[1];
+      // eslint-disable-next-line no-await-in-loop
+      duplicateTokens = await this.findOne({ $or: [{ tokenGtoP }, { tokenPtoG }] });
+    } while (duplicateTokens != null);
+
+
+    return { tokenGtoP, tokenPtoG };
+  }
+
+}
+
+module.exports = function(crowi) {
+  SlackAppIntegrationMock.crowi = crowi;
+  schema.loadClass(SlackAppIntegrationMock);
+  return mongoose.model('SlackAppIntegrationMock', schema);
+};

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

@@ -0,0 +1,65 @@
+import {
+  Column, CreateDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn, ManyToOne, Index,
+} from 'typeorm';
+import { differenceInMilliseconds } from 'date-fns';
+import { Installation } from './installation';
+
+
+// expected data see below
+//   commandToChannelMap: {
+//     create: ['srv', 'admin'],
+//     search: ['admin'],
+//   }
+interface PermittedChannels {
+  commandToChannelMap: { [command: string]: string[] };
+}
+
+@Entity()
+@Index(['installation', 'growiUri'], { unique: true })
+export class RelationMock {
+
+  @PrimaryGeneratedColumn()
+  readonly id: number;
+
+  @CreateDateColumn()
+  readonly createdAt: Date;
+
+  @UpdateDateColumn()
+  readonly updatedAt: Date;
+
+  @ManyToOne(() => Installation)
+  readonly installation: Installation;
+
+  @Column()
+  @Index({ unique: true })
+  tokenGtoP: string;
+
+  @Column()
+  @Index()
+  tokenPtoG: string;
+
+  @Column()
+  growiUri: string;
+
+  @Column('simple-array')
+  supportedCommandsForBroadcastUse: string[];
+
+  @Column('simple-array')
+  supportedCommandsForSingleUse: string[];
+
+  @Column({ type: 'json' })
+  permittedChannels: PermittedChannels
+
+  @CreateDateColumn()
+  expiredAtCommands: Date;
+
+  isExpiredCommands():boolean {
+    const now = Date.now();
+    return this.expiredAtCommands.getTime() < now;
+  }
+
+  getDistanceInMillisecondsToExpiredAt(baseDate:Date):number {
+    return differenceInMilliseconds(this.expiredAtCommands, baseDate);
+  }
+
+}