2017-11-09 10:51:58 -06:00
|
|
|
import * as express from 'express'
|
2017-11-22 03:29:55 -06:00
|
|
|
import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, RootActivity } from '../../../shared'
|
2017-11-10 07:34:45 -06:00
|
|
|
import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { logger } from '../../helpers/logger'
|
2017-11-22 03:29:55 -06:00
|
|
|
import { processActivities } from '../../lib/activitypub/process/process'
|
2017-11-13 10:39:41 -06:00
|
|
|
import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares'
|
2017-11-10 07:34:45 -06:00
|
|
|
import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { ActorModel } from '../../models/activitypub/actor'
|
2017-11-09 10:51:58 -06:00
|
|
|
|
|
|
|
const inboxRouter = express.Router()
|
|
|
|
|
2017-11-13 10:39:41 -06:00
|
|
|
inboxRouter.post('/inbox',
|
2017-11-09 10:51:58 -06:00
|
|
|
signatureValidator,
|
|
|
|
asyncMiddleware(checkSignature),
|
2018-02-23 08:09:12 -06:00
|
|
|
asyncMiddleware(activityPubValidator),
|
2017-11-09 10:51:58 -06:00
|
|
|
asyncMiddleware(inboxController)
|
|
|
|
)
|
|
|
|
|
2018-01-03 04:36:03 -06:00
|
|
|
inboxRouter.post('/accounts/:name/inbox',
|
2017-11-13 10:39:41 -06:00
|
|
|
signatureValidator,
|
|
|
|
asyncMiddleware(checkSignature),
|
2018-04-06 04:54:24 -05:00
|
|
|
asyncMiddleware(localAccountValidator),
|
2018-02-23 08:09:12 -06:00
|
|
|
asyncMiddleware(activityPubValidator),
|
2017-11-13 10:39:41 -06:00
|
|
|
asyncMiddleware(inboxController)
|
|
|
|
)
|
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
inboxRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function inboxController (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const rootActivity: RootActivity = req.body
|
|
|
|
let activities: Activity[] = []
|
|
|
|
|
2018-04-06 04:54:24 -05:00
|
|
|
console.log(rootActivity)
|
|
|
|
|
2017-11-09 10:51:58 -06:00
|
|
|
if ([ 'Collection', 'CollectionPage' ].indexOf(rootActivity.type) !== -1) {
|
|
|
|
activities = (rootActivity as ActivityPubCollection).items
|
|
|
|
} else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].indexOf(rootActivity.type) !== -1) {
|
2017-11-23 09:55:13 -06:00
|
|
|
activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
|
2017-11-09 10:51:58 -06:00
|
|
|
} else {
|
|
|
|
activities = [ rootActivity as Activity ]
|
|
|
|
}
|
|
|
|
|
2017-11-10 07:34:45 -06:00
|
|
|
// Only keep activities we are able to process
|
2017-11-30 07:15:17 -06:00
|
|
|
logger.debug('Filtering %d activities...', activities.length)
|
2017-11-10 07:34:45 -06:00
|
|
|
activities = activities.filter(a => isActivityValid(a))
|
2017-11-14 10:31:26 -06:00
|
|
|
logger.debug('We keep %d activities.', activities.length, { activities })
|
2017-11-10 07:34:45 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
let specificActor: ActorModel = undefined
|
|
|
|
if (res.locals.account) {
|
|
|
|
specificActor = res.locals.account
|
|
|
|
} else if (res.locals.videoChannel) {
|
|
|
|
specificActor = res.locals.videoChannel
|
|
|
|
}
|
|
|
|
|
2018-03-19 09:02:36 -05:00
|
|
|
logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
|
2018-02-28 11:04:46 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
await processActivities(activities, res.locals.signature.actor, specificActor)
|
2017-11-09 10:51:58 -06:00
|
|
|
|
|
|
|
res.status(204).end()
|
|
|
|
}
|