diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-09 17:51:58 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:51 +0100 |
commit | e4f97babf701481b55cc10fb3448feab5f97c867 (patch) | |
tree | af37402a594dc5ff09f71ecb0687e8cfe4cdb471 /server/controllers | |
parent | 343ad675f2a26c15b86150a9a3552e619d5d44f4 (diff) | |
download | PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.gz PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.zst PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.zip |
Begin activitypub
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/activitypub/client.ts | 65 | ||||
-rw-r--r-- | server/controllers/activitypub/inbox.ts | 72 | ||||
-rw-r--r-- | server/controllers/activitypub/index.ts | 15 | ||||
-rw-r--r-- | server/controllers/activitypub/pods.ts (renamed from server/controllers/api/remote/pods.ts) | 0 | ||||
-rw-r--r-- | server/controllers/activitypub/videos.ts (renamed from server/controllers/api/remote/videos.ts) | 0 | ||||
-rw-r--r-- | server/controllers/api/remote/index.ts | 18 |
6 files changed, 152 insertions, 18 deletions
diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts new file mode 100644 index 000000000..28d08b3f4 --- /dev/null +++ b/server/controllers/activitypub/client.ts | |||
@@ -0,0 +1,65 @@ | |||
1 | // Intercept ActivityPub client requests | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { database as db } from '../../initializers' | ||
5 | import { executeIfActivityPub, localAccountValidator } from '../../middlewares' | ||
6 | import { pageToStartAndCount } from '../../helpers' | ||
7 | import { AccountInstance } from '../../models' | ||
8 | import { activityPubCollectionPagination } from '../../helpers/activitypub' | ||
9 | import { ACTIVITY_PUB } from '../../initializers/constants' | ||
10 | import { asyncMiddleware } from '../../middlewares/async' | ||
11 | |||
12 | const activityPubClientRouter = express.Router() | ||
13 | |||
14 | activityPubClientRouter.get('/account/:name', | ||
15 | executeIfActivityPub(localAccountValidator), | ||
16 | executeIfActivityPub(asyncMiddleware(accountController)) | ||
17 | ) | ||
18 | |||
19 | activityPubClientRouter.get('/account/:name/followers', | ||
20 | executeIfActivityPub(localAccountValidator), | ||
21 | executeIfActivityPub(asyncMiddleware(accountFollowersController)) | ||
22 | ) | ||
23 | |||
24 | activityPubClientRouter.get('/account/:name/following', | ||
25 | executeIfActivityPub(localAccountValidator), | ||
26 | executeIfActivityPub(asyncMiddleware(accountFollowingController)) | ||
27 | ) | ||
28 | |||
29 | // --------------------------------------------------------------------------- | ||
30 | |||
31 | export { | ||
32 | activityPubClientRouter | ||
33 | } | ||
34 | |||
35 | // --------------------------------------------------------------------------- | ||
36 | |||
37 | async function accountController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
38 | const account: AccountInstance = res.locals.account | ||
39 | |||
40 | return res.json(account.toActivityPubObject()).end() | ||
41 | } | ||
42 | |||
43 | async function accountFollowersController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
44 | const account: AccountInstance = res.locals.account | ||
45 | |||
46 | const page = req.params.page || 1 | ||
47 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | ||
48 | |||
49 | const result = await db.Account.listFollowerUrlsForApi(account.name, start, count) | ||
50 | const activityPubResult = activityPubCollectionPagination(req.url, page, result) | ||
51 | |||
52 | return res.json(activityPubResult) | ||
53 | } | ||
54 | |||
55 | async function accountFollowingController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
56 | const account: AccountInstance = res.locals.account | ||
57 | |||
58 | const page = req.params.page || 1 | ||
59 | const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) | ||
60 | |||
61 | const result = await db.Account.listFollowingUrlsForApi(account.name, start, count) | ||
62 | const activityPubResult = activityPubCollectionPagination(req.url, page, result) | ||
63 | |||
64 | return res.json(activityPubResult) | ||
65 | } | ||
diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts new file mode 100644 index 000000000..79d989c2c --- /dev/null +++ b/server/controllers/activitypub/inbox.ts | |||
@@ -0,0 +1,72 @@ | |||
1 | import * as express from 'express' | ||
2 | |||
3 | import { | ||
4 | processCreateActivity, | ||
5 | processUpdateActivity, | ||
6 | processFlagActivity | ||
7 | } from '../../lib' | ||
8 | import { | ||
9 | Activity, | ||
10 | ActivityType, | ||
11 | RootActivity, | ||
12 | ActivityPubCollection, | ||
13 | ActivityPubOrderedCollection | ||
14 | } from '../../../shared' | ||
15 | import { | ||
16 | signatureValidator, | ||
17 | checkSignature, | ||
18 | asyncMiddleware | ||
19 | } from '../../middlewares' | ||
20 | import { logger } from '../../helpers' | ||
21 | |||
22 | const processActivity: { [ P in ActivityType ]: (activity: Activity) => Promise<any> } = { | ||
23 | Create: processCreateActivity, | ||
24 | Update: processUpdateActivity, | ||
25 | Flag: processFlagActivity | ||
26 | } | ||
27 | |||
28 | const inboxRouter = express.Router() | ||
29 | |||
30 | inboxRouter.post('/', | ||
31 | signatureValidator, | ||
32 | asyncMiddleware(checkSignature), | ||
33 | // inboxValidator, | ||
34 | asyncMiddleware(inboxController) | ||
35 | ) | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | export { | ||
40 | inboxRouter | ||
41 | } | ||
42 | |||
43 | // --------------------------------------------------------------------------- | ||
44 | |||
45 | async function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
46 | const rootActivity: RootActivity = req.body | ||
47 | let activities: Activity[] = [] | ||
48 | |||
49 | if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) { | ||
50 | activities = (rootActivity as ActivityPubCollection).items | ||
51 | } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) { | ||
52 | activities = (rootActivity as ActivityPubOrderedCollection).orderedItems | ||
53 | } else { | ||
54 | activities = [ rootActivity as Activity ] | ||
55 | } | ||
56 | |||
57 | await processActivities(activities) | ||
58 | |||
59 | res.status(204).end() | ||
60 | } | ||
61 | |||
62 | async function processActivities (activities: Activity[]) { | ||
63 | for (const activity of activities) { | ||
64 | const activityProcessor = processActivity[activity.type] | ||
65 | if (activityProcessor === undefined) { | ||
66 | logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id }) | ||
67 | continue | ||
68 | } | ||
69 | |||
70 | await activityProcessor(activity) | ||
71 | } | ||
72 | } | ||
diff --git a/server/controllers/activitypub/index.ts b/server/controllers/activitypub/index.ts new file mode 100644 index 000000000..7a4602b37 --- /dev/null +++ b/server/controllers/activitypub/index.ts | |||
@@ -0,0 +1,15 @@ | |||
1 | import * as express from 'express' | ||
2 | |||
3 | import { badRequest } from '../../helpers' | ||
4 | import { inboxRouter } from './inbox' | ||
5 | |||
6 | const remoteRouter = express.Router() | ||
7 | |||
8 | remoteRouter.use('/inbox', inboxRouter) | ||
9 | remoteRouter.use('/*', badRequest) | ||
10 | |||
11 | // --------------------------------------------------------------------------- | ||
12 | |||
13 | export { | ||
14 | remoteRouter | ||
15 | } | ||
diff --git a/server/controllers/api/remote/pods.ts b/server/controllers/activitypub/pods.ts index 326eb61ac..326eb61ac 100644 --- a/server/controllers/api/remote/pods.ts +++ b/server/controllers/activitypub/pods.ts | |||
diff --git a/server/controllers/api/remote/videos.ts b/server/controllers/activitypub/videos.ts index cba47f0a1..cba47f0a1 100644 --- a/server/controllers/api/remote/videos.ts +++ b/server/controllers/activitypub/videos.ts | |||
diff --git a/server/controllers/api/remote/index.ts b/server/controllers/api/remote/index.ts deleted file mode 100644 index d3522772b..000000000 --- a/server/controllers/api/remote/index.ts +++ /dev/null | |||
@@ -1,18 +0,0 @@ | |||
1 | import * as express from 'express' | ||
2 | |||
3 | import { badRequest } from '../../../helpers' | ||
4 | |||
5 | import { remotePodsRouter } from './pods' | ||
6 | import { remoteVideosRouter } from './videos' | ||
7 | |||
8 | const remoteRouter = express.Router() | ||
9 | |||
10 | remoteRouter.use('/pods', remotePodsRouter) | ||
11 | remoteRouter.use('/videos', remoteVideosRouter) | ||
12 | remoteRouter.use('/*', badRequest) | ||
13 | |||
14 | // --------------------------------------------------------------------------- | ||
15 | |||
16 | export { | ||
17 | remoteRouter | ||
18 | } | ||