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) = ` +
147 `CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
150 'countReportsForReportee__deletedVideo'
158 where: searchAttribute(options.searchReporter, 'name')
163 where: searchAttribute(options.searchVideo, 'name'),
166 model: ThumbnailModel
169 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
170 where: searchAttribute(options.searchVideoChannel, 'name')
173 attributes: [ 'id', 'reason', 'unfederated' ],
174 model: VideoBlacklistModel
184 tableName: 'videoAbuse',
187 fields: [ 'videoId' ]
190 fields: [ 'reporterAccountId' ]
194 export class VideoAbuseModel extends Model<VideoAbuseModel> {
198 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
199 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
204 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
206 state: VideoAbuseState
210 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
211 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
212 moderationComment: string
216 @Column(DataType.JSONB)
217 deletedVideo: VideoDetails
225 @ForeignKey(() => AccountModel)
227 reporterAccountId: number
229 @BelongsTo(() => AccountModel, {
235 Account: AccountModel
237 @ForeignKey(() => VideoModel)
241 @BelongsTo(() => VideoModel, {
249 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
250 const videoAttributes = {}
251 if (videoId) videoAttributes['videoId'] = videoId
252 if (uuid) videoAttributes['deletedVideo'] = { uuid }
260 return VideoAbuseModel.findOne(query)
263 static listForApi (parameters: {
268 serverAccountId: number
269 user?: MUserAccountId
271 const { start, count, sort, search, user, serverAccountId } = parameters
272 const userAccountId = user ? user.Account.id : undefined
277 order: getSort(sort),
278 col: 'VideoAbuseModel.id',
288 return VideoAbuseModel
289 .scope({ method: [ ScopeNames.FOR_API, filters ] })
290 .findAndCountAll(query)
291 .then(({ rows, count }) => {
292 return { total: count, data: rows }
296 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
297 const countReportsForVideo = this.get('countReportsForVideo') as number
298 const nthReportForVideo = this.get('nthReportForVideo') as number
299 const countReportsForReporterVideo = this.get('countReportsForReporter__video') as number
300 const countReportsForReporterDeletedVideo = this.get('countReportsForReporter__deletedVideo') as number
301 const countReportsForReporteeVideo = this.get('countReportsForReportee__video') as number
302 const countReportsForReporteeDeletedVideo = this.get('countReportsForReportee__deletedVideo') as number
304 const video = this.Video
311 reporterAccount: this.Account.toFormattedJSON(),
314 label: VideoAbuseModel.getStateLabel(this.state)
316 moderationComment: this.moderationComment,
322 deleted: !this.Video,
323 blacklisted: this.Video && this.Video.isBlacklisted(),
324 thumbnailPath: this.Video?.getMiniatureStaticPath(),
325 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
327 createdAt: this.createdAt,
328 updatedAt: this.updatedAt,
329 count: countReportsForVideo || 0,
330 nth: nthReportForVideo || 0,
331 countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0),
332 countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0)
336 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
338 type: 'Flag' as 'Flag',
339 content: this.reason,
340 object: this.Video.url
344 private static getStateLabel (id: number) {
345 return VIDEO_ABUSE_STATES[id] || 'Unknown'