]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | ||
3 | import { database as db } from '../../initializers/database' | |
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' | |
23 | import { | |
24 | PodInstance | |
25 | } from '../../models' | |
26 | import { Pod as FormatedPod } from '../../../shared' | |
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, | |
41 | makeFriendsController | |
42 | ) | |
43 | podsRouter.get('/quitfriends', | |
44 | authenticate, | |
45 | ensureIsAdmin, | |
46 | quitFriendsController | |
47 | ) | |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | export { | |
52 | podsRouter | |
53 | } | |
54 | ||
55 | // --------------------------------------------------------------------------- | |
56 | ||
57 | function addPods (req: express.Request, res: express.Response, next: express.NextFunction) { | |
58 | const informations = req.body | |
59 | ||
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)) | |
72 | } | |
73 | ||
74 | function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { | |
75 | db.Pod.list() | |
76 | .then(podsList => res.json(getFormatedObjects<FormatedPod, PodInstance>(podsList, podsList.length))) | |
77 | .catch(err => next(err)) | |
78 | } | |
79 | ||
80 | function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
81 | const hosts = req.body.hosts as string[] | |
82 | ||
83 | makeFriends(hosts) | |
84 | .then(() => logger.info('Made friends!')) | |
85 | .catch(err => logger.error('Could not make friends.', err)) | |
86 | ||
87 | // Don't wait the process that could be long | |
88 | res.type('json').status(204).end() | |
89 | } | |
90 | ||
91 | function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
92 | quitFriends() | |
93 | .then(() => res.type('json').status(204).end()) | |
94 | .catch(err => next(err)) | |
95 | } |