]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/server/server.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / server / server.ts
1 import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { isHostValid } from '../../helpers/custom-validators/servers'
3 import { ActorModel } from '../activitypub/actor'
4 import { throwIfNotValid } from '../utils'
5 import { ServerBlocklistModel } from './server-blocklist'
6 import * as Bluebird from 'bluebird'
7 import { MServer, MServerFormattable } from '@server/typings/models/server'
8
9 @Table({
10 tableName: 'server',
11 indexes: [
12 {
13 fields: [ 'host' ],
14 unique: true
15 }
16 ]
17 })
18 export class ServerModel extends Model<ServerModel> {
19
20 @AllowNull(false)
21 @Is('Host', value => throwIfNotValid(value, isHostValid, 'valid host'))
22 @Column
23 host: string
24
25 @AllowNull(false)
26 @Default(false)
27 @Column
28 redundancyAllowed: boolean
29
30 @CreatedAt
31 createdAt: Date
32
33 @UpdatedAt
34 updatedAt: Date
35
36 @HasMany(() => ActorModel, {
37 foreignKey: {
38 name: 'serverId',
39 allowNull: true
40 },
41 onDelete: 'CASCADE',
42 hooks: true
43 })
44 Actors: ActorModel[]
45
46 @HasMany(() => ServerBlocklistModel, {
47 foreignKey: {
48 allowNull: false
49 },
50 onDelete: 'CASCADE'
51 })
52 BlockedByAccounts: ServerBlocklistModel[]
53
54 static loadByHost (host: string): Bluebird<MServer> {
55 const query = {
56 where: {
57 host
58 }
59 }
60
61 return ServerModel.findOne(query)
62 }
63
64 isBlocked () {
65 return this.BlockedByAccounts && this.BlockedByAccounts.length !== 0
66 }
67
68 toFormattedJSON (this: MServerFormattable) {
69 return {
70 host: this.host
71 }
72 }
73 }