]>
Commit | Line | Data |
---|---|---|
65fcc311 C |
1 | import { CONFIG } from '../initializers' |
2 | import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../helpers' | |
3 | import { getSort } from './utils' | |
55fa55a9 C |
4 | |
5 | module.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) { | |
65fcc311 | 13 | const res = isVideoAbuseReporterUsernameValid(value) |
55fa55a9 C |
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) { | |
65fcc311 | 23 | const res = isVideoAbuseReasonValid(value) |
55fa55a9 C |
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 | ||
54 | function 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 | ||
72 | function listForApi (start, count, sort, callback) { | |
73 | const query = { | |
74 | offset: start, | |
75 | limit: count, | |
65fcc311 | 76 | order: [ getSort(sort) ], |
55fa55a9 C |
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 | ||
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 | |
65fcc311 | 99 | reporterPodHost = CONFIG.WEBSERVER.HOST |
55fa55a9 C |
100 | } |
101 | ||
102 | const json = { | |
103 | id: this.id, | |
104 | reporterPodHost, | |
105 | reason: this.reason, | |
106 | reporterUsername: this.reporterUsername, | |
11ac88de C |
107 | videoId: this.videoId, |
108 | createdAt: this.createdAt | |
55fa55a9 C |
109 | } |
110 | ||
111 | return json | |
112 | } |