1
0
Fork 0
peertube/server/core/controllers/api/videos/blacklist.ts

113 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-08-27 12:32:44 +00:00
import express from 'express'
import { blacklistVideo, unblacklistVideo } from '@server/lib/video-blacklist.js'
import { HttpStatusCode, UserRight, VideoBlacklistCreate } from '@peertube/peertube-models'
import { logger } from '../../../helpers/logger.js'
import { getFormattedObjects } from '../../../helpers/utils.js'
import { sequelizeTypescript } from '../../../initializers/database.js'
2017-05-15 20:22:03 +00:00
import {
2018-08-13 14:57:13 +00:00
asyncMiddleware,
authenticate,
blacklistSortValidator,
ensureUserHasRight,
openapiOperationDoc,
2018-08-13 14:57:13 +00:00
paginationValidator,
setBlacklistSort,
setDefaultPagination,
videosBlacklistAddValidator,
2019-08-15 09:53:26 +00:00
videosBlacklistFiltersValidator,
2018-08-13 14:57:13 +00:00
videosBlacklistRemoveValidator,
2019-08-15 09:53:26 +00:00
videosBlacklistUpdateValidator
} from '../../../middlewares/index.js'
import { VideoBlacklistModel } from '../../../models/video/video-blacklist.js'
2017-05-15 20:22:03 +00:00
const blacklistRouter = express.Router()
2017-10-10 08:02:18 +00:00
blacklistRouter.post('/:videoId/blacklist',
openapiOperationDoc({ operationId: 'addVideoBlock' }),
2017-05-15 20:22:03 +00:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
2017-11-27 16:30:46 +00:00
asyncMiddleware(videosBlacklistAddValidator),
2020-05-07 12:58:24 +00:00
asyncMiddleware(addVideoToBlacklistController)
2017-05-05 14:53:35 +00:00
)
2017-10-10 08:02:18 +00:00
blacklistRouter.get('/blacklist',
openapiOperationDoc({ operationId: 'getVideoBlocks' }),
2017-10-10 08:02:18 +00:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
2017-10-10 08:02:18 +00:00
paginationValidator,
blacklistSortValidator,
setBlacklistSort,
setDefaultPagination,
videosBlacklistFiltersValidator,
2017-10-25 09:55:06 +00:00
asyncMiddleware(listBlacklist)
2017-10-10 08:02:18 +00:00
)
2018-08-13 14:57:13 +00:00
blacklistRouter.put('/:videoId/blacklist',
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
asyncMiddleware(videosBlacklistUpdateValidator),
asyncMiddleware(updateVideoBlacklistController)
)
2017-10-10 08:02:18 +00:00
blacklistRouter.delete('/:videoId/blacklist',
openapiOperationDoc({ operationId: 'delVideoBlock' }),
2017-10-10 08:02:18 +00:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
2017-11-27 16:30:46 +00:00
asyncMiddleware(videosBlacklistRemoveValidator),
2017-10-25 09:55:06 +00:00
asyncMiddleware(removeVideoFromBlacklistController)
2017-10-10 08:02:18 +00:00
)
2017-05-05 14:53:35 +00:00
// ---------------------------------------------------------------------------
2017-05-15 20:22:03 +00:00
export {
blacklistRouter
}
2017-05-05 14:53:35 +00:00
// ---------------------------------------------------------------------------
2020-05-07 12:58:24 +00:00
async function addVideoToBlacklistController (req: express.Request, res: express.Response) {
2019-08-15 09:53:26 +00:00
const videoInstance = res.locals.videoAll
2018-08-13 14:57:13 +00:00
const body: VideoBlacklistCreate = req.body
2017-05-05 14:53:35 +00:00
2020-05-07 12:58:24 +00:00
await blacklistVideo(videoInstance, body)
2018-12-26 09:36:24 +00:00
2019-08-15 09:53:26 +00:00
logger.info('Video %s blacklisted.', videoInstance.uuid)
2018-12-26 09:36:24 +00:00
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
2017-05-05 14:53:35 +00:00
}
2017-10-10 08:02:18 +00:00
2018-08-13 14:57:13 +00:00
async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
2019-03-19 09:35:15 +00:00
const videoBlacklist = res.locals.videoBlacklist
2018-08-13 14:57:13 +00:00
if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
await sequelizeTypescript.transaction(t => {
return videoBlacklist.save({ transaction: t })
})
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
2018-08-13 14:57:13 +00:00
}
2019-07-23 07:48:48 +00:00
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
})
2017-10-25 09:55:06 +00:00
2019-07-23 07:48:48 +00:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
2017-10-10 08:02:18 +00:00
}
2019-07-23 07:48:48 +00:00
async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
2019-03-19 09:35:15 +00:00
const videoBlacklist = res.locals.videoBlacklist
2019-08-15 09:53:26 +00:00
const video = res.locals.videoAll
2017-10-10 08:02:18 +00:00
2020-05-07 12:58:24 +00:00
await unblacklistVideo(videoBlacklist, video)
2019-08-15 09:53:26 +00:00
logger.info('Video %s removed from blacklist.', video.uuid)
2017-10-25 09:55:06 +00:00
return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
2017-10-10 08:02:18 +00:00
}