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 } 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: {
30 searchReporter?: string
32 searchVideoChannel?: string
33 serverAccountId: number
38 [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')')
43 where = Object.assign(where, {
47 { videoId: { [Op.not]: null } },
48 searchAttribute(options.search, '$Video.name$')
53 { videoId: { [Op.not]: null } },
54 searchAttribute(options.search, '$Video.VideoChannel.name$')
59 { deletedVideo: { [Op.not]: null } },
60 { deletedVideo: searchAttribute(options.search, 'name') }
65 { deletedVideo: { [Op.not]: null } },
66 { deletedVideo: { channel: searchAttribute(options.search, 'displayName') } }
69 searchAttribute(options.search, '$Account.name$')
78 // we don't care about this count for deleted videos, so there are not included
82 'FROM "videoAbuse" ' +
83 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
86 'countReportsForVideo'
89 // we don't care about this count for deleted videos, so there are not included
95 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
96 'FROM "videoAbuse" ' +
98 'WHERE t.id = "VideoAbuseModel".id ' +
106 'SELECT count("videoAbuse"."id") ' +
107 'FROM "videoAbuse" ' +
108 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
109 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
110 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
111 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
114 'countReportsForReporter__video'
119 'SELECT count(DISTINCT "videoAbuse"."id") ' +
120 'FROM "videoAbuse" ' +
121 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "VideoAbuseModel"."reporterAccountId" ` +
124 'countReportsForReporter__deletedVideo'
129 'SELECT count(DISTINCT "videoAbuse"."id") ' +
130 'FROM "videoAbuse" ' +
131 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
132 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
133 'INNER JOIN "account" ON ' +
134 '"videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
135 `OR "videoChannel"."accountId" = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
138 'countReportsForReportee__video'
143 'SELECT count(DISTINCT "videoAbuse"."id") ' +
144 'FROM "videoAbuse" ' +
145 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "Video->VideoChannel"."accountId" ` +
146 `OR CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
149 'countReportsForReportee__deletedVideo'
157 where: searchAttribute(options.searchReporter, 'name')
162 where: searchAttribute(options.searchVideo, 'name'),
165 model: ThumbnailModel
168 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
169 where: searchAttribute(options.searchVideoChannel, 'name')
172 attributes: [ 'id', 'reason', 'unfederated' ],
173 model: VideoBlacklistModel
183 tableName: 'videoAbuse',
186 fields: [ 'videoId' ]
189 fields: [ 'reporterAccountId' ]
193 export class VideoAbuseModel extends Model<VideoAbuseModel> {
197 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
198 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
203 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
205 state: VideoAbuseState
209 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
210 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
211 moderationComment: string
215 @Column(DataType.JSONB)
216 deletedVideo: VideoDetails
224 @ForeignKey(() => AccountModel)
226 reporterAccountId: number
228 @BelongsTo(() => AccountModel, {
234 Account: AccountModel
236 @ForeignKey(() => VideoModel)
240 @BelongsTo(() => VideoModel, {
248 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
249 const videoAttributes = {}
250 if (videoId) videoAttributes['videoId'] = videoId
251 if (uuid) videoAttributes['deletedVideo'] = { uuid }
259 return VideoAbuseModel.findOne(query)
262 static listForApi (parameters: {
267 serverAccountId: number
268 user?: MUserAccountId
270 const { start, count, sort, search, user, serverAccountId } = parameters
271 const userAccountId = user ? user.Account.id : undefined
276 order: getSort(sort),
277 col: 'VideoAbuseModel.id',
287 return VideoAbuseModel
288 .scope({ method: [ ScopeNames.FOR_API, filters ] })
289 .findAndCountAll(query)
290 .then(({ rows, count }) => {
291 return { total: count, data: rows }
295 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
296 const countReportsForVideo = this.get('countReportsForVideo') as number
297 const nthReportForVideo = this.get('nthReportForVideo') as number
298 const countReportsForReporterVideo = this.get('countReportsForReporter__video') as number
299 const countReportsForReporterDeletedVideo = this.get('countReportsForReporter__deletedVideo') as number
300 const countReportsForReporteeVideo = this.get('countReportsForReportee__video') as number
301 const countReportsForReporteeDeletedVideo = this.get('countReportsForReportee__deletedVideo') as number
303 const video = this.Video
310 reporterAccount: this.Account.toFormattedJSON(),
313 label: VideoAbuseModel.getStateLabel(this.state)
315 moderationComment: this.moderationComment,
321 deleted: !this.Video,
322 blacklisted: this.Video && this.Video.isBlacklisted(),
323 thumbnailPath: this.Video?.getMiniatureStaticPath(),
324 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
326 createdAt: this.createdAt,
327 updatedAt: this.updatedAt,
328 count: countReportsForVideo || 0,
329 nth: nthReportForVideo || 0,
330 countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0),
331 countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0)
335 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
337 type: 'Flag' as 'Flag',
338 content: this.reason,
339 object: this.Video.url
343 private static getStateLabel (id: number) {
344 return VIDEO_ABUSE_STATES[id] || 'Unknown'