]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-blacklist.ts
Dissociate video file names and video uuid
[github/Chocobozzz/PeerTube.git] / server / models / video / video-blacklist.ts
1 import { FindOptions } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models'
4 import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
5 import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
6 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
7 import { getBlacklistSort, searchAttribute, SortType, throwIfNotValid } from '../utils'
8 import { ThumbnailModel } from './thumbnail'
9 import { VideoModel } from './video'
10 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
11
12 @Table({
13 tableName: 'videoBlacklist',
14 indexes: [
15 {
16 fields: [ 'videoId' ],
17 unique: true
18 }
19 ]
20 })
21 export class VideoBlacklistModel extends Model {
22
23 @AllowNull(true)
24 @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
25 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
26 reason: string
27
28 @AllowNull(false)
29 @Column
30 unfederated: boolean
31
32 @AllowNull(false)
33 @Default(null)
34 @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
35 @Column
36 type: VideoBlacklistType
37
38 @CreatedAt
39 createdAt: Date
40
41 @UpdatedAt
42 updatedAt: Date
43
44 @ForeignKey(() => VideoModel)
45 @Column
46 videoId: number
47
48 @BelongsTo(() => VideoModel, {
49 foreignKey: {
50 allowNull: false
51 },
52 onDelete: 'cascade'
53 })
54 Video: VideoModel
55
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
65 function buildBaseQuery (): FindOptions {
66 return {
67 offset: start,
68 limit: count,
69 order: getBlacklistSort(sort.sortModel, sort.sortValue)
70 }
71 }
72
73 const countQuery = buildBaseQuery()
74
75 const findQuery = buildBaseQuery()
76 findQuery.include = [
77 {
78 model: VideoModel,
79 required: true,
80 where: searchAttribute(search, 'name'),
81 include: [
82 {
83 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
84 required: true
85 },
86 {
87 model: ThumbnailModel,
88 attributes: [ 'type', 'filename' ],
89 required: false
90 }
91 ]
92 }
93 ]
94
95 if (type) {
96 countQuery.where = { type }
97 findQuery.where = { type }
98 }
99
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 })
109 }
110
111 static loadByVideoId (id: number): Promise<MVideoBlacklist> {
112 const query = {
113 where: {
114 videoId: id
115 }
116 }
117
118 return VideoBlacklistModel.findOne(query)
119 }
120
121 toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
122 return {
123 id: this.id,
124 createdAt: this.createdAt,
125 updatedAt: this.updatedAt,
126 reason: this.reason,
127 unfederated: this.unfederated,
128 type: this.type,
129
130 video: this.Video.toFormattedJSON()
131 }
132 }
133 }