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