]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/moderation.ts
Merge branch 'release/4.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / moderation.ts
1 import { VideoUploadFile } from 'express'
2 import { PathLike } from 'fs-extra'
3 import { Transaction } from 'sequelize/types'
4 import { AbuseAuditView, auditLoggerFactory } from '@server/helpers/audit-logger'
5 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
6 import { logger } from '@server/helpers/logger'
7 import { AbuseModel } from '@server/models/abuse/abuse'
8 import { VideoAbuseModel } from '@server/models/abuse/video-abuse'
9 import { VideoCommentAbuseModel } from '@server/models/abuse/video-comment-abuse'
10 import { VideoFileModel } from '@server/models/video/video-file'
11 import { FilteredModelAttributes } from '@server/types'
12 import {
13 MAbuseFull,
14 MAccountDefault,
15 MAccountLight,
16 MCommentAbuseAccountVideo,
17 MCommentOwnerVideo,
18 MUser,
19 MVideoAbuseVideoFull,
20 MVideoAccountLightBlacklistAllFiles
21 } from '@server/types/models'
22 import { ActivityCreate } from '../../shared/models/activitypub'
23 import { VideoObject } from '../../shared/models/activitypub/objects'
24 import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object'
25 import { LiveVideoCreate, VideoCreate, VideoImportCreate } from '../../shared/models/videos'
26 import { VideoCommentCreate } from '../../shared/models/videos/comment'
27 import { ActorModel } from '../models/actor/actor'
28 import { UserModel } from '../models/user/user'
29 import { VideoModel } from '../models/video/video'
30 import { VideoCommentModel } from '../models/video/video-comment'
31 import { sendAbuse } from './activitypub/send/send-flag'
32 import { Notifier } from './notifier'
33
34 export type AcceptResult = {
35 accepted: boolean
36 errorMessage?: string
37 }
38
39 // Can be filtered by plugins
40 function isLocalVideoAccepted (object: {
41 videoBody: VideoCreate
42 videoFile: VideoUploadFile
43 user: UserModel
44 }): AcceptResult {
45 return { accepted: true }
46 }
47
48 function isLocalLiveVideoAccepted (object: {
49 liveVideoBody: LiveVideoCreate
50 user: UserModel
51 }): AcceptResult {
52 return { accepted: true }
53 }
54
55 function isLocalVideoThreadAccepted (_object: {
56 commentBody: VideoCommentCreate
57 video: VideoModel
58 user: UserModel
59 }): AcceptResult {
60 return { accepted: true }
61 }
62
63 function isLocalVideoCommentReplyAccepted (_object: {
64 commentBody: VideoCommentCreate
65 parentComment: VideoCommentModel
66 video: VideoModel
67 user: UserModel
68 }): AcceptResult {
69 return { accepted: true }
70 }
71
72 function isRemoteVideoAccepted (_object: {
73 activity: ActivityCreate
74 videoAP: VideoObject
75 byActor: ActorModel
76 }): AcceptResult {
77 return { accepted: true }
78 }
79
80 function isRemoteVideoCommentAccepted (_object: {
81 activity: ActivityCreate
82 commentAP: VideoCommentObject
83 byActor: ActorModel
84 }): AcceptResult {
85 return { accepted: true }
86 }
87
88 function isPreImportVideoAccepted (object: {
89 videoImportBody: VideoImportCreate
90 user: MUser
91 }): AcceptResult {
92 return { accepted: true }
93 }
94
95 function isPostImportVideoAccepted (object: {
96 videoFilePath: PathLike
97 videoFile: VideoFileModel
98 user: MUser
99 }): AcceptResult {
100 return { accepted: true }
101 }
102
103 async function createVideoAbuse (options: {
104 baseAbuse: FilteredModelAttributes<AbuseModel>
105 videoInstance: MVideoAccountLightBlacklistAllFiles
106 startAt: number
107 endAt: number
108 transaction: Transaction
109 reporterAccount: MAccountDefault
110 skipNotification: boolean
111 }) {
112 const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount, skipNotification } = options
113
114 const associateFun = async (abuseInstance: MAbuseFull) => {
115 const videoAbuseInstance: MVideoAbuseVideoFull = await VideoAbuseModel.create({
116 abuseId: abuseInstance.id,
117 videoId: videoInstance.id,
118 startAt: startAt,
119 endAt: endAt
120 }, { transaction })
121
122 videoAbuseInstance.Video = videoInstance
123 abuseInstance.VideoAbuse = videoAbuseInstance
124
125 return { isOwned: videoInstance.isOwned() }
126 }
127
128 return createAbuse({
129 base: baseAbuse,
130 reporterAccount,
131 flaggedAccount: videoInstance.VideoChannel.Account,
132 transaction,
133 skipNotification,
134 associateFun
135 })
136 }
137
138 function createVideoCommentAbuse (options: {
139 baseAbuse: FilteredModelAttributes<AbuseModel>
140 commentInstance: MCommentOwnerVideo
141 transaction: Transaction
142 reporterAccount: MAccountDefault
143 skipNotification: boolean
144 }) {
145 const { baseAbuse, commentInstance, transaction, reporterAccount, skipNotification } = options
146
147 const associateFun = async (abuseInstance: MAbuseFull) => {
148 const commentAbuseInstance: MCommentAbuseAccountVideo = await VideoCommentAbuseModel.create({
149 abuseId: abuseInstance.id,
150 videoCommentId: commentInstance.id
151 }, { transaction })
152
153 commentAbuseInstance.VideoComment = commentInstance
154 abuseInstance.VideoCommentAbuse = commentAbuseInstance
155
156 return { isOwned: commentInstance.isOwned() }
157 }
158
159 return createAbuse({
160 base: baseAbuse,
161 reporterAccount,
162 flaggedAccount: commentInstance.Account,
163 transaction,
164 skipNotification,
165 associateFun
166 })
167 }
168
169 function createAccountAbuse (options: {
170 baseAbuse: FilteredModelAttributes<AbuseModel>
171 accountInstance: MAccountDefault
172 transaction: Transaction
173 reporterAccount: MAccountDefault
174 skipNotification: boolean
175 }) {
176 const { baseAbuse, accountInstance, transaction, reporterAccount, skipNotification } = options
177
178 const associateFun = () => {
179 return Promise.resolve({ isOwned: accountInstance.isOwned() })
180 }
181
182 return createAbuse({
183 base: baseAbuse,
184 reporterAccount,
185 flaggedAccount: accountInstance,
186 transaction,
187 skipNotification,
188 associateFun
189 })
190 }
191
192 export {
193 isLocalLiveVideoAccepted,
194
195 isLocalVideoAccepted,
196 isLocalVideoThreadAccepted,
197 isRemoteVideoAccepted,
198 isRemoteVideoCommentAccepted,
199 isLocalVideoCommentReplyAccepted,
200 isPreImportVideoAccepted,
201 isPostImportVideoAccepted,
202
203 createAbuse,
204 createVideoAbuse,
205 createVideoCommentAbuse,
206 createAccountAbuse
207 }
208
209 // ---------------------------------------------------------------------------
210
211 async function createAbuse (options: {
212 base: FilteredModelAttributes<AbuseModel>
213 reporterAccount: MAccountDefault
214 flaggedAccount: MAccountLight
215 associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean} >
216 skipNotification: boolean
217 transaction: Transaction
218 }) {
219 const { base, reporterAccount, flaggedAccount, associateFun, transaction, skipNotification } = options
220 const auditLogger = auditLoggerFactory('abuse')
221
222 const abuseAttributes = Object.assign({}, base, { flaggedAccountId: flaggedAccount.id })
223 const abuseInstance: MAbuseFull = await AbuseModel.create(abuseAttributes, { transaction })
224
225 abuseInstance.ReporterAccount = reporterAccount
226 abuseInstance.FlaggedAccount = flaggedAccount
227
228 const { isOwned } = await associateFun(abuseInstance)
229
230 if (isOwned === false) {
231 sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction)
232 }
233
234 const abuseJSON = abuseInstance.toFormattedAdminJSON()
235 auditLogger.create(reporterAccount.Actor.getIdentifier(), new AbuseAuditView(abuseJSON))
236
237 if (!skipNotification) {
238 afterCommitIfTransaction(transaction, () => {
239 Notifier.Instance.notifyOnNewAbuse({
240 abuse: abuseJSON,
241 abuseInstance,
242 reporter: reporterAccount.Actor.getIdentifier()
243 })
244 })
245 }
246
247 logger.info('Abuse report %d created.', abuseInstance.id)
248
249 return abuseJSON
250 }