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