]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/v1/pods.js
Update to standard 7. Goodbye snake_case, I used to love you
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
CommitLineData
9f10b292 1'use strict'
8c308c2b 2
f0f5567b 3const express = require('express')
f0f5567b
C
4
5const logger = require('../../../helpers/logger')
6const friends = require('../../../lib/friends')
7const middleware = require('../../../middlewares')
8const cacheMiddleware = middleware.cache
f0f5567b
C
9const Pods = require('../../../models/pods')
10const reqValidator = middleware.reqValidators.pods
11const secureMiddleware = middleware.secure
12const secureRequest = middleware.reqValidators.remote.secureRequest
cbe2f7c3 13const videos = require('../../../lib/videos')
f0f5567b
C
14const Videos = require('../../../models/videos')
15
16const router = express.Router()
8c308c2b 17
9f10b292
C
18router.get('/', cacheMiddleware.cache(false), listPods)
19router.post('/', reqValidator.podsAdd, cacheMiddleware.cache(false), addPods)
20router.get('/makefriends', reqValidator.makeFriends, cacheMiddleware.cache(false), makeFriends)
21router.get('/quitfriends', cacheMiddleware.cache(false), quitFriends)
22// Post because this is a secured request
23router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
c45f7f84 24
9f10b292 25// ---------------------------------------------------------------------------
c45f7f84 26
9f10b292 27module.exports = router
c45f7f84 28
9f10b292 29// ---------------------------------------------------------------------------
8c308c2b 30
9f10b292 31function addPods (req, res, next) {
f0f5567b 32 const informations = req.body.data
9f10b292
C
33 Pods.add(informations, function (err) {
34 if (err) return next(err)
8c308c2b 35
cbe2f7c3
C
36 // Create the remote videos from the new pod
37 videos.createRemoteVideos(informations.videos, function (err) {
38 if (err) logger.error('Cannot create remote videos.', { error: err })
39 })
c173e565 40
cbe2f7c3 41 friends.getMyCertificate(function (err, cert) {
9f10b292
C
42 if (err) {
43 logger.error('Cannot read cert file.')
44 return next(err)
45 }
46
bc503c2a 47 Videos.listOwned(function (err, videosList) {
c173e565 48 if (err) {
9f10b292 49 logger.error('Cannot get the list of owned videos.')
c173e565
C
50 return next(err)
51 }
52
bc503c2a 53 res.json({ cert: cert, videos: videosList })
c173e565 54 })
8c308c2b 55 })
9f10b292
C
56 })
57}
8c308c2b 58
9f10b292 59function listPods (req, res, next) {
bc503c2a 60 Pods.list(function (err, podsList) {
9f10b292 61 if (err) return next(err)
45239549 62
bc503c2a 63 res.json(podsList)
9f10b292
C
64 })
65}
45239549 66
9f10b292
C
67function makeFriends (req, res, next) {
68 friends.makeFriends(function (err) {
69 if (err) return next(err)
45239549 70
dc8bc31b 71 res.type('json').status(204).end()
9f10b292
C
72 })
73}
45239549 74
9f10b292 75function removePods (req, res, next) {
f0f5567b 76 const url = req.body.signature.url
9f10b292
C
77 Pods.remove(url, function (err) {
78 if (err) return next(err)
8c308c2b 79
bc503c2a 80 Videos.listFromUrl(url, function (err, videosList) {
cbe2f7c3
C
81 if (err) {
82 logger.error('Cannot list videos from url.', { error: err })
83 next(err)
84 }
8425cb89 85
bc503c2a 86 videos.removeRemoteVideos(videosList, function (err) {
cbe2f7c3
C
87 if (err) {
88 logger.error('Cannot remove remote videos.', { error: err })
89 next(err)
90 }
91
92 res.type('json').status(204).end()
93 })
8c308c2b 94 })
9f10b292
C
95 })
96}
8c308c2b 97
9f10b292
C
98function quitFriends (req, res, next) {
99 friends.quitFriends(function (err) {
100 if (err) return next(err)
8c308c2b 101
dc8bc31b 102 res.type('json').status(204).end()
9f10b292
C
103 })
104}