]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-blacklist.ts
cdb725e7ad82c4db8a96f24898c84231cc4117fb
[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 { getBlacklistSort, getSort, SortType, throwIfNotValid } from '../utils'
3 import { ScopeNames as VideoModelScopeNames, VideoModel } from './video'
4 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, 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, literal } from 'sequelize'
9 import { ThumbnailModel } from './thumbnail'
10
11 @Table({
12 tableName: 'videoBlacklist',
13 indexes: [
14 {
15 fields: [ 'videoId' ],
16 unique: true
17 }
18 ]
19 })
20 export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
21
22 @AllowNull(true)
23 @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
24 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
25 reason: string
26
27 @AllowNull(false)
28 @Column
29 unfederated: boolean
30
31 @AllowNull(false)
32 @Default(null)
33 @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
34 @Column
35 type: VideoBlacklistType
36
37 @CreatedAt
38 createdAt: Date
39
40 @UpdatedAt
41 updatedAt: Date
42
43 @ForeignKey(() => VideoModel)
44 @Column
45 videoId: number
46
47 @BelongsTo(() => VideoModel, {
48 foreignKey: {
49 allowNull: false
50 },
51 onDelete: 'cascade'
52 })
53 Video: VideoModel
54
55 static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) {
56 function buildBaseQuery (): FindOptions {
57 return {
58 offset: start,
59 limit: count,
60 order: getBlacklistSort(sort.sortModel, sort.sortValue)
61 }
62 }
63
64 const countQuery = buildBaseQuery()
65
66 const findQuery = buildBaseQuery()
67 findQuery.include = [
68 {
69 model: VideoModel,
70 required: true,
71 include: [
72 {
73 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
74 required: true
75 },
76 {
77 model: ThumbnailModel,
78 attributes: [ 'type', 'filename' ],
79 required: false
80 }
81 ]
82 }
83 ]
84
85 if (type) {
86 countQuery.where = { type }
87 findQuery.where = { type }
88 }
89
90 return Promise.all([
91 VideoBlacklistModel.count(countQuery),
92 VideoBlacklistModel.findAll(findQuery)
93 ]).then(([ count, rows ]) => {
94 return {
95 data: rows,
96 total: count
97 }
98 })
99 }
100
101 static loadByVideoId (id: number) {
102 const query = {
103 where: {
104 videoId: id
105 }
106 }
107
108 return VideoBlacklistModel.findOne(query)
109 }
110
111 toFormattedJSON (): VideoBlacklist {
112 return {
113 id: this.id,
114 createdAt: this.createdAt,
115 updatedAt: this.updatedAt,
116 reason: this.reason,
117 unfederated: this.unfederated,
118 type: this.type,
119
120 video: this.Video.toFormattedJSON()
121 }
122 }
123 }