]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-abuse.ts
Add search for video, reporter and channel name fields
[github/Chocobozzz/PeerTube.git] / server / models / video / video-abuse.ts
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 } from '../utils'
13 import { VideoModel } from './video'
14 import { VideoAbuseState, Video } 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 { VideoChannelModel } from './video-channel'
21 import { ActorModel } from '../activitypub/actor'
22 import { VideoBlacklistModel } from './video-blacklist'
23
24 export enum ScopeNames {
25 FOR_API = 'FOR_API'
26 }
27
28 @Scopes(() => ({
29 [ScopeNames.FOR_API]: (options: {
30 search?: string
31 searchReporter?: string
32 searchVideo?: string
33 searchVideoChannel?: string
34 serverAccountId: number
35 userAccountId: any
36 }) => {
37 const search = (sourceField, targetField) => sourceField ? ({
38 [targetField]: {
39 [Op.iLike]: `%${sourceField}%`
40 }
41 }) : {}
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 { '$Video.name$': { [Op.iLike]: `%${options.search}%` } }
56 ]
57 },
58 {
59 [Op.and]: [
60 { videoId: { [Op.not]: null } },
61 { '$Video.VideoChannel.name$': { [Op.iLike]: `%${options.search}%` } }
62 ]
63 },
64 {
65 [Op.and]: [
66 { deletedVideo: { [Op.not]: null } },
67 { deletedVideo: { name: { [Op.iLike]: `%${options.search}%` } } }
68 ]
69 },
70 {
71 [Op.and]: [
72 { deletedVideo: { [Op.not]: null } },
73 { deletedVideo: { channel: { displayName: { [Op.iLike]: `%${options.search}%` } } } }
74 ]
75 },
76 { '$Account.name$': { [Op.iLike]: `%${options.search}%` } }
77 ]
78 })
79 }
80
81 console.log(where)
82
83 return {
84 include: [
85 {
86 model: AccountModel,
87 required: true,
88 where: { ...search(options.searchReporter, 'name') }
89 },
90 {
91 model: VideoModel,
92 required: false,
93 where: { ...search(options.searchVideo, 'name') },
94 include: [
95 {
96 model: ThumbnailModel
97 },
98 {
99 model: VideoChannelModel.unscoped(),
100 where: { ...search(options.searchVideoChannel, 'name') },
101 include: [
102 {
103 model: ActorModel
104 }
105 ]
106 },
107 {
108 attributes: [ 'id', 'reason', 'unfederated' ],
109 model: VideoBlacklistModel
110 }
111 ]
112 }
113 ],
114 where
115 }
116 }
117 }))
118 @Table({
119 tableName: 'videoAbuse',
120 indexes: [
121 {
122 fields: [ 'videoId' ]
123 },
124 {
125 fields: [ 'reporterAccountId' ]
126 }
127 ]
128 })
129 export class VideoAbuseModel extends Model<VideoAbuseModel> {
130
131 @AllowNull(false)
132 @Default(null)
133 @Is('VideoAbuseReason', value => throwIfNotValid(value, isVideoAbuseReasonValid, 'reason'))
134 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.REASON.max))
135 reason: string
136
137 @AllowNull(false)
138 @Default(null)
139 @Is('VideoAbuseState', value => throwIfNotValid(value, isVideoAbuseStateValid, 'state'))
140 @Column
141 state: VideoAbuseState
142
143 @AllowNull(true)
144 @Default(null)
145 @Is('VideoAbuseModerationComment', value => throwIfNotValid(value, isVideoAbuseModerationCommentValid, 'moderationComment', true))
146 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_ABUSES.MODERATION_COMMENT.max))
147 moderationComment: string
148
149 @AllowNull(true)
150 @Default(null)
151 @Column(DataType.JSONB)
152 deletedVideo: Video
153
154 @CreatedAt
155 createdAt: Date
156
157 @UpdatedAt
158 updatedAt: Date
159
160 @ForeignKey(() => AccountModel)
161 @Column
162 reporterAccountId: number
163
164 @BelongsTo(() => AccountModel, {
165 foreignKey: {
166 allowNull: true
167 },
168 onDelete: 'set null'
169 })
170 Account: AccountModel
171
172 @ForeignKey(() => VideoModel)
173 @Column
174 videoId: number
175
176 @BelongsTo(() => VideoModel, {
177 foreignKey: {
178 allowNull: true
179 },
180 onDelete: 'set null'
181 })
182 Video: VideoModel
183
184 static loadByIdAndVideoId (id: number, videoId?: number, uuid?: string): Bluebird<MVideoAbuse> {
185 const videoAttributes = {}
186 if (videoId) videoAttributes['videoId'] = videoId
187 if (uuid) videoAttributes['deletedVideo'] = { uuid }
188
189 const query = {
190 where: {
191 id,
192 ...videoAttributes
193 }
194 }
195 return VideoAbuseModel.findOne(query)
196 }
197
198 static listForApi (parameters: {
199 start: number
200 count: number
201 sort: string
202 search?: string
203 serverAccountId: number
204 user?: MUserAccountId
205 }) {
206 const { start, count, sort, search, user, serverAccountId } = parameters
207 const userAccountId = user ? user.Account.id : undefined
208
209 const query = {
210 offset: start,
211 limit: count,
212 order: getSort(sort),
213 col: 'VideoAbuseModel.id',
214 distinct: true
215 }
216
217 const filters = {
218 search,
219 serverAccountId,
220 userAccountId
221 }
222
223 return VideoAbuseModel
224 .scope({ method: [ ScopeNames.FOR_API, filters ] })
225 .findAndCountAll(query)
226 .then(({ rows, count }) => {
227 return { total: count, data: rows }
228 })
229 }
230
231 toFormattedJSON (this: MVideoAbuseFormattable): VideoAbuse {
232 const video = this.Video
233 ? this.Video
234 : this.deletedVideo
235
236 return {
237 id: this.id,
238 reason: this.reason,
239 reporterAccount: this.Account.toFormattedJSON(),
240 state: {
241 id: this.state,
242 label: VideoAbuseModel.getStateLabel(this.state)
243 },
244 moderationComment: this.moderationComment,
245 video: {
246 id: video.id,
247 uuid: video.uuid,
248 name: video.name,
249 nsfw: video.nsfw,
250 deleted: !this.Video,
251 blacklisted: this.Video && this.Video.isBlacklisted(),
252 thumbnailPath: this.Video?.getMiniatureStaticPath(),
253 channel: this.Video?.VideoChannel.toFormattedSummaryJSON() || this.deletedVideo?.channel
254 },
255 createdAt: this.createdAt
256 }
257 }
258
259 toActivityPubObject (this: MVideoAbuseVideo): VideoAbuseObject {
260 return {
261 type: 'Flag' as 'Flag',
262 content: this.reason,
263 object: this.Video.url
264 }
265 }
266
267 private static getStateLabel (id: number) {
268 return VIDEO_ABUSE_STATES[id] || 'Unknown'
269 }
270 }