account-video-rate.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { values } from 'lodash'
  2. import { Transaction } from 'sequelize'
  3. import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
  4. import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
  5. import { VideoRateType } from '../../../shared/models/videos'
  6. import { VIDEO_RATE_TYPES } from '../../initializers'
  7. import { VideoModel } from '../video/video'
  8. import { AccountModel } from './account'
  9. import { ActorModel } from '../activitypub/actor'
  10. /*
  11. Account rates per video.
  12. */
  13. @Table({
  14. tableName: 'accountVideoRate',
  15. indexes: [
  16. {
  17. fields: [ 'videoId', 'accountId' ],
  18. unique: true
  19. },
  20. {
  21. fields: [ 'videoId' ]
  22. },
  23. {
  24. fields: [ 'accountId' ]
  25. },
  26. {
  27. fields: [ 'videoId', 'type' ]
  28. }
  29. ]
  30. })
  31. export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
  32. @AllowNull(false)
  33. @Column(DataType.ENUM(values(VIDEO_RATE_TYPES)))
  34. type: VideoRateType
  35. @CreatedAt
  36. createdAt: Date
  37. @UpdatedAt
  38. updatedAt: Date
  39. @ForeignKey(() => VideoModel)
  40. @Column
  41. videoId: number
  42. @BelongsTo(() => VideoModel, {
  43. foreignKey: {
  44. allowNull: false
  45. },
  46. onDelete: 'CASCADE'
  47. })
  48. Video: VideoModel
  49. @ForeignKey(() => AccountModel)
  50. @Column
  51. accountId: number
  52. @BelongsTo(() => AccountModel, {
  53. foreignKey: {
  54. allowNull: false
  55. },
  56. onDelete: 'CASCADE'
  57. })
  58. Account: AccountModel
  59. static load (accountId: number, videoId: number, transaction: Transaction) {
  60. const options: IFindOptions<AccountVideoRateModel> = {
  61. where: {
  62. accountId,
  63. videoId
  64. }
  65. }
  66. if (transaction) options.transaction = transaction
  67. return AccountVideoRateModel.findOne(options)
  68. }
  69. static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
  70. const query = {
  71. offset: start,
  72. limit: count,
  73. where: {
  74. videoId,
  75. type: rateType
  76. },
  77. transaction: t,
  78. include: [
  79. {
  80. attributes: [ 'actorId' ],
  81. model: AccountModel.unscoped(),
  82. required: true,
  83. include: [
  84. {
  85. attributes: [ 'url' ],
  86. model: ActorModel.unscoped(),
  87. required: true
  88. }
  89. ]
  90. }
  91. ]
  92. }
  93. return AccountVideoRateModel.findAndCountAll(query)
  94. }
  95. }