video-blacklist.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { FindOptions } from 'sequelize'
  2. import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
  3. import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models'
  4. import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
  5. import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
  6. import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
  7. import { getBlacklistSort, searchAttribute, SortType, throwIfNotValid } from '../utils'
  8. import { ThumbnailModel } from './thumbnail'
  9. import { VideoModel } from './video'
  10. import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
  11. @Table({
  12. tableName: 'videoBlacklist',
  13. indexes: [
  14. {
  15. fields: [ 'videoId' ],
  16. unique: true
  17. }
  18. ]
  19. })
  20. export class VideoBlacklistModel extends Model {
  21. @AllowNull(true)
  22. @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
  23. @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
  24. reason: string
  25. @AllowNull(false)
  26. @Column
  27. unfederated: boolean
  28. @AllowNull(false)
  29. @Default(null)
  30. @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
  31. @Column
  32. type: VideoBlacklistType
  33. @CreatedAt
  34. createdAt: Date
  35. @UpdatedAt
  36. updatedAt: Date
  37. @ForeignKey(() => VideoModel)
  38. @Column
  39. videoId: number
  40. @BelongsTo(() => VideoModel, {
  41. foreignKey: {
  42. allowNull: false
  43. },
  44. onDelete: 'cascade'
  45. })
  46. Video: VideoModel
  47. static listForApi (parameters: {
  48. start: number
  49. count: number
  50. sort: SortType
  51. search?: string
  52. type?: VideoBlacklistType
  53. }) {
  54. const { start, count, sort, search, type } = parameters
  55. function buildBaseQuery (): FindOptions {
  56. return {
  57. offset: start,
  58. limit: count,
  59. order: getBlacklistSort(sort.sortModel, sort.sortValue)
  60. }
  61. }
  62. const countQuery = buildBaseQuery()
  63. const findQuery = buildBaseQuery()
  64. findQuery.include = [
  65. {
  66. model: VideoModel,
  67. required: true,
  68. where: searchAttribute(search, 'name'),
  69. include: [
  70. {
  71. model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
  72. required: true
  73. },
  74. {
  75. model: ThumbnailModel,
  76. attributes: [ 'type', 'filename' ],
  77. required: false
  78. }
  79. ]
  80. }
  81. ]
  82. if (type) {
  83. countQuery.where = { type }
  84. findQuery.where = { type }
  85. }
  86. return Promise.all([
  87. VideoBlacklistModel.count(countQuery),
  88. VideoBlacklistModel.findAll(findQuery)
  89. ]).then(([ count, rows ]) => {
  90. return {
  91. data: rows,
  92. total: count
  93. }
  94. })
  95. }
  96. static loadByVideoId (id: number): Promise<MVideoBlacklist> {
  97. const query = {
  98. where: {
  99. videoId: id
  100. }
  101. }
  102. return VideoBlacklistModel.findOne(query)
  103. }
  104. toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
  105. return {
  106. id: this.id,
  107. createdAt: this.createdAt,
  108. updatedAt: this.updatedAt,
  109. reason: this.reason,
  110. unfederated: this.unfederated,
  111. type: this.type,
  112. video: this.Video.toFormattedJSON()
  113. }
  114. }
  115. }