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