]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-abuse.ts
Rename Pod -> Server
[github/Chocobozzz/PeerTube.git] / server / models / video / video-abuse.ts
CommitLineData
e02643f3
C
1import * as Sequelize from 'sequelize'
2
74889a71
C
3import { CONFIG } from '../../initializers'
4import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../../helpers'
55fa55a9 5
74889a71 6import { addMethodsToModel, getSort } from '../utils'
e02643f3 7import {
e02643f3
C
8 VideoAbuseInstance,
9 VideoAbuseAttributes,
10
11 VideoAbuseMethods
12} from './video-abuse-interface'
13
14let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
0aef76c4 15let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON
e02643f3
C
16let listForApi: VideoAbuseMethods.ListForApi
17
127944aa
C
18export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
19 VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse',
55fa55a9
C
20 {
21 reporterUsername: {
22 type: DataTypes.STRING,
23 allowNull: false,
24 validate: {
075f16ca 25 reporterUsernameValid: value => {
65fcc311 26 const res = isVideoAbuseReporterUsernameValid(value)
55fa55a9
C
27 if (res === false) throw new Error('Video abuse reporter username is not valid.')
28 }
29 }
30 },
31 reason: {
32 type: DataTypes.STRING,
33 allowNull: false,
34 validate: {
075f16ca 35 reasonValid: value => {
65fcc311 36 const res = isVideoAbuseReasonValid(value)
55fa55a9
C
37 if (res === false) throw new Error('Video abuse reason is not valid.')
38 }
39 }
40 }
41 },
42 {
43 indexes: [
44 {
45 fields: [ 'videoId' ]
46 },
47 {
60862425 48 fields: [ 'reporterServerId' ]
55fa55a9 49 }
e02643f3 50 ]
55fa55a9
C
51 }
52 )
53
e02643f3
C
54 const classMethods = [
55 associate,
56
57 listForApi
58 ]
59 const instanceMethods = [
0aef76c4 60 toFormattedJSON
e02643f3
C
61 ]
62 addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
63
55fa55a9
C
64 return VideoAbuse
65}
66
e02643f3
C
67// ------------------------------ METHODS ------------------------------
68
0aef76c4 69toFormattedJSON = function (this: VideoAbuseInstance) {
60862425 70 let reporterServerHost
e02643f3 71
60862425
C
72 if (this.Server) {
73 reporterServerHost = this.Server.host
e02643f3
C
74 } else {
75 // It means it's our video
60862425 76 reporterServerHost = CONFIG.WEBSERVER.HOST
e02643f3
C
77 }
78
79 const json = {
80 id: this.id,
60862425 81 reporterServerHost,
e02643f3
C
82 reason: this.reason,
83 reporterUsername: this.reporterUsername,
84 videoId: this.videoId,
85 createdAt: this.createdAt
86 }
87
88 return json
89}
90
91// ------------------------------ STATICS ------------------------------
55fa55a9
C
92
93function associate (models) {
60862425 94 VideoAbuse.belongsTo(models.Server, {
55fa55a9 95 foreignKey: {
60862425 96 name: 'reporterServerId',
55fa55a9
C
97 allowNull: true
98 },
0a6658fd 99 onDelete: 'CASCADE'
55fa55a9
C
100 })
101
e02643f3 102 VideoAbuse.belongsTo(models.Video, {
55fa55a9
C
103 foreignKey: {
104 name: 'videoId',
105 allowNull: false
106 },
0a6658fd 107 onDelete: 'CASCADE'
55fa55a9
C
108 })
109}
110
6fcd19ba 111listForApi = function (start: number, count: number, sort: string) {
55fa55a9
C
112 const query = {
113 offset: start,
114 limit: count,
65fcc311 115 order: [ getSort(sort) ],
55fa55a9
C
116 include: [
117 {
60862425 118 model: VideoAbuse['sequelize'].models.Server,
55fa55a9
C
119 required: false
120 }
121 ]
122 }
123
6fcd19ba
C
124 return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => {
125 return { total: count, data: rows }
55fa55a9
C
126 })
127}