PeerTube/server/models/server/server.ts

91 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-09-11 09:27:07 -05:00
import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2017-12-12 10:53:50 -06:00
import { isHostValid } from '../../helpers/custom-validators/servers'
import { ActorModel } from '../activitypub/actor'
2017-12-12 10:53:50 -06:00
import { throwIfNotValid } from '../utils'
2019-07-31 08:57:32 -05:00
import { ServerBlocklistModel } from './server-blocklist'
2019-08-15 04:53:26 -05:00
import * as Bluebird from 'bluebird'
2019-08-20 12:05:31 -05:00
import { MServer, MServerFormattable } from '@server/typings/models/server'
2017-11-15 04:00:25 -06:00
2017-12-12 10:53:50 -06:00
@Table({
tableName: 'server',
indexes: [
2017-11-15 04:00:25 -06:00
{
2017-12-12 10:53:50 -06:00
fields: [ 'host' ],
unique: true
2017-11-15 04:00:25 -06:00
}
]
2017-12-12 10:53:50 -06:00
})
export class ServerModel extends Model<ServerModel> {
@AllowNull(false)
@Is('Host', value => throwIfNotValid(value, isHostValid, 'valid host'))
@Column
host: string
2018-09-11 09:27:07 -05:00
@AllowNull(false)
@Default(false)
@Column
redundancyAllowed: boolean
2017-12-12 10:53:50 -06:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@HasMany(() => ActorModel, {
foreignKey: {
name: 'serverId',
allowNull: true
},
onDelete: 'CASCADE',
hooks: true
})
Actors: ActorModel[]
2018-09-11 09:27:07 -05:00
2019-07-31 08:57:32 -05:00
@HasMany(() => ServerBlocklistModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
BlockedByAccounts: ServerBlocklistModel[]
static load (id: number): Bluebird<MServer> {
const query = {
where: {
id
}
}
return ServerModel.findOne(query)
}
2019-08-15 04:53:26 -05:00
static loadByHost (host: string): Bluebird<MServer> {
2018-09-11 09:27:07 -05:00
const query = {
where: {
host
}
}
return ServerModel.findOne(query)
}
2020-05-07 07:58:24 -05:00
static async loadOrCreateByHost (host: string) {
let server = await ServerModel.loadByHost(host)
if (!server) server = await ServerModel.create({ host })
return server
}
2019-07-31 08:57:32 -05:00
isBlocked () {
return this.BlockedByAccounts && this.BlockedByAccounts.length !== 0
}
2019-08-20 12:05:31 -05:00
toFormattedJSON (this: MServerFormattable) {
return {
host: this.host
}
}
2017-11-23 10:36:15 -06:00
}