2
1

account-video-rate.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { values } from 'lodash'
  2. import { Transaction } from 'sequelize'
  3. import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
  4. import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
  5. import { VideoRateType } from '../../../shared/models/videos'
  6. import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers'
  7. import { VideoModel } from '../video/video'
  8. import { AccountModel } from './account'
  9. import { ActorModel } from '../activitypub/actor'
  10. import { throwIfNotValid } from '../utils'
  11. import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
  12. /*
  13. Account rates per video.
  14. */
  15. @Table({
  16. tableName: 'accountVideoRate',
  17. indexes: [
  18. {
  19. fields: [ 'videoId', 'accountId' ],
  20. unique: true
  21. },
  22. {
  23. fields: [ 'videoId' ]
  24. },
  25. {
  26. fields: [ 'accountId' ]
  27. },
  28. {
  29. fields: [ 'videoId', 'type' ]
  30. },
  31. {
  32. fields: [ 'url' ],
  33. unique: true
  34. }
  35. ]
  36. })
  37. export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
  38. @AllowNull(false)
  39. @Column(DataType.ENUM(values(VIDEO_RATE_TYPES)))
  40. type: VideoRateType
  41. @AllowNull(false)
  42. @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
  43. @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
  44. url: string
  45. @CreatedAt
  46. createdAt: Date
  47. @UpdatedAt
  48. updatedAt: Date
  49. @ForeignKey(() => VideoModel)
  50. @Column
  51. videoId: number
  52. @BelongsTo(() => VideoModel, {
  53. foreignKey: {
  54. allowNull: false
  55. },
  56. onDelete: 'CASCADE'
  57. })
  58. Video: VideoModel
  59. @ForeignKey(() => AccountModel)
  60. @Column
  61. accountId: number
  62. @BelongsTo(() => AccountModel, {
  63. foreignKey: {
  64. allowNull: false
  65. },
  66. onDelete: 'CASCADE'
  67. })
  68. Account: AccountModel
  69. static load (accountId: number, videoId: number, transaction?: Transaction) {
  70. const options: IFindOptions<AccountVideoRateModel> = {
  71. where: {
  72. accountId,
  73. videoId
  74. }
  75. }
  76. if (transaction) options.transaction = transaction
  77. return AccountVideoRateModel.findOne(options)
  78. }
  79. static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
  80. const options: IFindOptions<AccountVideoRateModel> = {
  81. where: {
  82. videoId,
  83. type: rateType
  84. },
  85. include: [
  86. {
  87. model: AccountModel.unscoped(),
  88. required: true,
  89. include: [
  90. {
  91. attributes: [ 'id', 'url', 'preferredUsername' ],
  92. model: ActorModel.unscoped(),
  93. required: true,
  94. where: {
  95. preferredUsername: accountName
  96. }
  97. }
  98. ]
  99. },
  100. {
  101. model: VideoModel.unscoped(),
  102. required: true
  103. }
  104. ]
  105. }
  106. if (transaction) options.transaction = transaction
  107. return AccountVideoRateModel.findOne(options)
  108. }
  109. static loadByUrl (url: string, transaction: Transaction) {
  110. const options: IFindOptions<AccountVideoRateModel> = {
  111. where: {
  112. url
  113. }
  114. }
  115. if (transaction) options.transaction = transaction
  116. return AccountVideoRateModel.findOne(options)
  117. }
  118. static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
  119. const query = {
  120. offset: start,
  121. limit: count,
  122. where: {
  123. videoId,
  124. type: rateType
  125. },
  126. transaction: t,
  127. include: [
  128. {
  129. attributes: [ 'actorId' ],
  130. model: AccountModel.unscoped(),
  131. required: true,
  132. include: [
  133. {
  134. attributes: [ 'url' ],
  135. model: ActorModel.unscoped(),
  136. required: true
  137. }
  138. ]
  139. }
  140. ]
  141. }
  142. return AccountVideoRateModel.findAndCountAll(query)
  143. }
  144. }