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/activitypub/inbox.ts | |
parent | 343ad675f2a26c15b86150a9a3552e619d5d44f4 (diff) | |
download | PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.gz PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.tar.zst PeerTube-e4f97babf701481b55cc10fb3448feab5f97c867.zip |
Begin activitypub
Diffstat (limited to 'server/controllers/activitypub/inbox.ts')
-rw-r--r-- | server/controllers/activitypub/inbox.ts | 72 |
1 files changed, 72 insertions, 0 deletions
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 | } | ||