PeerTube/server/controllers/api/videos/blacklist.ts

86 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-06-05 19:53:49 +00:00
import * as express from 'express'
2017-05-05 14:53:35 +00:00
2017-10-10 08:02:18 +00:00
import { database as db } from '../../../initializers'
import { logger, getFormattedObjects } from '../../../helpers'
2017-05-15 20:22:03 +00:00
import {
authenticate,
ensureIsAdmin,
2017-10-10 08:02:18 +00:00
videosBlacklistAddValidator,
videosBlacklistRemoveValidator,
paginationValidator,
blacklistSortValidator,
setBlacklistSort,
setPagination
2017-05-15 20:22:03 +00:00
} from '../../../middlewares'
2017-10-10 08:02:18 +00:00
import { BlacklistedVideoInstance } from '../../../models'
import { BlacklistedVideo } from '../../../../shared'
2017-05-15 20:22:03 +00:00
const blacklistRouter = express.Router()
2017-10-10 08:02:18 +00:00
blacklistRouter.post('/:videoId/blacklist',
2017-05-15 20:22:03 +00:00
authenticate,
ensureIsAdmin,
2017-10-10 08:02:18 +00:00
videosBlacklistAddValidator,
2017-05-05 14:53:35 +00:00
addVideoToBlacklist
)
2017-10-10 08:02:18 +00:00
blacklistRouter.get('/blacklist',
authenticate,
ensureIsAdmin,
paginationValidator,
blacklistSortValidator,
setBlacklistSort,
setPagination,
listBlacklist
)
blacklistRouter.delete('/:videoId/blacklist',
authenticate,
ensureIsAdmin,
videosBlacklistRemoveValidator,
removeVideoFromBlacklistController
)
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
// ---------------------------------------------------------------------------
2017-06-10 20:15:25 +00:00
function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-05-05 14:53:35 +00:00
const videoInstance = res.locals.video
const toCreate = {
videoId: videoInstance.id
}
db.BlacklistedVideo.create(toCreate)
.then(() => res.type('json').status(204).end())
.catch(err => {
2017-07-07 16:26:12 +00:00
logger.error('Errors when blacklisting video ', err)
2017-05-05 14:53:35 +00:00
return next(err)
})
2017-05-05 14:53:35 +00:00
}
2017-10-10 08:02:18 +00:00
function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort)
.then(resultList => res.json(getFormattedObjects<BlacklistedVideo, BlacklistedVideoInstance>(resultList.data, resultList.total)))
.catch(err => next(err))
}
function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance
blacklistedVideo.destroy()
.then(() => {
logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
res.sendStatus(204)
})
.catch(err => {
logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err)
next(err)
})
}