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