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

104 lines
3.2 KiB
TypeScript
Raw Normal View History

2017-06-05 15:53:49 -04:00
import * as express from 'express'
2018-08-14 03:08:47 -04:00
import { VideoBlacklist, UserRight, VideoBlacklistCreate } from '../../../../shared'
2017-12-28 05:16:08 -05:00
import { logger } from '../../../helpers/logger'
import { getFormattedObjects } from '../../../helpers/utils'
2017-05-15 16:22:03 -04:00
import {
2018-08-13 10:57:13 -04:00
asyncMiddleware,
authenticate,
blacklistSortValidator,
ensureUserHasRight,
paginationValidator,
setBlacklistSort,
setDefaultPagination,
videosBlacklistAddValidator,
videosBlacklistRemoveValidator,
videosBlacklistUpdateValidator
2017-05-15 16:22:03 -04:00
} from '../../../middlewares'
2017-12-12 11:53:50 -05:00
import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
2018-08-13 10:57:13 -04:00
import { sequelizeTypescript } from '../../../initializers'
2017-05-15 16:22:03 -04:00
const blacklistRouter = express.Router()
2017-10-10 04:02:18 -04:00
blacklistRouter.post('/:videoId/blacklist',
2017-05-15 16:22:03 -04:00
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
2017-11-27 11:30:46 -05:00
asyncMiddleware(videosBlacklistAddValidator),
2017-10-25 05:55:06 -04:00
asyncMiddleware(addVideoToBlacklist)
2017-05-05 10:53:35 -04:00
)
2017-10-10 04:02:18 -04:00
blacklistRouter.get('/blacklist',
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
2017-10-10 04:02:18 -04:00
paginationValidator,
blacklistSortValidator,
setBlacklistSort,
setDefaultPagination,
2017-10-25 05:55:06 -04:00
asyncMiddleware(listBlacklist)
2017-10-10 04:02:18 -04:00
)
2018-08-13 10:57:13 -04:00
blacklistRouter.put('/:videoId/blacklist',
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
asyncMiddleware(videosBlacklistUpdateValidator),
asyncMiddleware(updateVideoBlacklistController)
)
2017-10-10 04:02:18 -04:00
blacklistRouter.delete('/:videoId/blacklist',
authenticate,
ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
2017-11-27 11:30:46 -05:00
asyncMiddleware(videosBlacklistRemoveValidator),
2017-10-25 05:55:06 -04:00
asyncMiddleware(removeVideoFromBlacklistController)
2017-10-10 04:02:18 -04:00
)
2017-05-05 10:53:35 -04:00
// ---------------------------------------------------------------------------
2017-05-15 16:22:03 -04:00
export {
blacklistRouter
}
2017-05-05 10:53:35 -04:00
// ---------------------------------------------------------------------------
2018-08-13 10:57:13 -04:00
async function addVideoToBlacklist (req: express.Request, res: express.Response) {
2017-05-05 10:53:35 -04:00
const videoInstance = res.locals.video
2018-08-13 10:57:13 -04:00
const body: VideoBlacklistCreate = req.body
2017-05-05 10:53:35 -04:00
const toCreate = {
2018-08-13 10:57:13 -04:00
videoId: videoInstance.id,
reason: body.reason
2017-05-05 10:53:35 -04:00
}
2017-12-12 11:53:50 -05:00
await VideoBlacklistModel.create(toCreate)
2017-10-25 05:55:06 -04:00
return res.type('json').status(204).end()
2017-05-05 10:53:35 -04:00
}
2017-10-10 04:02:18 -04:00
2018-08-13 10:57:13 -04:00
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()
}
2017-10-25 05:55:06 -04:00
async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-12-12 11:53:50 -05:00
const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
2017-10-25 05:55:06 -04:00
2018-08-14 03:08:47 -04:00
return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total))
2017-10-10 04:02:18 -04:00
}
2017-10-25 05:55:06 -04:00
async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
2018-08-13 10:57:13 -04:00
const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
2017-10-10 04:02:18 -04:00
2018-08-13 10:57:13 -04:00
await sequelizeTypescript.transaction(t => {
return videoBlacklist.destroy({ transaction: t })
})
2017-10-25 05:55:06 -04:00
2018-08-13 10:57:13 -04:00
logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
2017-10-25 05:55:06 -04:00
2018-08-13 10:57:13 -04:00
return res.type('json').status(204).end()
2017-10-10 04:02:18 -04:00
}