]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/server/server.ts
Add auto follow back support for instances
[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 6import * as Bluebird from 'bluebird'
1ca9f7c3 7import { MServer, MServerFormattable } 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
8424c402
C
54 static load (id: number): Bluebird<MServer> {
55 const query = {
56 where: {
57 id
58 }
59 }
60
61 return ServerModel.findOne(query)
62 }
63
453e83ea 64 static loadByHost (host: string): Bluebird<MServer> {
c48e82b5
C
65 const query = {
66 where: {
67 host
68 }
69 }
70
71 return ServerModel.findOne(query)
72 }
7ad9b984 73
bfbd9128
C
74 isBlocked () {
75 return this.BlockedByAccounts && this.BlockedByAccounts.length !== 0
76 }
77
1ca9f7c3 78 toFormattedJSON (this: MServerFormattable) {
7ad9b984
C
79 return {
80 host: this.host
81 }
82 }
39445ead 83}