1 import * as express from 'express'
2 import { UserRight, VideoAbuseCreate, VideoAbuseState } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { getFormattedObjects } from '../../../helpers/utils'
5 import { sequelizeTypescript } from '../../../initializers/database'
8 asyncRetryTransactionMiddleware,
14 videoAbuseGetValidator,
15 videoAbuseReportValidator,
16 videoAbusesSortValidator,
17 videoAbuseUpdateValidator,
18 videoAbuseListValidator
19 } from '../../../middlewares'
20 import { AccountModel } from '../../../models/account/account'
21 import { VideoAbuseModel } from '../../../models/video/video-abuse'
22 import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger'
23 import { Notifier } from '../../../lib/notifier'
24 import { sendVideoAbuse } from '../../../lib/activitypub/send/send-flag'
25 import { MVideoAbuseAccountVideo } from '../../../typings/models/video'
26 import { getServerActor } from '@server/models/application/application'
28 const auditLogger = auditLoggerFactory('abuse')
29 const abuseVideoRouter = express.Router()
31 abuseVideoRouter.get('/abuse',
33 ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
35 videoAbusesSortValidator,
38 videoAbuseListValidator,
39 asyncMiddleware(listVideoAbuses)
41 abuseVideoRouter.put('/:videoId/abuse/:id',
43 ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
44 asyncMiddleware(videoAbuseUpdateValidator),
45 asyncRetryTransactionMiddleware(updateVideoAbuse)
47 abuseVideoRouter.post('/:videoId/abuse',
49 asyncMiddleware(videoAbuseReportValidator),
50 asyncRetryTransactionMiddleware(reportVideoAbuse)
52 abuseVideoRouter.delete('/:videoId/abuse/:id',
54 ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
55 asyncMiddleware(videoAbuseGetValidator),
56 asyncRetryTransactionMiddleware(deleteVideoAbuse)
59 // ---------------------------------------------------------------------------
65 // ---------------------------------------------------------------------------
67 async function listVideoAbuses (req: express.Request, res: express.Response) {
68 const user = res.locals.oauth.token.user
69 const serverActor = await getServerActor()
71 const resultList = await VideoAbuseModel.listForApi({
72 start: req.query.start,
73 count: req.query.count,
76 search: req.query.search,
77 state: req.query.state,
78 videoIs: req.query.videoIs,
79 searchReporter: req.query.searchReporter,
80 searchReportee: req.query.searchReportee,
81 searchVideo: req.query.searchVideo,
82 searchVideoChannel: req.query.searchVideoChannel,
83 serverAccountId: serverActor.Account.id,
87 return res.json(getFormattedObjects(resultList.data, resultList.total))
90 async function updateVideoAbuse (req: express.Request, res: express.Response) {
91 const videoAbuse = res.locals.videoAbuse
93 if (req.body.moderationComment !== undefined) videoAbuse.moderationComment = req.body.moderationComment
94 if (req.body.state !== undefined) videoAbuse.state = req.body.state
96 await sequelizeTypescript.transaction(t => {
97 return videoAbuse.save({ transaction: t })
100 // Do not send the delete to other instances, we updated OUR copy of this video abuse
102 return res.type('json').status(204).end()
105 async function deleteVideoAbuse (req: express.Request, res: express.Response) {
106 const videoAbuse = res.locals.videoAbuse
108 await sequelizeTypescript.transaction(t => {
109 return videoAbuse.destroy({ transaction: t })
112 // Do not send the delete to other instances, we delete OUR copy of this video abuse
114 return res.type('json').status(204).end()
117 async function reportVideoAbuse (req: express.Request, res: express.Response) {
118 const videoInstance = res.locals.videoAll
119 const body: VideoAbuseCreate = req.body
121 const videoAbuse = await sequelizeTypescript.transaction(async t => {
122 const reporterAccount = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
124 const abuseToCreate = {
125 reporterAccountId: reporterAccount.id,
127 videoId: videoInstance.id,
128 state: VideoAbuseState.PENDING
131 const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
132 videoAbuseInstance.Video = videoInstance
133 videoAbuseInstance.Account = reporterAccount
135 // We send the video abuse to the origin server
136 if (videoInstance.isOwned() === false) {
137 await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t)
140 auditLogger.create(reporterAccount.Actor.getIdentifier(), new VideoAbuseAuditView(videoAbuseInstance.toFormattedJSON()))
142 return videoAbuseInstance
145 Notifier.Instance.notifyOnNewVideoAbuse(videoAbuse)
147 logger.info('Abuse report for video %s created.', videoInstance.name)
149 return res.json({ videoAbuse: videoAbuse.toFormattedJSON() }).end()