PeerTube/server/controllers/api/pods.ts

122 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-06-05 21:53:49 +02:00
import * as express from 'express'
2017-05-15 22:22:03 +02:00
import { waterfall } from 'async'
2017-05-22 20:58:25 +02:00
import { database as db } from '../../initializers/database'
2017-05-15 22:22:03 +02:00
import { CONFIG } from '../../initializers'
import {
logger,
getMyPublicCert,
getFormatedObjects
} from '../../helpers'
import {
sendOwnedVideosToPod,
makeFriends,
quitFriends
} from '../../lib'
import {
podsAddValidator,
authenticate,
ensureIsAdmin,
makeFriendsValidator,
setBodyHostPort,
setBodyHostsPort
} from '../../middlewares'
2017-06-10 22:15:25 +02:00
import {
PodInstance
} from '../../models'
2017-05-15 22:22:03 +02:00
const podsRouter = express.Router()
podsRouter.get('/', listPods)
podsRouter.post('/',
setBodyHostPort, // We need to modify the host before running the validator!
podsAddValidator,
addPods
)
podsRouter.post('/makefriends',
authenticate,
ensureIsAdmin,
makeFriendsValidator,
setBodyHostsPort,
2017-05-22 20:58:25 +02:00
makeFriendsController
2017-05-15 22:22:03 +02:00
)
podsRouter.get('/quitfriends',
authenticate,
ensureIsAdmin,
2017-05-22 20:58:25 +02:00
quitFriendsController
2017-05-15 22:22:03 +02:00
)
// ---------------------------------------------------------------------------
export {
podsRouter
}
// ---------------------------------------------------------------------------
2017-06-10 22:15:25 +02:00
function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-05-15 22:22:03 +02:00
const informations = req.body
2017-06-10 22:15:25 +02:00
waterfall<string, Error>([
2017-05-15 22:22:03 +02:00
function addPod (callback) {
const pod = db.Pod.build(informations)
pod.save().asCallback(function (err, podCreated) {
// Be sure about the number of parameters for the callback
return callback(err, podCreated)
})
},
2017-06-10 22:15:25 +02:00
function sendMyVideos (podCreated: PodInstance, callback) {
2017-05-15 22:22:03 +02:00
sendOwnedVideosToPod(podCreated.id)
callback(null)
},
function fetchMyCertificate (callback) {
getMyPublicCert(function (err, cert) {
if (err) {
logger.error('Cannot read cert file.')
return callback(err)
}
return callback(null, cert)
})
}
], function (err, cert) {
if (err) return next(err)
return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
})
}
2017-06-10 22:15:25 +02:00
function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-05-15 22:22:03 +02:00
db.Pod.list(function (err, podsList) {
if (err) return next(err)
res.json(getFormatedObjects(podsList, podsList.length))
})
}
2017-06-10 22:15:25 +02:00
function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
const hosts = req.body.hosts as string[]
2017-05-15 22:22:03 +02:00
makeFriends(hosts, function (err) {
if (err) {
logger.error('Could not make friends.', { error: err })
return
}
logger.info('Made friends!')
})
res.type('json').status(204).end()
}
2017-06-10 22:15:25 +02:00
function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-05-15 22:22:03 +02:00
quitFriends(function (err) {
if (err) return next(err)
res.type('json').status(204).end()
})
}