]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/server/server.ts
Fix live ending banner
[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 { MServer, MServerFormattable } from '@server/types/models/server'
3 import { isHostValid } from '../../helpers/custom-validators/servers'
4 import { ActorModel } from '../activitypub/actor'
5 import { throwIfNotValid } from '../utils'
6 import { ServerBlocklistModel } from './server-blocklist'
7
8 @Table({
9 tableName: 'server',
10 indexes: [
11 {
12 fields: [ 'host' ],
13 unique: true
14 }
15 ]
16 })
17 export class ServerModel extends Model {
18
19 @AllowNull(false)
20 @Is('Host', value => throwIfNotValid(value, isHostValid, 'valid host'))
21 @Column
22 host: string
23
24 @AllowNull(false)
25 @Default(false)
26 @Column
27 redundancyAllowed: boolean
28
29 @CreatedAt
30 createdAt: Date
31
32 @UpdatedAt
33 updatedAt: Date
34
35 @HasMany(() => ActorModel, {
36 foreignKey: {
37 name: 'serverId',
38 allowNull: true
39 },
40 onDelete: 'CASCADE',
41 hooks: true
42 })
43 Actors: ActorModel[]
44
45 @HasMany(() => ServerBlocklistModel, {
46 foreignKey: {
47 allowNull: false
48 },
49 onDelete: 'CASCADE'
50 })
51 BlockedByAccounts: ServerBlocklistModel[]
52
53 static load (id: number): Promise<MServer> {
54 const query = {
55 where: {
56 id
57 }
58 }
59
60 return ServerModel.findOne(query)
61 }
62
63 static loadByHost (host: string): Promise<MServer> {
64 const query = {
65 where: {
66 host
67 }
68 }
69
70 return ServerModel.findOne(query)
71 }
72
73 static async loadOrCreateByHost (host: string) {
74 let server = await ServerModel.loadByHost(host)
75 if (!server) server = await ServerModel.create({ host })
76
77 return server
78 }
79
80 isBlocked () {
81 return this.BlockedByAccounts && this.BlockedByAccounts.length !== 0
82 }
83
84 toFormattedJSON (this: MServerFormattable) {
85 return {
86 host: this.host
87 }
88 }
89 }