2017-06-05 21:53:49 +02:00
|
|
|
import * as express from 'express'
|
2017-05-15 22:22:03 +02:00
|
|
|
|
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,
|
2017-08-25 11:45:31 +02:00
|
|
|
getFormattedObjects
|
2017-05-15 22:22:03 +02:00
|
|
|
} from '../../helpers'
|
|
|
|
import {
|
|
|
|
sendOwnedVideosToPod,
|
|
|
|
makeFriends,
|
2017-08-02 21:50:42 +02:00
|
|
|
quitFriends,
|
|
|
|
removeFriend
|
2017-05-15 22:22:03 +02:00
|
|
|
} from '../../lib'
|
|
|
|
import {
|
|
|
|
podsAddValidator,
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
makeFriendsValidator,
|
|
|
|
setBodyHostPort,
|
2017-08-02 21:50:42 +02:00
|
|
|
setBodyHostsPort,
|
|
|
|
podRemoveValidator
|
2017-05-15 22:22:03 +02:00
|
|
|
} from '../../middlewares'
|
2017-06-10 22:15:25 +02:00
|
|
|
import {
|
|
|
|
PodInstance
|
|
|
|
} from '../../models'
|
2017-08-25 11:45:31 +02:00
|
|
|
import { Pod as FormattedPod } from '../../../shared'
|
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
|
|
|
)
|
2017-08-02 21:50:42 +02:00
|
|
|
podsRouter.delete('/:id',
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
podRemoveValidator,
|
|
|
|
removeFriendController
|
|
|
|
)
|
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-07-05 13:26:25 +02:00
|
|
|
const pod = db.Pod.build(informations)
|
|
|
|
pod.save()
|
|
|
|
.then(podCreated => {
|
|
|
|
return sendOwnedVideosToPod(podCreated.id)
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return getMyPublicCert()
|
|
|
|
})
|
|
|
|
.then(cert => {
|
|
|
|
return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
|
|
|
|
})
|
|
|
|
.catch(err => next(err))
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-05 13:26:25 +02:00
|
|
|
db.Pod.list()
|
2017-08-25 11:45:31 +02:00
|
|
|
.then(podsList => res.json(getFormattedObjects<FormattedPod, PodInstance>(podsList, podsList.length)))
|
2017-07-05 13:26:25 +02:00
|
|
|
.catch(err => next(err))
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
makeFriends(hosts)
|
|
|
|
.then(() => logger.info('Made friends!'))
|
2017-07-07 18:26:12 +02:00
|
|
|
.catch(err => logger.error('Could not make friends.', err))
|
2017-05-15 22:22:03 +02:00
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
// Don't wait the process that could be long
|
2017-05-15 22:22:03 +02:00
|
|
|
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-07-05 13:26:25 +02:00
|
|
|
quitFriends()
|
|
|
|
.then(() => res.type('json').status(204).end())
|
|
|
|
.catch(err => next(err))
|
2017-05-15 22:22:03 +02:00
|
|
|
}
|
2017-08-02 21:50:42 +02:00
|
|
|
|
|
|
|
function removeFriendController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const pod = res.locals.pod as PodInstance
|
|
|
|
|
|
|
|
removeFriend(pod)
|
|
|
|
.then(() => (res.type('json').status(204).end()))
|
|
|
|
.catch(err => next(err))
|
|
|
|
}
|