1 import * as Sequelize from 'sequelize'
3 import { CONFIG } from '../initializers'
4 import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../helpers'
6 import { addMethodsToModel, getSort } from './utils'
13 } from './video-abuse-interface'
15 let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
16 let listForApi: VideoAbuseMethods.ListForApi
18 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
19 VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse',
22 type: DataTypes.STRING,
25 reporterUsernameValid: function (value) {
26 const res = isVideoAbuseReporterUsernameValid(value)
27 if (res === false) throw new Error('Video abuse reporter username is not valid.')
32 type: DataTypes.STRING,
35 reasonValid: function (value) {
36 const res = isVideoAbuseReasonValid(value)
37 if (res === false) throw new Error('Video abuse reason is not valid.')
48 fields: [ 'reporterPodId' ]
54 const classMethods = [
59 const instanceMethods = [
62 addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
67 // ------------------------------ METHODS ------------------------------
69 function toFormatedJSON () {
73 reporterPodHost = this.Pod.host
75 // It means it's our video
76 reporterPodHost = CONFIG.WEBSERVER.HOST
83 reporterUsername: this.reporterUsername,
84 videoId: this.videoId,
85 createdAt: this.createdAt
91 // ------------------------------ STATICS ------------------------------
93 function associate (models) {
94 VideoAbuse.belongsTo(models.Pod, {
96 name: 'reporterPodId',
102 VideoAbuse.belongsTo(models.Video, {
111 listForApi = function (start, count, sort, callback) {
115 order: [ getSort(sort) ],
118 model: VideoAbuse['sequelize'].models.Pod,
124 return VideoAbuse.findAndCountAll(query).asCallback(function (err, result) {
125 if (err) return callback(err)
127 return callback(null, result.rows, result.count)