]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-blacklist.ts
Add user notification base code
[github/Chocobozzz/PeerTube.git] / server / models / video / video-blacklist.ts
CommitLineData
26b7305a
C
1import {
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 15import { getSortOnModel, SortType, throwIfNotValid } from '../utils'
3fd3ab2d 16import { VideoModel } from './video'
26b7305a
C
17import { isVideoBlacklistReasonValid } from '../../helpers/custom-validators/video-blacklist'
18import { Emailer } from '../../lib/emailer'
191764f3 19import { VideoBlacklist } from '../../../shared/models/videos'
26b7305a 20import { 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})
31export 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
56 static listForApi (start: number, count: number, sort: SortType) {
57 const query = {
58 offset: start,
59 limit: count,
3bb6c526 60 order: getSortOnModel(sort.sortModel, sort.sortValue),
191764f3
C
61 include: [
62 {
63 model: VideoModel,
64 required: true
65 }
66 ]
3fd3ab2d 67 }
198b205c 68
3fd3ab2d
C
69 return VideoBlacklistModel.findAndCountAll(query)
70 .then(({ rows, count }) => {
71 return {
72 data: rows,
73 total: count
74 }
75 })
198b205c
GS
76 }
77
3fd3ab2d
C
78 static loadByVideoId (id: number) {
79 const query = {
80 where: {
81 videoId: id
82 }
6fcd19ba 83 }
198b205c 84
3fd3ab2d 85 return VideoBlacklistModel.findOne(query)
198b205c
GS
86 }
87
191764f3 88 toFormattedJSON (): VideoBlacklist {
3fd3ab2d
C
89 const video = this.Video
90
91 return {
92 id: this.id,
3fd3ab2d
C
93 createdAt: this.createdAt,
94 updatedAt: this.updatedAt,
26b7305a
C
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 }
3fd3ab2d
C
108 }
109 }
198b205c 110}