1
0
Fork 0
peertube/server/controllers/activitypub/inbox.ts

73 lines
2.6 KiB
TypeScript
Raw Normal View History

2017-11-09 11:51:58 -05:00
import * as express from 'express'
2020-12-15 07:34:58 -05:00
import { InboxManager } from '@server/lib/activitypub/inbox-manager'
2017-11-22 04:29:55 -05:00
import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, RootActivity } from '../../../shared'
2020-12-15 07:34:58 -05:00
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
2017-11-10 08:34:45 -05:00
import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
2017-12-28 05:16:08 -05:00
import { logger } from '../../helpers/logger'
import { asyncMiddleware, checkSignature, localAccountValidator, localVideoChannelValidator, signatureValidator } from '../../middlewares'
2017-11-10 08:34:45 -05:00
import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
2017-11-09 11:51:58 -05:00
const inboxRouter = express.Router()
2017-11-13 11:39:41 -05:00
inboxRouter.post('/inbox',
2017-11-09 11:51:58 -05:00
signatureValidator,
asyncMiddleware(checkSignature),
asyncMiddleware(activityPubValidator),
2018-09-14 10:47:15 -04:00
inboxController
2017-11-09 11:51:58 -05:00
)
2018-01-03 05:36:03 -05:00
inboxRouter.post('/accounts/:name/inbox',
2017-11-13 11:39:41 -05:00
signatureValidator,
asyncMiddleware(checkSignature),
2018-04-06 05:54:24 -04:00
asyncMiddleware(localAccountValidator),
asyncMiddleware(activityPubValidator),
2018-09-14 10:47:15 -04:00
inboxController
2017-11-13 11:39:41 -05:00
)
inboxRouter.post('/video-channels/:name/inbox',
signatureValidator,
asyncMiddleware(checkSignature),
asyncMiddleware(localVideoChannelValidator),
asyncMiddleware(activityPubValidator),
2018-09-14 10:47:15 -04:00
inboxController
)
2017-11-13 11:39:41 -05:00
2017-11-09 11:51:58 -05:00
// ---------------------------------------------------------------------------
export {
inboxRouter
}
// ---------------------------------------------------------------------------
2018-11-14 09:01:28 -05:00
function inboxController (req: express.Request, res: express.Response) {
2017-11-09 11:51:58 -05:00
const rootActivity: RootActivity = req.body
let activities: Activity[]
2017-11-09 11:51:58 -05:00
2020-02-28 10:03:39 -05:00
if ([ 'Collection', 'CollectionPage' ].includes(rootActivity.type)) {
2017-11-09 11:51:58 -05:00
activities = (rootActivity as ActivityPubCollection).items
2020-02-28 10:03:39 -05:00
} else if ([ 'OrderedCollection', 'OrderedCollectionPage' ].includes(rootActivity.type)) {
2017-11-23 10:55:13 -05:00
activities = (rootActivity as ActivityPubOrderedCollection<Activity>).orderedItems
2017-11-09 11:51:58 -05:00
} else {
activities = [ rootActivity as Activity ]
}
2017-11-10 08:34:45 -05:00
// Only keep activities we are able to process
2017-11-30 08:15:17 -05:00
logger.debug('Filtering %d activities...', activities.length)
2017-11-10 08:34:45 -05:00
activities = activities.filter(a => isActivityValid(a))
2017-11-14 11:31:26 -05:00
logger.debug('We keep %d activities.', activities.length, { activities })
2017-11-10 08:34:45 -05:00
2019-03-19 05:35:15 -04:00
const accountOrChannel = res.locals.account || res.locals.videoChannel
2017-12-14 11:38:41 -05:00
logger.info('Receiving inbox requests for %d activities by %s.', activities.length, res.locals.signature.actor.url)
2018-02-28 12:04:46 -05:00
2020-12-15 07:34:58 -05:00
InboxManager.Instance.addInboxMessage({
2018-09-14 10:47:15 -04:00
activities,
signatureActor: res.locals.signature.actor,
2020-12-15 07:34:58 -05:00
inboxActor: accountOrChannel
? accountOrChannel.Actor
: undefined
2018-09-14 10:47:15 -04:00
})
2017-11-09 11:51:58 -05:00
return res.status(HttpStatusCode.NO_CONTENT_204).end()
2017-11-09 11:51:58 -05:00
}