]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, RootActivity } from '../../../shared' | |
3 | import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity' | |
4 | import { logger } from '../../helpers/logger' | |
5 | import { processActivities } from '../../lib/activitypub/process/process' | |
6 | import { asyncMiddleware, checkSignature, localAccountValidator, localVideoChannelValidator, signatureValidator } from '../../middlewares' | |
7 | import { activityPubValidator } from '../../middlewares/validators/activitypub/activity' | |
8 | import { VideoChannelModel } from '../../models/video/video-channel' | |
9 | import { AccountModel } from '../../models/account/account' | |
10 | ||
11 | const inboxRouter = express.Router() | |
12 | ||
13 | inboxRouter.post('/inbox', | |
14 | signatureValidator, | |
15 | asyncMiddleware(checkSignature), | |
16 | asyncMiddleware(activityPubValidator), | |
17 | asyncMiddleware(inboxController) | |
18 | ) | |
19 | ||
20 | inboxRouter.post('/accounts/:name/inbox', | |
21 | signatureValidator, | |
22 | asyncMiddleware(checkSignature), | |
23 | asyncMiddleware(localAccountValidator), | |
24 | asyncMiddleware(activityPubValidator), | |
25 | asyncMiddleware(inboxController) | |
26 | ) | |
27 | inboxRouter.post('/video-channels/:name/inbox', | |
28 | signatureValidator, | |
29 | asyncMiddleware(checkSignature), | |
30 | asyncMiddleware(localVideoChannelValidator), | |
31 | asyncMiddleware(activityPubValidator), | |
32 | asyncMiddleware(inboxController) | |
33 | ) | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | ||
37 | export { | |
38 | inboxRouter | |
39 | } | |
40 | ||
41 | // --------------------------------------------------------------------------- | |
42 | ||
43 | async function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) { | |
44 | const rootActivity: RootActivity = req.body | |
45 | let activities: Activity[] = [] | |
46 | ||
47 | if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) { | |
48 | activities = (rootActivity as ActivityPubCollection).items | |
49 | } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) { | |
50 | activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems | |
51 | } else { | |
52 | activities = [ rootActivity as Activity ] | |
53 | } | |
54 | ||
55 | // Only keep activities we are able to process | |
56 | logger.debug('Filtering %d activities...', activities.length) | |
57 | activities = activities.filter(a => isActivityValid(a)) | |
58 | logger.debug('We keep %d activities.', activities.length, { activities }) | |
59 | ||
60 | let accountOrChannel: VideoChannelModel | AccountModel | |
61 | if (res.locals.account) { | |
62 | accountOrChannel = res.locals.account | |
63 | } else if (res.locals.videoChannel) { | |
64 | accountOrChannel = res.locals.videoChannel | |
65 | } | |
66 | ||
67 | logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url) | |
68 | ||
69 | await processActivities(activities, res.locals.signature.actor, accountOrChannel ? accountOrChannel.Actor : undefined) | |
70 | ||
71 | res.status(204).end() | |
72 | } |