]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/server/server.ts
Move typescript utils in its own directory
[github/Chocobozzz/PeerTube.git] / server / models / server / server.ts
CommitLineData
eae0365b 1import { Transaction } from 'sequelize'
c48e82b5 2import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
b49f22d8 3import { MServer, MServerFormattable } from '@server/types/models/server'
6b5f72be 4import { AttributesOnly } from '@shared/typescript-utils'
3fd3ab2d 5import { isHostValid } from '../../helpers/custom-validators/servers'
7d9ba5c0 6import { ActorModel } from '../actor/actor'
3fd3ab2d 7import { throwIfNotValid } from '../utils'
bfbd9128 8import { ServerBlocklistModel } from './server-blocklist'
60862425 9
3fd3ab2d
C
10@Table({
11 tableName: 'server',
12 indexes: [
60862425 13 {
3fd3ab2d
C
14 fields: [ 'host' ],
15 unique: true
60862425 16 }
60862425 17 ]
3fd3ab2d 18})
16c016e8 19export class ServerModel extends Model<Partial<AttributesOnly<ServerModel>>> {
3fd3ab2d
C
20
21 @AllowNull(false)
22 @Is('Host', value => throwIfNotValid(value, isHostValid, 'valid host'))
23 @Column
24 host: string
25
c48e82b5
C
26 @AllowNull(false)
27 @Default(false)
28 @Column
29 redundancyAllowed: boolean
30
3fd3ab2d
C
31 @CreatedAt
32 createdAt: Date
33
34 @UpdatedAt
35 updatedAt: Date
f05a1c30
C
36
37 @HasMany(() => ActorModel, {
38 foreignKey: {
39 name: 'serverId',
40 allowNull: true
41 },
42 onDelete: 'CASCADE',
43 hooks: true
44 })
45 Actors: ActorModel[]
c48e82b5 46
bfbd9128
C
47 @HasMany(() => ServerBlocklistModel, {
48 foreignKey: {
49 allowNull: false
50 },
51 onDelete: 'CASCADE'
52 })
2760b454 53 BlockedBy: ServerBlocklistModel[]
bfbd9128 54
eae0365b 55 static load (id: number, transaction?: Transaction): Promise<MServer> {
8424c402
C
56 const query = {
57 where: {
58 id
eae0365b
C
59 },
60 transaction
8424c402
C
61 }
62
63 return ServerModel.findOne(query)
64 }
65
b49f22d8 66 static loadByHost (host: string): Promise<MServer> {
c48e82b5
C
67 const query = {
68 where: {
69 host
70 }
71 }
72
73 return ServerModel.findOne(query)
74 }
7ad9b984 75
80fdaf06
C
76 static async loadOrCreateByHost (host: string) {
77 let server = await ServerModel.loadByHost(host)
78 if (!server) server = await ServerModel.create({ host })
79
80 return server
81 }
82
bfbd9128 83 isBlocked () {
2760b454 84 return this.BlockedBy && this.BlockedBy.length !== 0
bfbd9128
C
85 }
86
1ca9f7c3 87 toFormattedJSON (this: MServerFormattable) {
7ad9b984
C
88 return {
89 host: this.host
90 }
91 }
39445ead 92}