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