PeerTube/server/core/models/server/server.ts

104 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-06-15 02:17:19 -05:00
import { Transaction } from 'sequelize'
2024-02-22 03:12:04 -06:00
import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { MServer, MServerFormattable } from '@server/types/models/server/index.js'
import { isHostValid } from '../../helpers/custom-validators/servers.js'
import { ActorModel } from '../actor/actor.js'
2024-02-22 03:12:04 -06:00
import { SequelizeModel, buildSQLAttributes, throwIfNotValid } from '../shared/index.js'
import { ServerBlocklistModel } from './server-blocklist.js'
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
})
2024-02-22 03:12:04 -06:00
export class ServerModel extends SequelizeModel<ServerModel> {
2017-12-12 10:53:50 -06:00
@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: Awaited<ActorModel>[]
2018-09-11 09:27:07 -05:00
2019-07-31 08:57:32 -05:00
@HasMany(() => ServerBlocklistModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
BlockedBy: Awaited<ServerBlocklistModel>[]
2019-07-31 08:57:32 -05:00
2023-01-09 03:29:23 -06:00
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
// ---------------------------------------------------------------------------
2021-06-15 02:17:19 -05:00
static load (id: number, transaction?: Transaction): Promise<MServer> {
const query = {
where: {
id
2021-06-15 02:17:19 -05:00
},
transaction
}
return ServerModel.findOne(query)
}
2020-12-08 07:30:29 -06:00
static loadByHost (host: string): Promise<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.BlockedBy && this.BlockedBy.length !== 0
2019-07-31 08:57:32 -05:00
}
2019-08-20 12:05:31 -05:00
toFormattedJSON (this: MServerFormattable) {
return {
host: this.host
}
}
2017-11-23 10:36:15 -06:00
}