]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-blacklist.ts
Merge remote-tracking branch 'origin/pr/1785' into develop
[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'
3fd3ab2d 3import { VideoModel } from './video'
1735c825 4import { ScopeNames as VideoChannelScopeNames, 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'
e02643f3 9
3fd3ab2d
C
10@Table({
11 tableName: 'videoBlacklist',
12 indexes: [
198b205c 13 {
3fd3ab2d
C
14 fields: [ 'videoId' ],
15 unique: true
198b205c 16 }
e02643f3 17 ]
3fd3ab2d
C
18})
19export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
198b205c 20
26b7305a 21 @AllowNull(true)
1735c825 22 @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
26b7305a
C
23 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
24 reason: string
25
5abb9fbb
C
26 @AllowNull(false)
27 @Column
28 unfederated: boolean
29
7ccddd7b
JM
30 @AllowNull(false)
31 @Default(null)
32 @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
33 @Column
34 type: VideoBlacklistType
35
3fd3ab2d
C
36 @CreatedAt
37 createdAt: Date
198b205c 38
3fd3ab2d
C
39 @UpdatedAt
40 updatedAt: Date
198b205c 41
3fd3ab2d
C
42 @ForeignKey(() => VideoModel)
43 @Column
44 videoId: number
198b205c 45
3fd3ab2d 46 @BelongsTo(() => VideoModel, {
0a6658fd 47 foreignKey: {
0a6658fd
C
48 allowNull: false
49 },
3fd3ab2d 50 onDelete: 'cascade'
198b205c 51 })
3fd3ab2d
C
52 Video: VideoModel
53
7ccddd7b 54 static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) {
1735c825 55 const query: FindOptions = {
3fd3ab2d
C
56 offset: start,
57 limit: count,
3bb6c526 58 order: getSortOnModel(sort.sortModel, sort.sortValue),
191764f3
C
59 include: [
60 {
61 model: VideoModel,
7ccddd7b
JM
62 required: true,
63 include: [
64 {
65 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }),
66 required: true
67 }
68 ]
191764f3
C
69 }
70 ]
3fd3ab2d 71 }
198b205c 72
7ccddd7b
JM
73 if (type) {
74 query.where = { type }
75 }
76
3fd3ab2d
C
77 return VideoBlacklistModel.findAndCountAll(query)
78 .then(({ rows, count }) => {
79 return {
80 data: rows,
81 total: count
82 }
83 })
198b205c
GS
84 }
85
3fd3ab2d
C
86 static loadByVideoId (id: number) {
87 const query = {
88 where: {
89 videoId: id
90 }
6fcd19ba 91 }
198b205c 92
3fd3ab2d 93 return VideoBlacklistModel.findOne(query)
198b205c
GS
94 }
95
191764f3 96 toFormattedJSON (): VideoBlacklist {
3fd3ab2d
C
97 return {
98 id: this.id,
3fd3ab2d
C
99 createdAt: this.createdAt,
100 updatedAt: this.updatedAt,
26b7305a 101 reason: this.reason,
5abb9fbb 102 unfederated: this.unfederated,
7ccddd7b 103 type: this.type,
26b7305a 104
7ccddd7b 105 video: this.Video.toFormattedJSON()
3fd3ab2d
C
106 }
107 }
198b205c 108}