]>
Commit | Line | Data |
---|---|---|
e02643f3 C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
74889a71 C |
3 | import { CONFIG } from '../../initializers' |
4 | import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../../helpers' | |
55fa55a9 | 5 | |
74889a71 | 6 | import { addMethodsToModel, getSort } from '../utils' |
e02643f3 C |
7 | import { |
8 | VideoAbuseClass, | |
9 | VideoAbuseInstance, | |
10 | VideoAbuseAttributes, | |
11 | ||
12 | VideoAbuseMethods | |
13 | } from './video-abuse-interface' | |
14 | ||
15 | let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes> | |
154898b0 | 16 | let toFormatedJSON: VideoAbuseMethods.ToFormatedJSON |
e02643f3 C |
17 | let listForApi: VideoAbuseMethods.ListForApi |
18 | ||
127944aa C |
19 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
20 | VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse', | |
55fa55a9 C |
21 | { |
22 | reporterUsername: { | |
23 | type: DataTypes.STRING, | |
24 | allowNull: false, | |
25 | validate: { | |
26 | reporterUsernameValid: function (value) { | |
65fcc311 | 27 | const res = isVideoAbuseReporterUsernameValid(value) |
55fa55a9 C |
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) { | |
65fcc311 | 37 | const res = isVideoAbuseReasonValid(value) |
55fa55a9 C |
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 | } | |
e02643f3 | 51 | ] |
55fa55a9 C |
52 | } |
53 | ) | |
54 | ||
e02643f3 C |
55 | const classMethods = [ |
56 | associate, | |
57 | ||
58 | listForApi | |
59 | ] | |
60 | const instanceMethods = [ | |
61 | toFormatedJSON | |
62 | ] | |
63 | addMethodsToModel(VideoAbuse, classMethods, instanceMethods) | |
64 | ||
55fa55a9 C |
65 | return VideoAbuse |
66 | } | |
67 | ||
e02643f3 C |
68 | // ------------------------------ METHODS ------------------------------ |
69 | ||
154898b0 | 70 | toFormatedJSON = function (this: VideoAbuseInstance) { |
e02643f3 C |
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 ------------------------------ | |
55fa55a9 C |
93 | |
94 | function associate (models) { | |
e02643f3 | 95 | VideoAbuse.belongsTo(models.Pod, { |
55fa55a9 C |
96 | foreignKey: { |
97 | name: 'reporterPodId', | |
98 | allowNull: true | |
99 | }, | |
100 | onDelete: 'cascade' | |
101 | }) | |
102 | ||
e02643f3 | 103 | VideoAbuse.belongsTo(models.Video, { |
55fa55a9 C |
104 | foreignKey: { |
105 | name: 'videoId', | |
106 | allowNull: false | |
107 | }, | |
108 | onDelete: 'cascade' | |
109 | }) | |
110 | } | |
111 | ||
154898b0 | 112 | listForApi = function (start: number, count: number, sort: string, callback: VideoAbuseMethods.ListForApiCallback) { |
55fa55a9 C |
113 | const query = { |
114 | offset: start, | |
115 | limit: count, | |
65fcc311 | 116 | order: [ getSort(sort) ], |
55fa55a9 C |
117 | include: [ |
118 | { | |
e02643f3 | 119 | model: VideoAbuse['sequelize'].models.Pod, |
55fa55a9 C |
120 | required: false |
121 | } | |
122 | ] | |
123 | } | |
124 | ||
e02643f3 | 125 | return VideoAbuse.findAndCountAll(query).asCallback(function (err, result) { |
55fa55a9 C |
126 | if (err) return callback(err) |
127 | ||
128 | return callback(null, result.rows, result.count) | |
129 | }) | |
130 | } | |
131 | ||
55fa55a9 | 132 |