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, {
89 static loadByIdAndVideoId (id: number, videoId: number) {
96 return VideoAbuseModel.findOne(query)
99 static listForApi (start: number, count: number, sort: string) {
103 order: getSort(sort),
116 return VideoAbuseModel.findAndCountAll(query)
117 .then(({ rows, count }) => {
118 return { total: count, data: rows }
122 toFormattedJSON (): VideoAbuse {
126 reporterAccount: this.Account.toFormattedJSON(),
129 label: VideoAbuseModel.getStateLabel(this.state)
131 moderationComment: this.moderationComment,
134 uuid: this.Video.uuid,
135 name: this.Video.name
137 createdAt: this.createdAt
141 toActivityPubObject (): VideoAbuseObject {
143 type: 'Flag' as 'Flag',
144 content: this.reason,
145 object: this.Video.url
149 private static getStateLabel (id: number) {
150 return VIDEO_ABUSE_STATES[id] || 'Unknown'