2 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt, Scopes
3 } from 'sequelize-typescript'
4 import { VideoAbuseObject } from '../../../shared/models/activitypub/objects'
5 import { VideoAbuse } from '../../../shared/models/videos'
7 isVideoAbuseModerationCommentValid,
8 isVideoAbuseReasonValid,
10 } from '../../helpers/custom-validators/video-abuses'
11 import { AccountModel } from '../account/account'
12 import { buildBlockedAccountSQL, getSort, throwIfNotValid } from '../utils'
13 import { VideoModel } from './video'
14 import { VideoAbuseState, Video } from '../../../shared'
15 import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
16 import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models'
17 import * as Bluebird from 'bluebird'
18 import { literal, Op } from 'sequelize'
19 import { ThumbnailModel } from './thumbnail'
20 import { VideoChannelModel } from './video-channel'
21 import { ActorModel } from '../activitypub/actor'
22 import { VideoBlacklistModel } from './video-blacklist'
24 export enum ScopeNames {
29 [ScopeNames.FOR_API]: (options: {
31 searchReporter?: string
33 searchVideoChannel?: string
34 serverAccountId: number
37 const search = (sourceField, targetField) => sourceField ? ({
39 [Op.iLike]: `%${sourceField}%`
45 [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')')
50 where = Object.assign(where, {
54 { videoId: { [Op.not]: null } },
55 { '$Video.name$': { [Op.iLike]: `%${options.search}%` } }
60 { videoId: { [Op.not]: null } },
61 { '$Video.VideoChannel.name$': { [Op.iLike]: `%${options.search}%` } }
66 { deletedVideo: { [Op.not]: null } },
67 { deletedVideo: { name: { [Op.iLike]: `%${options.search}%` } } }
72 { deletedVideo: { [Op.not]: null } },
73 { deletedVideo: { channel: { displayName: { [Op.iLike]: `%${options.search}%` } } } }
76 { '$Account.name$': { [Op.iLike]: `%${options.search}%` } }
88 where: { ...search(options.searchReporter, 'name') }
93 where: { ...search(options.searchVideo, 'name') },
99 model: VideoChannelModel.unscoped(),
100 where: { ...search(options.searchVideoChannel, 'name') },
108 attributes: [ 'id', 'reason', 'unfederated' ],
109 model: VideoBlacklistModel
119 tableName: 'videoAbuse',
122 fields: [ 'videoId' ]
125 fields: [ 'reporterAccountId' ]
129 export class VideoAbuseModel extends Model<VideoAbuseModel> {
133 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
134 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
139 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
141 state: VideoAbuseState
145 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
146 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
147 moderationComment: string
151 @Column(DataType.JSONB)
160 @ForeignKey(() => AccountModel)
162 reporterAccountId: number
164 @BelongsTo(() => AccountModel, {
170 Account: AccountModel
172 @ForeignKey(() => VideoModel)
176 @BelongsTo(() => VideoModel, {
184 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
185 const videoAttributes = {}
186 if (videoId) videoAttributes['videoId'] = videoId
187 if (uuid) videoAttributes['deletedVideo'] = { uuid }
195 return VideoAbuseModel.findOne(query)
198 static listForApi (parameters: {
203 serverAccountId: number
204 user?: MUserAccountId
206 const { start, count, sort, search, user, serverAccountId } = parameters
207 const userAccountId = user ? user.Account.id : undefined
212 order: getSort(sort),
213 col: 'VideoAbuseModel.id',
223 return VideoAbuseModel
224 .scope({ method: [ ScopeNames.FOR_API, filters ] })
225 .findAndCountAll(query)
226 .then(({ rows, count }) => {
227 return { total: count, data: rows }
231 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
232 const video = this.Video
239 reporterAccount: this.Account.toFormattedJSON(),
242 label: VideoAbuseModel.getStateLabel(this.state)
244 moderationComment: this.moderationComment,
250 deleted: !this.Video,
251 blacklisted: this.Video && this.Video.isBlacklisted(),
252 thumbnailPath: this.Video?.getMiniatureStaticPath(),
253 channel: this.Video?.VideoChannel.toFormattedSummaryJSON() || this.deletedVideo?.channel
255 createdAt: this.createdAt
259 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
261 type: 'Flag' as 'Flag',
262 content: this.reason,
263 object: this.Video.url
267 private static getStateLabel (id: number) {
268 return VIDEO_ABUSE_STATES[id] || 'Unknown'