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

84 lines
2.3 KiB
TypeScript
Raw Normal View History

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