14 } from 'sequelize-typescript'
15 import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
16 import { VideoAbuse } from '../../../shared/models/videos'
18 isVideoAbuseModerationCommentValid,
19 isVideoAbuseReasonValid,
20 isVideoAbuseStateValid
21 } from '../../helpers/custom-validators/video-abuses'
22 import { Emailer } from '../../lib/emailer'
23 import { AccountModel } from '../account/account'
24 import { getSort, throwIfNotValid } from '../utils'
25 import { VideoModel } from './video'
26 import { VideoAbuseState } from '../../../shared'
27 import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers'
30 tableName: 'videoAbuse',
36 fields: [ 'reporterAccountId' ]
40 export class VideoAbuseModel extends Model<VideoAbuseModel> {
43 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
49 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
51 state: VideoAbuseState
55 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment'))
56 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
57 moderationComment: string
65 @ForeignKey(() => AccountModel)
67 reporterAccountId: number
69 @BelongsTo(() => AccountModel, {
77 @ForeignKey(() => VideoModel)
81 @BelongsTo(() => VideoModel, {
90 static sendEmailNotification (instance: VideoAbuseModel) {
91 return Emailer.Instance.addVideoAbuseReportJob(instance.videoId)
94 static loadByIdAndVideoId (id: number, videoId: number) {
101 return VideoAbuseModel.findOne(query)
104 static listForApi (start: number, count: number, sort: string) {
108 order: getSort(sort),
121 return VideoAbuseModel.findAndCountAll(query)
122 .then(({ rows, count }) => {
123 return { total: count, data: rows }
127 toFormattedJSON (): VideoAbuse {
131 reporterAccount: this.Account.toFormattedJSON(),
134 label: VideoAbuseModel.getStateLabel(this.state)
136 moderationComment: this.moderationComment,
139 uuid: this.Video.uuid,
140 name: this.Video.name
142 createdAt: this.createdAt
146 toActivityPubObject (): VideoAbuseObject {
148 type: 'Flag' as 'Flag',
149 content: this.reason,
150 object: this.Video.url
154 private static getStateLabel (id: number) {
155 return VIDEO_ABUSE_STATES[id] || 'Unknown'