Create a dedicated table to track video thumbnails
[oweals/peertube.git] / server / models / video / video-blacklist.ts
1 import {
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'
14 import { getSortOnModel, SortType, throwIfNotValid } from '../utils'
15 import { VideoModel } from './video'
16 import { VideoChannelModel, ScopeNames as VideoChannelScopeNames } from './video-channel'
17 import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist'
18 import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos'
19 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
20
21 @Table({
22   tableName: 'videoBlacklist',
23   indexes: [
24     {
25       fields: [ 'videoId' ],
26       unique: true
27     }
28   ]
29 })
30 export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
31
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
37   @AllowNull(false)
38   @Column
39   unfederated: boolean
40
41   @AllowNull(false)
42   @Default(null)
43   @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
44   @Column
45   type: VideoBlacklistType
46
47   @CreatedAt
48   createdAt: Date
49
50   @UpdatedAt
51   updatedAt: Date
52
53   @ForeignKey(() => VideoModel)
54   @Column
55   videoId: number
56
57   @BelongsTo(() => VideoModel, {
58     foreignKey: {
59       allowNull: false
60     },
61     onDelete: 'cascade'
62   })
63   Video: VideoModel
64
65   static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) {
66     const query: IFindOptions<VideoBlacklistModel> = {
67       offset: start,
68       limit: count,
69       order: getSortOnModel(sort.sortModel, sort.sortValue),
70       include: [
71         {
72           model: VideoModel,
73           required: true,
74           include: [
75             {
76               model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, true ] }),
77               required: true
78             }
79           ]
80         }
81       ]
82     }
83
84     if (type) {
85       query.where = { type }
86     }
87
88     return VideoBlacklistModel.findAndCountAll(query)
89       .then(({ rows, count }) => {
90         return {
91           data: rows,
92           total: count
93         }
94       })
95   }
96
97   static loadByVideoId (id: number) {
98     const query = {
99       where: {
100         videoId: id
101       }
102     }
103
104     return VideoBlacklistModel.findOne(query)
105   }
106
107   toFormattedJSON (): VideoBlacklist {
108     return {
109       id: this.id,
110       createdAt: this.createdAt,
111       updatedAt: this.updatedAt,
112       reason: this.reason,
113       unfederated: this.unfederated,
114       type: this.type,
115
116       video: this.Video.toFormattedJSON()
117     }
118   }
119 }