aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/pods.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/controllers/api/pods.ts
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-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.ts118
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 @@
1import express = require('express')
2import { waterfall } from 'async'
3
4const db = require('../../initializers/database')
5import { CONFIG } from '../../initializers'
6import {
7 logger,
8 getMyPublicCert,
9 getFormatedObjects
10} from '../../helpers'
11import {
12 sendOwnedVideosToPod,
13 makeFriends,
14 quitFriends
15} from '../../lib'
16import {
17 podsAddValidator,
18 authenticate,
19 ensureIsAdmin,
20 makeFriendsValidator,
21 setBodyHostPort,
22 setBodyHostsPort
23} from '../../middlewares'
24
25const podsRouter = express.Router()
26
27podsRouter.get('/', listPods)
28podsRouter.post('/',
29 setBodyHostPort, // We need to modify the host before running the validator!
30 podsAddValidator,
31 addPods
32)
33podsRouter.post('/makefriends',
34 authenticate,
35 ensureIsAdmin,
36 makeFriendsValidator,
37 setBodyHostsPort,
38 makeFriends
39)
40podsRouter.get('/quitfriends',
41 authenticate,
42 ensureIsAdmin,
43 quitFriends
44)
45
46// ---------------------------------------------------------------------------
47
48export {
49 podsRouter
50}
51
52// ---------------------------------------------------------------------------
53
54function 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
89function 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
97function 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
112function quitFriendsController (req, res, next) {
113 quitFriends(function (err) {
114 if (err) return next(err)
115
116 res.type('json').status(204).end()
117 })
118}