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