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