]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/server/redundancy.ts
Import original publication date (web UI)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / server / redundancy.ts
1 import * as express from 'express'
2 import { UserRight } from '../../../../shared/models/users'
3 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares'
4 import { updateServerRedundancyValidator } from '../../../middlewares/validators/redundancy'
5 import { ServerModel } from '../../../models/server/server'
6 import { removeRedundancyOf } from '../../../lib/redundancy'
7 import { logger } from '../../../helpers/logger'
8
9 const serverRedundancyRouter = express.Router()
10
11 serverRedundancyRouter.put('/redundancy/:host',
12 authenticate,
13 ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW),
14 asyncMiddleware(updateServerRedundancyValidator),
15 asyncMiddleware(updateRedundancy)
16 )
17
18 // ---------------------------------------------------------------------------
19
20 export {
21 serverRedundancyRouter
22 }
23
24 // ---------------------------------------------------------------------------
25
26 async function updateRedundancy (req: express.Request, res: express.Response, next: express.NextFunction) {
27 const server = res.locals.server as ServerModel
28
29 server.redundancyAllowed = req.body.redundancyAllowed
30
31 await server.save()
32
33 // Async, could be long
34 removeRedundancyOf(server.id)
35 .catch(err => logger.error('Cannot remove redundancy of %s.', server.host, err))
36
37 return res.sendStatus(204)
38 }