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 toFormatedJSON: VideoAbuseMethods.ToFormatedJSON
17 let listForApi: VideoAbuseMethods.ListForApi
19 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
20 VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse',
23 type: DataTypes.STRING,
26 reporterUsernameValid: function (value) {
27 const res = isVideoAbuseReporterUsernameValid(value)
28 if (res === false) throw new Error('Video abuse reporter username is not valid.')
33 type: DataTypes.STRING,
36 reasonValid: function (value) {
37 const res = isVideoAbuseReasonValid(value)
38 if (res === false) throw new Error('Video abuse reason is not valid.')
49 fields: [ 'reporterPodId' ]
55 const classMethods = [
60 const instanceMethods = [
63 addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
68 // ------------------------------ METHODS ------------------------------
70 toFormatedJSON = function (this: VideoAbuseInstance) {
74 reporterPodHost = this.Pod.host
76 // It means it's our video
77 reporterPodHost = CONFIG.WEBSERVER.HOST
84 reporterUsername: this.reporterUsername,
85 videoId: this.videoId,
86 createdAt: this.createdAt
92 // ------------------------------ STATICS ------------------------------
94 function associate (models) {
95 VideoAbuse.belongsTo(models.Pod, {
97 name: 'reporterPodId',
103 VideoAbuse.belongsTo(models.Video, {
112 listForApi = function (start: number, count: number, sort: string, callback: VideoAbuseMethods.ListForApiCallback) {
116 order: [ getSort(sort) ],
119 model: VideoAbuse['sequelize'].models.Pod,
125 return VideoAbuse.findAndCountAll(query).asCallback(function (err, result) {
126 if (err) return callback(err)
128 return callback(null, result.rows, result.count)