video-change-ownership.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/typings/models/video/video-change-ownership'
  7. import * as Bluebird from 'bluebird'
  8. enum ScopeNames {
  9. WITH_ACCOUNTS = 'WITH_ACCOUNTS',
  10. WITH_VIDEO = 'WITH_VIDEO'
  11. }
  12. @Table({
  13. tableName: 'videoChangeOwnership',
  14. indexes: [
  15. {
  16. fields: [ 'videoId' ]
  17. },
  18. {
  19. fields: [ 'initiatorAccountId' ]
  20. },
  21. {
  22. fields: [ 'nextOwnerAccountId' ]
  23. }
  24. ]
  25. })
  26. @Scopes(() => ({
  27. [ScopeNames.WITH_ACCOUNTS]: {
  28. include: [
  29. {
  30. model: AccountModel,
  31. as: 'Initiator',
  32. required: true
  33. },
  34. {
  35. model: AccountModel,
  36. as: 'NextOwner',
  37. required: true
  38. }
  39. ]
  40. },
  41. [ScopeNames.WITH_VIDEO]: {
  42. include: [
  43. {
  44. model: VideoModel.scope([
  45. VideoScopeNames.WITH_THUMBNAILS,
  46. VideoScopeNames.WITH_WEBTORRENT_FILES,
  47. VideoScopeNames.WITH_STREAMING_PLAYLISTS
  48. ]),
  49. required: true
  50. }
  51. ]
  52. }
  53. }))
  54. export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> {
  55. @CreatedAt
  56. createdAt: Date
  57. @UpdatedAt
  58. updatedAt: Date
  59. @AllowNull(false)
  60. @Column
  61. status: VideoChangeOwnershipStatus
  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: 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: AccountModel
  84. @ForeignKey(() => VideoModel)
  85. @Column
  86. videoId: number
  87. @BelongsTo(() => VideoModel, {
  88. foreignKey: {
  89. allowNull: false
  90. },
  91. onDelete: 'cascade'
  92. })
  93. Video: 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): Bluebird<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: {
  119. id: this.Video.id,
  120. uuid: this.Video.uuid,
  121. url: this.Video.url,
  122. name: this.Video.name
  123. },
  124. createdAt: this.createdAt
  125. }
  126. }
  127. }