video-change-ownership.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
  2. import { AccountModel } from '../account/account'
  3. import { ScopeNames as VideoScopeNames, VideoModel } from './video'
  4. import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos'
  5. import { getSort } from '../utils'
  6. enum ScopeNames {
  7. WITH_ACCOUNTS = 'WITH_ACCOUNTS',
  8. WITH_VIDEO = 'WITH_VIDEO'
  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.WITH_ACCOUNTS]: {
  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. },
  39. [ScopeNames.WITH_VIDEO]: {
  40. include: [
  41. {
  42. model: VideoModel.scope([ VideoScopeNames.WITH_THUMBNAILS, VideoScopeNames.WITH_FILES ]),
  43. required: true
  44. }
  45. ]
  46. }
  47. }))
  48. export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> {
  49. @CreatedAt
  50. createdAt: Date
  51. @UpdatedAt
  52. updatedAt: Date
  53. @AllowNull(false)
  54. @Column
  55. status: VideoChangeOwnershipStatus
  56. @ForeignKey(() => AccountModel)
  57. @Column
  58. initiatorAccountId: number
  59. @BelongsTo(() => AccountModel, {
  60. foreignKey: {
  61. name: 'initiatorAccountId',
  62. allowNull: false
  63. },
  64. onDelete: 'cascade'
  65. })
  66. Initiator: AccountModel
  67. @ForeignKey(() => AccountModel)
  68. @Column
  69. nextOwnerAccountId: number
  70. @BelongsTo(() => AccountModel, {
  71. foreignKey: {
  72. name: 'nextOwnerAccountId',
  73. allowNull: false
  74. },
  75. onDelete: 'cascade'
  76. })
  77. NextOwner: AccountModel
  78. @ForeignKey(() => VideoModel)
  79. @Column
  80. videoId: number
  81. @BelongsTo(() => VideoModel, {
  82. foreignKey: {
  83. allowNull: false
  84. },
  85. onDelete: 'cascade'
  86. })
  87. Video: VideoModel
  88. static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
  89. const query = {
  90. offset: start,
  91. limit: count,
  92. order: getSort(sort),
  93. where: {
  94. nextOwnerAccountId: nextOwnerId
  95. }
  96. }
  97. return Promise.all([
  98. VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query),
  99. VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll(query)
  100. ]).then(([ count, rows ]) => ({ total: count, data: rows }))
  101. }
  102. static load (id: number) {
  103. return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
  104. .findByPk(id)
  105. }
  106. toFormattedJSON (): VideoChangeOwnership {
  107. return {
  108. id: this.id,
  109. status: this.status,
  110. initiatorAccount: this.Initiator.toFormattedJSON(),
  111. nextOwnerAccount: this.NextOwner.toFormattedJSON(),
  112. video: {
  113. id: this.Video.id,
  114. uuid: this.Video.uuid,
  115. url: this.Video.url,
  116. name: this.Video.name
  117. },
  118. createdAt: this.createdAt
  119. }
  120. }
  121. }