]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
4d4e5cd4 1import * as express from 'express'
da854ddd
C
2import { BlacklistedVideo, UserRight } from '../../../../shared'
3import { logger } from '../../../helpers/logger'
4import { getFormattedObjects } from '../../../helpers/utils'
65fcc311 5import {
f05a1c30 6 asyncMiddleware, authenticate, blacklistSortValidator, ensureUserHasRight, paginationValidator, setBlacklistSort, setDefaultPagination,
da854ddd 7 videosBlacklistAddValidator, videosBlacklistRemoveValidator
65fcc311 8} from '../../../middlewares'
3fd3ab2d 9import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
65fcc311
C
10
11const blacklistRouter = express.Router()
12
35bf0c83 13blacklistRouter.post('/:videoId/blacklist',
65fcc311 14 authenticate,
954605a8 15 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 16 asyncMiddleware(videosBlacklistAddValidator),
eb080476 17 asyncMiddleware(addVideoToBlacklist)
d33242b0
C
18)
19
35bf0c83
C
20blacklistRouter.get('/blacklist',
21 authenticate,
954605a8 22 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
35bf0c83
C
23 paginationValidator,
24 blacklistSortValidator,
25 setBlacklistSort,
f05a1c30 26 setDefaultPagination,
eb080476 27 asyncMiddleware(listBlacklist)
35bf0c83
C
28)
29
30blacklistRouter.delete('/:videoId/blacklist',
31 authenticate,
954605a8 32 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 33 asyncMiddleware(videosBlacklistRemoveValidator),
eb080476 34 asyncMiddleware(removeVideoFromBlacklistController)
35bf0c83
C
35)
36
d33242b0
C
37// ---------------------------------------------------------------------------
38
65fcc311
C
39export {
40 blacklistRouter
41}
d33242b0
C
42
43// ---------------------------------------------------------------------------
44
eb080476 45async function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
d33242b0
C
46 const videoInstance = res.locals.video
47
48 const toCreate = {
49 videoId: videoInstance.id
50 }
51
3fd3ab2d 52 await VideoBlacklistModel.create(toCreate)
eb080476 53 return res.type('json').status(204).end()
d33242b0 54}
35bf0c83 55
eb080476 56async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 57 const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
eb080476 58
3fd3ab2d 59 return res.json(getFormattedObjects<BlacklistedVideo, VideoBlacklistModel>(resultList.data, resultList.total))
35bf0c83
C
60}
61
eb080476 62async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 63 const blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel
35bf0c83 64
eb080476
C
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 }
35bf0c83 75}