]>
Commit | Line | Data |
---|---|---|
1735c825 | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
06215f15 | 2 | import { getSortOnModel, SortType, throwIfNotValid } from '../utils' |
453e83ea | 3 | import { VideoModel } from './video' |
bfbd9128 | 4 | import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' |
7ccddd7b JM |
5 | import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist' |
6 | import { VideoBlacklist, VideoBlacklistType } from '../../../shared/models/videos' | |
74dc3bca | 7 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
1735c825 | 8 | import { FindOptions } from 'sequelize' |
ac3d2e05 | 9 | import { ThumbnailModel } from './thumbnail' |
453e83ea | 10 | import * as Bluebird from 'bluebird' |
1ca9f7c3 | 11 | import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/typings/models' |
e02643f3 | 12 | |
3fd3ab2d C |
13 | @Table({ |
14 | tableName: 'videoBlacklist', | |
15 | indexes: [ | |
198b205c | 16 | { |
3fd3ab2d C |
17 | fields: [ 'videoId' ], |
18 | unique: true | |
198b205c | 19 | } |
e02643f3 | 20 | ] |
3fd3ab2d C |
21 | }) |
22 | export class VideoBlacklistModel extends Model<VideoBlacklistModel> { | |
198b205c | 23 | |
26b7305a | 24 | @AllowNull(true) |
1735c825 | 25 | @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true)) |
26b7305a C |
26 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max)) |
27 | reason: string | |
28 | ||
5abb9fbb C |
29 | @AllowNull(false) |
30 | @Column | |
31 | unfederated: boolean | |
32 | ||
7ccddd7b JM |
33 | @AllowNull(false) |
34 | @Default(null) | |
35 | @Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type')) | |
36 | @Column | |
37 | type: VideoBlacklistType | |
38 | ||
3fd3ab2d C |
39 | @CreatedAt |
40 | createdAt: Date | |
198b205c | 41 | |
3fd3ab2d C |
42 | @UpdatedAt |
43 | updatedAt: Date | |
198b205c | 44 | |
3fd3ab2d C |
45 | @ForeignKey(() => VideoModel) |
46 | @Column | |
47 | videoId: number | |
198b205c | 48 | |
3fd3ab2d | 49 | @BelongsTo(() => VideoModel, { |
0a6658fd | 50 | foreignKey: { |
0a6658fd C |
51 | allowNull: false |
52 | }, | |
3fd3ab2d | 53 | onDelete: 'cascade' |
198b205c | 54 | }) |
3fd3ab2d C |
55 | Video: VideoModel |
56 | ||
7ccddd7b | 57 | static listForApi (start: number, count: number, sort: SortType, type?: VideoBlacklistType) { |
ac3d2e05 C |
58 | function buildBaseQuery (): FindOptions { |
59 | return { | |
60 | offset: start, | |
61 | limit: count, | |
62 | order: getSortOnModel(sort.sortModel, sort.sortValue) | |
63 | } | |
3fd3ab2d | 64 | } |
198b205c | 65 | |
ac3d2e05 C |
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 | { | |
bfbd9128 | 76 | model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), |
ac3d2e05 C |
77 | required: true |
78 | }, | |
79 | { | |
80 | model: ThumbnailModel, | |
81 | attributes: [ 'type', 'filename' ], | |
82 | required: false | |
83 | } | |
84 | ] | |
85 | } | |
86 | ] | |
87 | ||
7ccddd7b | 88 | if (type) { |
ac3d2e05 C |
89 | countQuery.where = { type } |
90 | findQuery.where = { type } | |
7ccddd7b JM |
91 | } |
92 | ||
ac3d2e05 C |
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 | }) | |
198b205c GS |
102 | } |
103 | ||
453e83ea | 104 | static loadByVideoId (id: number): Bluebird<MVideoBlacklist> { |
3fd3ab2d C |
105 | const query = { |
106 | where: { | |
107 | videoId: id | |
108 | } | |
6fcd19ba | 109 | } |
198b205c | 110 | |
3fd3ab2d | 111 | return VideoBlacklistModel.findOne(query) |
198b205c GS |
112 | } |
113 | ||
1ca9f7c3 | 114 | toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist { |
3fd3ab2d C |
115 | return { |
116 | id: this.id, | |
3fd3ab2d C |
117 | createdAt: this.createdAt, |
118 | updatedAt: this.updatedAt, | |
26b7305a | 119 | reason: this.reason, |
5abb9fbb | 120 | unfederated: this.unfederated, |
7ccddd7b | 121 | type: this.type, |
26b7305a | 122 | |
7ccddd7b | 123 | video: this.Video.toFormattedJSON() |
3fd3ab2d C |
124 | } |
125 | } | |
198b205c | 126 | } |