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'
14 import { MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models'
15 import * as Bluebird from 'bluebird'
18 tableName: 'videoAbuse',
24 fields: [ 'reporterAccountId' ]
28 export class VideoAbuseModel extends Model<VideoAbuseModel> {
32 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
33 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
38 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
40 state: VideoAbuseState
44 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
45 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
46 moderationComment: string
54 @ForeignKey(() => AccountModel)
56 reporterAccountId: number
58 @BelongsTo(() => AccountModel, {
66 @ForeignKey(() => VideoModel)
70 @BelongsTo(() => VideoModel, {
78 static loadByIdAndVideoId (id: number, videoId: number): Bluebird<MVideoAbuse> {
85 return VideoAbuseModel.findOne(query)
88 static listForApi (start: number, count: number, sort: string) {
105 return VideoAbuseModel.findAndCountAll(query)
106 .then(({ rows, count }) => {
107 return { total: count, data: rows }
111 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
115 reporterAccount: this.Account.toFormattedJSON(),
118 label: VideoAbuseModel.getStateLabel(this.state)
120 moderationComment: this.moderationComment,
123 uuid: this.Video.uuid,
124 name: this.Video.name
126 createdAt: this.createdAt
130 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
132 type: 'Flag' as 'Flag',
133 content: this.reason,
134 object: this.Video.url
138 private static getStateLabel (id: number) {
139 return VIDEO_ABUSE_STATES[id] || 'Unknown'