1 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
3 import { VideoAbuse } from '../../../shared/models/videos'
5 isVideoAbuseModerationCommentValid,
6 isVideoAbuseReasonValid,
8 } from '../../helpers/custom-validators/video-abuses'
9 import { AccountModel } from '../account/account'
10 import { getSort, throwIfNotValid } from '../utils'
11 import { VideoModel } from './video'
12 import { VideoAbuseState } from '../../../shared'
13 import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
16 tableName: 'videoAbuse',
22 fields: [ 'reporterAccountId' ]
26 export class VideoAbuseModel extends Model<VideoAbuseModel> {
30 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
31 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
36 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
38 state: VideoAbuseState
42 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment'))
43 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
44 moderationComment: string
52 @ForeignKey(() => AccountModel)
54 reporterAccountId: number
56 @BelongsTo(() => AccountModel, {
64 @ForeignKey(() => VideoModel)
68 @BelongsTo(() => VideoModel, {
76 static loadByIdAndVideoId (id: number, videoId: number) {
83 return VideoAbuseModel.findOne(query)
86 static listForApi (start: number, count: number, sort: string) {
103 return VideoAbuseModel.findAndCountAll(query)
104 .then(({ rows, count }) => {
105 return { total: count, data: rows }
109 toFormattedJSON (): VideoAbuse {
113 reporterAccount: this.Account.toFormattedJSON(),
116 label: VideoAbuseModel.getStateLabel(this.state)
118 moderationComment: this.moderationComment,
121 uuid: this.Video.uuid,
122 name: this.Video.name
124 createdAt: this.createdAt
128 toActivityPubObject (): VideoAbuseObject {
130 type: 'Flag' as 'Flag',
131 content: this.reason,
132 object: this.Video.url
136 private static getStateLabel (id: number) {
137 return VIDEO_ABUSE_STATES[id] || 'Unknown'