From dedcd583b2461895f816c3dd67d56f05d3274f6b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Nov 2021 14:11:19 +0100 Subject: [PATCH] Fix remote interaction When we fetch a ressource that is a redirection of another ressource --- .../controllers/api/search/search-video-channels.ts | 6 ++++-- .../controllers/api/search/search-video-playlists.ts | 6 ++++-- server/controllers/api/search/search-videos.ts | 7 +++++-- server/helpers/requests.ts | 11 +++++++++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/server/controllers/api/search/search-video-channels.ts b/server/controllers/api/search/search-video-channels.ts index c9e81bffa..aa5afb46e 100644 --- a/server/controllers/api/search/search-video-channels.ts +++ b/server/controllers/api/search/search-video-channels.ts @@ -1,7 +1,7 @@ import express from 'express' import { sanitizeUrl } from '@server/helpers/core-utils' import { pickSearchChannelQuery } from '@server/helpers/query' -import { doJSONRequest } from '@server/helpers/requests' +import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests' import { CONFIG } from '@server/initializers/config' import { WEBSERVER } from '@server/initializers/constants' import { Hooks } from '@server/lib/plugins/hooks' @@ -126,7 +126,9 @@ async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean if (isUserAbleToSearchRemoteURI(res)) { try { - const actor = await getOrCreateAPActor(uri, 'all', true, true) + const latestUri = await findLatestRedirection(uri, { activityPub: true }) + + const actor = await getOrCreateAPActor(latestUri, 'all', true, true) videoChannel = actor.VideoChannel } catch (err) { logger.info('Cannot search remote video channel %s.', uri, { err }) diff --git a/server/controllers/api/search/search-video-playlists.ts b/server/controllers/api/search/search-video-playlists.ts index 61a11c74a..e76d65fde 100644 --- a/server/controllers/api/search/search-video-playlists.ts +++ b/server/controllers/api/search/search-video-playlists.ts @@ -3,7 +3,7 @@ import { sanitizeUrl } from '@server/helpers/core-utils' import { isUserAbleToSearchRemoteURI } from '@server/helpers/express-utils' import { logger } from '@server/helpers/logger' import { pickSearchPlaylistQuery } from '@server/helpers/query' -import { doJSONRequest } from '@server/helpers/requests' +import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests' import { getFormattedObjects } from '@server/helpers/utils' import { CONFIG } from '@server/initializers/config' import { WEBSERVER } from '@server/initializers/constants' @@ -105,7 +105,9 @@ async function searchVideoPlaylistsURI (search: string, res: express.Response) { if (isUserAbleToSearchRemoteURI(res)) { try { - videoPlaylist = await getOrCreateAPVideoPlaylist(search) + const url = await findLatestRedirection(search, { activityPub: true }) + + videoPlaylist = await getOrCreateAPVideoPlaylist(url) } catch (err) { logger.info('Cannot search remote video playlist %s.', search, { err }) } diff --git a/server/controllers/api/search/search-videos.ts b/server/controllers/api/search/search-videos.ts index 6db70acdf..68428d766 100644 --- a/server/controllers/api/search/search-videos.ts +++ b/server/controllers/api/search/search-videos.ts @@ -1,7 +1,7 @@ import express from 'express' import { sanitizeUrl } from '@server/helpers/core-utils' import { pickSearchVideoQuery } from '@server/helpers/query' -import { doJSONRequest } from '@server/helpers/requests' +import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests' import { CONFIG } from '@server/initializers/config' import { WEBSERVER } from '@server/initializers/constants' import { getOrCreateAPVideo } from '@server/lib/activitypub/videos' @@ -142,7 +142,10 @@ async function searchVideoURI (url: string, res: express.Response) { refreshVideo: false } - const result = await getOrCreateAPVideo({ videoObject: url, syncParam }) + const result = await getOrCreateAPVideo({ + videoObject: await findLatestRedirection(url, { activityPub: true }), + syncParam + }) video = result ? result.video : undefined } catch (err) { logger.info('Cannot search remote video %s.', url, { err }) diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 799034b90..57299eee1 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts @@ -184,6 +184,16 @@ function isBinaryResponse (result: Response) { return BINARY_CONTENT_TYPES.has(result.headers['content-type']) } +async function findLatestRedirection (url: string, options: PeerTubeRequestOptions, iteration = 1) { + if (iteration > 10) throw new Error('Too much iterations to find final URL ' + url) + + const { headers } = await peertubeGot(url, { followRedirect: false, ...buildGotOptions(options) }) + + if (headers.location) return findLatestRedirection(headers.location, options, iteration + 1) + + return url +} + // --------------------------------------------------------------------------- export { @@ -192,6 +202,7 @@ export { doRequestAndSaveToFile, isBinaryResponse, downloadImage, + findLatestRedirection, peertubeGot }