diff options
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/api/pods.ts | 53 | ||||
-rw-r--r-- | server/controllers/api/remote/pods.ts | 45 |
2 files changed, 57 insertions, 41 deletions
diff --git a/server/controllers/api/pods.ts b/server/controllers/api/pods.ts index e1e8ff6ca..ec94efc35 100644 --- a/server/controllers/api/pods.ts +++ b/server/controllers/api/pods.ts | |||
@@ -1,39 +1,33 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | 2 | ||
3 | import { database as db } from '../../initializers/database' | 3 | import { database as db } from '../../initializers/database' |
4 | import { CONFIG } from '../../initializers' | 4 | import { logger, getFormattedObjects } from '../../helpers' |
5 | import { | 5 | import { |
6 | logger, | ||
7 | getMyPublicCert, | ||
8 | getFormattedObjects | ||
9 | } from '../../helpers' | ||
10 | import { | ||
11 | sendOwnedVideosToPod, | ||
12 | makeFriends, | 6 | makeFriends, |
13 | quitFriends, | 7 | quitFriends, |
14 | removeFriend | 8 | removeFriend |
15 | } from '../../lib' | 9 | } from '../../lib' |
16 | import { | 10 | import { |
17 | podsAddValidator, | ||
18 | authenticate, | 11 | authenticate, |
19 | ensureIsAdmin, | 12 | ensureIsAdmin, |
20 | makeFriendsValidator, | 13 | makeFriendsValidator, |
21 | setBodyHostPort, | ||
22 | setBodyHostsPort, | 14 | setBodyHostsPort, |
23 | podRemoveValidator | 15 | podRemoveValidator, |
16 | paginationValidator, | ||
17 | setPagination, | ||
18 | setPodsSort, | ||
19 | podsSortValidator | ||
24 | } from '../../middlewares' | 20 | } from '../../middlewares' |
25 | import { | 21 | import { PodInstance } from '../../models' |
26 | PodInstance | ||
27 | } from '../../models' | ||
28 | import { Pod as FormattedPod } from '../../../shared' | ||
29 | 22 | ||
30 | const podsRouter = express.Router() | 23 | const podsRouter = express.Router() |
31 | 24 | ||
32 | podsRouter.get('/', listPods) | 25 | podsRouter.get('/', |
33 | podsRouter.post('/', | 26 | paginationValidator, |
34 | setBodyHostPort, // We need to modify the host before running the validator! | 27 | podsSortValidator, |
35 | podsAddValidator, | 28 | setPodsSort, |
36 | addPods | 29 | setPagination, |
30 | listPods | ||
37 | ) | 31 | ) |
38 | podsRouter.post('/make-friends', | 32 | podsRouter.post('/make-friends', |
39 | authenticate, | 33 | authenticate, |
@@ -62,26 +56,9 @@ export { | |||
62 | 56 | ||
63 | // --------------------------------------------------------------------------- | 57 | // --------------------------------------------------------------------------- |
64 | 58 | ||
65 | function addPods (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
66 | const informations = req.body | ||
67 | |||
68 | const pod = db.Pod.build(informations) | ||
69 | pod.save() | ||
70 | .then(podCreated => { | ||
71 | return sendOwnedVideosToPod(podCreated.id) | ||
72 | }) | ||
73 | .then(() => { | ||
74 | return getMyPublicCert() | ||
75 | }) | ||
76 | .then(cert => { | ||
77 | return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL }) | ||
78 | }) | ||
79 | .catch(err => next(err)) | ||
80 | } | ||
81 | |||
82 | function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { | 59 | function listPods (req: express.Request, res: express.Response, next: express.NextFunction) { |
83 | db.Pod.list() | 60 | db.Pod.listForApi(req.query.start, req.query.count, req.query.sort) |
84 | .then(podsList => res.json(getFormattedObjects<FormattedPod, PodInstance>(podsList, podsList.length))) | 61 | .then(resultList => res.json(getFormattedObjects(resultList.data, resultList.total))) |
85 | .catch(err => next(err)) | 62 | .catch(err => next(err)) |
86 | } | 63 | } |
87 | 64 | ||
diff --git a/server/controllers/api/remote/pods.ts b/server/controllers/api/remote/pods.ts index 69bbd4378..6f7b5f651 100644 --- a/server/controllers/api/remote/pods.ts +++ b/server/controllers/api/remote/pods.ts | |||
@@ -1,18 +1,34 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | 2 | ||
3 | import { database as db } from '../../../initializers/database' | 3 | import { database as db } from '../../../initializers/database' |
4 | import { checkSignature, signatureValidator } from '../../../middlewares' | 4 | import { |
5 | import { PodSignature } from '../../../../shared' | 5 | checkSignature, |
6 | signatureValidator, | ||
7 | setBodyHostPort, | ||
8 | remotePodsAddValidator | ||
9 | } from '../../../middlewares' | ||
10 | import { sendOwnedVideosToPod } from '../../../lib' | ||
11 | import { getMyPublicCert, getFormattedObjects } from '../../../helpers' | ||
12 | import { CONFIG } from '../../../initializers' | ||
13 | import { PodInstance } from '../../../models' | ||
14 | import { PodSignature, Pod as FormattedPod } from '../../../../shared' | ||
6 | 15 | ||
7 | const remotePodsRouter = express.Router() | 16 | const remotePodsRouter = express.Router() |
8 | 17 | ||
9 | // Post because this is a secured request | ||
10 | remotePodsRouter.post('/remove', | 18 | remotePodsRouter.post('/remove', |
11 | signatureValidator, | 19 | signatureValidator, |
12 | checkSignature, | 20 | checkSignature, |
13 | removePods | 21 | removePods |
14 | ) | 22 | ) |
15 | 23 | ||
24 | remotePodsRouter.post('/list', remotePodsList) | ||
25 | |||
26 | remotePodsRouter.post('/add', | ||
27 | setBodyHostPort, // We need to modify the host before running the validator! | ||
28 | remotePodsAddValidator, | ||
29 | addPods | ||
30 | ) | ||
31 | |||
16 | // --------------------------------------------------------------------------- | 32 | // --------------------------------------------------------------------------- |
17 | 33 | ||
18 | export { | 34 | export { |
@@ -21,6 +37,29 @@ export { | |||
21 | 37 | ||
22 | // --------------------------------------------------------------------------- | 38 | // --------------------------------------------------------------------------- |
23 | 39 | ||
40 | function addPods (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
41 | const information = req.body | ||
42 | |||
43 | const pod = db.Pod.build(information) | ||
44 | pod.save() | ||
45 | .then(podCreated => { | ||
46 | return sendOwnedVideosToPod(podCreated.id) | ||
47 | }) | ||
48 | .then(() => { | ||
49 | return getMyPublicCert() | ||
50 | }) | ||
51 | .then(cert => { | ||
52 | return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL }) | ||
53 | }) | ||
54 | .catch(err => next(err)) | ||
55 | } | ||
56 | |||
57 | function remotePodsList (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
58 | db.Pod.list() | ||
59 | .then(podsList => res.json(getFormattedObjects<FormattedPod, PodInstance>(podsList, podsList.length))) | ||
60 | .catch(err => next(err)) | ||
61 | } | ||
62 | |||
24 | function removePods (req: express.Request, res: express.Response, next: express.NextFunction) { | 63 | function removePods (req: express.Request, res: express.Response, next: express.NextFunction) { |
25 | const signature: PodSignature = req.body.signature | 64 | const signature: PodSignature = req.body.signature |
26 | const host = signature.host | 65 | const host = signature.host |