diff options
Diffstat (limited to 'server/models/video-abuse.ts')
-rw-r--r-- | server/models/video-abuse.ts | 95 |
1 files changed, 57 insertions, 38 deletions
diff --git a/server/models/video-abuse.ts b/server/models/video-abuse.ts index 2a18a293d..92168439c 100644 --- a/server/models/video-abuse.ts +++ b/server/models/video-abuse.ts | |||
@@ -1,9 +1,22 @@ | |||
1 | import * as Sequelize from 'sequelize' | ||
2 | |||
1 | import { CONFIG } from '../initializers' | 3 | import { CONFIG } from '../initializers' |
2 | import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../helpers' | 4 | import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../helpers' |
3 | import { getSort } from './utils' | ||
4 | 5 | ||
5 | module.exports = function (sequelize, DataTypes) { | 6 | import { addMethodsToModel, getSort } from './utils' |
6 | const VideoAbuse = sequelize.define('VideoAbuse', | 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 listForApi: VideoAbuseMethods.ListForApi | ||
17 | |||
18 | export default function (sequelize, DataTypes) { | ||
19 | VideoAbuse = sequelize.define('VideoAbuse', | ||
7 | { | 20 | { |
8 | reporterUsername: { | 21 | reporterUsername: { |
9 | type: DataTypes.STRING, | 22 | type: DataTypes.STRING, |
@@ -34,25 +47,51 @@ module.exports = function (sequelize, DataTypes) { | |||
34 | { | 47 | { |
35 | fields: [ 'reporterPodId' ] | 48 | fields: [ 'reporterPodId' ] |
36 | } | 49 | } |
37 | ], | 50 | ] |
38 | classMethods: { | ||
39 | associate, | ||
40 | |||
41 | listForApi | ||
42 | }, | ||
43 | instanceMethods: { | ||
44 | toFormatedJSON | ||
45 | } | ||
46 | } | 51 | } |
47 | ) | 52 | ) |
48 | 53 | ||
54 | const classMethods = [ | ||
55 | associate, | ||
56 | |||
57 | listForApi | ||
58 | ] | ||
59 | const instanceMethods = [ | ||
60 | toFormatedJSON | ||
61 | ] | ||
62 | addMethodsToModel(VideoAbuse, classMethods, instanceMethods) | ||
63 | |||
49 | return VideoAbuse | 64 | return VideoAbuse |
50 | } | 65 | } |
51 | 66 | ||
52 | // --------------------------------------------------------------------------- | 67 | // ------------------------------ METHODS ------------------------------ |
68 | |||
69 | function toFormatedJSON () { | ||
70 | let reporterPodHost | ||
71 | |||
72 | if (this.Pod) { | ||
73 | reporterPodHost = this.Pod.host | ||
74 | } else { | ||
75 | // It means it's our video | ||
76 | reporterPodHost = CONFIG.WEBSERVER.HOST | ||
77 | } | ||
78 | |||
79 | const json = { | ||
80 | id: this.id, | ||
81 | reporterPodHost, | ||
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 ------------------------------ | ||
53 | 92 | ||
54 | function associate (models) { | 93 | function associate (models) { |
55 | this.belongsTo(models.Pod, { | 94 | VideoAbuse.belongsTo(models.Pod, { |
56 | foreignKey: { | 95 | foreignKey: { |
57 | name: 'reporterPodId', | 96 | name: 'reporterPodId', |
58 | allowNull: true | 97 | allowNull: true |
@@ -60,7 +99,7 @@ function associate (models) { | |||
60 | onDelete: 'cascade' | 99 | onDelete: 'cascade' |
61 | }) | 100 | }) |
62 | 101 | ||
63 | this.belongsTo(models.Video, { | 102 | VideoAbuse.belongsTo(models.Video, { |
64 | foreignKey: { | 103 | foreignKey: { |
65 | name: 'videoId', | 104 | name: 'videoId', |
66 | allowNull: false | 105 | allowNull: false |
@@ -69,44 +108,24 @@ function associate (models) { | |||
69 | }) | 108 | }) |
70 | } | 109 | } |
71 | 110 | ||
72 | function listForApi (start, count, sort, callback) { | 111 | listForApi = function (start, count, sort, callback) { |
73 | const query = { | 112 | const query = { |
74 | offset: start, | 113 | offset: start, |
75 | limit: count, | 114 | limit: count, |
76 | order: [ getSort(sort) ], | 115 | order: [ getSort(sort) ], |
77 | include: [ | 116 | include: [ |
78 | { | 117 | { |
79 | model: this.sequelize.models.Pod, | 118 | model: VideoAbuse['sequelize'].models.Pod, |
80 | required: false | 119 | required: false |
81 | } | 120 | } |
82 | ] | 121 | ] |
83 | } | 122 | } |
84 | 123 | ||
85 | return this.findAndCountAll(query).asCallback(function (err, result) { | 124 | return VideoAbuse.findAndCountAll(query).asCallback(function (err, result) { |
86 | if (err) return callback(err) | 125 | if (err) return callback(err) |
87 | 126 | ||
88 | return callback(null, result.rows, result.count) | 127 | return callback(null, result.rows, result.count) |
89 | }) | 128 | }) |
90 | } | 129 | } |
91 | 130 | ||
92 | function 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 | 131 | ||
111 | return json | ||
112 | } | ||