]>
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 | removeFriend | |
15 | } from '../../lib' | |
16 | import { | |
17 | podsAddValidator, | |
18 | authenticate, | |
19 | ensureIsAdmin, | |
20 | makeFriendsValidator, | |
21 | setBodyHostPort, | |
22 | setBodyHostsPort, | |
23 | podRemoveValidator | |
24 | } from '../../middlewares' | |
25 | import { | |
26 | PodInstance | |
27 | } from '../../models' | |
28 | import { Pod as FormatedPod } from '../../../shared' | |
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, | |
43 | makeFriendsController | |
44 | ) | |
45 | podsRouter.get('/quitfriends', | |
46 | authenticate, | |
47 | ensureIsAdmin, | |
48 | quitFriendsController | |
49 | ) | |
50 | podsRouter.delete('/:id', | |
51 | authenticate, | |
52 | ensureIsAdmin, | |
53 | podRemoveValidator, | |
54 | removeFriendController | |
55 | ) | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | export { | |
60 | podsRouter | |
61 | } | |
62 | ||
63 | // --------------------------------------------------------------------------- | |
64 | ||
65 | function addPods (req: express.Request, res: express.Response, next: express.NextFunction) { | |
66 | const informations = req.body | |
67 | ||
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)) | |
80 | } | |
81 | ||
82 | function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { | |
83 | db.Pod.list() | |
84 | .then(podsList => res.json(getFormatedObjects<FormatedPod, PodInstance>(podsList, podsList.length))) | |
85 | .catch(err => next(err)) | |
86 | } | |
87 | ||
88 | function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
89 | const hosts = req.body.hosts as string[] | |
90 | ||
91 | makeFriends(hosts) | |
92 | .then(() => logger.info('Made friends!')) | |
93 | .catch(err => logger.error('Could not make friends.', err)) | |
94 | ||
95 | // Don't wait the process that could be long | |
96 | res.type('json').status(204).end() | |
97 | } | |
98 | ||
99 | function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
100 | quitFriends() | |
101 | .then(() => res.type('json').status(204).end()) | |
102 | .catch(err => next(err)) | |
103 | } | |
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 | } |