Add import finished and video published notifs
[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   static listForApi (start: number, count: number, sort: SortType) {
57     const query = {
58       offset: start,
59       limit: count,
60       order: getSortOnModel(sort.sortModel, sort.sortValue),
61       include: [
62         {
63           model: VideoModel,
64           required: true
65         }
66       ]
67     }
68
69     return VideoBlacklistModel.findAndCountAll(query)
70       .then(({ rows, count }) => {
71         return {
72           data: rows,
73           total: count
74         }
75       })
76   }
77
78   static loadByVideoId (id: number) {
79     const query = {
80       where: {
81         videoId: id
82       }
83     }
84
85     return VideoBlacklistModel.findOne(query)
86   }
87
88   toFormattedJSON (): VideoBlacklist {
89     const video = this.Video
90
91     return {
92       id: this.id,
93       createdAt: this.createdAt,
94       updatedAt: this.updatedAt,
95       reason: this.reason,
96
97       video: {
98         id: video.id,
99         name: video.name,
100         uuid: video.uuid,
101         description: video.description,
102         duration: video.duration,
103         views: video.views,
104         likes: video.likes,
105         dislikes: video.dislikes,
106         nsfw: video.nsfw
107       }
108     }
109   }
110 }