2023-04-21 07:55:10 -05:00
|
|
|
import { FindOptions, literal } from 'sequelize'
|
2024-02-22 03:12:04 -06:00
|
|
|
import { AllowNull, Column, CreatedAt, HasMany, Table, UpdatedAt } from 'sequelize-typescript'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { MRunnerRegistrationToken } from '@server/types/models/runners/index.js'
|
|
|
|
import { RunnerRegistrationToken } from '@peertube/peertube-models'
|
2024-02-22 03:12:04 -06:00
|
|
|
import { SequelizeModel, getSort } from '../shared/index.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { RunnerModel } from './runner.js'
|
2023-04-21 07:55:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Tokens used by PeerTube runners to register themselves to the PeerTube instance
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'runnerRegistrationToken',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'registrationToken' ],
|
|
|
|
unique: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2024-02-22 03:12:04 -06:00
|
|
|
export class RunnerRegistrationTokenModel extends SequelizeModel<RunnerRegistrationTokenModel> {
|
2023-04-21 07:55:10 -05:00
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
registrationToken: string
|
|
|
|
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@HasMany(() => RunnerModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
Runners: Awaited<RunnerModel>[]
|
2023-04-21 07:55:10 -05:00
|
|
|
|
|
|
|
static load (id: number) {
|
|
|
|
return RunnerRegistrationTokenModel.findByPk(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
static loadByRegistrationToken (registrationToken: string) {
|
|
|
|
const query = {
|
|
|
|
where: { registrationToken }
|
|
|
|
}
|
|
|
|
|
|
|
|
return RunnerRegistrationTokenModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
static countTotal () {
|
|
|
|
return RunnerRegistrationTokenModel.unscoped().count()
|
|
|
|
}
|
|
|
|
|
|
|
|
static listForApi (options: {
|
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
sort: string
|
|
|
|
}) {
|
|
|
|
const { start, count, sort } = options
|
|
|
|
|
|
|
|
const query: FindOptions = {
|
|
|
|
attributes: {
|
|
|
|
include: [
|
|
|
|
[
|
|
|
|
literal('(SELECT COUNT(*) FROM "runner" WHERE "runner"."runnerRegistrationTokenId" = "RunnerRegistrationTokenModel"."id")'),
|
|
|
|
'registeredRunnersCount'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
|
|
|
order: getSort(sort)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
RunnerRegistrationTokenModel.count(query),
|
|
|
|
RunnerRegistrationTokenModel.findAll<MRunnerRegistrationToken>(query)
|
|
|
|
]).then(([ total, data ]) => ({ total, data }))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
toFormattedJSON (this: MRunnerRegistrationToken): RunnerRegistrationToken {
|
|
|
|
const registeredRunnersCount = this.get('registeredRunnersCount') as number
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
|
|
|
|
registrationToken: this.registrationToken,
|
|
|
|
|
|
|
|
createdAt: this.createdAt,
|
|
|
|
updatedAt: this.updatedAt,
|
|
|
|
|
|
|
|
registeredRunnersCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|