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