]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
65fcc311 | 2 | |
e02643f3 | 3 | import { database as db } from '../../initializers/database' |
8a02bd04 | 4 | import { logger, getFormattedObjects } from '../../helpers' |
65fcc311 | 5 | import { |
65fcc311 | 6 | makeFriends, |
d5f5a670 GS |
7 | quitFriends, |
8 | removeFriend | |
65fcc311 C |
9 | } from '../../lib' |
10 | import { | |
65fcc311 | 11 | authenticate, |
954605a8 | 12 | ensureUserHasRight, |
65fcc311 | 13 | makeFriendsValidator, |
d5f5a670 | 14 | setBodyHostsPort, |
8a02bd04 C |
15 | podRemoveValidator, |
16 | paginationValidator, | |
17 | setPagination, | |
18 | setPodsSort, | |
eb080476 C |
19 | podsSortValidator, |
20 | asyncMiddleware | |
65fcc311 | 21 | } from '../../middlewares' |
8a02bd04 | 22 | import { PodInstance } from '../../models' |
954605a8 | 23 | import { UserRight } from '../../../shared' |
65fcc311 C |
24 | |
25 | const podsRouter = express.Router() | |
26 | ||
8a02bd04 C |
27 | podsRouter.get('/', |
28 | paginationValidator, | |
29 | podsSortValidator, | |
30 | setPodsSort, | |
31 | setPagination, | |
eb080476 | 32 | asyncMiddleware(listPods) |
65fcc311 | 33 | ) |
aa2e7f15 | 34 | podsRouter.post('/make-friends', |
65fcc311 | 35 | authenticate, |
954605a8 | 36 | ensureUserHasRight(UserRight.MANAGE_PODS), |
65fcc311 C |
37 | makeFriendsValidator, |
38 | setBodyHostsPort, | |
eb080476 | 39 | asyncMiddleware(makeFriendsController) |
65fcc311 | 40 | ) |
aa2e7f15 | 41 | podsRouter.get('/quit-friends', |
65fcc311 | 42 | authenticate, |
954605a8 | 43 | ensureUserHasRight(UserRight.MANAGE_PODS), |
eb080476 | 44 | asyncMiddleware(quitFriendsController) |
65fcc311 | 45 | ) |
d5f5a670 GS |
46 | podsRouter.delete('/:id', |
47 | authenticate, | |
954605a8 | 48 | ensureUserHasRight(UserRight.MANAGE_PODS), |
d5f5a670 | 49 | podRemoveValidator, |
eb080476 | 50 | asyncMiddleware(removeFriendController) |
d5f5a670 | 51 | ) |
65fcc311 C |
52 | |
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | export { | |
56 | podsRouter | |
57 | } | |
58 | ||
59 | // --------------------------------------------------------------------------- | |
60 | ||
eb080476 C |
61 | async function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { |
62 | const resultList = await db.Pod.listForApi(req.query.start, req.query.count, req.query.sort) | |
63 | ||
64 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
65fcc311 C |
65 | } |
66 | ||
eb080476 | 67 | async function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
69818c93 | 68 | const hosts = req.body.hosts as string[] |
65fcc311 | 69 | |
eb080476 | 70 | // Don't wait the process that could be long |
6fcd19ba C |
71 | makeFriends(hosts) |
72 | .then(() => logger.info('Made friends!')) | |
ad0997ad | 73 | .catch(err => logger.error('Could not make friends.', err)) |
65fcc311 | 74 | |
eb080476 | 75 | return res.type('json').status(204).end() |
65fcc311 C |
76 | } |
77 | ||
eb080476 C |
78 | async function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
79 | await quitFriends() | |
80 | ||
81 | return res.type('json').status(204).end() | |
65fcc311 | 82 | } |
d5f5a670 | 83 | |
eb080476 | 84 | async function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) { |
d5f5a670 GS |
85 | const pod = res.locals.pod as PodInstance |
86 | ||
eb080476 C |
87 | await removeFriend(pod) |
88 | ||
89 | return res.type('json').status(204).end() | |
d5f5a670 | 90 | } |