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