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'
11 const inboxRouter = express.Router()
13 inboxRouter.post('/inbox',
15 asyncMiddleware(checkSignature),
16 asyncMiddleware(activityPubValidator),
17 asyncMiddleware(inboxController)
20 inboxRouter.post('/accounts/:name/inbox',
22 asyncMiddleware(checkSignature),
23 asyncMiddleware(localAccountValidator),
24 asyncMiddleware(activityPubValidator),
25 asyncMiddleware(inboxController)
27 inboxRouter.post('/video-channels/:name/inbox',
29 asyncMiddleware(checkSignature),
30 asyncMiddleware(localVideoChannelValidator),
31 asyncMiddleware(activityPubValidator),
32 asyncMiddleware(inboxController)
35 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 async function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
44 const rootActivity: RootActivity = req.body
45 let activities: Activity[] = []
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
52 activities = [ rootActivity as Activity ]
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 })
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
67 logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
69 await processActivities(activities, res.locals.signature.actor, accountOrChannel ? accountOrChannel.Actor : undefined)