]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
65fcc311 | 2 | |
e02643f3 | 3 | import { database as db } from '../../initializers/database' |
65fcc311 C |
4 | import { CONFIG } from '../../initializers' |
5 | import { | |
6 | logger, | |
7 | getMyPublicCert, | |
0aef76c4 | 8 | getFormattedObjects |
65fcc311 C |
9 | } from '../../helpers' |
10 | import { | |
11 | sendOwnedVideosToPod, | |
12 | makeFriends, | |
d5f5a670 GS |
13 | quitFriends, |
14 | removeFriend | |
65fcc311 C |
15 | } from '../../lib' |
16 | import { | |
17 | podsAddValidator, | |
18 | authenticate, | |
19 | ensureIsAdmin, | |
20 | makeFriendsValidator, | |
21 | setBodyHostPort, | |
d5f5a670 GS |
22 | setBodyHostsPort, |
23 | podRemoveValidator | |
65fcc311 | 24 | } from '../../middlewares' |
69818c93 C |
25 | import { |
26 | PodInstance | |
27 | } from '../../models' | |
0aef76c4 | 28 | import { Pod as FormattedPod } from '../../../shared' |
65fcc311 C |
29 | |
30 | const podsRouter = express.Router() | |
31 | ||
32 | podsRouter.get('/', listPods) | |
33 | podsRouter.post('/', | |
34 | setBodyHostPort, // We need to modify the host before running the validator! | |
35 | podsAddValidator, | |
36 | addPods | |
37 | ) | |
38 | podsRouter.post('/makefriends', | |
39 | authenticate, | |
40 | ensureIsAdmin, | |
41 | makeFriendsValidator, | |
42 | setBodyHostsPort, | |
e02643f3 | 43 | makeFriendsController |
65fcc311 C |
44 | ) |
45 | podsRouter.get('/quitfriends', | |
46 | authenticate, | |
47 | ensureIsAdmin, | |
e02643f3 | 48 | quitFriendsController |
65fcc311 | 49 | ) |
d5f5a670 GS |
50 | podsRouter.delete('/:id', |
51 | authenticate, | |
52 | ensureIsAdmin, | |
53 | podRemoveValidator, | |
54 | removeFriendController | |
55 | ) | |
65fcc311 C |
56 | |
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | export { | |
60 | podsRouter | |
61 | } | |
62 | ||
63 | // --------------------------------------------------------------------------- | |
64 | ||
69818c93 | 65 | function addPods (req: express.Request, res: express.Response, next: express.NextFunction) { |
65fcc311 C |
66 | const informations = req.body |
67 | ||
6fcd19ba C |
68 | const pod = db.Pod.build(informations) |
69 | pod.save() | |
70 | .then(podCreated => { | |
71 | return sendOwnedVideosToPod(podCreated.id) | |
72 | }) | |
73 | .then(() => { | |
74 | return getMyPublicCert() | |
75 | }) | |
76 | .then(cert => { | |
77 | return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL }) | |
78 | }) | |
79 | .catch(err => next(err)) | |
65fcc311 C |
80 | } |
81 | ||
69818c93 | 82 | function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba | 83 | db.Pod.list() |
0aef76c4 | 84 | .then(podsList => res.json(getFormattedObjects<FormattedPod, PodInstance>(podsList, podsList.length))) |
6fcd19ba | 85 | .catch(err => next(err)) |
65fcc311 C |
86 | } |
87 | ||
69818c93 C |
88 | function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
89 | const hosts = req.body.hosts as string[] | |
65fcc311 | 90 | |
6fcd19ba C |
91 | makeFriends(hosts) |
92 | .then(() => logger.info('Made friends!')) | |
ad0997ad | 93 | .catch(err => logger.error('Could not make friends.', err)) |
65fcc311 | 94 | |
6fcd19ba | 95 | // Don't wait the process that could be long |
65fcc311 C |
96 | res.type('json').status(204).end() |
97 | } | |
98 | ||
69818c93 | 99 | function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba C |
100 | quitFriends() |
101 | .then(() => res.type('json').status(204).end()) | |
102 | .catch(err => next(err)) | |
65fcc311 | 103 | } |
d5f5a670 GS |
104 | |
105 | function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
106 | const pod = res.locals.pod as PodInstance | |
107 | ||
108 | removeFriend(pod) | |
109 | .then(() => (res.type('json').status(204).end())) | |
110 | .catch(err => next(err)) | |
111 | } |