video-change-ownership.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { VideoChangeOwnership, type VideoChangeOwnershipStatusType } from '@peertube/peertube-models'
  2. import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership.js'
  3. import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
  4. import { AccountModel } from '../account/account.js'
  5. import { SequelizeModel, getSort } from '../shared/index.js'
  6. import { VideoModel, ScopeNames as VideoScopeNames } from './video.js'
  7. enum ScopeNames {
  8. WITH_ACCOUNTS = 'WITH_ACCOUNTS',
  9. WITH_VIDEO = 'WITH_VIDEO'
  10. }
  11. @Table({
  12. tableName: 'videoChangeOwnership',
  13. indexes: [
  14. {
  15. fields: [ 'videoId' ]
  16. },
  17. {
  18. fields: [ 'initiatorAccountId' ]
  19. },
  20. {
  21. fields: [ 'nextOwnerAccountId' ]
  22. }
  23. ]
  24. })
  25. @Scopes(() => ({
  26. [ScopeNames.WITH_ACCOUNTS]: {
  27. include: [
  28. {
  29. model: AccountModel,
  30. as: 'Initiator',
  31. required: true
  32. },
  33. {
  34. model: AccountModel,
  35. as: 'NextOwner',
  36. required: true
  37. }
  38. ]
  39. },
  40. [ScopeNames.WITH_VIDEO]: {
  41. include: [
  42. {
  43. model: VideoModel.scope([
  44. VideoScopeNames.WITH_THUMBNAILS,
  45. VideoScopeNames.WITH_WEB_VIDEO_FILES,
  46. VideoScopeNames.WITH_STREAMING_PLAYLISTS,
  47. VideoScopeNames.WITH_ACCOUNT_DETAILS
  48. ]),
  49. required: true
  50. }
  51. ]
  52. }
  53. }))
  54. export class VideoChangeOwnershipModel extends SequelizeModel<VideoChangeOwnershipModel> {
  55. @CreatedAt
  56. createdAt: Date
  57. @UpdatedAt
  58. updatedAt: Date
  59. @AllowNull(false)
  60. @Column
  61. status: VideoChangeOwnershipStatusType
  62. @ForeignKey(() => AccountModel)
  63. @Column
  64. initiatorAccountId: number
  65. @BelongsTo(() => AccountModel, {
  66. foreignKey: {
  67. name: 'initiatorAccountId',
  68. allowNull: false
  69. },
  70. onDelete: 'cascade'
  71. })
  72. Initiator: Awaited<AccountModel>
  73. @ForeignKey(() => AccountModel)
  74. @Column
  75. nextOwnerAccountId: number
  76. @BelongsTo(() => AccountModel, {
  77. foreignKey: {
  78. name: 'nextOwnerAccountId',
  79. allowNull: false
  80. },
  81. onDelete: 'cascade'
  82. })
  83. NextOwner: Awaited<AccountModel>
  84. @ForeignKey(() => VideoModel)
  85. @Column
  86. videoId: number
  87. @BelongsTo(() => VideoModel, {
  88. foreignKey: {
  89. allowNull: false
  90. },
  91. onDelete: 'cascade'
  92. })
  93. Video: Awaited<VideoModel>
  94. static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
  95. const query = {
  96. offset: start,
  97. limit: count,
  98. order: getSort(sort),
  99. where: {
  100. nextOwnerAccountId: nextOwnerId
  101. }
  102. }
  103. return Promise.all([
  104. VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query),
  105. VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll<MVideoChangeOwnershipFull>(query)
  106. ]).then(([ count, rows ]) => ({ total: count, data: rows }))
  107. }
  108. static load (id: number): Promise<MVideoChangeOwnershipFull> {
  109. return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
  110. .findByPk(id)
  111. }
  112. toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership {
  113. return {
  114. id: this.id,
  115. status: this.status,
  116. initiatorAccount: this.Initiator.toFormattedJSON(),
  117. nextOwnerAccount: this.NextOwner.toFormattedJSON(),
  118. video: this.Video.toFormattedJSON(),
  119. createdAt: this.createdAt
  120. }
  121. }
  122. }