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 } 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 { VideoChannelModel } from './video-channel'
21 import { VideoBlacklistModel } from './video-blacklist'
23 export enum ScopeNames {
28 [ScopeNames.FOR_API]: (options: {
30 searchReporter?: string
32 searchVideoChannel?: string
33 serverAccountId: number
36 const search = (sourceField, targetField) => sourceField ? ({
38 [Op.iLike]: `%${sourceField}%`
44 [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')')
49 where = Object.assign(where, {
53 { videoId: { [Op.not]: null } },
54 { '$Video.name$': { [Op.iLike]: `%${options.search}%` } }
59 { videoId: { [Op.not]: null } },
60 { '$Video.VideoChannel.name$': { [Op.iLike]: `%${options.search}%` } }
65 { deletedVideo: { [Op.not]: null } },
66 { deletedVideo: { name: { [Op.iLike]: `%${options.search}%` } } }
71 { deletedVideo: { [Op.not]: null } },
72 { deletedVideo: { channel: { displayName: { [Op.iLike]: `%${options.search}%` } } } }
75 { '$Account.name$': { [Op.iLike]: `%${options.search}%` } }
89 'count(id) OVER (PARTITION BY "videoId") ' +
90 'FROM "videoAbuse" ' +
92 'WHERE t.id = "VideoAbuseModel".id ' +
95 'countReportsForVideo'
103 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
104 'FROM "videoAbuse" ' +
106 'WHERE t.id = "VideoAbuseModel".id ' +
114 'SELECT count("videoAbuse"."id") ' +
115 'FROM "videoAbuse" ' +
116 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
117 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
118 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
119 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
122 'countReportsForReporter'
129 'SELECT "account"."id" ' +
131 'INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ' +
132 'INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" ' +
133 'WHERE "video"."id" = "VideoAbuseModel"."videoId" ' +
135 'SELECT count("videoAbuse"."id") ' +
136 'FROM "videoAbuse" ' +
137 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
138 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
139 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
140 'INNER JOIN ids ON "account"."id" = ids.id ' +
143 'countReportsForReportee'
151 where: { ...search(options.searchReporter, 'name') }
156 where: { ...search(options.searchVideo, 'name') },
159 model: ThumbnailModel
162 model: VideoChannelModel.scope([ 'WITH_ACTOR', 'WITH_ACCOUNT' ]),
163 where: { ...search(options.searchVideoChannel, 'name') }
166 attributes: [ 'id', 'reason', 'unfederated' ],
167 model: VideoBlacklistModel
177 tableName: 'videoAbuse',
180 fields: [ 'videoId' ]
183 fields: [ 'reporterAccountId' ]
187 export class VideoAbuseModel extends Model<VideoAbuseModel> {
191 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
192 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
197 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
199 state: VideoAbuseState
203 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
204 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
205 moderationComment: string
209 @Column(DataType.JSONB)
210 deletedVideo: VideoDetails
218 @ForeignKey(() => AccountModel)
220 reporterAccountId: number
222 @BelongsTo(() => AccountModel, {
228 Account: AccountModel
230 @ForeignKey(() => VideoModel)
234 @BelongsTo(() => VideoModel, {
242 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
243 const videoAttributes = {}
244 if (videoId) videoAttributes['videoId'] = videoId
245 if (uuid) videoAttributes['deletedVideo'] = { uuid }
253 return VideoAbuseModel.findOne(query)
256 static listForApi (parameters: {
261 serverAccountId: number
262 user?: MUserAccountId
264 const { start, count, sort, search, user, serverAccountId } = parameters
265 const userAccountId = user ? user.Account.id : undefined
270 order: getSort(sort),
271 col: 'VideoAbuseModel.id',
281 return VideoAbuseModel
282 .scope({ method: [ ScopeNames.FOR_API, filters ] })
283 .findAndCountAll(query)
284 .then(({ rows, count }) => {
285 return { total: count, data: rows }
289 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
290 const countReportsForVideo = this.get('countReportsForVideo') as number
291 const nthReportForVideo = this.get('nthReportForVideo') as number
292 const countReportsForReporter = this.get('countReportsForReporter') as number
293 const countReportsForReportee = this.get('countReportsForReportee') as number
295 const video = this.Video
302 reporterAccount: this.Account.toFormattedJSON(),
305 label: VideoAbuseModel.getStateLabel(this.state)
307 moderationComment: this.moderationComment,
313 deleted: !this.Video,
314 blacklisted: this.Video && this.Video.isBlacklisted(),
315 thumbnailPath: this.Video?.getMiniatureStaticPath(),
316 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
318 createdAt: this.createdAt,
319 updatedAt: this.updatedAt,
320 count: countReportsForVideo || 0,
321 nth: nthReportForVideo || 0,
322 countReportsForReporter: countReportsForReporter || 0,
323 countReportsForReportee: countReportsForReportee || 0
327 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
329 type: 'Flag' as 'Flag',
330 content: this.reason,
331 object: this.Video.url
335 private static getStateLabel (id: number) {
336 return VIDEO_ABUSE_STATES[id] || 'Unknown'