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
37 state?: VideoAbuseState
40 serverAccountId: number
45 [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')')
50 where = Object.assign(where, {
54 { videoId: { [Op.not]: null } },
55 searchAttribute(options.search, '$Video.name$')
60 { videoId: { [Op.not]: null } },
61 searchAttribute(options.search, '$Video.VideoChannel.name$')
66 { deletedVideo: { [Op.not]: null } },
67 { deletedVideo: searchAttribute(options.search, 'name') }
72 { deletedVideo: { [Op.not]: null } },
73 { deletedVideo: { channel: searchAttribute(options.search, 'displayName') } }
76 searchAttribute(options.search, '$Account.name$')
82 where = Object.assign(where, {
88 where = Object.assign(where, {
93 let onlyBlacklisted = false
94 if (options.is === "deleted") {
95 where = Object.assign(where, {
96 deletedVideo: { [Op.not]: null }
98 } else if (options.is === "blacklisted") {
99 onlyBlacklisted = true
106 // we don't care about this count for deleted videos, so there are not included
110 'FROM "videoAbuse" ' +
111 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
114 'countReportsForVideo'
117 // we don't care about this count for deleted videos, so there are not included
123 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
124 'FROM "videoAbuse" ' +
126 'WHERE t.id = "VideoAbuseModel".id ' +
134 'SELECT count("videoAbuse"."id") ' +
135 'FROM "videoAbuse" ' +
136 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
137 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
138 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
139 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
142 'countReportsForReporter__video'
147 'SELECT count(DISTINCT "videoAbuse"."id") ' +
148 'FROM "videoAbuse" ' +
149 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "VideoAbuseModel"."reporterAccountId" ` +
152 'countReportsForReporter__deletedVideo'
157 'SELECT count(DISTINCT "videoAbuse"."id") ' +
158 'FROM "videoAbuse" ' +
159 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
160 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
161 'INNER JOIN "account" ON ' +
162 '"videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
163 `OR "videoChannel"."accountId" = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
166 'countReportsForReportee__video'
171 'SELECT count(DISTINCT "videoAbuse"."id") ' +
172 'FROM "videoAbuse" ' +
173 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "Video->VideoChannel"."accountId" ` +
174 `OR CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = ` +
175 `CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
178 'countReportsForReportee__deletedVideo'
186 where: searchAttribute(options.searchReporter, 'name')
190 required: onlyBlacklisted,
191 where: searchAttribute(options.searchVideo, 'name'),
194 model: ThumbnailModel
197 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
198 where: searchAttribute(options.searchVideoChannel, 'name'),
202 where: searchAttribute(options.searchReportee, 'name')
207 attributes: [ 'id', 'reason', 'unfederated' ],
208 model: VideoBlacklistModel,
209 required: onlyBlacklisted
219 tableName: 'videoAbuse',
222 fields: [ 'videoId' ]
225 fields: [ 'reporterAccountId' ]
229 export class VideoAbuseModel extends Model<VideoAbuseModel> {
233 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
234 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
239 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
241 state: VideoAbuseState
245 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
246 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
247 moderationComment: string
251 @Column(DataType.JSONB)
252 deletedVideo: VideoDetails
260 @ForeignKey(() => AccountModel)
262 reporterAccountId: number
264 @BelongsTo(() => AccountModel, {
270 Account: AccountModel
272 @ForeignKey(() => VideoModel)
276 @BelongsTo(() => VideoModel, {
284 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
285 const videoAttributes = {}
286 if (videoId) videoAttributes['videoId'] = videoId
287 if (uuid) videoAttributes['deletedVideo'] = { uuid }
295 return VideoAbuseModel.findOne(query)
298 static listForApi (parameters: {
303 serverAccountId: number
304 user?: MUserAccountId
306 const { start, count, sort, search, user, serverAccountId } = parameters
307 const userAccountId = user ? user.Account.id : undefined
312 order: getSort(sort),
313 col: 'VideoAbuseModel.id',
318 ...parseQueryStringFilter(search, {
326 if (v === "accepted") return VideoAbuseState.ACCEPTED
327 if (v === "pending") return VideoAbuseState.PENDING
328 if (v === "rejected") return VideoAbuseState.REJECTED
335 if (v === "deleted") return v
336 if (v === "blacklisted") return v
353 return VideoAbuseModel
354 .scope({ method: [ ScopeNames.FOR_API, filters ] })
355 .findAndCountAll(query)
356 .then(({ rows, count }) => {
357 return { total: count, data: rows }
361 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
362 const countReportsForVideo = this.get('countReportsForVideo') as number
363 const nthReportForVideo = this.get('nthReportForVideo') as number
364 const countReportsForReporterVideo = this.get('countReportsForReporter__video') as number
365 const countReportsForReporterDeletedVideo = this.get('countReportsForReporter__deletedVideo') as number
366 const countReportsForReporteeVideo = this.get('countReportsForReportee__video') as number
367 const countReportsForReporteeDeletedVideo = this.get('countReportsForReportee__deletedVideo') as number
369 const video = this.Video
376 reporterAccount: this.Account.toFormattedJSON(),
379 label: VideoAbuseModel.getStateLabel(this.state)
381 moderationComment: this.moderationComment,
387 deleted: !this.Video,
388 blacklisted: this.Video && this.Video.isBlacklisted(),
389 thumbnailPath: this.Video?.getMiniatureStaticPath(),
390 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
392 createdAt: this.createdAt,
393 updatedAt: this.updatedAt,
394 count: countReportsForVideo || 0,
395 nth: nthReportForVideo || 0,
396 countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0),
397 countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0)
401 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
403 type: 'Flag' as 'Flag',
404 content: this.reason,
405 object: this.Video.url
409 private static getStateLabel (id: number) {
410 return VIDEO_ABUSE_STATES[id] || 'Unknown'