PeerTube/server/controllers/api/v1/pods.js

94 lines
2.6 KiB
JavaScript
Raw Normal View History

'use strict'
2015-06-09 10:41:40 -05:00
2016-03-16 16:29:27 -05:00
const express = require('express')
const fs = require('fs')
const logger = require('../../../helpers/logger')
const friends = require('../../../lib/friends')
const middleware = require('../../../middlewares')
const cacheMiddleware = middleware.cache
const peertubeCrypto = require('../../../helpers/peertubeCrypto')
const Pods = require('../../../models/pods')
const reqValidator = middleware.reqValidators.pods
const secureMiddleware = middleware.secure
const secureRequest = middleware.reqValidators.remote.secureRequest
const Videos = require('../../../models/videos')
const router = express.Router()
2015-06-09 10:41:40 -05:00
router.get('/', cacheMiddleware.cache(false), listPods)
router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods)
router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends)
router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends)
// Post because this is a secured request
router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
2016-01-31 04:23:52 -06:00
// ---------------------------------------------------------------------------
2016-01-31 04:23:52 -06:00
module.exports = router
2016-01-31 04:23:52 -06:00
// ---------------------------------------------------------------------------
2015-06-09 10:41:40 -05:00
function addPods (req, res, next) {
2016-03-16 16:29:27 -05:00
const informations = req.body.data
Pods.add(informations, function (err) {
if (err) return next(err)
2015-06-09 10:41:40 -05:00
Videos.addRemotes(informations.videos)
2016-02-04 14:10:33 -06:00
fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
if (err) {
logger.error('Cannot read cert file.')
return next(err)
}
Videos.listOwned(function (err, videos_list) {
2016-02-04 14:10:33 -06:00
if (err) {
logger.error('Cannot get the list of owned videos.')
2016-02-04 14:10:33 -06:00
return next(err)
}
res.json({ cert: cert, videos: videos_list })
2016-02-04 14:10:33 -06:00
})
2015-06-09 10:41:40 -05:00
})
})
}
2015-06-09 10:41:40 -05:00
function listPods (req, res, next) {
Pods.list(function (err, pods_list) {
if (err) return next(err)
res.json(pods_list)
})
}
function makeFriends (req, res, next) {
friends.makeFriends(function (err) {
if (err) return next(err)
2016-03-14 07:50:19 -05:00
res.type('json').status(204).end()
})
}
function removePods (req, res, next) {
2016-03-16 16:29:27 -05:00
const url = req.body.signature.url
Pods.remove(url, function (err) {
if (err) return next(err)
2015-06-09 10:41:40 -05:00
Videos.removeAllRemotesOf(url, function (err) {
if (err) logger.error('Cannot remove all remote videos of %s.', url)
else logger.info('%s pod removed.', url)
2016-02-05 12:02:05 -06:00
2016-03-14 07:50:19 -05:00
res.type('json').status(204).end()
2015-06-09 10:41:40 -05:00
})
})
}
2015-06-09 10:41:40 -05:00
function quitFriends (req, res, next) {
friends.quitFriends(function (err) {
if (err) return next(err)
2015-06-09 10:41:40 -05:00
2016-03-14 07:50:19 -05:00
res.type('json').status(204).end()
})
}