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