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