X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fblacklist.ts;h=abd09387cf45b44cde1d71101541ba14b65f6efd;hb=feb34f6b6b991046aab6a10df747b48fa4da07a7;hp=7eee460d4f1e087488e9b7a334b32c42884b40ed;hpb=f05a1c30c15d2ae35c11e241ca039a72eeb7d6ad;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/blacklist.ts b/server/controllers/api/videos/blacklist.ts index 7eee460d4..abd09387c 100644 --- a/server/controllers/api/videos/blacklist.ts +++ b/server/controllers/api/videos/blacklist.ts @@ -1,12 +1,26 @@ import * as express from 'express' -import { BlacklistedVideo, UserRight } from '../../../../shared' +import { UserRight, VideoBlacklistCreate, VideoBlacklistType } 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, + videosBlacklistFiltersValidator, + videosBlacklistRemoveValidator, + videosBlacklistUpdateValidator } from '../../../middlewares' import { VideoBlacklistModel } from '../../../models/video/video-blacklist' +import { sequelizeTypescript } from '../../../initializers' +import { Notifier } from '../../../lib/notifier' +import { sendDeleteVideo } from '../../../lib/activitypub/send' +import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' +import { MVideoBlacklistVideo } from '@server/typings/models' const blacklistRouter = express.Router() @@ -24,9 +38,17 @@ blacklistRouter.get('/blacklist', blacklistSortValidator, setBlacklistSort, setDefaultPagination, + videosBlacklistFiltersValidator, 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,34 +64,85 @@ export { // --------------------------------------------------------------------------- -async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) { - const videoInstance = res.locals.video +async function addVideoToBlacklist (req: express.Request, res: express.Response) { + const videoInstance = res.locals.videoAll + const body: VideoBlacklistCreate = req.body const toCreate = { - videoId: videoInstance.id + videoId: videoInstance.id, + unfederated: body.unfederate === true, + reason: body.reason, + type: VideoBlacklistType.MANUAL } - await VideoBlacklistModel.create(toCreate) + const blacklist: MVideoBlacklistVideo = await VideoBlacklistModel.create(toCreate) + blacklist.Video = videoInstance + + if (body.unfederate === true) { + await sendDeleteVideo(videoInstance, undefined) + } + + Notifier.Instance.notifyOnVideoBlacklist(blacklist) + + logger.info('Video %s blacklisted.', videoInstance.uuid) + 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) +async function updateVideoBlacklistController (req: express.Request, res: express.Response) { + const videoBlacklist = res.locals.videoBlacklist - return res.json(getFormattedObjects(resultList.data, resultList.total)) + 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 removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) { - const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel +async function listBlacklist (req: express.Request, res: express.Response) { + const resultList = await VideoBlacklistModel.listForApi({ + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search, + type: req.query.type + }) + + return res.json(getFormattedObjects(resultList.data, resultList.total)) +} + +async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) { + const videoBlacklist = res.locals.videoBlacklist + const video = res.locals.videoAll + + const videoBlacklistType = await sequelizeTypescript.transaction(async t => { + const unfederated = videoBlacklist.unfederated + const videoBlacklistType = videoBlacklist.type - try { - await blacklistedVideo.destroy() + await videoBlacklist.destroy({ transaction: t }) + video.VideoBlacklist = undefined - logger.info('Video %s removed from blacklist.', res.locals.video.uuid) + // Re federate the video + if (unfederated === true) { + await federateVideoIfNeeded(video, true, t) + } - return res.sendStatus(204) - } catch (err) { - logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err) - throw err + return videoBlacklistType + }) + + Notifier.Instance.notifyOnVideoUnblacklist(video) + + if (videoBlacklistType === VideoBlacklistType.AUTO_BEFORE_PUBLISHED) { + Notifier.Instance.notifyOnVideoPublishedAfterRemovedFromAutoBlacklist(video) + + // Delete on object so new video notifications will send + delete video.VideoBlacklist + Notifier.Instance.notifyOnNewVideoIfNeeded(video) } + + logger.info('Video %s removed from blacklist.', video.uuid) + + return res.type('json').status(204).end() }