1
0
Fork 0
peertube/server/lib/activitypub/process/process.ts

88 lines
3.3 KiB
TypeScript
Raw Normal View History

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