Merge branch 'develop' of https://github.com/Chocobozzz/PeerTube into move-utils...
[oweals/peertube.git] / server / models / video / video-blacklist.ts
1 import {
2   AfterCreate,
3   AfterDestroy,
4   AllowNull,
5   BelongsTo,
6   Column,
7   CreatedAt,
8   DataType,
9   ForeignKey,
10   Is,
11   Model,
12   Table,
13   UpdatedAt
14 } from 'sequelize-typescript'
15 import { getSortOnModel, SortType, 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 { VideoBlacklist } 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: [
72         {
73           model: VideoModel,
74           required: true
75         }
76       ]
77     }
78
79     return VideoBlacklistModel.findAndCountAll(query)
80       .then(({ rows, count }) => {
81         return {
82           data: rows,
83           total: count
84         }
85       })
86   }
87
88   static loadByVideoId (id: number) {
89     const query = {
90       where: {
91         videoId: id
92       }
93     }
94
95     return VideoBlacklistModel.findOne(query)
96   }
97
98   toFormattedJSON (): VideoBlacklist {
99     const video = this.Video
100
101     return {
102       id: this.id,
103       createdAt: this.createdAt,
104       updatedAt: this.updatedAt,
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       }
118     }
119   }
120 }