1 import * as Sequelize from 'sequelize'
3 import { CONFIG } from '../../initializers'
4 import { isVideoAbuseReasonValid } from '../../helpers'
6 import { addMethodsToModel, getSort } from '../utils'
12 } from './video-abuse-interface'
14 let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
15 let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON
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 reasonValid: value => {
26 const res = isVideoAbuseReasonValid(value)
27 if (res === false) throw new Error('Video abuse reason is not valid.')
38 fields: [ 'reporterAccountId' ]
44 const classMethods = [
49 const instanceMethods = [
52 addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
57 // ------------------------------ METHODS ------------------------------
59 toFormattedJSON = function (this: VideoAbuseInstance) {
60 let reporterServerHost
62 if (this.Account.Server) {
63 reporterServerHost = this.Account.Server.host
65 // It means it's our video
66 reporterServerHost = CONFIG.WEBSERVER.HOST
72 reporterUsername: this.Account.name,
74 videoId: this.Video.id,
75 videoUUID: this.Video.uuid,
76 videoName: this.Video.name,
77 createdAt: this.createdAt
83 // ------------------------------ STATICS ------------------------------
85 function associate (models) {
86 VideoAbuse.belongsTo(models.Account, {
88 name: 'reporterAccountId',
94 VideoAbuse.belongsTo(models.Video, {
103 listForApi = function (start: number, count: number, sort: string) {
107 order: [ getSort(sort) ],
110 model: VideoAbuse['sequelize'].models.Account,
114 model: VideoAbuse['sequelize'].models.Server,
120 model: VideoAbuse['sequelize'].models.Video,
126 return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => {
127 return { total: count, data: rows }