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 { queue } from 'async'
9 import { MActorDefault, MActorSignature } from '../../typings/models'
11 const inboxRouter = express.Router()
13 inboxRouter.post('/inbox',
15 asyncMiddleware(checkSignature),
16 asyncMiddleware(activityPubValidator),
20 inboxRouter.post('/accounts/:name/inbox',
22 asyncMiddleware(checkSignature),
23 asyncMiddleware(localAccountValidator),
24 asyncMiddleware(activityPubValidator),
27 inboxRouter.post('/video-channels/:name/inbox',
29 asyncMiddleware(checkSignature),
30 asyncMiddleware(localVideoChannelValidator),
31 asyncMiddleware(activityPubValidator),
35 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 type QueueParam = { activities: Activity[], signatureActor?: MActorSignature, inboxActor?: MActorDefault }
44 const inboxQueue = queue<QueueParam, Error>((task, cb) => {
45 const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor }
47 processActivities(task.activities, options)
51 function inboxController (req: express.Request, res: express.Response) {
52 const rootActivity: RootActivity = req.body
53 let activities: Activity[] = []
55 if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) {
56 activities = (rootActivity as ActivityPubCollection).items
57 } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) {
58 activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
60 activities = [ rootActivity as Activity ]
63 // Only keep activities we are able to process
64 logger.debug('Filtering %d activities...', activities.length)
65 activities = activities.filter(a => isActivityValid(a))
66 logger.debug('We keep %d activities.', activities.length, { activities })
68 const accountOrChannel = res.locals.account || res.locals.videoChannel
70 logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
74 signatureActor: res.locals.signature.actor,
75 inboxActor: accountOrChannel ? accountOrChannel.Actor : undefined
78 return res.status(204).end()