]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/videos/blacklist.ts
Don't show videos of remote instance after unfollow
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { BlacklistedVideo, UserRight } from '../../../../shared'
3import { logger } from '../../../helpers/logger'
4import { getFormattedObjects } from '../../../helpers/utils'
5import {
6 asyncMiddleware, authenticate, blacklistSortValidator, ensureUserHasRight, paginationValidator, setBlacklistSort, setDefaultPagination,
7 videosBlacklistAddValidator, videosBlacklistRemoveValidator
8} from '../../../middlewares'
9import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
10
11const blacklistRouter = express.Router()
12
13blacklistRouter.post('/:videoId/blacklist',
14 authenticate,
15 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
16 asyncMiddleware(videosBlacklistAddValidator),
17 asyncMiddleware(addVideoToBlacklist)
18)
19
20blacklistRouter.get('/blacklist',
21 authenticate,
22 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
23 paginationValidator,
24 blacklistSortValidator,
25 setBlacklistSort,
26 setDefaultPagination,
27 asyncMiddleware(listBlacklist)
28)
29
30blacklistRouter.delete('/:videoId/blacklist',
31 authenticate,
32 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
33 asyncMiddleware(videosBlacklistRemoveValidator),
34 asyncMiddleware(removeVideoFromBlacklistController)
35)
36
37// ---------------------------------------------------------------------------
38
39export {
40 blacklistRouter
41}
42
43// ---------------------------------------------------------------------------
44
45async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
46 const videoInstance = res.locals.video
47
48 const toCreate = {
49 videoId: videoInstance.id
50 }
51
52 await VideoBlacklistModel.create(toCreate)
53 return res.type('json').status(204).end()
54}
55
56async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
57 const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
58
59 return res.json(getFormattedObjects<BlacklistedVideo, VideoBlacklistModel>(resultList.data, resultList.total))
60}
61
62async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
63 const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel
64
65 try {
66 await blacklistedVideo.destroy()
67
68 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
69
70 return res.sendStatus(204)
71 } catch (err) {
72 logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err)
73 throw err
74 }
75}