]>
Commit | Line | Data |
---|---|---|
86521a67 | 1 | import { |
844db39e | 2 | AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Model, Table, UpdatedAt, Scopes |
86521a67 | 3 | } from 'sequelize-typescript' |
3fd3ab2d | 4 | import { VideoAbuseObject } from '../../../shared/models/activitypub/objects' |
19a3b914 | 5 | import { VideoAbuse } from '../../../shared/models/videos' |
268eebed C |
6 | import { |
7 | isVideoAbuseModerationCommentValid, | |
8 | isVideoAbuseReasonValid, | |
9 | isVideoAbuseStateValid | |
10 | } from '../../helpers/custom-validators/video-abuses' | |
3fd3ab2d | 11 | import { AccountModel } from '../account/account' |
0d3a2982 | 12 | import { buildBlockedAccountSQL, getSort, throwIfNotValid, searchAttribute, parseQueryStringFilter } from '../utils' |
3fd3ab2d | 13 | import { VideoModel } from './video' |
5fd4ca00 | 14 | import { VideoAbuseState, VideoDetails } from '../../../shared' |
74dc3bca | 15 | import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants' |
f0a47bc9 | 16 | import { MUserAccountId, MVideoAbuse, MVideoAbuseFormattable, MVideoAbuseVideo } from '../../typings/models' |
453e83ea | 17 | import * as Bluebird from 'bluebird' |
efa012ed | 18 | import { literal, Op } from 'sequelize' |
86521a67 | 19 | import { ThumbnailModel } from './thumbnail' |
86521a67 | 20 | import { VideoBlacklistModel } from './video-blacklist' |
e0a92917 | 21 | import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from './video-channel' |
3fd3ab2d | 22 | |
844db39e RK |
23 | export enum ScopeNames { |
24 | FOR_API = 'FOR_API' | |
25 | } | |
26 | ||
27 | @Scopes(() => ({ | |
28 | [ScopeNames.FOR_API]: (options: { | |
0d3a2982 | 29 | // search |
844db39e RK |
30 | search?: string |
31 | searchReporter?: string | |
0d3a2982 | 32 | searchReportee?: string |
844db39e RK |
33 | searchVideo?: string |
34 | searchVideoChannel?: string | |
fc8aabd0 | 35 | |
0d3a2982 RK |
36 | // filters |
37 | id?: number | |
38 | state?: VideoAbuseState | |
fc8aabd0 C |
39 | is?: 'deleted' | 'blacklisted' |
40 | ||
0d3a2982 | 41 | // accountIds |
844db39e | 42 | serverAccountId: number |
0251197e | 43 | userAccountId: number |
844db39e | 44 | }) => { |
844db39e RK |
45 | let where = { |
46 | reporterAccountId: { | |
47 | [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')') | |
48 | } | |
49 | } | |
50 | ||
51 | if (options.search) { | |
52 | where = Object.assign(where, { | |
53 | [Op.or]: [ | |
54 | { | |
55 | [Op.and]: [ | |
56 | { videoId: { [Op.not]: null } }, | |
0251197e | 57 | searchAttribute(options.search, '$Video.name$') |
844db39e RK |
58 | ] |
59 | }, | |
60 | { | |
61 | [Op.and]: [ | |
62 | { videoId: { [Op.not]: null } }, | |
0251197e | 63 | searchAttribute(options.search, '$Video.VideoChannel.name$') |
844db39e RK |
64 | ] |
65 | }, | |
66 | { | |
67 | [Op.and]: [ | |
68 | { deletedVideo: { [Op.not]: null } }, | |
0251197e | 69 | { deletedVideo: searchAttribute(options.search, 'name') } |
844db39e RK |
70 | ] |
71 | }, | |
72 | { | |
73 | [Op.and]: [ | |
74 | { deletedVideo: { [Op.not]: null } }, | |
0251197e | 75 | { deletedVideo: { channel: searchAttribute(options.search, 'displayName') } } |
844db39e RK |
76 | ] |
77 | }, | |
0251197e | 78 | searchAttribute(options.search, '$Account.name$') |
844db39e RK |
79 | ] |
80 | }) | |
81 | } | |
82 | ||
0d3a2982 RK |
83 | if (options.id) { |
84 | where = Object.assign(where, { | |
85 | id: options.id | |
86 | }) | |
87 | } | |
88 | ||
89 | if (options.state) { | |
90 | where = Object.assign(where, { | |
91 | state: options.state | |
92 | }) | |
93 | } | |
94 | ||
9b1fa49b | 95 | let onlyBlacklisted = false |
fc8aabd0 | 96 | if (options.is === 'deleted') { |
0d3a2982 | 97 | where = Object.assign(where, { |
9b1fa49b | 98 | deletedVideo: { [Op.not]: null } |
0d3a2982 | 99 | }) |
fc8aabd0 | 100 | } else if (options.is === 'blacklisted') { |
9b1fa49b | 101 | onlyBlacklisted = true |
0d3a2982 RK |
102 | } |
103 | ||
844db39e | 104 | return { |
5fd4ca00 RK |
105 | attributes: { |
106 | include: [ | |
107 | [ | |
efa012ed | 108 | // we don't care about this count for deleted videos, so there are not included |
5fd4ca00 RK |
109 | literal( |
110 | '(' + | |
0251197e RK |
111 | 'SELECT count(*) ' + |
112 | 'FROM "videoAbuse" ' + | |
113 | 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' + | |
5fd4ca00 RK |
114 | ')' |
115 | ), | |
116 | 'countReportsForVideo' | |
117 | ], | |
118 | [ | |
efa012ed | 119 | // we don't care about this count for deleted videos, so there are not included |
5fd4ca00 RK |
120 | literal( |
121 | '(' + | |
122 | 'SELECT t.nth ' + | |
123 | 'FROM ( ' + | |
124 | 'SELECT id, ' + | |
125 | 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' + | |
126 | 'FROM "videoAbuse" ' + | |
127 | ') t ' + | |
128 | 'WHERE t.id = "VideoAbuseModel".id ' + | |
129 | ')' | |
130 | ), | |
131 | 'nthReportForVideo' | |
132 | ], | |
133 | [ | |
134 | literal( | |
135 | '(' + | |
136 | 'SELECT count("videoAbuse"."id") ' + | |
137 | 'FROM "videoAbuse" ' + | |
138 | 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' + | |
139 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
140 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | |
141 | 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' + | |
142 | ')' | |
143 | ), | |
efa012ed RK |
144 | 'countReportsForReporter__video' |
145 | ], | |
146 | [ | |
147 | literal( | |
148 | '(' + | |
149 | 'SELECT count(DISTINCT "videoAbuse"."id") ' + | |
150 | 'FROM "videoAbuse" ' + | |
151 | `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "VideoAbuseModel"."reporterAccountId" ` + | |
152 | ')' | |
153 | ), | |
154 | 'countReportsForReporter__deletedVideo' | |
5fd4ca00 RK |
155 | ], |
156 | [ | |
157 | literal( | |
158 | '(' + | |
0251197e | 159 | 'SELECT count(DISTINCT "videoAbuse"."id") ' + |
5fd4ca00 RK |
160 | 'FROM "videoAbuse" ' + |
161 | 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' + | |
162 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
efa012ed RK |
163 | 'INNER JOIN "account" ON ' + |
164 | '"videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' + | |
165 | `OR "videoChannel"."accountId" = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` + | |
166 | ')' | |
167 | ), | |
168 | 'countReportsForReportee__video' | |
169 | ], | |
170 | [ | |
171 | literal( | |
172 | '(' + | |
173 | 'SELECT count(DISTINCT "videoAbuse"."id") ' + | |
174 | 'FROM "videoAbuse" ' + | |
175 | `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "Video->VideoChannel"."accountId" ` + | |
197876ea RK |
176 | `OR CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = ` + |
177 | `CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` + | |
5fd4ca00 RK |
178 | ')' |
179 | ), | |
efa012ed | 180 | 'countReportsForReportee__deletedVideo' |
5fd4ca00 RK |
181 | ] |
182 | ] | |
183 | }, | |
86521a67 RK |
184 | include: [ |
185 | { | |
844db39e RK |
186 | model: AccountModel, |
187 | required: true, | |
0251197e | 188 | where: searchAttribute(options.searchReporter, 'name') |
86521a67 RK |
189 | }, |
190 | { | |
844db39e | 191 | model: VideoModel, |
9b1fa49b | 192 | required: onlyBlacklisted, |
0251197e | 193 | where: searchAttribute(options.searchVideo, 'name'), |
86521a67 RK |
194 | include: [ |
195 | { | |
844db39e RK |
196 | model: ThumbnailModel |
197 | }, | |
198 | { | |
e0a92917 | 199 | model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), |
0d3a2982 RK |
200 | where: searchAttribute(options.searchVideoChannel, 'name'), |
201 | include: [ | |
202 | { | |
203 | model: AccountModel, | |
204 | where: searchAttribute(options.searchReportee, 'name') | |
205 | } | |
206 | ] | |
844db39e RK |
207 | }, |
208 | { | |
209 | attributes: [ 'id', 'reason', 'unfederated' ], | |
9b1fa49b RK |
210 | model: VideoBlacklistModel, |
211 | required: onlyBlacklisted | |
86521a67 RK |
212 | } |
213 | ] | |
86521a67 | 214 | } |
844db39e RK |
215 | ], |
216 | where | |
86521a67 | 217 | } |
844db39e | 218 | } |
86521a67 | 219 | })) |
3fd3ab2d C |
220 | @Table({ |
221 | tableName: 'videoAbuse', | |
222 | indexes: [ | |
55fa55a9 | 223 | { |
3fd3ab2d | 224 | fields: [ 'videoId' ] |
55fa55a9 C |
225 | }, |
226 | { | |
3fd3ab2d | 227 | fields: [ 'reporterAccountId' ] |
55fa55a9 | 228 | } |
e02643f3 | 229 | ] |
3fd3ab2d C |
230 | }) |
231 | export class VideoAbuseModel extends Model<VideoAbuseModel> { | |
e02643f3 | 232 | |
3fd3ab2d | 233 | @AllowNull(false) |
1506307f | 234 | @Default(null) |
3fd3ab2d | 235 | @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason')) |
1506307f | 236 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max)) |
3fd3ab2d | 237 | reason: string |
21e0727a | 238 | |
268eebed C |
239 | @AllowNull(false) |
240 | @Default(null) | |
241 | @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state')) | |
242 | @Column | |
243 | state: VideoAbuseState | |
244 | ||
245 | @AllowNull(true) | |
246 | @Default(null) | |
1735c825 | 247 | @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true)) |
268eebed C |
248 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max)) |
249 | moderationComment: string | |
250 | ||
68d19a0a RK |
251 | @AllowNull(true) |
252 | @Default(null) | |
253 | @Column(DataType.JSONB) | |
5fd4ca00 | 254 | deletedVideo: VideoDetails |
68d19a0a | 255 | |
3fd3ab2d C |
256 | @CreatedAt |
257 | createdAt: Date | |
21e0727a | 258 | |
3fd3ab2d C |
259 | @UpdatedAt |
260 | updatedAt: Date | |
e02643f3 | 261 | |
3fd3ab2d C |
262 | @ForeignKey(() => AccountModel) |
263 | @Column | |
264 | reporterAccountId: number | |
55fa55a9 | 265 | |
3fd3ab2d | 266 | @BelongsTo(() => AccountModel, { |
55fa55a9 | 267 | foreignKey: { |
68d19a0a | 268 | allowNull: true |
55fa55a9 | 269 | }, |
68d19a0a | 270 | onDelete: 'set null' |
55fa55a9 | 271 | }) |
3fd3ab2d C |
272 | Account: AccountModel |
273 | ||
274 | @ForeignKey(() => VideoModel) | |
275 | @Column | |
276 | videoId: number | |
55fa55a9 | 277 | |
3fd3ab2d | 278 | @BelongsTo(() => VideoModel, { |
55fa55a9 | 279 | foreignKey: { |
68d19a0a | 280 | allowNull: true |
55fa55a9 | 281 | }, |
68d19a0a | 282 | onDelete: 'set null' |
55fa55a9 | 283 | }) |
3fd3ab2d C |
284 | Video: VideoModel |
285 | ||
68d19a0a RK |
286 | static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> { |
287 | const videoAttributes = {} | |
288 | if (videoId) videoAttributes['videoId'] = videoId | |
289 | if (uuid) videoAttributes['deletedVideo'] = { uuid } | |
290 | ||
268eebed C |
291 | const query = { |
292 | where: { | |
293 | id, | |
68d19a0a | 294 | ...videoAttributes |
268eebed C |
295 | } |
296 | } | |
297 | return VideoAbuseModel.findOne(query) | |
298 | } | |
299 | ||
f0a47bc9 | 300 | static listForApi (parameters: { |
a1587156 C |
301 | start: number |
302 | count: number | |
303 | sort: string | |
844db39e | 304 | search?: string |
f0a47bc9 C |
305 | serverAccountId: number |
306 | user?: MUserAccountId | |
307 | }) { | |
844db39e | 308 | const { start, count, sort, search, user, serverAccountId } = parameters |
f0a47bc9 C |
309 | const userAccountId = user ? user.Account.id : undefined |
310 | ||
3fd3ab2d C |
311 | const query = { |
312 | offset: start, | |
313 | limit: count, | |
3bb6c526 | 314 | order: getSort(sort), |
86521a67 RK |
315 | col: 'VideoAbuseModel.id', |
316 | distinct: true | |
3fd3ab2d | 317 | } |
55fa55a9 | 318 | |
844db39e | 319 | const filters = { |
0d3a2982 RK |
320 | ...parseQueryStringFilter(search, { |
321 | id: { | |
322 | prefix: '#', | |
323 | handler: v => v | |
324 | }, | |
325 | state: { | |
326 | prefix: 'state:', | |
327 | handler: v => { | |
fc8aabd0 C |
328 | if (v === 'accepted') return VideoAbuseState.ACCEPTED |
329 | if (v === 'pending') return VideoAbuseState.PENDING | |
330 | if (v === 'rejected') return VideoAbuseState.REJECTED | |
0d3a2982 RK |
331 | return undefined |
332 | } | |
333 | }, | |
334 | is: { | |
335 | prefix: 'is:', | |
336 | handler: v => { | |
fc8aabd0 C |
337 | if (v === 'deleted') return v |
338 | if (v === 'blacklisted') return v | |
0d3a2982 RK |
339 | return undefined |
340 | } | |
341 | }, | |
342 | searchReporter: { | |
343 | prefix: 'reporter:', | |
344 | handler: v => v | |
345 | }, | |
346 | searchReportee: { | |
347 | prefix: 'reportee:', | |
348 | handler: v => v | |
349 | } | |
350 | }), | |
844db39e RK |
351 | serverAccountId, |
352 | userAccountId | |
353 | } | |
354 | ||
355 | return VideoAbuseModel | |
356 | .scope({ method: [ ScopeNames.FOR_API, filters ] }) | |
357 | .findAndCountAll(query) | |
3fd3ab2d C |
358 | .then(({ rows, count }) => { |
359 | return { total: count, data: rows } | |
360 | }) | |
55fa55a9 C |
361 | } |
362 | ||
1ca9f7c3 | 363 | toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse { |
5fd4ca00 RK |
364 | const countReportsForVideo = this.get('countReportsForVideo') as number |
365 | const nthReportForVideo = this.get('nthReportForVideo') as number | |
efa012ed RK |
366 | const countReportsForReporterVideo = this.get('countReportsForReporter__video') as number |
367 | const countReportsForReporterDeletedVideo = this.get('countReportsForReporter__deletedVideo') as number | |
368 | const countReportsForReporteeVideo = this.get('countReportsForReportee__video') as number | |
369 | const countReportsForReporteeDeletedVideo = this.get('countReportsForReportee__deletedVideo') as number | |
5fd4ca00 | 370 | |
68d19a0a RK |
371 | const video = this.Video |
372 | ? this.Video | |
373 | : this.deletedVideo | |
374 | ||
3fd3ab2d C |
375 | return { |
376 | id: this.id, | |
377 | reason: this.reason, | |
19a3b914 | 378 | reporterAccount: this.Account.toFormattedJSON(), |
268eebed C |
379 | state: { |
380 | id: this.state, | |
381 | label: VideoAbuseModel.getStateLabel(this.state) | |
382 | }, | |
383 | moderationComment: this.moderationComment, | |
19a3b914 | 384 | video: { |
68d19a0a RK |
385 | id: video.id, |
386 | uuid: video.uuid, | |
387 | name: video.name, | |
388 | nsfw: video.nsfw, | |
86521a67 RK |
389 | deleted: !this.Video, |
390 | blacklisted: this.Video && this.Video.isBlacklisted(), | |
391 | thumbnailPath: this.Video?.getMiniatureStaticPath(), | |
5fd4ca00 | 392 | channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel |
19a3b914 | 393 | }, |
5fd4ca00 RK |
394 | createdAt: this.createdAt, |
395 | updatedAt: this.updatedAt, | |
396 | count: countReportsForVideo || 0, | |
397 | nth: nthReportForVideo || 0, | |
efa012ed RK |
398 | countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0), |
399 | countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0) | |
3fd3ab2d C |
400 | } |
401 | } | |
402 | ||
453e83ea | 403 | toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject { |
3fd3ab2d C |
404 | return { |
405 | type: 'Flag' as 'Flag', | |
406 | content: this.reason, | |
407 | object: this.Video.url | |
408 | } | |
409 | } | |
268eebed C |
410 | |
411 | private static getStateLabel (id: number) { | |
412 | return VIDEO_ABUSE_STATES[id] || 'Unknown' | |
413 | } | |
55fa55a9 | 414 | } |