X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fabuse.ts;h=9c4d008496c247ddadd92e92fc112cddc4540356;hb=403c69c5a34e6db621f30c7b2bfb2b80dc8e74c1;hp=68db025b7f3bf67a0ec079b61039bdd5fdd01171;hpb=4d4e5cd4dca78480ec7f40e747f424cd107376a4;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index 68db025b7..9c4d00849 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts @@ -1,41 +1,53 @@ import * as express from 'express' -import { waterfall } from 'async' - -import { database as db } from '../../../initializers/database' -import * as friends from '../../../lib/friends' -import { - logger, - getFormatedObjects, - retryTransactionWrapper, - startSerializableTransaction, - commitTransaction, - rollbackTransaction -} from '../../../helpers' +import { AbuseModel } from '@server/models/abuse/abuse' +import { getServerActor } from '@server/models/application/application' +import { AbuseCreate, UserRight, VideoAbuseCreate } from '../../../../shared' import { + abusesSortValidator, + asyncMiddleware, + asyncRetryTransactionMiddleware, authenticate, - ensureIsAdmin, + ensureUserHasRight, paginationValidator, + setDefaultPagination, + setDefaultSort, + videoAbuseGetValidator, + videoAbuseListValidator, videoAbuseReportValidator, - videoAbusesSortValidator, - setVideoAbusesSort, - setPagination + videoAbuseUpdateValidator } from '../../../middlewares' +import { deleteAbuse, reportAbuse, updateAbuse } from '../abuse' + +// FIXME: deprecated in 2.3. Remove this controller const abuseVideoRouter = express.Router() abuseVideoRouter.get('/abuse', authenticate, - ensureIsAdmin, + ensureUserHasRight(UserRight.MANAGE_ABUSES), paginationValidator, - videoAbusesSortValidator, - setVideoAbusesSort, - setPagination, - listVideoAbuses + abusesSortValidator, + setDefaultSort, + setDefaultPagination, + videoAbuseListValidator, + asyncMiddleware(listVideoAbuses) ) -abuseVideoRouter.post('/:id/abuse', +abuseVideoRouter.put('/:videoId/abuse/:id', authenticate, - videoAbuseReportValidator, - reportVideoAbuseRetryWrapper + ensureUserHasRight(UserRight.MANAGE_ABUSES), + asyncMiddleware(videoAbuseUpdateValidator), + asyncRetryTransactionMiddleware(updateVideoAbuse) +) +abuseVideoRouter.post('/:videoId/abuse', + authenticate, + asyncMiddleware(videoAbuseReportValidator), + asyncRetryTransactionMiddleware(reportVideoAbuse) +) +abuseVideoRouter.delete('/:videoId/abuse/:id', + authenticate, + ensureUserHasRight(UserRight.MANAGE_ABUSES), + asyncMiddleware(videoAbuseGetValidator), + asyncRetryTransactionMiddleware(deleteVideoAbuse) ) // --------------------------------------------------------------------------- @@ -46,72 +58,57 @@ export { // --------------------------------------------------------------------------- -function listVideoAbuses (req, res, next) { - db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) { - if (err) return next(err) - - res.json(getFormatedObjects(abusesList, abusesTotal)) +async function listVideoAbuses (req: express.Request, res: express.Response) { + const user = res.locals.oauth.token.user + const serverActor = await getServerActor() + + const resultList = await AbuseModel.listForAdminApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + id: req.query.id, + filter: 'video', + predefinedReason: req.query.predefinedReason, + search: req.query.search, + state: req.query.state, + videoIs: req.query.videoIs, + searchReporter: req.query.searchReporter, + searchReportee: req.query.searchReportee, + searchVideo: req.query.searchVideo, + searchVideoChannel: req.query.searchVideoChannel, + serverAccountId: serverActor.Account.id, + user }) -} -function reportVideoAbuseRetryWrapper (req, res, next) { - const options = { - arguments: [ req, res ], - errorMessage: 'Cannot report abuse to the video with many retries.' - } - - retryTransactionWrapper(reportVideoAbuse, options, function (err) { - if (err) return next(err) - - return res.type('json').status(204).end() + return res.json({ + total: resultList.total, + data: resultList.data.map(d => d.toFormattedAdminJSON()) }) } -function reportVideoAbuse (req, res, finalCallback) { - const videoInstance = res.locals.video - const reporterUsername = res.locals.oauth.token.User.username - - const abuse = { - reporterUsername, - reason: req.body.reason, - videoId: videoInstance.id, - reporterPodId: null // This is our pod that reported this abuse - } - - waterfall([ - - startSerializableTransaction, - - function createAbuse (t, callback) { - db.VideoAbuse.create(abuse).asCallback(function (err, abuse) { - return callback(err, t, abuse) - }) - }, +async function updateVideoAbuse (req: express.Request, res: express.Response) { + return updateAbuse(req, res) +} - function sendToFriendsIfNeeded (t, abuse, callback) { - // We send the information to the destination pod - if (videoInstance.isOwned() === false) { - const reportData = { - reporterUsername, - reportReason: abuse.reason, - videoRemoteId: videoInstance.remoteId - } +async function deleteVideoAbuse (req: express.Request, res: express.Response) { + return deleteAbuse(req, res) +} - friends.reportAbuseVideoToFriend(reportData, videoInstance) - } +async function reportVideoAbuse (req: express.Request, res: express.Response) { + const oldBody = req.body as VideoAbuseCreate - return callback(null, t) - }, + req.body = { + accountId: res.locals.videoAll.VideoChannel.accountId, - commitTransaction + reason: oldBody.reason, + predefinedReasons: oldBody.predefinedReasons, - ], function andFinally (err, t) { - if (err) { - logger.debug('Cannot update the video.', { error: err }) - return rollbackTransaction(err, t, finalCallback) + video: { + id: res.locals.videoAll.id, + startAt: oldBody.startAt, + endAt: oldBody.endAt } + } as AbuseCreate - logger.info('Abuse report for video %s created.', videoInstance.name) - return finalCallback(null) - }) + return reportAbuse(req, res) }