Move to eslint
[oweals/peertube.git] / server / controllers / api / server / redundancy.ts
1 import * as express from 'express'
2 import { UserRight } from '../../../../shared/models/users'
3 import {
4   asyncMiddleware,
5   authenticate,
6   ensureUserHasRight,
7   paginationValidator,
8   setDefaultPagination,
9   setDefaultVideoRedundanciesSort,
10   videoRedundanciesSortValidator
11 } from '../../../middlewares'
12 import {
13   listVideoRedundanciesValidator,
14   updateServerRedundancyValidator,
15   addVideoRedundancyValidator,
16   removeVideoRedundancyValidator
17 } from '../../../middlewares/validators/redundancy'
18 import { removeRedundanciesOfServer, removeVideoRedundancy } from '../../../lib/redundancy'
19 import { logger } from '../../../helpers/logger'
20 import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
21 import { JobQueue } from '@server/lib/job-queue'
22
23 const serverRedundancyRouter = express.Router()
24
25 serverRedundancyRouter.put('/redundancy/:host',
26   authenticate,
27   ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
28   asyncMiddleware(updateServerRedundancyValidator),
29   asyncMiddleware(updateRedundancy)
30 )
31
32 serverRedundancyRouter.get('/redundancy/videos',
33   authenticate,
34   ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
35   listVideoRedundanciesValidator,
36   paginationValidator,
37   videoRedundanciesSortValidator,
38   setDefaultVideoRedundanciesSort,
39   setDefaultPagination,
40   asyncMiddleware(listVideoRedundancies)
41 )
42
43 serverRedundancyRouter.post('/redundancy/videos',
44   authenticate,
45   ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
46   addVideoRedundancyValidator,
47   asyncMiddleware(addVideoRedundancy)
48 )
49
50 serverRedundancyRouter.delete('/redundancy/videos/:redundancyId',
51   authenticate,
52   ensureUserHasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES),
53   removeVideoRedundancyValidator,
54   asyncMiddleware(removeVideoRedundancyController)
55 )
56
57 // ---------------------------------------------------------------------------
58
59 export {
60   serverRedundancyRouter
61 }
62
63 // ---------------------------------------------------------------------------
64
65 async function listVideoRedundancies (req: express.Request, res: express.Response) {
66   const resultList = await VideoRedundancyModel.listForApi({
67     start: req.query.start,
68     count: req.query.count,
69     sort: req.query.sort,
70     target: req.query.target,
71     strategy: req.query.strategy
72   })
73
74   const result = {
75     total: resultList.total,
76     data: resultList.data.map(r => VideoRedundancyModel.toFormattedJSONStatic(r))
77   }
78
79   return res.json(result)
80 }
81
82 async function addVideoRedundancy (req: express.Request, res: express.Response) {
83   const payload = {
84     videoId: res.locals.onlyVideo.id
85   }
86
87   await JobQueue.Instance.createJobWithPromise({
88     type: 'video-redundancy',
89     payload
90   })
91
92   return res.sendStatus(204)
93 }
94
95 async function removeVideoRedundancyController (req: express.Request, res: express.Response) {
96   await removeVideoRedundancy(res.locals.videoRedundancy)
97
98   return res.sendStatus(204)
99 }
100
101 async function updateRedundancy (req: express.Request, res: express.Response) {
102   const server = res.locals.server
103
104   server.redundancyAllowed = req.body.redundancyAllowed
105
106   await server.save()
107
108   // Async, could be long
109   removeRedundanciesOfServer(server.id)
110     .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, { err }))
111
112   return res.sendStatus(204)
113 }