]>
Commit | Line | Data |
---|---|---|
e4f97bab | 1 | import * as express from 'express' |
0d0e8dd0 | 2 | import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, ActivityType, RootActivity } from '../../../shared' |
e4f97bab | 3 | import { logger } from '../../helpers' |
0d0e8dd0 C |
4 | import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity' |
5 | import { processCreateActivity, processFlagActivity, processUpdateActivity } from '../../lib' | |
7a7724e6 | 6 | import { processAcceptActivity } from '../../lib/activitypub/process-accept' |
0d0e8dd0 | 7 | import { processAddActivity } from '../../lib/activitypub/process-add' |
7a7724e6 C |
8 | import { processDeleteActivity } from '../../lib/activitypub/process-delete' |
9 | import { processFollowActivity } from '../../lib/activitypub/process-follow' | |
10 | import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares' | |
0d0e8dd0 | 11 | import { activityPubValidator } from '../../middlewares/validators/activitypub/activity' |
7a7724e6 | 12 | import { AccountInstance } from '../../models/account/account-interface' |
e4f97bab | 13 | |
7a7724e6 | 14 | const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = { |
e4f97bab | 15 | Create: processCreateActivity, |
0d0e8dd0 | 16 | Add: processAddActivity, |
e4f97bab | 17 | Update: processUpdateActivity, |
7a7724e6 C |
18 | Flag: processFlagActivity, |
19 | Delete: processDeleteActivity, | |
20 | Follow: processFollowActivity, | |
21 | Accept: processAcceptActivity | |
e4f97bab C |
22 | } |
23 | ||
24 | const inboxRouter = express.Router() | |
25 | ||
7a7724e6 | 26 | inboxRouter.post('/inbox', |
e4f97bab C |
27 | signatureValidator, |
28 | asyncMiddleware(checkSignature), | |
0d0e8dd0 | 29 | activityPubValidator, |
e4f97bab C |
30 | asyncMiddleware(inboxController) |
31 | ) | |
32 | ||
350e31d6 | 33 | inboxRouter.post('/account/:name/inbox', |
7a7724e6 C |
34 | signatureValidator, |
35 | asyncMiddleware(checkSignature), | |
36 | localAccountValidator, | |
37 | activityPubValidator, | |
38 | asyncMiddleware(inboxController) | |
39 | ) | |
40 | ||
e4f97bab C |
41 | // --------------------------------------------------------------------------- |
42 | ||
43 | export { | |
44 | inboxRouter | |
45 | } | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | async function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
50 | const rootActivity: RootActivity = req.body | |
51 | let activities: Activity[] = [] | |
52 | ||
53 | if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) { | |
54 | activities = (rootActivity as ActivityPubCollection).items | |
55 | } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) { | |
56 | activities = (rootActivity as ActivityPubOrderedCollection).orderedItems | |
57 | } else { | |
58 | activities = [ rootActivity as Activity ] | |
59 | } | |
60 | ||
0d0e8dd0 | 61 | // Only keep activities we are able to process |
350e31d6 | 62 | logger.debug('Filtering activities...', { activities }) |
0d0e8dd0 | 63 | activities = activities.filter(a => isActivityValid(a)) |
350e31d6 | 64 | logger.debug('We keep %d activities.', activities.length, { activities }) |
0d0e8dd0 | 65 | |
7a7724e6 | 66 | await processActivities(activities, res.locals.account) |
e4f97bab C |
67 | |
68 | res.status(204).end() | |
69 | } | |
70 | ||
7a7724e6 | 71 | async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) { |
e4f97bab C |
72 | for (const activity of activities) { |
73 | const activityProcessor = processActivity[activity.type] | |
74 | if (activityProcessor === undefined) { | |
75 | logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id }) | |
76 | continue | |
77 | } | |
78 | ||
7a7724e6 | 79 | await activityProcessor(activity, inboxAccount) |
e4f97bab C |
80 | } |
81 | } |