2017-11-09 16:51:58 +00:00
|
|
|
import * as express from 'express'
|
2017-11-22 09:29:55 +00:00
|
|
|
import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, RootActivity } from '../../../shared'
|
2017-11-09 16:51:58 +00:00
|
|
|
import { logger } from '../../helpers'
|
2017-11-10 13:34:45 +00:00
|
|
|
import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
|
2017-11-22 09:29:55 +00:00
|
|
|
import { processActivities } from '../../lib/activitypub/process/process'
|
2017-11-13 16:39:41 +00:00
|
|
|
import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares'
|
2017-11-10 13:34:45 +00:00
|
|
|
import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
|
2017-11-09 16:51:58 +00:00
|
|
|
|
|
|
|
const inboxRouter = express.Router()
|
|
|
|
|
2017-11-13 16:39:41 +00:00
|
|
|
inboxRouter.post('/inbox',
|
2017-11-09 16:51:58 +00:00
|
|
|
signatureValidator,
|
|
|
|
asyncMiddleware(checkSignature),
|
2017-11-10 13:34:45 +00:00
|
|
|
activityPubValidator,
|
2017-11-09 16:51:58 +00:00
|
|
|
asyncMiddleware(inboxController)
|
|
|
|
)
|
|
|
|
|
2017-11-14 16:31:26 +00:00
|
|
|
inboxRouter.post('/account/:name/inbox',
|
2017-11-13 16:39:41 +00:00
|
|
|
signatureValidator,
|
|
|
|
asyncMiddleware(checkSignature),
|
|
|
|
localAccountValidator,
|
|
|
|
activityPubValidator,
|
|
|
|
asyncMiddleware(inboxController)
|
|
|
|
)
|
|
|
|
|
2017-11-09 16:51:58 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
inboxRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const rootActivity: RootActivity = req.body
|
|
|
|
let activities: Activity[] = []
|
|
|
|
|
|
|
|
if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) {
|
|
|
|
activities = (rootActivity as ActivityPubCollection).items
|
|
|
|
} else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) {
|
2017-11-23 15:55:13 +00:00
|
|
|
activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
|
2017-11-09 16:51:58 +00:00
|
|
|
} else {
|
|
|
|
activities = [ rootActivity as Activity ]
|
|
|
|
}
|
|
|
|
|
2017-11-10 13:34:45 +00:00
|
|
|
// Only keep activities we are able to process
|
2017-11-14 16:31:26 +00:00
|
|
|
logger.debug('Filtering activities...', { activities })
|
2017-11-10 13:34:45 +00:00
|
|
|
activities = activities.filter(a => isActivityValid(a))
|
2017-11-14 16:31:26 +00:00
|
|
|
logger.debug('We keep %d activities.', activities.length, { activities })
|
2017-11-10 13:34:45 +00:00
|
|
|
|
2017-11-13 16:39:41 +00:00
|
|
|
await processActivities(activities, res.locals.account)
|
2017-11-09 16:51:58 +00:00
|
|
|
|
|
|
|
res.status(204).end()
|
|
|
|
}
|