]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/abuse.ts
Rename Pod -> Server
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / abuse.ts
1 import * as express from 'express'
2
3 import { database as db } from '../../../initializers/database'
4 import {
5 logger,
6 getFormattedObjects,
7 retryTransactionWrapper
8 } from '../../../helpers'
9 import {
10 authenticate,
11 ensureUserHasRight,
12 paginationValidator,
13 videoAbuseReportValidator,
14 videoAbusesSortValidator,
15 setVideoAbusesSort,
16 setPagination,
17 asyncMiddleware
18 } from '../../../middlewares'
19 import { VideoInstance } from '../../../models'
20 import { VideoAbuseCreate, UserRight } from '../../../../shared'
21
22 const abuseVideoRouter = express.Router()
23
24 abuseVideoRouter.get('/abuse',
25 authenticate,
26 ensureUserHasRight(UserRight.MANAGE_VIDEO_ABUSES),
27 paginationValidator,
28 videoAbusesSortValidator,
29 setVideoAbusesSort,
30 setPagination,
31 asyncMiddleware(listVideoAbuses)
32 )
33 abuseVideoRouter.post('/:id/abuse',
34 authenticate,
35 videoAbuseReportValidator,
36 asyncMiddleware(reportVideoAbuseRetryWrapper)
37 )
38
39 // ---------------------------------------------------------------------------
40
41 export {
42 abuseVideoRouter
43 }
44
45 // ---------------------------------------------------------------------------
46
47 async function listVideoAbuses (req: express.Request, res: express.Response, next: express.NextFunction) {
48 const resultList = await db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort)
49
50 return res.json(getFormattedObjects(resultList.data, resultList.total))
51 }
52
53 async function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
54 const options = {
55 arguments: [ req, res ],
56 errorMessage: 'Cannot report abuse to the video with many retries.'
57 }
58
59 await retryTransactionWrapper(reportVideoAbuse, options)
60
61 return res.type('json').status(204).end()
62 }
63
64 async function reportVideoAbuse (req: express.Request, res: express.Response) {
65 const videoInstance = res.locals.video as VideoInstance
66 const reporterUsername = res.locals.oauth.token.User.username
67 const body: VideoAbuseCreate = req.body
68
69 const abuseToCreate = {
70 reporterUsername,
71 reason: body.reason,
72 videoId: videoInstance.id,
73 reporterServerId: null // This is our server that reported this abuse
74 }
75
76 await db.sequelize.transaction(async t => {
77 const abuse = await db.VideoAbuse.create(abuseToCreate, { transaction: t })
78 // We send the information to the destination server
79 if (videoInstance.isOwned() === false) {
80 const reportData = {
81 reporterUsername,
82 reportReason: abuse.reason,
83 videoUUID: videoInstance.uuid
84 }
85
86 // await friends.reportAbuseVideoToFriend(reportData, videoInstance, t)
87 // TODO: send abuse to origin server
88 }
89 })
90
91 logger.info('Abuse report for video %s created.', videoInstance.name)
92 }