]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-abuse.ts
Add reportee stats for deleted videos
[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'
e0a92917 12import { buildBlockedAccountSQL, getSort, throwIfNotValid, searchAttribute } 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: {
29 search?: string
30 searchReporter?: string
31 searchVideo?: string
32 searchVideoChannel?: string
33 serverAccountId: number
0251197e 34 userAccountId: number
844db39e 35 }) => {
844db39e
RK
36 let where = {
37 reporterAccountId: {
38 [Op.notIn]: literal('(' + buildBlockedAccountSQL(options.serverAccountId, options.userAccountId) + ')')
39 }
40 }
41
42 if (options.search) {
43 where = Object.assign(where, {
44 [Op.or]: [
45 {
46 [Op.and]: [
47 { videoId: { [Op.not]: null } },
0251197e 48 searchAttribute(options.search, '$Video.name$')
844db39e
RK
49 ]
50 },
51 {
52 [Op.and]: [
53 { videoId: { [Op.not]: null } },
0251197e 54 searchAttribute(options.search, '$Video.VideoChannel.name$')
844db39e
RK
55 ]
56 },
57 {
58 [Op.and]: [
59 { deletedVideo: { [Op.not]: null } },
0251197e 60 { deletedVideo: searchAttribute(options.search, 'name') }
844db39e
RK
61 ]
62 },
63 {
64 [Op.and]: [
65 { deletedVideo: { [Op.not]: null } },
0251197e 66 { deletedVideo: { channel: searchAttribute(options.search, 'displayName') } }
844db39e
RK
67 ]
68 },
0251197e 69 searchAttribute(options.search, '$Account.name$')
844db39e
RK
70 ]
71 })
72 }
73
844db39e 74 return {
5fd4ca00
RK
75 attributes: {
76 include: [
77 [
efa012ed 78 // we don't care about this count for deleted videos, so there are not included
5fd4ca00
RK
79 literal(
80 '(' +
0251197e
RK
81 'SELECT count(*) ' +
82 'FROM "videoAbuse" ' +
83 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' +
5fd4ca00
RK
84 ')'
85 ),
86 'countReportsForVideo'
87 ],
88 [
efa012ed 89 // we don't care about this count for deleted videos, so there are not included
5fd4ca00
RK
90 literal(
91 '(' +
92 'SELECT t.nth ' +
93 'FROM ( ' +
94 'SELECT id, ' +
95 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
96 'FROM "videoAbuse" ' +
97 ') t ' +
98 'WHERE t.id = "VideoAbuseModel".id ' +
99 ')'
100 ),
101 'nthReportForVideo'
102 ],
103 [
104 literal(
105 '(' +
106 'SELECT count("videoAbuse"."id") ' +
107 'FROM "videoAbuse" ' +
108 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
109 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
110 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
111 'WHERE "account"."id" = "VideoAbuseModel"."reporterAccountId" ' +
112 ')'
113 ),
efa012ed
RK
114 'countReportsForReporter__video'
115 ],
116 [
117 literal(
118 '(' +
119 'SELECT count(DISTINCT "videoAbuse"."id") ' +
120 'FROM "videoAbuse" ' +
121 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "VideoAbuseModel"."reporterAccountId" ` +
122 ')'
123 ),
124 'countReportsForReporter__deletedVideo'
5fd4ca00
RK
125 ],
126 [
127 literal(
128 '(' +
0251197e 129 'SELECT count(DISTINCT "videoAbuse"."id") ' +
5fd4ca00
RK
130 'FROM "videoAbuse" ' +
131 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' +
132 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
efa012ed
RK
133 'INNER JOIN "account" ON ' +
134 '"videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' +
135 `OR "videoChannel"."accountId" = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
136 ')'
137 ),
138 'countReportsForReportee__video'
139 ],
140 [
141 literal(
142 '(' +
143 'SELECT count(DISTINCT "videoAbuse"."id") ' +
144 'FROM "videoAbuse" ' +
145 `WHERE CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = "Video->VideoChannel"."accountId" ` +
146 `OR CAST("deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) = CAST("VideoAbuseModel"."deletedVideo"->'channel'->'ownerAccount'->>'id' AS INTEGER) ` +
5fd4ca00
RK
147 ')'
148 ),
efa012ed 149 'countReportsForReportee__deletedVideo'
5fd4ca00
RK
150 ]
151 ]
152 },
86521a67
RK
153 include: [
154 {
844db39e
RK
155 model: AccountModel,
156 required: true,
0251197e 157 where: searchAttribute(options.searchReporter, 'name')
86521a67
RK
158 },
159 {
844db39e
RK
160 model: VideoModel,
161 required: false,
0251197e 162 where: searchAttribute(options.searchVideo, 'name'),
86521a67
RK
163 include: [
164 {
844db39e
RK
165 model: ThumbnailModel
166 },
167 {
e0a92917 168 model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
0251197e 169 where: searchAttribute(options.searchVideoChannel, 'name')
844db39e
RK
170 },
171 {
172 attributes: [ 'id', 'reason', 'unfederated' ],
173 model: VideoBlacklistModel
86521a67
RK
174 }
175 ]
86521a67 176 }
844db39e
RK
177 ],
178 where
86521a67 179 }
844db39e 180 }
86521a67 181}))
3fd3ab2d
C
182@Table({
183 tableName: 'videoAbuse',
184 indexes: [
55fa55a9 185 {
3fd3ab2d 186 fields: [ 'videoId' ]
55fa55a9
C
187 },
188 {
3fd3ab2d 189 fields: [ 'reporterAccountId' ]
55fa55a9 190 }
e02643f3 191 ]
3fd3ab2d
C
192})
193export class VideoAbuseModel extends Model<VideoAbuseModel> {
e02643f3 194
3fd3ab2d 195 @AllowNull(false)
1506307f 196 @Default(null)
3fd3ab2d 197 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
1506307f 198 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
3fd3ab2d 199 reason: string
21e0727a 200
268eebed
C
201 @AllowNull(false)
202 @Default(null)
203 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
204 @Column
205 state: VideoAbuseState
206
207 @AllowNull(true)
208 @Default(null)
1735c825 209 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
268eebed
C
210 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
211 moderationComment: string
212
68d19a0a
RK
213 @AllowNull(true)
214 @Default(null)
215 @Column(DataType.JSONB)
5fd4ca00 216 deletedVideo: VideoDetails
68d19a0a 217
3fd3ab2d
C
218 @CreatedAt
219 createdAt: Date
21e0727a 220
3fd3ab2d
C
221 @UpdatedAt
222 updatedAt: Date
e02643f3 223
3fd3ab2d
C
224 @ForeignKey(() => AccountModel)
225 @Column
226 reporterAccountId: number
55fa55a9 227
3fd3ab2d 228 @BelongsTo(() => AccountModel, {
55fa55a9 229 foreignKey: {
68d19a0a 230 allowNull: true
55fa55a9 231 },
68d19a0a 232 onDelete: 'set null'
55fa55a9 233 })
3fd3ab2d
C
234 Account: AccountModel
235
236 @ForeignKey(() => VideoModel)
237 @Column
238 videoId: number
55fa55a9 239
3fd3ab2d 240 @BelongsTo(() => VideoModel, {
55fa55a9 241 foreignKey: {
68d19a0a 242 allowNull: true
55fa55a9 243 },
68d19a0a 244 onDelete: 'set null'
55fa55a9 245 })
3fd3ab2d
C
246 Video: VideoModel
247
68d19a0a
RK
248 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
249 const videoAttributes = {}
250 if (videoId) videoAttributes['videoId'] = videoId
251 if (uuid) videoAttributes['deletedVideo'] = { uuid }
252
268eebed
C
253 const query = {
254 where: {
255 id,
68d19a0a 256 ...videoAttributes
268eebed
C
257 }
258 }
259 return VideoAbuseModel.findOne(query)
260 }
261
f0a47bc9 262 static listForApi (parameters: {
a1587156
C
263 start: number
264 count: number
265 sort: string
844db39e 266 search?: string
f0a47bc9
C
267 serverAccountId: number
268 user?: MUserAccountId
269 }) {
844db39e 270 const { start, count, sort, search, user, serverAccountId } = parameters
f0a47bc9
C
271 const userAccountId = user ? user.Account.id : undefined
272
3fd3ab2d
C
273 const query = {
274 offset: start,
275 limit: count,
3bb6c526 276 order: getSort(sort),
86521a67
RK
277 col: 'VideoAbuseModel.id',
278 distinct: true
3fd3ab2d 279 }
55fa55a9 280
844db39e
RK
281 const filters = {
282 search,
283 serverAccountId,
284 userAccountId
285 }
286
287 return VideoAbuseModel
288 .scope({ method: [ ScopeNames.FOR_API, filters ] })
289 .findAndCountAll(query)
3fd3ab2d
C
290 .then(({ rows, count }) => {
291 return { total: count, data: rows }
292 })
55fa55a9
C
293 }
294
1ca9f7c3 295 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
5fd4ca00
RK
296 const countReportsForVideo = this.get('countReportsForVideo') as number
297 const nthReportForVideo = this.get('nthReportForVideo') as number
efa012ed
RK
298 const countReportsForReporterVideo = this.get('countReportsForReporter__video') as number
299 const countReportsForReporterDeletedVideo = this.get('countReportsForReporter__deletedVideo') as number
300 const countReportsForReporteeVideo = this.get('countReportsForReportee__video') as number
301 const countReportsForReporteeDeletedVideo = this.get('countReportsForReportee__deletedVideo') as number
5fd4ca00 302
68d19a0a
RK
303 const video = this.Video
304 ? this.Video
305 : this.deletedVideo
306
3fd3ab2d
C
307 return {
308 id: this.id,
309 reason: this.reason,
19a3b914 310 reporterAccount: this.Account.toFormattedJSON(),
268eebed
C
311 state: {
312 id: this.state,
313 label: VideoAbuseModel.getStateLabel(this.state)
314 },
315 moderationComment: this.moderationComment,
19a3b914 316 video: {
68d19a0a
RK
317 id: video.id,
318 uuid: video.uuid,
319 name: video.name,
320 nsfw: video.nsfw,
86521a67
RK
321 deleted: !this.Video,
322 blacklisted: this.Video && this.Video.isBlacklisted(),
323 thumbnailPath: this.Video?.getMiniatureStaticPath(),
5fd4ca00 324 channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel
19a3b914 325 },
5fd4ca00
RK
326 createdAt: this.createdAt,
327 updatedAt: this.updatedAt,
328 count: countReportsForVideo || 0,
329 nth: nthReportForVideo || 0,
efa012ed
RK
330 countReportsForReporter: (countReportsForReporterVideo || 0) + (countReportsForReporterDeletedVideo || 0),
331 countReportsForReportee: (countReportsForReporteeVideo || 0) + (countReportsForReporteeDeletedVideo || 0)
3fd3ab2d
C
332 }
333 }
334
453e83ea 335 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
3fd3ab2d
C
336 return {
337 type: 'Flag' as 'Flag',
338 content: this.reason,
339 object: this.Video.url
340 }
341 }
268eebed
C
342
343 private static getStateLabel (id: number) {
344 return VIDEO_ABUSE_STATES[id] || 'Unknown'
345 }
55fa55a9 346}