From 26b7305a232e547709f433a6edf700bf495935d8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 13 Aug 2018 16:57:13 +0200 Subject: Add blacklist reason field --- server/controllers/api/users.ts | 6 ++-- server/controllers/api/videos/blacklist.ts | 56 ++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 17 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 0e2be7123..543b20baa 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -201,14 +201,14 @@ async function getUserVideos (req: express.Request, res: express.Response, next: user.Account.id, req.query.start as number, req.query.count as number, - req.query.sort as VideoSortField, - false // Display my NSFW videos + req.query.sort as VideoSortField ) const additionalAttributes = { waitTranscoding: true, state: true, - scheduledUpdate: true + scheduledUpdate: true, + blacklistInfo: true } return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) } diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 8112b59b8..358f339ed 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,12 +1,21 @@ import * as express from 'express' -import { BlacklistedVideo, UserRight } from '../../../../shared' +import { BlacklistedVideo, UserRight, VideoBlacklistCreate } from '../../../../shared' import { logger } from '../../../helpers/logger' import { getFormattedObjects } from '../../../helpers/utils' import { - asyncMiddleware, authenticate, blacklistSortValidator, ensureUserHasRight, paginationValidator, setBlacklistSort, setDefaultPagination, - videosBlacklistAddValidator, videosBlacklistRemoveValidator + asyncMiddleware, + authenticate, + blacklistSortValidator, + ensureUserHasRight, + paginationValidator, + setBlacklistSort, + setDefaultPagination, + videosBlacklistAddValidator, + videosBlacklistRemoveValidator, + videosBlacklistUpdateValidator } from '../../../middlewares' import { VideoBlacklistModel } from '../../../models/video/video-blacklist' +import { sequelizeTypescript } from '../../../initializers' const blacklistRouter = express.Router() @@ -27,6 +36,13 @@ blacklistRouter.get('/blacklist', asyncMiddleware(listBlacklist) ) +blacklistRouter.put('/:videoId/blacklist', + authenticate, + ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), + asyncMiddleware(videosBlacklistUpdateValidator), + asyncMiddleware(updateVideoBlacklistController) +) + blacklistRouter.delete('/:videoId/blacklist', authenticate, ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST), @@ -42,17 +58,32 @@ export { // --------------------------------------------------------------------------- -async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { +async function addVideoToBlacklist (req: express.Request, res: express.Response) { const videoInstance = res.locals.video + const body: VideoBlacklistCreate = req.body const toCreate = { - videoId: videoInstance.id + videoId: videoInstance.id, + reason: body.reason } await VideoBlacklistModel.create(toCreate) return res.type('json').status(204).end() } +async function updateVideoBlacklistController (req: express.Request, res: express.Response) { + const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel + logger.info(videoBlacklist) + + if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason + + await sequelizeTypescript.transaction(t => { + return videoBlacklist.save({ transaction: t }) + }) + + return res.type('json').status(204).end() +} + async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort) @@ -60,16 +91,13 @@ async function listBlacklist (req: express.Request, res: express.Response, next: } async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { - const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel + const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel - try { - await blacklistedVideo.destroy() + await sequelizeTypescript.transaction(t => { + return videoBlacklist.destroy({ transaction: t }) + }) - logger.info('Video %s removed from blacklist.', res.locals.video.uuid) + logger.info('Video %s removed from blacklist.', res.locals.video.uuid) - return res.sendStatus(204) - } catch (err) { - logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, { err }) - throw err - } + return res.type('json').status(204).end() } -- cgit v1.2.3