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, Sequelize } 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$')
81 'FROM "videoAbuse" ' +
82 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
85 'countReportsForVideo'
93 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
94 'FROM "videoAbuse" ' +
96 'WHERE t.id = "VideoAbuseModel".id ' +
104 'SELECT count("videoAbuse"."id") ' +
105 'FROM "videoAbuse" ' +
106 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
107 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
108 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
109 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
112 'countReportsForReporter'
117 'SELECT count(DISTINCT "videoAbuse"."id") ' +
118 'FROM "videoAbuse" ' +
119 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
120 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
121 'INNER JOIN "account" ON "videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
124 'countReportsForReportee'
132 where: searchAttribute(options.searchReporter, 'name')
137 where: searchAttribute(options.searchVideo, 'name'),
140 model: ThumbnailModel
143 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
144 where: searchAttribute(options.searchVideoChannel, 'name')
147 attributes: [ 'id', 'reason', 'unfederated' ],
148 model: VideoBlacklistModel
158 tableName: 'videoAbuse',
161 fields: [ 'videoId' ]
164 fields: [ 'reporterAccountId' ]
168 export class VideoAbuseModel extends Model<VideoAbuseModel> {
172 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
173 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
178 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
180 state: VideoAbuseState
184 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
185 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
186 moderationComment: string
190 @Column(DataType.JSONB)
191 deletedVideo: VideoDetails
199 @ForeignKey(() => AccountModel)
201 reporterAccountId: number
203 @BelongsTo(() => AccountModel, {
209 Account: AccountModel
211 @ForeignKey(() => VideoModel)
215 @BelongsTo(() => VideoModel, {
223 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
224 const videoAttributes = {}
225 if (videoId) videoAttributes['videoId'] = videoId
226 if (uuid) videoAttributes['deletedVideo'] = { uuid }
234 return VideoAbuseModel.findOne(query)
237 static listForApi (parameters: {
242 serverAccountId: number
243 user?: MUserAccountId
245 const { start, count, sort, search, user, serverAccountId } = parameters
246 const userAccountId = user ? user.Account.id : undefined
251 order: getSort(sort),
252 col: 'VideoAbuseModel.id',
262 return VideoAbuseModel
263 .scope({ method: [ ScopeNames.FOR_API, filters ] })
264 .findAndCountAll(query)
265 .then(({ rows, count }) => {
266 return { total: count, data: rows }
270 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
271 const countReportsForVideo = this.get('countReportsForVideo') as number
272 const nthReportForVideo = this.get('nthReportForVideo') as number
273 const countReportsForReporter = this.get('countReportsForReporter') as number
274 const countReportsForReportee = this.get('countReportsForReportee') as number
276 const video = this.Video
283 reporterAccount: this.Account.toFormattedJSON(),
286 label: VideoAbuseModel.getStateLabel(this.state)
288 moderationComment: this.moderationComment,
294 deleted: !this.Video,
295 blacklisted: this.Video && this.Video.isBlacklisted(),
296 thumbnailPath: this.Video?.getMiniatureStaticPath(),
297 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
299 createdAt: this.createdAt,
300 updatedAt: this.updatedAt,
301 count: countReportsForVideo || 0,
302 nth: nthReportForVideo || 0,
303 countReportsForReporter: countReportsForReporter || 0,
304 countReportsForReportee: countReportsForReportee || 0
308 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
310 type: 'Flag' as 'Flag',
311 content: this.reason,
312 object: this.Video.url
316 private static getStateLabel (id: number) {
317 return VIDEO_ABUSE_STATES[id] || 'Unknown'