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