PeerTube/server/controllers/api/videos/abuse.ts

159 lines
5.3 KiB
TypeScript
Raw Normal View History

2017-06-05 21:53:49 +02:00
import * as express from 'express'
import { UserRight, VideoAbuseCreate, VideoAbuseState, VideoAbuse } from '../../../../shared'
2017-12-28 11:16:08 +01:00
import { logger } from '../../../helpers/logger'
2020-04-23 09:32:53 +02:00
import { getFormattedObjects } from '../../../helpers/utils'
2020-05-07 14:58:24 +02:00
import { sequelizeTypescript } from '../../../initializers/database'
2017-05-15 22:22:03 +02:00
import {
2018-06-13 14:27:40 +02:00
asyncMiddleware,
asyncRetryTransactionMiddleware,
authenticate,
ensureUserHasRight,
paginationValidator,
setDefaultPagination,
setDefaultSort,
videoAbuseGetValidator,
2018-06-13 14:27:40 +02:00
videoAbuseReportValidator,
videoAbusesSortValidator,
2020-05-06 17:39:07 +02:00
videoAbuseUpdateValidator,
videoAbuseListValidator
2017-05-15 22:22:03 +02:00
} from '../../../middlewares'
2017-12-12 17:53:50 +01:00
import { AccountModel } from '../../../models/account/account'
import { VideoAbuseModel } from '../../../models/video/video-abuse'
import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger'
2018-12-26 10:36:24 +01:00
import { Notifier } from '../../../lib/notifier'
import { sendVideoAbuse } from '../../../lib/activitypub/send/send-flag'
2019-08-15 11:53:26 +02:00
import { MVideoAbuseAccountVideo } from '../../../typings/models/video'
2020-04-23 09:32:53 +02:00
import { getServerActor } from '@server/models/application/application'
import { MAccountDefault } from '@server/typings/models'
2017-05-15 22:22:03 +02:00
const auditLogger = auditLoggerFactory('abuse')
2017-05-15 22:22:03 +02:00
const abuseVideoRouter = express.Router()
abuseVideoRouter.get('/abuse',
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
2017-05-15 22:22:03 +02:00
paginationValidator,
videoAbusesSortValidator,
2018-01-17 10:50:33 +01:00
setDefaultSort,
setDefaultPagination,
2020-05-06 17:39:07 +02:00
videoAbuseListValidator,
2017-10-25 11:55:06 +02:00
asyncMiddleware(listVideoAbuses)
2017-05-05 16:53:35 +02:00
)
abuseVideoRouter.put('/:videoId/abuse/:id',
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
asyncMiddleware(videoAbuseUpdateValidator),
asyncRetryTransactionMiddleware(updateVideoAbuse)
)
abuseVideoRouter.post('/:videoId/abuse',
2017-05-15 22:22:03 +02:00
authenticate,
2017-11-27 17:30:46 +01:00
asyncMiddleware(videoAbuseReportValidator),
2018-06-13 14:27:40 +02:00
asyncRetryTransactionMiddleware(reportVideoAbuse)
2017-05-05 16:53:35 +02:00
)
abuseVideoRouter.delete('/:videoId/abuse/:id',
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
asyncMiddleware(videoAbuseGetValidator),
asyncRetryTransactionMiddleware(deleteVideoAbuse)
)
2017-05-05 16:53:35 +02:00
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
abuseVideoRouter
}
2017-05-05 16:53:35 +02:00
// ---------------------------------------------------------------------------
async function listVideoAbuses (req: express.Request, res: express.Response) {
2019-08-29 14:31:04 +02:00
const user = res.locals.oauth.token.user
const serverActor = await getServerActor()
const resultList = await VideoAbuseModel.listForApi({
start: req.query.start,
count: req.query.count,
sort: req.query.sort,
2020-05-06 17:39:07 +02:00
id: req.query.id,
search: req.query.search,
2020-05-06 17:39:07 +02:00
state: req.query.state,
videoIs: req.query.videoIs,
searchReporter: req.query.searchReporter,
searchReportee: req.query.searchReportee,
searchVideo: req.query.searchVideo,
searchVideoChannel: req.query.searchVideoChannel,
2019-08-29 14:31:04 +02:00
serverAccountId: serverActor.Account.id,
user
})
2017-10-25 11:55:06 +02:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
2017-05-05 16:53:35 +02:00
}
async function updateVideoAbuse (req: express.Request, res: express.Response) {
2019-03-19 10:35:15 +01:00
const videoAbuse = res.locals.videoAbuse
if (req.body.moderationComment !== undefined) videoAbuse.moderationComment = req.body.moderationComment
if (req.body.state !== undefined) videoAbuse.state = req.body.state
await sequelizeTypescript.transaction(t => {
return videoAbuse.save({ transaction: t })
})
// Do not send the delete to other instances, we updated OUR copy of this video abuse
return res.type('json').status(204).end()
}
async function deleteVideoAbuse (req: express.Request, res: express.Response) {
2019-03-19 10:35:15 +01:00
const videoAbuse = res.locals.videoAbuse
await sequelizeTypescript.transaction(t => {
return videoAbuse.destroy({ transaction: t })
})
// Do not send the delete to other instances, we delete OUR copy of this video abuse
return res.type('json').status(204).end()
}
2017-10-25 11:55:06 +02:00
async function reportVideoAbuse (req: express.Request, res: express.Response) {
2019-08-15 11:53:26 +02:00
const videoInstance = res.locals.videoAll
const body: VideoAbuseCreate = req.body
let reporterAccount: MAccountDefault
let videoAbuseJSON: VideoAbuse
2017-05-05 16:53:35 +02:00
const videoAbuseInstance = await sequelizeTypescript.transaction(async t => {
reporterAccount = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
const abuseToCreate = {
reporterAccountId: reporterAccount.id,
reason: body.reason,
videoId: videoInstance.id,
state: VideoAbuseState.PENDING
}
2019-08-15 11:53:26 +02:00
const videoAbuseInstance: MVideoAbuseAccountVideo = await VideoAbuseModel.create(abuseToCreate, { transaction: t })
2017-11-16 17:04:19 +01:00
videoAbuseInstance.Video = videoInstance
videoAbuseInstance.Account = reporterAccount
2017-11-15 15:12:23 +01:00
// We send the video abuse to the origin server
2017-10-25 11:55:06 +02:00
if (videoInstance.isOwned() === false) {
2019-07-29 11:59:29 +02:00
await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t)
2017-10-25 11:55:06 +02:00
}
videoAbuseJSON = videoAbuseInstance.toFormattedJSON()
auditLogger.create(reporterAccount.Actor.getIdentifier(), new VideoAbuseAuditView(videoAbuseJSON))
return videoAbuseInstance
})
2018-06-13 14:27:40 +02:00
Notifier.Instance.notifyOnNewVideoAbuse({
videoAbuse: videoAbuseJSON,
videoAbuseInstance,
reporter: reporterAccount.Actor.getIdentifier()
})
2019-07-29 11:59:29 +02:00
logger.info('Abuse report for video %s created.', videoInstance.name)
return res.json({ videoAbuse: videoAbuseJSON }).end()
2017-05-05 16:53:35 +02:00
}