Fix AP collections pagination
[oweals/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
6 @Table({
7   tableName: 'server',
8   indexes: [
9     {
10       fields: [ 'host' ],
11       unique: true
12     }
13   ]
14 })
15 export class ServerModel extends Model<ServerModel> {
16
17   @AllowNull(false)
18   @Is('Host', value => throwIfNotValid(value, isHostValid, 'valid host'))
19   @Column
20   host: string
21
22   @AllowNull(false)
23   @Default(false)
24   @Column
25   redundancyAllowed: boolean
26
27   @CreatedAt
28   createdAt: Date
29
30   @UpdatedAt
31   updatedAt: Date
32
33   @HasMany(() => ActorModel, {
34     foreignKey: {
35       name: 'serverId',
36       allowNull: true
37     },
38     onDelete: 'CASCADE',
39     hooks: true
40   })
41   Actors: ActorModel[]
42
43   static loadByHost (host: string) {
44     const query = {
45       where: {
46         host
47       }
48     }
49
50     return ServerModel.findOne(query)
51   }
52
53   toFormattedJSON () {
54     return {
55       host: this.host
56     }
57   }
58 }