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-10 13:34:45 +00:00
|
|
|
import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
|
2017-12-28 10:16:08 +00:00
|
|
|
import { logger } from '../../helpers/logger'
|
2017-11-22 09:29:55 +00:00
|
|
|
import { processActivities } from '../../lib/activitypub/process/process'
|
2018-08-16 13:25:20 +00:00
|
|
|
import { asyncMiddleware, checkSignature, localAccountValidator, localVideoChannelValidator, signatureValidator } from '../../middlewares'
|
2017-11-10 13:34:45 +00:00
|
|
|
import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
|
2018-09-14 14:47:15 +00:00
|
|
|
import { queue } from 'async'
|
|
|
|
import { ActorModel } from '../../models/activitypub/actor'
|
2019-08-09 06:17:16 +00:00
|
|
|
import { SignatureActorModel } from '../../typings/models'
|
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),
|
2018-02-23 14:09:12 +00:00
|
|
|
asyncMiddleware(activityPubValidator),
|
2018-09-14 14:47:15 +00:00
|
|
|
inboxController
|
2017-11-09 16:51:58 +00:00
|
|
|
)
|
|
|
|
|
2018-01-03 10:36:03 +00:00
|
|
|
inboxRouter.post('/accounts/:name/inbox',
|
2017-11-13 16:39:41 +00:00
|
|
|
signatureValidator,
|
|
|
|
asyncMiddleware(checkSignature),
|
2018-04-06 09:54:24 +00:00
|
|
|
asyncMiddleware(localAccountValidator),
|
2018-02-23 14:09:12 +00:00
|
|
|
asyncMiddleware(activityPubValidator),
|
2018-09-14 14:47:15 +00:00
|
|
|
inboxController
|
2017-11-13 16:39:41 +00:00
|
|
|
)
|
2018-08-16 13:25:20 +00:00
|
|
|
inboxRouter.post('/video-channels/:name/inbox',
|
|
|
|
signatureValidator,
|
|
|
|
asyncMiddleware(checkSignature),
|
|
|
|
asyncMiddleware(localVideoChannelValidator),
|
|
|
|
asyncMiddleware(activityPubValidator),
|
2018-09-14 14:47:15 +00:00
|
|
|
inboxController
|
2018-08-16 13:25:20 +00:00
|
|
|
)
|
2017-11-13 16:39:41 +00:00
|
|
|
|
2017-11-09 16:51:58 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
inboxRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-09 06:17:16 +00:00
|
|
|
const inboxQueue = queue<{ activities: Activity[], signatureActor?: SignatureActorModel, inboxActor?: ActorModel }, Error>((task, cb) => {
|
2018-11-14 14:01:28 +00:00
|
|
|
const options = { signatureActor: task.signatureActor, inboxActor: task.inboxActor }
|
|
|
|
|
|
|
|
processActivities(task.activities, options)
|
2018-09-14 14:47:15 +00:00
|
|
|
.then(() => cb())
|
|
|
|
})
|
|
|
|
|
2018-11-14 14:01:28 +00:00
|
|
|
function inboxController (req: express.Request, res: express.Response) {
|
2017-11-09 16:51:58 +00:00
|
|
|
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-30 13:15:17 +00:00
|
|
|
logger.debug('Filtering %d activities...', activities.length)
|
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
|
|
|
|
2019-03-19 09:35:15 +00:00
|
|
|
const accountOrChannel = res.locals.account || res.locals.videoChannel
|
2017-12-14 16:38:41 +00:00
|
|
|
|
2018-03-19 14:02:36 +00:00
|
|
|
logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
|
2018-02-28 17:04:46 +00:00
|
|
|
|
2018-09-14 14:47:15 +00:00
|
|
|
inboxQueue.push({
|
|
|
|
activities,
|
|
|
|
signatureActor: res.locals.signature.actor,
|
|
|
|
inboxActor: accountOrChannel ? accountOrChannel.Actor : undefined
|
|
|
|
})
|
2017-11-09 16:51:58 +00:00
|
|
|
|
2018-09-14 14:47:15 +00:00
|
|
|
return res.status(204).end()
|
2017-11-09 16:51:58 +00:00
|
|
|
}
|