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