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)
50 logger.error('Error in process activities.', { err })
55 function inboxController (req: express.Request, res: express.Response) {
56 const rootActivity: RootActivity = req.body
57 let activities: Activity[]
59 if ([ 'Collection', 'CollectionPage' ].includes(rootActivity.type)) {
60 activities = (rootActivity as ActivityPubCollection).items
61 } else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].includes(rootActivity.type)) {
62 activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
64 activities = [ rootActivity as Activity ]
67 // Only keep activities we are able to process
68 logger.debug('Filtering %d activities...', activities.length)
69 activities = activities.filter(a => isActivityValid(a))
70 logger.debug('We keep %d activities.', activities.length, { activities })
72 const accountOrChannel = res.locals.account || res.locals.videoChannel
74 logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
78 signatureActor: res.locals.signature.actor,
79 inboxActor: accountOrChannel ? accountOrChannel.Actor : undefined
82 return res.status(204).end()