video-change-ownership.ts 2.8 KB

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