2017-06-05 21:53:49 +02:00
|
|
|
import * as express from 'express'
|
2019-02-26 10:55:40 +01:00
|
|
|
import { UserRight, VideoBlacklist, VideoBlacklistCreate } from '../../../../shared'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { getFormattedObjects } from '../../../helpers/utils'
|
2017-05-15 22:22:03 +02:00
|
|
|
import {
|
2018-08-13 16:57:13 +02:00
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
blacklistSortValidator,
|
|
|
|
ensureUserHasRight,
|
|
|
|
paginationValidator,
|
|
|
|
setBlacklistSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
videosBlacklistAddValidator,
|
|
|
|
videosBlacklistRemoveValidator,
|
|
|
|
videosBlacklistUpdateValidator
|
2017-05-15 22:22:03 +02:00
|
|
|
} from '../../../middlewares'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
|
2018-08-13 16:57:13 +02:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
2018-12-26 10:36:24 +01:00
|
|
|
import { Notifier } from '../../../lib/notifier'
|
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2019-02-26 10:55:40 +01:00
|
|
|
import { sendDeleteVideo } from '../../../lib/activitypub/send'
|
2019-01-10 15:39:51 +01:00
|
|
|
import { federateVideoIfNeeded } from '../../../lib/activitypub'
|
2017-05-15 22:22:03 +02:00
|
|
|
|
|
|
|
const blacklistRouter = express.Router()
|
|
|
|
|
2017-10-10 10:02:18 +02:00
|
|
|
blacklistRouter.post('/:videoId/blacklist',
|
2017-05-15 22:22:03 +02:00
|
|
|
authenticate,
|
2017-10-27 16:55:03 +02:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-11-27 17:30:46 +01:00
|
|
|
asyncMiddleware(videosBlacklistAddValidator),
|
2017-10-25 11:55:06 +02:00
|
|
|
asyncMiddleware(addVideoToBlacklist)
|
2017-05-05 16:53:35 +02:00
|
|
|
)
|
|
|
|
|
2017-10-10 10:02:18 +02:00
|
|
|
blacklistRouter.get('/blacklist',
|
|
|
|
authenticate,
|
2017-10-27 16:55:03 +02:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-10-10 10:02:18 +02:00
|
|
|
paginationValidator,
|
|
|
|
blacklistSortValidator,
|
|
|
|
setBlacklistSort,
|
2018-01-18 10:53:54 +01:00
|
|
|
setDefaultPagination,
|
2017-10-25 11:55:06 +02:00
|
|
|
asyncMiddleware(listBlacklist)
|
2017-10-10 10:02:18 +02:00
|
|
|
)
|
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
blacklistRouter.put('/:videoId/blacklist',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
|
|
|
asyncMiddleware(videosBlacklistUpdateValidator),
|
|
|
|
asyncMiddleware(updateVideoBlacklistController)
|
|
|
|
)
|
|
|
|
|
2017-10-10 10:02:18 +02:00
|
|
|
blacklistRouter.delete('/:videoId/blacklist',
|
|
|
|
authenticate,
|
2017-10-27 16:55:03 +02:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
|
2017-11-27 17:30:46 +01:00
|
|
|
asyncMiddleware(videosBlacklistRemoveValidator),
|
2017-10-25 11:55:06 +02:00
|
|
|
asyncMiddleware(removeVideoFromBlacklistController)
|
2017-10-10 10:02:18 +02:00
|
|
|
)
|
|
|
|
|
2017-05-05 16:53:35 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 22:22:03 +02:00
|
|
|
export {
|
|
|
|
blacklistRouter
|
|
|
|
}
|
2017-05-05 16:53:35 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
async function addVideoToBlacklist (req: express.Request, res: express.Response) {
|
2017-05-05 16:53:35 +02:00
|
|
|
const videoInstance = res.locals.video
|
2018-08-13 16:57:13 +02:00
|
|
|
const body: VideoBlacklistCreate = req.body
|
2017-05-05 16:53:35 +02:00
|
|
|
|
|
|
|
const toCreate = {
|
2018-08-13 16:57:13 +02:00
|
|
|
videoId: videoInstance.id,
|
2019-01-10 15:39:51 +01:00
|
|
|
unfederated: body.unfederate === true,
|
2018-08-13 16:57:13 +02:00
|
|
|
reason: body.reason
|
2017-05-05 16:53:35 +02:00
|
|
|
}
|
|
|
|
|
2018-12-26 10:36:24 +01:00
|
|
|
const blacklist = await VideoBlacklistModel.create(toCreate)
|
|
|
|
blacklist.Video = videoInstance
|
|
|
|
|
2019-01-10 15:39:51 +01:00
|
|
|
if (body.unfederate === true) {
|
|
|
|
await sendDeleteVideo(videoInstance, undefined)
|
|
|
|
}
|
|
|
|
|
2018-12-26 10:36:24 +01:00
|
|
|
Notifier.Instance.notifyOnVideoBlacklist(blacklist)
|
|
|
|
|
|
|
|
logger.info('Video %s blacklisted.', res.locals.video.uuid)
|
|
|
|
|
2017-10-25 11:55:06 +02:00
|
|
|
return res.type('json').status(204).end()
|
2017-05-05 16:53:35 +02:00
|
|
|
}
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
|
|
|
|
const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:55:06 +02:00
|
|
|
async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 17:53:50 +01:00
|
|
|
const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
|
2017-10-25 11:55:06 +02:00
|
|
|
|
2018-08-14 09:08:47 +02:00
|
|
|
return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total))
|
2017-10-10 10:02:18 +02:00
|
|
|
}
|
|
|
|
|
2017-10-25 11:55:06 +02:00
|
|
|
async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-08-13 16:57:13 +02:00
|
|
|
const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
|
2018-12-26 10:36:24 +01:00
|
|
|
const video: VideoModel = res.locals.video
|
2017-10-10 10:02:18 +02:00
|
|
|
|
2019-01-10 15:39:51 +01:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
const unfederated = videoBlacklist.unfederated
|
|
|
|
await videoBlacklist.destroy({ transaction: t })
|
|
|
|
|
|
|
|
// Re federate the video
|
|
|
|
if (unfederated === true) {
|
|
|
|
await federateVideoIfNeeded(video, true, t)
|
|
|
|
}
|
2018-08-13 16:57:13 +02:00
|
|
|
})
|
2017-10-25 11:55:06 +02:00
|
|
|
|
2018-12-26 10:36:24 +01:00
|
|
|
Notifier.Instance.notifyOnVideoUnblacklist(video)
|
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
|
2017-10-25 11:55:06 +02:00
|
|
|
|
2018-08-13 16:57:13 +02:00
|
|
|
return res.type('json').status(204).end()
|
2017-10-10 10:02:18 +02:00
|
|
|
}
|