]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-blacklist.ts
1b8a338cb5e334e23dd5b1611ccec08bcfa88e5e
[github/Chocobozzz/PeerTube.git] / server / models / video / video-blacklist.ts
1 import {
2 AfterCreate,
3 AfterDestroy,
4 AllowNull,
5 BelongsTo,
6 Column,
7 CreatedAt, DataType,
8 ForeignKey,
9 Is,
10 Model,
11 Table,
12 UpdatedAt
13 } from 'sequelize-typescript'
14 import { SortType } from '../../helpers/utils'
15 import { getSortOnModel, throwIfNotValid } from '../utils'
16 import { VideoModel } from './video'
17 import { isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist'
18 import { Emailer } from '../../lib/emailer'
19 import { BlacklistedVideo } from '../../../shared/models/videos'
20 import { CONSTRAINTS_FIELDS } from '../../initializers'
21
22 @Table({
23 tableName: 'videoBlacklist',
24 indexes: [
25 {
26 fields: [ 'videoId' ],
27 unique: true
28 }
29 ]
30 })
31 export class VideoBlacklistModel extends Model<VideoBlacklistModel> {
32
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
38 @CreatedAt
39 createdAt: Date
40
41 @UpdatedAt
42 updatedAt: Date
43
44 @ForeignKey(() => VideoModel)
45 @Column
46 videoId: number
47
48 @BelongsTo(() => VideoModel, {
49 foreignKey: {
50 allowNull: false
51 },
52 onDelete: 'cascade'
53 })
54 Video: VideoModel
55
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
66 static listForApi (start: number, count: number, sort: SortType) {
67 const query = {
68 offset: start,
69 limit: count,
70 order: getSortOnModel(sort.sortModel, sort.sortValue),
71 include: [ { model: VideoModel } ]
72 }
73
74 return VideoBlacklistModel.findAndCountAll(query)
75 .then(({ rows, count }) => {
76 return {
77 data: rows,
78 total: count
79 }
80 })
81 }
82
83 static loadByVideoId (id: number) {
84 const query = {
85 where: {
86 videoId: id
87 }
88 }
89
90 return VideoBlacklistModel.findOne(query)
91 }
92
93 toFormattedJSON (): BlacklistedVideo {
94 const video = this.Video
95
96 return {
97 id: this.id,
98 createdAt: this.createdAt,
99 updatedAt: this.updatedAt,
100 reason: this.reason,
101
102 video: {
103 id: video.id,
104 name: video.name,
105 uuid: video.uuid,
106 description: video.description,
107 duration: video.duration,
108 views: video.views,
109 likes: video.likes,
110 dislikes: video.dislikes,
111 nsfw: video.nsfw
112 }
113 }
114 }
115 }