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