PeerTube/server/controllers/activitypub/inbox.ts

80 lines
3.0 KiB
TypeScript
Raw Normal View History

2017-11-09 17:51:58 +01:00
import * as express from 'express'
2017-11-10 14:34:45 +01:00
import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, ActivityType, RootActivity } from '../../../shared'
2017-11-09 17:51:58 +01:00
import { logger } from '../../helpers'
2017-11-10 14:34:45 +01:00
import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
import { processCreateActivity, processFlagActivity, processUpdateActivity } from '../../lib'
2017-11-13 17:39:41 +01:00
import { processAcceptActivity } from '../../lib/activitypub/process-accept'
2017-11-10 14:34:45 +01:00
import { processAddActivity } from '../../lib/activitypub/process-add'
2017-11-13 17:39:41 +01:00
import { processDeleteActivity } from '../../lib/activitypub/process-delete'
import { processFollowActivity } from '../../lib/activitypub/process-follow'
import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares'
2017-11-10 14:34:45 +01:00
import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
2017-11-13 17:39:41 +01:00
import { AccountInstance } from '../../models/account/account-interface'
2017-11-09 17:51:58 +01:00
2017-11-13 17:39:41 +01:00
const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = {
2017-11-09 17:51:58 +01:00
Create: processCreateActivity,
2017-11-10 14:34:45 +01:00
Add: processAddActivity,
2017-11-09 17:51:58 +01:00
Update: processUpdateActivity,
2017-11-13 17:39:41 +01:00
Flag: processFlagActivity,
Delete: processDeleteActivity,
Follow: processFollowActivity,
Accept: processAcceptActivity
2017-11-09 17:51:58 +01:00
}
const inboxRouter = express.Router()
2017-11-13 17:39:41 +01:00
inboxRouter.post('/inbox',
2017-11-09 17:51:58 +01:00
signatureValidator,
asyncMiddleware(checkSignature),
2017-11-10 14:34:45 +01:00
activityPubValidator,
2017-11-09 17:51:58 +01:00
asyncMiddleware(inboxController)
)
2017-11-13 17:39:41 +01:00
inboxRouter.post('/:nameWithHost/inbox',
signatureValidator,
asyncMiddleware(checkSignature),
localAccountValidator,
activityPubValidator,
asyncMiddleware(inboxController)
)
2017-11-09 17:51:58 +01: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) {
activities = (rootActivity as ActivityPubOrderedCollection).orderedItems
} else {
activities = [ rootActivity as Activity ]
}
2017-11-10 14:34:45 +01:00
// Only keep activities we are able to process
activities = activities.filter(a => isActivityValid(a))
2017-11-13 17:39:41 +01:00
await processActivities(activities, res.locals.account)
2017-11-09 17:51:58 +01:00
res.status(204).end()
}
2017-11-13 17:39:41 +01:00
async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) {
2017-11-09 17:51:58 +01:00
for (const activity of activities) {
const activityProcessor = processActivity[activity.type]
if (activityProcessor === undefined) {
logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id })
continue
}
2017-11-13 17:39:41 +01:00
await activityProcessor(activity, inboxAccount)
2017-11-09 17:51:58 +01:00
}
}