]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/server.ts
Skip videos count on client if we don't use it
[github/Chocobozzz/PeerTube.git] / server / models / server / server.ts
index c43146156efad09d366c76df309695ac01ac8f0d..8b07115f1f810a43c0403f7ef7b75b3702ff64a0 100644 (file)
@@ -1,6 +1,10 @@
-import { AllowNull, Column, CreatedAt, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
 import { isHostValid } from '../../helpers/custom-validators/servers'
+import { ActorModel } from '../activitypub/actor'
 import { throwIfNotValid } from '../utils'
+import { ServerBlocklistModel } from './server-blocklist'
+import * as Bluebird from 'bluebird'
+import { MServer, MServerFormattable } from '@server/typings/models/server'
 
 @Table({
   tableName: 'server',
@@ -18,9 +22,62 @@ export class ServerModel extends Model<ServerModel> {
   @Column
   host: string
 
+  @AllowNull(false)
+  @Default(false)
+  @Column
+  redundancyAllowed: boolean
+
   @CreatedAt
   createdAt: Date
 
   @UpdatedAt
   updatedAt: Date
+
+  @HasMany(() => ActorModel, {
+    foreignKey: {
+      name: 'serverId',
+      allowNull: true
+    },
+    onDelete: 'CASCADE',
+    hooks: true
+  })
+  Actors: ActorModel[]
+
+  @HasMany(() => ServerBlocklistModel, {
+    foreignKey: {
+      allowNull: false
+    },
+    onDelete: 'CASCADE'
+  })
+  BlockedByAccounts: ServerBlocklistModel[]
+
+  static load (id: number): Bluebird<MServer> {
+    const query = {
+      where: {
+        id
+      }
+    }
+
+    return ServerModel.findOne(query)
+  }
+
+  static loadByHost (host: string): Bluebird<MServer> {
+    const query = {
+      where: {
+        host
+      }
+    }
+
+    return ServerModel.findOne(query)
+  }
+
+  isBlocked () {
+    return this.BlockedByAccounts && this.BlockedByAccounts.length !== 0
+  }
+
+  toFormattedJSON (this: MServerFormattable) {
+    return {
+      host: this.host
+    }
+  }
 }