From a2f99b54dfdfe489e14714681189cb13c89f60a3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Nov 2021 10:05:12 +0100 Subject: [PATCH] Optimize AP fetch --- server/helpers/activitypub.ts | 6 +++--- server/helpers/custom-validators/activitypub/actor.ts | 3 ++- server/lib/activitypub/process/process-announce.ts | 6 +++++- server/lib/activitypub/process/process-create.ts | 3 +++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index 8b56d2d50..fe721cbac 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts @@ -187,10 +187,10 @@ function buildSignedActivity (byActor: MActor, data: T, contextType?: Contex return signJsonLDObject(byActor, activity) } -function getAPId (activity: string | { id: string }) { - if (typeof activity === 'string') return activity +function getAPId (object: string | { id: string }) { + if (typeof object === 'string') return object - return activity.id + return object.id } function checkUrlsSameHost (url1: string, url2: string) { diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index 675a7b663..a4b152722 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts @@ -60,11 +60,12 @@ function isActorDeleteActivityValid (activity: any) { } function sanitizeAndCheckActorObject (actor: any) { + if (!isActorTypeValid(actor.type)) return false + normalizeActor(actor) return exists(actor) && isActivityPubUrlValid(actor.id) && - isActorTypeValid(actor.type) && isActivityPubUrlValid(actor.inbox) && isActorPreferredUsernameValid(actor.preferredUsername) && isActivityPubUrlValid(actor.url) && diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index ec23c705e..2619d9754 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts @@ -8,12 +8,16 @@ import { Notifier } from '../../notifier' import { logger } from '../../../helpers/logger' import { APProcessorOptions } from '../../../types/activitypub-processor.model' import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models' +import { getAPId } from '@server/helpers/activitypub' async function processAnnounceActivity (options: APProcessorOptions) { const { activity, byActor: actorAnnouncer } = options // Only notify if it is not from a fetcher job const notify = options.fromFetch !== true + // Announces on accounts are not supported + if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group') return + return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify) } @@ -26,7 +30,7 @@ export { // --------------------------------------------------------------------------- async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) { - const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id + const objectUri = getAPId(activity.object) let video: MVideoAccountLightBlacklistAllFiles let videoCreated: boolean diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index 70e048d6e..93f1d1c59 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -28,6 +28,9 @@ async function processCreateActivity (options: APProcessorOptions