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, searchAttribute, parseQueryStringFilter } from '../utils'
13 import { VideoModel } from './video'
14 import { VideoAbuseState, VideoDetails } 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 { VideoBlacklistModel } from './video-blacklist'
21 import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel'
23 export enum ScopeNames {
28 [ScopeNames.FOR_API]: (options: {
31 searchReporter?: string
32 searchReportee?: string
34 searchVideoChannel?: string
38 state?: VideoAbuseState
39 is?: 'deleted' | 'blacklisted'
42 serverAccountId: number
47 [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')')
52 where = Object.assign(where, {
56 { videoId: { [Op.not]: null } },
57 searchAttribute(options.search, '$Video.name$')
62 { videoId: { [Op.not]: null } },
63 searchAttribute(options.search, '$Video.VideoChannel.name$')
68 { deletedVideo: { [Op.not]: null } },
69 { deletedVideo: searchAttribute(options.search, 'name') }
74 { deletedVideo: { [Op.not]: null } },
75 { deletedVideo: { channel: searchAttribute(options.search, 'displayName') } }
78 searchAttribute(options.search, '$Account.name$')
84 where = Object.assign(where, {
90 where = Object.assign(where, {
95 let onlyBlacklisted = false
96 if (options.is === 'deleted') {
97 where = Object.assign(where, {
98 deletedVideo: { [Op.not]: null }
100 } else if (options.is === 'blacklisted') {
101 onlyBlacklisted = true
108 // we don't care about this count for deleted videos, so there are not included
112 'FROM "videoAbuse" ' +
113 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
116 'countReportsForVideo'
119 // we don't care about this count for deleted videos, so there are not included
125 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
126 'FROM "videoAbuse" ' +
128 'WHERE t.id = "VideoAbuseModel".id ' +
136 'SELECT count("videoAbuse"."id") ' +
137 'FROM "videoAbuse" ' +
138 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
139 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
140 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
141 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
144 'countReportsForReporter__video'
149 'SELECT count(DISTINCT "videoAbuse"."id") ' +
150 'FROM "videoAbuse" ' +
151 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "VideoAbuseModel"."reporterAccountId" ` +
154 'countReportsForReporter__deletedVideo'
159 'SELECT count(DISTINCT "videoAbuse"."id") ' +
160 'FROM "videoAbuse" ' +
161 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
162 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
163 'INNER JOIN "account" ON ' +
164 '"videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
165 `OR "videoChannel"."accountId" = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
168 'countReportsForReportee__video'
173 'SELECT count(DISTINCT "videoAbuse"."id") ' +
174 'FROM "videoAbuse" ' +
175 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "Video->VideoChannel"."accountId" ` +
176 `OR CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = ` +
177 `CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
180 'countReportsForReportee__deletedVideo'
188 where: searchAttribute(options.searchReporter, 'name')
192 required: onlyBlacklisted,
193 where: searchAttribute(options.searchVideo, 'name'),
196 model: ThumbnailModel
199 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
200 where: searchAttribute(options.searchVideoChannel, 'name'),
204 where: searchAttribute(options.searchReportee, 'name')
209 attributes: [ 'id', 'reason', 'unfederated' ],
210 model: VideoBlacklistModel,
211 required: onlyBlacklisted
221 tableName: 'videoAbuse',
224 fields: [ 'videoId' ]
227 fields: [ 'reporterAccountId' ]
231 export class VideoAbuseModel extends Model<VideoAbuseModel> {
235 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
236 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
241 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
243 state: VideoAbuseState
247 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
248 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
249 moderationComment: string
253 @Column(DataType.JSONB)
254 deletedVideo: VideoDetails
262 @ForeignKey(() => AccountModel)
264 reporterAccountId: number
266 @BelongsTo(() => AccountModel, {
272 Account: AccountModel
274 @ForeignKey(() => VideoModel)
278 @BelongsTo(() => VideoModel, {
286 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
287 const videoAttributes = {}
288 if (videoId) videoAttributes['videoId'] = videoId
289 if (uuid) videoAttributes['deletedVideo'] = { uuid }
297 return VideoAbuseModel.findOne(query)
300 static listForApi (parameters: {
305 serverAccountId: number
306 user?: MUserAccountId
308 const { start, count, sort, search, user, serverAccountId } = parameters
309 const userAccountId = user ? user.Account.id : undefined
314 order: getSort(sort),
315 col: 'VideoAbuseModel.id',
320 ...parseQueryStringFilter(search, {
328 if (v === 'accepted') return VideoAbuseState.ACCEPTED
329 if (v === 'pending') return VideoAbuseState.PENDING
330 if (v === 'rejected') return VideoAbuseState.REJECTED
337 if (v === 'deleted') return v
338 if (v === 'blacklisted') return v
355 return VideoAbuseModel
356 .scope({ method: [ ScopeNames.FOR_API, filters ] })
357 .findAndCountAll(query)
358 .then(({ rows, count }) => {
359 return { total: count, data: rows }
363 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
364 const countReportsForVideo = this.get('countReportsForVideo') as number
365 const nthReportForVideo = this.get('nthReportForVideo') as number
366 const countReportsForReporterVideo = this.get('countReportsForReporter__video') as number
367 const countReportsForReporterDeletedVideo = this.get('countReportsForReporter__deletedVideo') as number
368 const countReportsForReporteeVideo = this.get('countReportsForReportee__video') as number
369 const countReportsForReporteeDeletedVideo = this.get('countReportsForReportee__deletedVideo') as number
371 const video = this.Video
378 reporterAccount: this.Account.toFormattedJSON(),
381 label: VideoAbuseModel.getStateLabel(this.state)
383 moderationComment: this.moderationComment,
389 deleted: !this.Video,
390 blacklisted: this.Video && this.Video.isBlacklisted(),
391 thumbnailPath: this.Video?.getMiniatureStaticPath(),
392 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
394 createdAt: this.createdAt,
395 updatedAt: this.updatedAt,
396 count: countReportsForVideo || 0,
397 nth: nthReportForVideo || 0,
398 countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0),
399 countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0)
403 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
405 type: 'Flag' as 'Flag',
406 content: this.reason,
407 object: this.Video.url
411 private static getStateLabel (id: number) {
412 return VIDEO_ABUSE_STATES[id] || 'Unknown'