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, {
94 where = Object.assign(where, {
103 // we don't care about this count for deleted videos, so there are not included
107 'FROM "videoAbuse" ' +
108 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
111 'countReportsForVideo'
114 // we don't care about this count for deleted videos, so there are not included
120 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
121 'FROM "videoAbuse" ' +
123 'WHERE t.id = "VideoAbuseModel".id ' +
131 'SELECT count("videoAbuse"."id") ' +
132 'FROM "videoAbuse" ' +
133 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
134 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
135 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
136 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
139 'countReportsForReporter__video'
144 'SELECT count(DISTINCT "videoAbuse"."id") ' +
145 'FROM "videoAbuse" ' +
146 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "VideoAbuseModel"."reporterAccountId" ` +
149 'countReportsForReporter__deletedVideo'
154 'SELECT count(DISTINCT "videoAbuse"."id") ' +
155 'FROM "videoAbuse" ' +
156 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
157 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
158 'INNER JOIN "account" ON ' +
159 '"videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
160 `OR "videoChannel"."accountId" = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
163 'countReportsForReportee__video'
168 'SELECT count(DISTINCT "videoAbuse"."id") ' +
169 'FROM "videoAbuse" ' +
170 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "Video->VideoChannel"."accountId" ` +
171 `OR CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = ` +
172 `CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
175 'countReportsForReportee__deletedVideo'
183 where: searchAttribute(options.searchReporter, 'name')
188 where: searchAttribute(options.searchVideo, 'name'),
191 model: ThumbnailModel
194 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
195 where: searchAttribute(options.searchVideoChannel, 'name'),
199 where: searchAttribute(options.searchReportee, 'name')
204 attributes: [ 'id', 'reason', 'unfederated' ],
205 model: VideoBlacklistModel
215 tableName: 'videoAbuse',
218 fields: [ 'videoId' ]
221 fields: [ 'reporterAccountId' ]
225 export class VideoAbuseModel extends Model<VideoAbuseModel> {
229 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
230 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
235 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
237 state: VideoAbuseState
241 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
242 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
243 moderationComment: string
247 @Column(DataType.JSONB)
248 deletedVideo: VideoDetails
256 @ForeignKey(() => AccountModel)
258 reporterAccountId: number
260 @BelongsTo(() => AccountModel, {
266 Account: AccountModel
268 @ForeignKey(() => VideoModel)
272 @BelongsTo(() => VideoModel, {
280 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
281 const videoAttributes = {}
282 if (videoId) videoAttributes['videoId'] = videoId
283 if (uuid) videoAttributes['deletedVideo'] = { uuid }
291 return VideoAbuseModel.findOne(query)
294 static listForApi (parameters: {
299 serverAccountId: number
300 user?: MUserAccountId
302 const { start, count, sort, search, user, serverAccountId } = parameters
303 const userAccountId = user ? user.Account.id : undefined
308 order: getSort(sort),
309 col: 'VideoAbuseModel.id',
314 ...parseQueryStringFilter(search, {
322 if (v === "accepted") return VideoAbuseState.ACCEPTED
323 if (v === "pending") return VideoAbuseState.PENDING
324 if (v === "rejected") return VideoAbuseState.REJECTED
331 if (v === "deleted") return { deletedVideo: { [Op.not]: null } }
348 return VideoAbuseModel
349 .scope({ method: [ ScopeNames.FOR_API, filters ] })
350 .findAndCountAll(query)
351 .then(({ rows, count }) => {
352 return { total: count, data: rows }
356 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
357 const countReportsForVideo = this.get('countReportsForVideo') as number
358 const nthReportForVideo = this.get('nthReportForVideo') as number
359 const countReportsForReporterVideo = this.get('countReportsForReporter__video') as number
360 const countReportsForReporterDeletedVideo = this.get('countReportsForReporter__deletedVideo') as number
361 const countReportsForReporteeVideo = this.get('countReportsForReportee__video') as number
362 const countReportsForReporteeDeletedVideo = this.get('countReportsForReportee__deletedVideo') as number
364 const video = this.Video
371 reporterAccount: this.Account.toFormattedJSON(),
374 label: VideoAbuseModel.getStateLabel(this.state)
376 moderationComment: this.moderationComment,
382 deleted: !this.Video,
383 blacklisted: this.Video && this.Video.isBlacklisted(),
384 thumbnailPath: this.Video?.getMiniatureStaticPath(),
385 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
387 createdAt: this.createdAt,
388 updatedAt: this.updatedAt,
389 count: countReportsForVideo || 0,
390 nth: nthReportForVideo || 0,
391 countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0),
392 countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0)
396 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
398 type: 'Flag' as 'Flag',
399 content: this.reason,
400 object: this.Video.url
404 private static getStateLabel (id: number) {
405 return VIDEO_ABUSE_STATES[id] || 'Unknown'