]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-blacklist.ts
Handle playlist oembed
[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'
e0a92917 2import { getBlacklistSort, SortType, throwIfNotValid, searchAttribute } 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'
26d6bf65 11import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/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
e0a92917
RK
57 static listForApi (parameters: {
58 start: number
59 count: number
60 sort: SortType
61 search?: string
62 type?: VideoBlacklistType
63 }) {
64 const { start, count, sort, search, type } = parameters
65
ac3d2e05
C
66 function buildBaseQuery (): FindOptions {
67 return {
68 offset: start,
69 limit: count,
95153292 70 order: getBlacklistSort(sort.sortModel, sort.sortValue)
ac3d2e05 71 }
3fd3ab2d 72 }
198b205c 73
ac3d2e05
C
74 const countQuery = buildBaseQuery()
75
76 const findQuery = buildBaseQuery()
ac3d2e05
C
77 findQuery.include = [
78 {
79 model: VideoModel,
80 required: true,
0251197e 81 where: searchAttribute(search, 'name'),
ac3d2e05
C
82 include: [
83 {
bfbd9128 84 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
ac3d2e05
C
85 required: true
86 },
87 {
88 model: ThumbnailModel,
89 attributes: [ 'type', 'filename' ],
90 required: false
91 }
92 ]
93 }
94 ]
95
7ccddd7b 96 if (type) {
ac3d2e05
C
97 countQuery.where = { type }
98 findQuery.where = { type }
7ccddd7b
JM
99 }
100
ac3d2e05
C
101 return Promise.all([
102 VideoBlacklistModel.count(countQuery),
103 VideoBlacklistModel.findAll(findQuery)
104 ]).then(([ count, rows ]) => {
105 return {
106 data: rows,
107 total: count
108 }
109 })
198b205c
GS
110 }
111
453e83ea 112 static loadByVideoId (id: number): Bluebird<MVideoBlacklist> {
3fd3ab2d
C
113 const query = {
114 where: {
115 videoId: id
116 }
6fcd19ba 117 }
198b205c 118
3fd3ab2d 119 return VideoBlacklistModel.findOne(query)
198b205c
GS
120 }
121
1ca9f7c3 122 toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
3fd3ab2d
C
123 return {
124 id: this.id,
3fd3ab2d
C
125 createdAt: this.createdAt,
126 updatedAt: this.updatedAt,
26b7305a 127 reason: this.reason,
5abb9fbb 128 unfederated: this.unfederated,
7ccddd7b 129 type: this.type,
26b7305a 130
7ccddd7b 131 video: this.Video.toFormattedJSON()
3fd3ab2d
C
132 }
133 }
198b205c 134}