diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/controllers/api/pods.ts | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/controllers/api/pods.ts')
-rw-r--r-- | server/controllers/api/pods.ts | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/server/controllers/api/pods.ts b/server/controllers/api/pods.ts new file mode 100644 index 000000000..06dfd8295 --- /dev/null +++ b/server/controllers/api/pods.ts | |||
@@ -0,0 +1,118 @@ | |||
1 | import express = require('express') | ||
2 | import { waterfall } from 'async' | ||
3 | |||
4 | const db = require('../../initializers/database') | ||
5 | import { CONFIG } from '../../initializers' | ||
6 | import { | ||
7 | logger, | ||
8 | getMyPublicCert, | ||
9 | getFormatedObjects | ||
10 | } from '../../helpers' | ||
11 | import { | ||
12 | sendOwnedVideosToPod, | ||
13 | makeFriends, | ||
14 | quitFriends | ||
15 | } from '../../lib' | ||
16 | import { | ||
17 | podsAddValidator, | ||
18 | authenticate, | ||
19 | ensureIsAdmin, | ||
20 | makeFriendsValidator, | ||
21 | setBodyHostPort, | ||
22 | setBodyHostsPort | ||
23 | } from '../../middlewares' | ||
24 | |||
25 | const podsRouter = express.Router() | ||
26 | |||
27 | podsRouter.get('/', listPods) | ||
28 | podsRouter.post('/', | ||
29 | setBodyHostPort, // We need to modify the host before running the validator! | ||
30 | podsAddValidator, | ||
31 | addPods | ||
32 | ) | ||
33 | podsRouter.post('/makefriends', | ||
34 | authenticate, | ||
35 | ensureIsAdmin, | ||
36 | makeFriendsValidator, | ||
37 | setBodyHostsPort, | ||
38 | makeFriends | ||
39 | ) | ||
40 | podsRouter.get('/quitfriends', | ||
41 | authenticate, | ||
42 | ensureIsAdmin, | ||
43 | quitFriends | ||
44 | ) | ||
45 | |||
46 | // --------------------------------------------------------------------------- | ||
47 | |||
48 | export { | ||
49 | podsRouter | ||
50 | } | ||
51 | |||
52 | // --------------------------------------------------------------------------- | ||
53 | |||
54 | function addPods (req, res, next) { | ||
55 | const informations = req.body | ||
56 | |||
57 | waterfall([ | ||
58 | function addPod (callback) { | ||
59 | const pod = db.Pod.build(informations) | ||
60 | pod.save().asCallback(function (err, podCreated) { | ||
61 | // Be sure about the number of parameters for the callback | ||
62 | return callback(err, podCreated) | ||
63 | }) | ||
64 | }, | ||
65 | |||
66 | function sendMyVideos (podCreated, callback) { | ||
67 | sendOwnedVideosToPod(podCreated.id) | ||
68 | |||
69 | callback(null) | ||
70 | }, | ||
71 | |||
72 | function fetchMyCertificate (callback) { | ||
73 | getMyPublicCert(function (err, cert) { | ||
74 | if (err) { | ||
75 | logger.error('Cannot read cert file.') | ||
76 | return callback(err) | ||
77 | } | ||
78 | |||
79 | return callback(null, cert) | ||
80 | }) | ||
81 | } | ||
82 | ], function (err, cert) { | ||
83 | if (err) return next(err) | ||
84 | |||
85 | return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL }) | ||
86 | }) | ||
87 | } | ||
88 | |||
89 | function listPods (req, res, next) { | ||
90 | db.Pod.list(function (err, podsList) { | ||
91 | if (err) return next(err) | ||
92 | |||
93 | res.json(getFormatedObjects(podsList, podsList.length)) | ||
94 | }) | ||
95 | } | ||
96 | |||
97 | function makeFriendsController (req, res, next) { | ||
98 | const hosts = req.body.hosts | ||
99 | |||
100 | makeFriends(hosts, function (err) { | ||
101 | if (err) { | ||
102 | logger.error('Could not make friends.', { error: err }) | ||
103 | return | ||
104 | } | ||
105 | |||
106 | logger.info('Made friends!') | ||
107 | }) | ||
108 | |||
109 | res.type('json').status(204).end() | ||
110 | } | ||
111 | |||
112 | function quitFriendsController (req, res, next) { | ||
113 | quitFriends(function (err) { | ||
114 | if (err) return next(err) | ||
115 | |||
116 | res.type('json').status(204).end() | ||
117 | }) | ||
118 | } | ||