PeerTube/server/lib/activitypub/process/process.ts

76 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-12-12 10:53:50 -06:00
import { Activity, ActivityType } from '../../../../shared/models/activitypub'
2018-11-19 07:34:01 -06:00
import { checkUrlsSameHost, getAPUrl } from '../../../helpers/activitypub'
2017-12-28 04:16:08 -06:00
import { logger } from '../../../helpers/logger'
2017-12-14 10:38:41 -06:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-11-22 03:29:55 -06:00
import { processAcceptActivity } from './process-accept'
import { processAnnounceActivity } from './process-announce'
import { processCreateActivity } from './process-create'
import { processDeleteActivity } from './process-delete'
import { processFollowActivity } from './process-follow'
2017-11-23 07:19:55 -06:00
import { processLikeActivity } from './process-like'
2018-01-11 10:37:49 -06:00
import { processRejectActivity } from './process-reject'
2017-11-22 03:29:55 -06:00
import { processUndoActivity } from './process-undo'
import { processUpdateActivity } from './process-update'
import { getOrCreateActorAndServerAndModel } from '../actor'
2017-11-22 03:29:55 -06:00
const processActivity: { [ P in ActivityType ]: (activity: Activity, byActor: ActorModel, inboxActor?: ActorModel) => Promise<any> } = {
2017-11-22 03:29:55 -06:00
Create: processCreateActivity,
Update: processUpdateActivity,
Delete: processDeleteActivity,
Follow: processFollowActivity,
Accept: processAcceptActivity,
2018-01-11 10:37:49 -06:00
Reject: processRejectActivity,
2017-11-22 03:29:55 -06:00
Announce: processAnnounceActivity,
2017-11-23 07:19:55 -06:00
Undo: processUndoActivity,
Like: processLikeActivity
2017-11-22 03:29:55 -06:00
}
2018-11-14 08:01:28 -06:00
async function processActivities (
activities: Activity[],
options: {
signatureActor?: ActorModel
inboxActor?: ActorModel
outboxUrl?: string
} = {}) {
const actorsCache: { [ url: string ]: ActorModel } = {}
2017-11-22 03:29:55 -06:00
for (const activity of activities) {
if (!options.signatureActor && [ 'Create', 'Announce', 'Like' ].includes(activity.type) === false) {
2018-09-19 08:47:55 -05:00
logger.error('Cannot process activity %s (type: %s) without the actor signature.', activity.id, activity.type)
continue
}
2018-11-19 07:34:01 -06:00
const actorUrl = getAPUrl(activity.actor)
// When we fetch remote data, we don't have signature
2018-11-14 08:01:28 -06:00
if (options.signatureActor && actorUrl !== options.signatureActor.url) {
logger.warn('Signature mismatch between %s and %s, skipping.', actorUrl, options.signatureActor.url)
continue
}
2018-11-14 08:01:28 -06:00
if (options.outboxUrl && checkUrlsSameHost(options.outboxUrl, actorUrl) !== true) {
logger.warn('Host mismatch between outbox URL %s and actor URL %s, skipping.', options.outboxUrl, actorUrl)
continue
}
const byActor = options.signatureActor || actorsCache[actorUrl] || await getOrCreateActorAndServerAndModel(actorUrl)
actorsCache[actorUrl] = byActor
2017-11-22 03:29:55 -06:00
const activityProcessor = processActivity[activity.type]
if (activityProcessor === undefined) {
logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id })
continue
}
2017-11-23 09:55:13 -06:00
try {
2018-11-14 08:01:28 -06:00
await activityProcessor(activity, byActor, options.inboxActor)
2017-11-23 09:55:13 -06:00
} catch (err) {
2018-03-28 04:00:02 -05:00
logger.warn('Cannot process activity %s.', activity.type, { err })
2017-11-23 09:55:13 -06:00
}
2017-11-22 03:29:55 -06:00
}
}
export {
processActivities
}