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 { '$Video.name$': { [Op.iLike]: `%${options.search}%` } }
53 { videoId: { [Op.not]: null } },
54 { '$Video.VideoChannel.name$': { [Op.iLike]: `%${options.search}%` } }
59 { deletedVideo: { [Op.not]: null } },
60 { deletedVideo: { name: { [Op.iLike]: `%${options.search}%` } } }
65 { deletedVideo: { [Op.not]: null } },
66 { deletedVideo: { channel: { displayName: { [Op.iLike]: `%${options.search}%` } } } }
69 { '$Account.name$': { [Op.iLike]: `%${options.search}%` } }
83 'count(id) OVER (PARTITION BY "videoId") ' +
84 'FROM "videoAbuse" ' +
86 'WHERE t.id = "VideoAbuseModel".id ' +
89 'countReportsForVideo'
97 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
98 'FROM "videoAbuse" ' +
100 'WHERE t.id = "VideoAbuseModel".id ' +
108 'SELECT count("videoAbuse"."id") ' +
109 'FROM "videoAbuse" ' +
110 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
111 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
112 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
113 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
116 'countReportsForReporter'
123 'SELECT "account"."id" ' +
125 'INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ' +
126 'INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" ' +
127 'WHERE "video"."id" = "VideoAbuseModel"."videoId" ' +
129 'SELECT count("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 "videoChannel"."accountId" = "account"."id" ' +
134 'INNER JOIN ids ON "account"."id" = ids.id ' +
137 'countReportsForReportee'
145 where: { ...searchAttribute(options.searchReporter, 'name') }
150 where: { ...searchAttribute(options.searchVideo, 'name') },
153 model: ThumbnailModel
156 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
157 where: { ...searchAttribute(options.searchVideoChannel, 'name') }
160 attributes: [ 'id', 'reason', 'unfederated' ],
161 model: VideoBlacklistModel
171 tableName: 'videoAbuse',
174 fields: [ 'videoId' ]
177 fields: [ 'reporterAccountId' ]
181 export class VideoAbuseModel extends Model<VideoAbuseModel> {
185 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
186 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
191 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
193 state: VideoAbuseState
197 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
198 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
199 moderationComment: string
203 @Column(DataType.JSONB)
204 deletedVideo: VideoDetails
212 @ForeignKey(() => AccountModel)
214 reporterAccountId: number
216 @BelongsTo(() => AccountModel, {
222 Account: AccountModel
224 @ForeignKey(() => VideoModel)
228 @BelongsTo(() => VideoModel, {
236 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
237 const videoAttributes = {}
238 if (videoId) videoAttributes['videoId'] = videoId
239 if (uuid) videoAttributes['deletedVideo'] = { uuid }
247 return VideoAbuseModel.findOne(query)
250 static listForApi (parameters: {
255 serverAccountId: number
256 user?: MUserAccountId
258 const { start, count, sort, search, user, serverAccountId } = parameters
259 const userAccountId = user ? user.Account.id : undefined
264 order: getSort(sort),
265 col: 'VideoAbuseModel.id',
275 return VideoAbuseModel
276 .scope({ method: [ ScopeNames.FOR_API, filters ] })
277 .findAndCountAll(query)
278 .then(({ rows, count }) => {
279 return { total: count, data: rows }
283 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
284 const countReportsForVideo = this.get('countReportsForVideo') as number
285 const nthReportForVideo = this.get('nthReportForVideo') as number
286 const countReportsForReporter = this.get('countReportsForReporter') as number
287 const countReportsForReportee = this.get('countReportsForReportee') as number
289 const video = this.Video
296 reporterAccount: this.Account.toFormattedJSON(),
299 label: VideoAbuseModel.getStateLabel(this.state)
301 moderationComment: this.moderationComment,
307 deleted: !this.Video,
308 blacklisted: this.Video && this.Video.isBlacklisted(),
309 thumbnailPath: this.Video?.getMiniatureStaticPath(),
310 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
312 createdAt: this.createdAt,
313 updatedAt: this.updatedAt,
314 count: countReportsForVideo || 0,
315 nth: nthReportForVideo || 0,
316 countReportsForReporter: countReportsForReporter || 0,
317 countReportsForReportee: countReportsForReportee || 0
321 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
323 type: 'Flag' as 'Flag',
324 content: this.reason,
325 object: this.Video.url
329 private static getStateLabel (id: number) {
330 return VIDEO_ABUSE_STATES[id] || 'Unknown'