aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video-abuse.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
commit99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch)
treec9e04ccfcc5496d2300d7c26db5833e494b4cdad /server/models/video-abuse.js
parentfcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff)
parent91cc839af88730ba55f84997c56b85ea100070a7 (diff)
downloadPeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.gz
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.zst
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.zip
Merge branch 'postgresql'
Diffstat (limited to 'server/models/video-abuse.js')
-rw-r--r--server/models/video-abuse.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/server/models/video-abuse.js b/server/models/video-abuse.js
new file mode 100644
index 000000000..766a7568d
--- /dev/null
+++ b/server/models/video-abuse.js
@@ -0,0 +1,113 @@
1'use strict'
2
3const constants = require('../initializers/constants')
4const modelUtils = require('./utils')
5const customVideosValidators = require('../helpers/custom-validators').videos
6
7module.exports = function (sequelize, DataTypes) {
8 const VideoAbuse = sequelize.define('VideoAbuse',
9 {
10 reporterUsername: {
11 type: DataTypes.STRING,
12 allowNull: false,
13 validate: {
14 reporterUsernameValid: function (value) {
15 const res = customVideosValidators.isVideoAbuseReporterUsernameValid(value)
16 if (res === false) throw new Error('Video abuse reporter username is not valid.')
17 }
18 }
19 },
20 reason: {
21 type: DataTypes.STRING,
22 allowNull: false,
23 validate: {
24 reasonValid: function (value) {
25 const res = customVideosValidators.isVideoAbuseReasonValid(value)
26 if (res === false) throw new Error('Video abuse reason is not valid.')
27 }
28 }
29 }
30 },
31 {
32 indexes: [
33 {
34 fields: [ 'videoId' ]
35 },
36 {
37 fields: [ 'reporterPodId' ]
38 }
39 ],
40 classMethods: {
41 associate,
42
43 listForApi
44 },
45 instanceMethods: {
46 toFormatedJSON
47 }
48 }
49 )
50
51 return VideoAbuse
52}
53
54// ---------------------------------------------------------------------------
55
56function associate (models) {
57 this.belongsTo(models.Pod, {
58 foreignKey: {
59 name: 'reporterPodId',
60 allowNull: true
61 },
62 onDelete: 'cascade'
63 })
64
65 this.belongsTo(models.Video, {
66 foreignKey: {
67 name: 'videoId',
68 allowNull: false
69 },
70 onDelete: 'cascade'
71 })
72}
73
74function listForApi (start, count, sort, callback) {
75 const query = {
76 offset: start,
77 limit: count,
78 order: [ modelUtils.getSort(sort) ],
79 include: [
80 {
81 model: this.sequelize.models.Pod,
82 required: false
83 }
84 ]
85 }
86
87 return this.findAndCountAll(query).asCallback(function (err, result) {
88 if (err) return callback(err)
89
90 return callback(null, result.rows, result.count)
91 })
92}
93
94function toFormatedJSON () {
95 let reporterPodHost
96
97 if (this.Pod) {
98 reporterPodHost = this.Pod.host
99 } else {
100 // It means it's our video
101 reporterPodHost = constants.CONFIG.WEBSERVER.HOST
102 }
103
104 const json = {
105 id: this.id,
106 reporterPodHost,
107 reason: this.reason,
108 reporterUsername: this.reporterUsername,
109 videoId: this.videoId
110 }
111
112 return json
113}