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