From b5c361089f03f4d459fa1cdc49ff66dee736af12 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Mar 2021 14:01:44 +0100 Subject: Fix 404 AP status codes --- server/lib/activitypub/actor.ts | 22 +++++----- server/lib/activitypub/playlist.ts | 17 ++++---- server/lib/activitypub/videos.ts | 19 +++++---- .../lib/job-queue/handlers/activitypub-cleaner.ts | 49 ++++++++++++---------- 4 files changed, 57 insertions(+), 50 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index 52b6c1f56..3c9a7ba02 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -14,7 +14,7 @@ import { isActivityPubUrlValid } from '../../helpers/custom-validators/activityp import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils' import { logger } from '../../helpers/logger' import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto' -import { doJSONRequest } from '../../helpers/requests' +import { doJSONRequest, PeerTubeRequestError } from '../../helpers/requests' import { getUrlFromWebfinger } from '../../helpers/webfinger' import { MIMETYPES, WEBSERVER } from '../../initializers/constants' import { sequelizeTypescript } from '../../initializers/database' @@ -279,16 +279,7 @@ async function refreshActorIfNeeded ( updater: (url: string, newUrl: string) => Promise, deleter: (url: string) => Promise ): Promise<{ data: T, status: 'deleted' | 'updated' } | null> { - const { statusCode, body } = await doJSONRequest(url, { activityPub: true }) - - // Does not exist anymore, remove entry - if (statusCode === HttpStatusCode.NOT_FOUND_404) { + const on404OrTombstone = async () => { logger.info('Removing remote AP object %s.', url) const data = await deleter(url) - return { status: 'deleted', data } + return { status: 'deleted' as 'deleted', data } } - // If not same id, check same host and update - if (!body || !body.id || !bodyValidator(body)) throw new Error(`Body or body id of ${url} is invalid`) + try { + const { body } = await doJSONRequest(url, { activityPub: true }) - if (body.type === 'Tombstone') { - logger.info('Removing remote AP object %s.', url) - const data = await deleter(url) + // If not same id, check same host and update + if (!body || !body.id || !bodyValidator(body)) throw new Error(`Body or body id of ${url} is invalid`) - return { status: 'deleted', data } - } + if (body.type === 'Tombstone') { + return on404OrTombstone() + } - const newUrl = body.id - if (newUrl !== url) { - if (checkUrlsSameHost(newUrl, url) !== true) { - throw new Error(`New url ${newUrl} has not the same host than old url ${url}`) + const newUrl = body.id + if (newUrl !== url) { + if (checkUrlsSameHost(newUrl, url) !== true) { + throw new Error(`New url ${newUrl} has not the same host than old url ${url}`) + } + + logger.info('Updating remote AP object %s.', url) + const data = await updater(url, newUrl) + + return { status: 'updated', data } } - logger.info('Updating remote AP object %s.', url) - const data = await updater(url, newUrl) + return null + } catch (err) { + // Does not exist anymore, remove entry + if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { + return on404OrTombstone() + } - return { status: 'updated', data } + throw err } - - return null } function rateOptionsFactory () { -- cgit v1.2.3