]>
Commit | Line | Data |
---|---|---|
5abb9fbb | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
06215f15 | 2 | import { getSortOnModel, SortType, throwIfNotValid } from '../utils' |
3fd3ab2d | 3 | import { VideoModel } from './video' |
26b7305a | 4 | import { isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist' |
191764f3 | 5 | import { VideoBlacklist } from '../../../shared/models/videos' |
26b7305a | 6 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
e02643f3 | 7 | |
3fd3ab2d C |
8 | @Table({ |
9 | tableName: 'videoBlacklist', | |
10 | indexes: [ | |
198b205c | 11 | { |
3fd3ab2d C |
12 | fields: [ 'videoId' ], |
13 | unique: true | |
198b205c | 14 | } |
e02643f3 | 15 | ] |
3fd3ab2d C |
16 | }) |
17 | export class VideoBlacklistModel extends Model<VideoBlacklistModel> { | |
198b205c | 18 | |
26b7305a C |
19 | @AllowNull(true) |
20 | @Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason')) | |
21 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max)) | |
22 | reason: string | |
23 | ||
5abb9fbb C |
24 | @AllowNull(false) |
25 | @Column | |
26 | unfederated: boolean | |
27 | ||
3fd3ab2d C |
28 | @CreatedAt |
29 | createdAt: Date | |
198b205c | 30 | |
3fd3ab2d C |
31 | @UpdatedAt |
32 | updatedAt: Date | |
198b205c | 33 | |
3fd3ab2d C |
34 | @ForeignKey(() => VideoModel) |
35 | @Column | |
36 | videoId: number | |
198b205c | 37 | |
3fd3ab2d | 38 | @BelongsTo(() => VideoModel, { |
0a6658fd | 39 | foreignKey: { |
0a6658fd C |
40 | allowNull: false |
41 | }, | |
3fd3ab2d | 42 | onDelete: 'cascade' |
198b205c | 43 | }) |
3fd3ab2d C |
44 | Video: VideoModel |
45 | ||
46 | static listForApi (start: number, count: number, sort: SortType) { | |
47 | const query = { | |
48 | offset: start, | |
49 | limit: count, | |
3bb6c526 | 50 | order: getSortOnModel(sort.sortModel, sort.sortValue), |
191764f3 C |
51 | include: [ |
52 | { | |
53 | model: VideoModel, | |
54 | required: true | |
55 | } | |
56 | ] | |
3fd3ab2d | 57 | } |
198b205c | 58 | |
3fd3ab2d C |
59 | return VideoBlacklistModel.findAndCountAll(query) |
60 | .then(({ rows, count }) => { | |
61 | return { | |
62 | data: rows, | |
63 | total: count | |
64 | } | |
65 | }) | |
198b205c GS |
66 | } |
67 | ||
3fd3ab2d C |
68 | static loadByVideoId (id: number) { |
69 | const query = { | |
70 | where: { | |
71 | videoId: id | |
72 | } | |
6fcd19ba | 73 | } |
198b205c | 74 | |
3fd3ab2d | 75 | return VideoBlacklistModel.findOne(query) |
198b205c GS |
76 | } |
77 | ||
191764f3 | 78 | toFormattedJSON (): VideoBlacklist { |
3fd3ab2d C |
79 | const video = this.Video |
80 | ||
81 | return { | |
82 | id: this.id, | |
3fd3ab2d C |
83 | createdAt: this.createdAt, |
84 | updatedAt: this.updatedAt, | |
26b7305a | 85 | reason: this.reason, |
5abb9fbb | 86 | unfederated: this.unfederated, |
26b7305a C |
87 | |
88 | video: { | |
89 | id: video.id, | |
90 | name: video.name, | |
91 | uuid: video.uuid, | |
92 | description: video.description, | |
93 | duration: video.duration, | |
94 | views: video.views, | |
95 | likes: video.likes, | |
96 | dislikes: video.dislikes, | |
97 | nsfw: video.nsfw | |
98 | } | |
3fd3ab2d C |
99 | } |
100 | } | |
198b205c | 101 | } |