ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

runner-registration-token.ts 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { FindOptions, literal } from 'sequelize'
  2. import { AllowNull, Column, CreatedAt, HasMany, Table, UpdatedAt } from 'sequelize-typescript'
  3. import { MRunnerRegistrationToken } from '@server/types/models/runners/index.js'
  4. import { RunnerRegistrationToken } from '@peertube/peertube-models'
  5. import { SequelizeModel, getSort } from '../shared/index.js'
  6. import { RunnerModel } from './runner.js'
  7. /**
  8. *
  9. * Tokens used by PeerTube runners to register themselves to the PeerTube instance
  10. *
  11. */
  12. @Table({
  13. tableName: 'runnerRegistrationToken',
  14. indexes: [
  15. {
  16. fields: [ 'registrationToken' ],
  17. unique: true
  18. }
  19. ]
  20. })
  21. export class RunnerRegistrationTokenModel extends SequelizeModel<RunnerRegistrationTokenModel> {
  22. @AllowNull(false)
  23. @Column
  24. registrationToken: string
  25. @CreatedAt
  26. createdAt: Date
  27. @UpdatedAt
  28. updatedAt: Date
  29. @HasMany(() => RunnerModel, {
  30. foreignKey: {
  31. allowNull: true
  32. },
  33. onDelete: 'cascade'
  34. })
  35. Runners: Awaited<RunnerModel>[]
  36. static load (id: number) {
  37. return RunnerRegistrationTokenModel.findByPk(id)
  38. }
  39. static loadByRegistrationToken (registrationToken: string) {
  40. const query = {
  41. where: { registrationToken }
  42. }
  43. return RunnerRegistrationTokenModel.findOne(query)
  44. }
  45. static countTotal () {
  46. return RunnerRegistrationTokenModel.unscoped().count()
  47. }
  48. static listForApi (options: {
  49. start: number
  50. count: number
  51. sort: string
  52. }) {
  53. const { start, count, sort } = options
  54. const query: FindOptions = {
  55. attributes: {
  56. include: [
  57. [
  58. literal('(SELECT COUNT(*) FROM "runner" WHERE "runner"."runnerRegistrationTokenId" = "RunnerRegistrationTokenModel"."id")'),
  59. 'registeredRunnersCount'
  60. ]
  61. ]
  62. },
  63. offset: start,
  64. limit: count,
  65. order: getSort(sort)
  66. }
  67. return Promise.all([
  68. RunnerRegistrationTokenModel.count(query),
  69. RunnerRegistrationTokenModel.findAll<MRunnerRegistrationToken>(query)
  70. ]).then(([ total, data ]) => ({ total, data }))
  71. }
  72. // ---------------------------------------------------------------------------
  73. toFormattedJSON (this: MRunnerRegistrationToken): RunnerRegistrationToken {
  74. const registeredRunnersCount = this.get('registeredRunnersCount') as number
  75. return {
  76. id: this.id,
  77. registrationToken: this.registrationToken,
  78. createdAt: this.createdAt,
  79. updatedAt: this.updatedAt,
  80. registeredRunnersCount
  81. }
  82. }
  83. }