2016-02-07 11:23:23 +01:00
|
|
|
'use strict'
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-03-16 22:29:27 +01: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 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01: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 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
module.exports = router
|
2016-01-31 11:23:52 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function addPods (req, res, next) {
|
2016-03-16 22:29:27 +01:00
|
|
|
const informations = req.body.data
|
2016-02-07 11:23:23 +01:00
|
|
|
Pods.add(informations, function (err) {
|
|
|
|
if (err) return next(err)
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
Videos.addRemotes(informations.videos)
|
2016-02-04 21:10:33 +01:00
|
|
|
|
2016-02-07 11:23:23 +01: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 21:10:33 +01:00
|
|
|
if (err) {
|
2016-02-07 11:23:23 +01:00
|
|
|
logger.error('Cannot get the list of owned videos.')
|
2016-02-04 21:10:33 +01:00
|
|
|
return next(err)
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
res.json({ cert: cert, videos: videos_list })
|
2016-02-04 21:10:33 +01:00
|
|
|
})
|
2015-06-09 17:41:40 +02:00
|
|
|
})
|
2016-02-07 11:23:23 +01:00
|
|
|
})
|
|
|
|
}
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function listPods (req, res, next) {
|
|
|
|
Pods.list(function (err, pods_list) {
|
|
|
|
if (err) return next(err)
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
res.json(pods_list)
|
|
|
|
})
|
|
|
|
}
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function makeFriends (req, res, next) {
|
|
|
|
friends.makeFriends(function (err) {
|
|
|
|
if (err) return next(err)
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-03-14 13:50:19 +01:00
|
|
|
res.type('json').status(204).end()
|
2016-02-07 11:23:23 +01:00
|
|
|
})
|
|
|
|
}
|
2016-01-23 18:31:58 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function removePods (req, res, next) {
|
2016-03-16 22:29:27 +01:00
|
|
|
const url = req.body.signature.url
|
2016-02-07 11:23:23 +01:00
|
|
|
Pods.remove(url, function (err) {
|
|
|
|
if (err) return next(err)
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01: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 19:02:05 +01:00
|
|
|
|
2016-03-14 13:50:19 +01:00
|
|
|
res.type('json').status(204).end()
|
2015-06-09 17:41:40 +02:00
|
|
|
})
|
2016-02-07 11:23:23 +01:00
|
|
|
})
|
|
|
|
}
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
function quitFriends (req, res, next) {
|
|
|
|
friends.quitFriends(function (err) {
|
|
|
|
if (err) return next(err)
|
2015-06-09 17:41:40 +02:00
|
|
|
|
2016-03-14 13:50:19 +01:00
|
|
|
res.type('json').status(204).end()
|
2016-02-07 11:23:23 +01:00
|
|
|
})
|
|
|
|
}
|