Type toFormattedJSON
[oweals/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 } 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 } 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: getSortOnModel(sort.sortModel, sort.sortValue)
63       }
64     }
65
66     const countQuery = buildBaseQuery()
67
68     const findQuery = buildBaseQuery()
69     findQuery.subQuery = false
70     findQuery.include = [
71       {
72         model: VideoModel,
73         required: true,
74         include: [
75           {
76             model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
77             required: true
78           },
79           {
80             model: ThumbnailModel,
81             attributes: [ 'type', 'filename' ],
82             required: false
83           }
84         ]
85       }
86     ]
87
88     if (type) {
89       countQuery.where = { type }
90       findQuery.where = { type }
91     }
92
93     return Promise.all([
94       VideoBlacklistModel.count(countQuery),
95       VideoBlacklistModel.findAll(findQuery)
96     ]).then(([ count, rows ]) => {
97       return {
98         data: rows,
99         total: count
100       }
101     })
102   }
103
104   static loadByVideoId (id: number): Bluebird<MVideoBlacklist> {
105     const query = {
106       where: {
107         videoId: id
108       }
109     }
110
111     return VideoBlacklistModel.findOne(query)
112   }
113
114   toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
115     return {
116       id: this.id,
117       createdAt: this.createdAt,
118       updatedAt: this.updatedAt,
119       reason: this.reason,
120       unfederated: this.unfederated,
121       type: this.type,
122
123       video: this.Video.toFormattedJSON()
124     }
125   }
126 }