]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/blacklist.ts
Add user notification base code
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
191764f3 2import { VideoBlacklist, UserRight, VideoBlacklistCreate } from '../../../../shared'
da854ddd
C
3import { logger } from '../../../helpers/logger'
4import { getFormattedObjects } from '../../../helpers/utils'
65fcc311 5import {
26b7305a
C
6 asyncMiddleware,
7 authenticate,
8 blacklistSortValidator,
9 ensureUserHasRight,
10 paginationValidator,
11 setBlacklistSort,
12 setDefaultPagination,
13 videosBlacklistAddValidator,
14 videosBlacklistRemoveValidator,
15 videosBlacklistUpdateValidator
65fcc311 16} from '../../../middlewares'
3fd3ab2d 17import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
26b7305a 18import { sequelizeTypescript } from '../../../initializers'
cef534ed
C
19import { Notifier } from '../../../lib/notifier'
20import { VideoModel } from '../../../models/video/video'
65fcc311
C
21
22const blacklistRouter = express.Router()
23
35bf0c83 24blacklistRouter.post('/:videoId/blacklist',
65fcc311 25 authenticate,
954605a8 26 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 27 asyncMiddleware(videosBlacklistAddValidator),
eb080476 28 asyncMiddleware(addVideoToBlacklist)
d33242b0
C
29)
30
35bf0c83
C
31blacklistRouter.get('/blacklist',
32 authenticate,
954605a8 33 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
35bf0c83
C
34 paginationValidator,
35 blacklistSortValidator,
36 setBlacklistSort,
f05a1c30 37 setDefaultPagination,
eb080476 38 asyncMiddleware(listBlacklist)
35bf0c83
C
39)
40
26b7305a
C
41blacklistRouter.put('/:videoId/blacklist',
42 authenticate,
43 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
44 asyncMiddleware(videosBlacklistUpdateValidator),
45 asyncMiddleware(updateVideoBlacklistController)
46)
47
35bf0c83
C
48blacklistRouter.delete('/:videoId/blacklist',
49 authenticate,
954605a8 50 ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
a2431b7d 51 asyncMiddleware(videosBlacklistRemoveValidator),
eb080476 52 asyncMiddleware(removeVideoFromBlacklistController)
35bf0c83
C
53)
54
d33242b0
C
55// ---------------------------------------------------------------------------
56
65fcc311
C
57export {
58 blacklistRouter
59}
d33242b0
C
60
61// ---------------------------------------------------------------------------
62
26b7305a 63async function addVideoToBlacklist (req: express.Request, res: express.Response) {
d33242b0 64 const videoInstance = res.locals.video
26b7305a 65 const body: VideoBlacklistCreate = req.body
d33242b0
C
66
67 const toCreate = {
26b7305a
C
68 videoId: videoInstance.id,
69 reason: body.reason
d33242b0
C
70 }
71
cef534ed
C
72 const blacklist = await VideoBlacklistModel.create(toCreate)
73 blacklist.Video = videoInstance
74
75 Notifier.Instance.notifyOnVideoBlacklist(blacklist)
76
77 logger.info('Video %s blacklisted.', res.locals.video.uuid)
78
eb080476 79 return res.type('json').status(204).end()
d33242b0 80}
35bf0c83 81
26b7305a
C
82async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
83 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
26b7305a
C
84
85 if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
86
87 await sequelizeTypescript.transaction(t => {
88 return videoBlacklist.save({ transaction: t })
89 })
90
91 return res.type('json').status(204).end()
92}
93
eb080476 94async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
3fd3ab2d 95 const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
eb080476 96
191764f3 97 return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total))
35bf0c83
C
98}
99
eb080476 100async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
26b7305a 101 const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
cef534ed 102 const video: VideoModel = res.locals.video
35bf0c83 103
26b7305a
C
104 await sequelizeTypescript.transaction(t => {
105 return videoBlacklist.destroy({ transaction: t })
106 })
eb080476 107
cef534ed
C
108 Notifier.Instance.notifyOnVideoUnblacklist(video)
109
26b7305a 110 logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
eb080476 111
26b7305a 112 return res.type('json').status(204).end()
35bf0c83 113}