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 { buildBlockedAccountSQL, 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 { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models'
15 import * as Bluebird from 'bluebird'
16 import { literal, Op } from 'sequelize'
19 tableName: 'videoAbuse',
25 fields: [ 'reporterAccountId' ]
29 export class VideoAbuseModel extends Model<VideoAbuseModel> {
33 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
34 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
39 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
41 state: VideoAbuseState
45 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
46 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
47 moderationComment: string
55 @ForeignKey(() => AccountModel)
57 reporterAccountId: number
59 @BelongsTo(() => AccountModel, {
67 @ForeignKey(() => VideoModel)
71 @BelongsTo(() => VideoModel, {
79 static loadByIdAndVideoId (id: number, videoId: number): Bluebird<MVideoAbuse> {
86 return VideoAbuseModel.findOne(query)
89 static listForApi (parameters: {
93 serverAccountId: number
96 const { start, count, sort, user, serverAccountId } = parameters
97 const userAccountId = user ? user.Account.id : undefined
102 order: getSort(sort),
105 [Op.notIn]: literal('(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')')
120 return VideoAbuseModel.findAndCountAll(query)
121 .then(({ rows, count }) => {
122 return { total: count, data: rows }
126 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
130 reporterAccount: this.Account.toFormattedJSON(),
133 label: VideoAbuseModel.getStateLabel(this.state)
135 moderationComment: this.moderationComment,
138 uuid: this.Video.uuid,
139 name: this.Video.name
141 createdAt: this.createdAt
145 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
147 type: 'Flag' as 'Flag',
148 content: this.reason,
149 object: this.Video.url
153 private static getStateLabel (id: number) {
154 return VIDEO_ABUSE_STATES[id] || 'Unknown'