]>
Commit | Line | Data |
---|---|---|
1 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
2 | import { getSortOnModel, SortType, throwIfNotValid } from '../utils' | |
3 | import { VideoModel } from './video' | |
4 | import { isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist' | |
5 | import { VideoBlacklist } from '../../../shared/models/videos' | |
6 | import { CONSTRAINTS_FIELDS } from '../../initializers' | |
7 | ||
8 | @Table({ | |
9 | tableName: 'videoBlacklist', | |
10 | indexes: [ | |
11 | { | |
12 | fields: [ 'videoId' ], | |
13 | unique: true | |
14 | } | |
15 | ] | |
16 | }) | |
17 | export class VideoBlacklistModel extends Model<VideoBlacklistModel> { | |
18 | ||
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 | ||
24 | @AllowNull(false) | |
25 | @Column | |
26 | unfederated: boolean | |
27 | ||
28 | @CreatedAt | |
29 | createdAt: Date | |
30 | ||
31 | @UpdatedAt | |
32 | updatedAt: Date | |
33 | ||
34 | @ForeignKey(() => VideoModel) | |
35 | @Column | |
36 | videoId: number | |
37 | ||
38 | @BelongsTo(() => VideoModel, { | |
39 | foreignKey: { | |
40 | allowNull: false | |
41 | }, | |
42 | onDelete: 'cascade' | |
43 | }) | |
44 | Video: VideoModel | |
45 | ||
46 | static listForApi (start: number, count: number, sort: SortType) { | |
47 | const query = { | |
48 | offset: start, | |
49 | limit: count, | |
50 | order: getSortOnModel(sort.sortModel, sort.sortValue), | |
51 | include: [ | |
52 | { | |
53 | model: VideoModel, | |
54 | required: true | |
55 | } | |
56 | ] | |
57 | } | |
58 | ||
59 | return VideoBlacklistModel.findAndCountAll(query) | |
60 | .then(({ rows, count }) => { | |
61 | return { | |
62 | data: rows, | |
63 | total: count | |
64 | } | |
65 | }) | |
66 | } | |
67 | ||
68 | static loadByVideoId (id: number) { | |
69 | const query = { | |
70 | where: { | |
71 | videoId: id | |
72 | } | |
73 | } | |
74 | ||
75 | return VideoBlacklistModel.findOne(query) | |
76 | } | |
77 | ||
78 | toFormattedJSON (): VideoBlacklist { | |
79 | const video = this.Video | |
80 | ||
81 | return { | |
82 | id: this.id, | |
83 | createdAt: this.createdAt, | |
84 | updatedAt: this.updatedAt, | |
85 | reason: this.reason, | |
86 | unfederated: this.unfederated, | |
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 | } | |
99 | } | |
100 | } | |
101 | } |