]>
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' |
e0a92917 | 12 | import { buildBlockedAccountSQL, getSort, throwIfNotValid, searchAttribute } 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' |
0251197e | 18 | import { literal, Op, Sequelize } 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: { | |
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 | [ | |
78 | literal( | |
79 | '(' + | |
0251197e RK |
80 | 'SELECT count(*) ' + |
81 | 'FROM "videoAbuse" ' + | |
82 | 'WHERE "videoId" = "VideoAbuseModel"."videoId" ' + | |
5fd4ca00 RK |
83 | ')' |
84 | ), | |
85 | 'countReportsForVideo' | |
86 | ], | |
87 | [ | |
88 | literal( | |
89 | '(' + | |
90 | 'SELECT t.nth ' + | |
91 | 'FROM ( ' + | |
92 | 'SELECT id, ' + | |
93 | 'row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' + | |
94 | 'FROM "videoAbuse" ' + | |
95 | ') t ' + | |
96 | 'WHERE t.id = "VideoAbuseModel".id ' + | |
97 | ')' | |
98 | ), | |
99 | 'nthReportForVideo' | |
100 | ], | |
101 | [ | |
102 | literal( | |
103 | '(' + | |
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" ' + | |
110 | ')' | |
111 | ), | |
112 | 'countReportsForReporter' | |
113 | ], | |
114 | [ | |
115 | literal( | |
116 | '(' + | |
0251197e | 117 | 'SELECT count(DISTINCT "videoAbuse"."id") ' + |
5fd4ca00 RK |
118 | 'FROM "videoAbuse" ' + |
119 | 'INNER JOIN "video" ON "video"."id" = "videoAbuse"."videoId" ' + | |
120 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
0251197e | 121 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "Video->VideoChannel"."accountId" ' + |
5fd4ca00 RK |
122 | ')' |
123 | ), | |
124 | 'countReportsForReportee' | |
125 | ] | |
126 | ] | |
127 | }, | |
86521a67 RK |
128 | include: [ |
129 | { | |
844db39e RK |
130 | model: AccountModel, |
131 | required: true, | |
0251197e | 132 | where: searchAttribute(options.searchReporter, 'name') |
86521a67 RK |
133 | }, |
134 | { | |
844db39e RK |
135 | model: VideoModel, |
136 | required: false, | |
0251197e | 137 | where: searchAttribute(options.searchVideo, 'name'), |
86521a67 RK |
138 | include: [ |
139 | { | |
844db39e RK |
140 | model: ThumbnailModel |
141 | }, | |
142 | { | |
e0a92917 | 143 | model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), |
0251197e | 144 | where: searchAttribute(options.searchVideoChannel, 'name') |
844db39e RK |
145 | }, |
146 | { | |
147 | attributes: [ 'id', 'reason', 'unfederated' ], | |
148 | model: VideoBlacklistModel | |
86521a67 RK |
149 | } |
150 | ] | |
86521a67 | 151 | } |
844db39e RK |
152 | ], |
153 | where | |
86521a67 | 154 | } |
844db39e | 155 | } |
86521a67 | 156 | })) |
3fd3ab2d C |
157 | @Table({ |
158 | tableName: 'videoAbuse', | |
159 | indexes: [ | |
55fa55a9 | 160 | { |
3fd3ab2d | 161 | fields: [ 'videoId' ] |
55fa55a9 C |
162 | }, |
163 | { | |
3fd3ab2d | 164 | fields: [ 'reporterAccountId' ] |
55fa55a9 | 165 | } |
e02643f3 | 166 | ] |
3fd3ab2d C |
167 | }) |
168 | export class VideoAbuseModel extends Model<VideoAbuseModel> { | |
e02643f3 | 169 | |
3fd3ab2d | 170 | @AllowNull(false) |
1506307f | 171 | @Default(null) |
3fd3ab2d | 172 | @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason')) |
1506307f | 173 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max)) |
3fd3ab2d | 174 | reason: string |
21e0727a | 175 | |
268eebed C |
176 | @AllowNull(false) |
177 | @Default(null) | |
178 | @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state')) | |
179 | @Column | |
180 | state: VideoAbuseState | |
181 | ||
182 | @AllowNull(true) | |
183 | @Default(null) | |
1735c825 | 184 | @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true)) |
268eebed C |
185 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max)) |
186 | moderationComment: string | |
187 | ||
68d19a0a RK |
188 | @AllowNull(true) |
189 | @Default(null) | |
190 | @Column(DataType.JSONB) | |
5fd4ca00 | 191 | deletedVideo: VideoDetails |
68d19a0a | 192 | |
3fd3ab2d C |
193 | @CreatedAt |
194 | createdAt: Date | |
21e0727a | 195 | |
3fd3ab2d C |
196 | @UpdatedAt |
197 | updatedAt: Date | |
e02643f3 | 198 | |
3fd3ab2d C |
199 | @ForeignKey(() => AccountModel) |
200 | @Column | |
201 | reporterAccountId: number | |
55fa55a9 | 202 | |
3fd3ab2d | 203 | @BelongsTo(() => AccountModel, { |
55fa55a9 | 204 | foreignKey: { |
68d19a0a | 205 | allowNull: true |
55fa55a9 | 206 | }, |
68d19a0a | 207 | onDelete: 'set null' |
55fa55a9 | 208 | }) |
3fd3ab2d C |
209 | Account: AccountModel |
210 | ||
211 | @ForeignKey(() => VideoModel) | |
212 | @Column | |
213 | videoId: number | |
55fa55a9 | 214 | |
3fd3ab2d | 215 | @BelongsTo(() => VideoModel, { |
55fa55a9 | 216 | foreignKey: { |
68d19a0a | 217 | allowNull: true |
55fa55a9 | 218 | }, |
68d19a0a | 219 | onDelete: 'set null' |
55fa55a9 | 220 | }) |
3fd3ab2d C |
221 | Video: VideoModel |
222 | ||
68d19a0a RK |
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 } | |
227 | ||
268eebed C |
228 | const query = { |
229 | where: { | |
230 | id, | |
68d19a0a | 231 | ...videoAttributes |
268eebed C |
232 | } |
233 | } | |
234 | return VideoAbuseModel.findOne(query) | |
235 | } | |
236 | ||
f0a47bc9 | 237 | static listForApi (parameters: { |
a1587156 C |
238 | start: number |
239 | count: number | |
240 | sort: string | |
844db39e | 241 | search?: string |
f0a47bc9 C |
242 | serverAccountId: number |
243 | user?: MUserAccountId | |
244 | }) { | |
844db39e | 245 | const { start, count, sort, search, user, serverAccountId } = parameters |
f0a47bc9 C |
246 | const userAccountId = user ? user.Account.id : undefined |
247 | ||
3fd3ab2d C |
248 | const query = { |
249 | offset: start, | |
250 | limit: count, | |
3bb6c526 | 251 | order: getSort(sort), |
86521a67 RK |
252 | col: 'VideoAbuseModel.id', |
253 | distinct: true | |
3fd3ab2d | 254 | } |
55fa55a9 | 255 | |
844db39e RK |
256 | const filters = { |
257 | search, | |
258 | serverAccountId, | |
259 | userAccountId | |
260 | } | |
261 | ||
262 | return VideoAbuseModel | |
263 | .scope({ method: [ ScopeNames.FOR_API, filters ] }) | |
264 | .findAndCountAll(query) | |
3fd3ab2d C |
265 | .then(({ rows, count }) => { |
266 | return { total: count, data: rows } | |
267 | }) | |
55fa55a9 C |
268 | } |
269 | ||
1ca9f7c3 | 270 | toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse { |
5fd4ca00 RK |
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 | |
275 | ||
68d19a0a RK |
276 | const video = this.Video |
277 | ? this.Video | |
278 | : this.deletedVideo | |
279 | ||
3fd3ab2d C |
280 | return { |
281 | id: this.id, | |
282 | reason: this.reason, | |
19a3b914 | 283 | reporterAccount: this.Account.toFormattedJSON(), |
268eebed C |
284 | state: { |
285 | id: this.state, | |
286 | label: VideoAbuseModel.getStateLabel(this.state) | |
287 | }, | |
288 | moderationComment: this.moderationComment, | |
19a3b914 | 289 | video: { |
68d19a0a RK |
290 | id: video.id, |
291 | uuid: video.uuid, | |
292 | name: video.name, | |
293 | nsfw: video.nsfw, | |
86521a67 RK |
294 | deleted: !this.Video, |
295 | blacklisted: this.Video && this.Video.isBlacklisted(), | |
296 | thumbnailPath: this.Video?.getMiniatureStaticPath(), | |
5fd4ca00 | 297 | channel: this.Video?.VideoChannel.toFormattedJSON() || this.deletedVideo?.channel |
19a3b914 | 298 | }, |
5fd4ca00 RK |
299 | createdAt: this.createdAt, |
300 | updatedAt: this.updatedAt, | |
301 | count: countReportsForVideo || 0, | |
302 | nth: nthReportForVideo || 0, | |
303 | countReportsForReporter: countReportsForReporter || 0, | |
304 | countReportsForReportee: countReportsForReportee || 0 | |
3fd3ab2d C |
305 | } |
306 | } | |
307 | ||
453e83ea | 308 | toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject { |
3fd3ab2d C |
309 | return { |
310 | type: 'Flag' as 'Flag', | |
311 | content: this.reason, | |
312 | object: this.Video.url | |
313 | } | |
314 | } | |
268eebed C |
315 | |
316 | private static getStateLabel (id: number) { | |
317 | return VIDEO_ABUSE_STATES[id] || 'Unknown' | |
318 | } | |
55fa55a9 | 319 | } |