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'
13 import { VideoAbuseObject } from '../../../shared/models/activitypub/objects/video-abuse-object'
15 let VideoAbuse: Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes>
16 let toFormattedJSON: VideoAbuseMethods.ToFormattedJSON
17 let listForApi: VideoAbuseMethods.ListForApi
18 let toActivityPubObject: VideoAbuseMethods.ToActivityPubObject
20 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
21 VideoAbuse = sequelize.define<VideoAbuseInstance, VideoAbuseAttributes>('VideoAbuse',
24 type: DataTypes.STRING,
27 reasonValid: value => {
28 const res = isVideoAbuseReasonValid(value)
29 if (res === false) throw new Error('Video abuse reason is not valid.')
40 fields: [ 'reporterAccountId' ]
46 const classMethods = [
51 const instanceMethods = [
55 addMethodsToModel(VideoAbuse, classMethods, instanceMethods)
60 // ------------------------------ METHODS ------------------------------
62 toFormattedJSON = function (this: VideoAbuseInstance) {
63 let reporterServerHost
65 if (this.Account.Server) {
66 reporterServerHost = this.Account.Server.host
68 // It means it's our video
69 reporterServerHost = CONFIG.WEBSERVER.HOST
75 reporterUsername: this.Account.name,
77 videoId: this.Video.id,
78 videoUUID: this.Video.uuid,
79 videoName: this.Video.name,
80 createdAt: this.createdAt
86 toActivityPubObject = function (this: VideoAbuseInstance) {
87 const videoAbuseObject: VideoAbuseObject = {
88 type: 'Flag' as 'Flag',
90 object: this.Video.url
93 return videoAbuseObject
96 // ------------------------------ STATICS ------------------------------
98 function associate (models) {
99 VideoAbuse.belongsTo(models.Account, {
101 name: 'reporterAccountId',
107 VideoAbuse.belongsTo(models.Video, {
116 listForApi = function (start: number, count: number, sort: string) {
120 order: [ getSort(sort) ],
123 model: VideoAbuse['sequelize'].models.Account,
127 model: VideoAbuse['sequelize'].models.Server,
133 model: VideoAbuse['sequelize'].models.Video,
139 return VideoAbuse.findAndCountAll(query).then(({ rows, count }) => {
140 return { total: count, data: rows }