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