1 import * as Sequelize from 'sequelize'
3 import { CONFIG } from '../../initializers'
4 import { isVideoAbuseReporterUsernameValid, isVideoAbuseReasonValid } from '../../helpers'
6 import { addMethodsToModel, getSort } from '../utils'
12 } from './video-abuse-interface'
14 let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
15 let toFormatedJSON: VideoAbuseMethods.ToFormatedJSON
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 toFormatedJSON = function (this: VideoAbuseInstance) {
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: number, count: number, sort: string) {
115 order: [ getSort(sort) ],
118 model: VideoAbuse['sequelize'].models.Pod,
124 return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => {
125 return { total: count, data: rows }