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