Respect the TTL returned in OpenGraph tags

This commit is contained in:
Mark Felder 2024-02-18 22:24:27 -05:00
parent 68dc81b59e
commit b067fbde31
7 changed files with 467 additions and 11 deletions

View File

@ -428,7 +428,10 @@
Pleroma.Web.RichMedia.Parsers.OEmbed Pleroma.Web.RichMedia.Parsers.OEmbed
], ],
failure_backoff: 60_000, failure_backoff: 60_000,
ttl_setters: [Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl], ttl_setters: [
Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl,
Pleroma.Web.RichMedia.Parser.TTL.Opengraph
],
max_body: 5_000_000 max_body: 5_000_000
config :pleroma, :media_proxy, config :pleroma, :media_proxy,

View File

@ -78,8 +78,8 @@ def run(%{url: url, url_hash: url_hash}) do
end end
defp maybe_schedule_expiration(url, fields) do defp maybe_schedule_expiration(url, fields) do
case TTL.get_from_image(fields, url) do case TTL.process(fields, url) do
ttl when is_number(ttl) -> {:ok, ttl} when is_number(ttl) ->
timestamp = DateTime.from_unix!(ttl) timestamp = DateTime.from_unix!(ttl)
RichMediaExpirationWorker.new(%{"url" => url}, scheduled_at: timestamp) RichMediaExpirationWorker.new(%{"url" => url}, scheduled_at: timestamp)

View File

@ -5,15 +5,16 @@
defmodule Pleroma.Web.RichMedia.Parser.TTL do defmodule Pleroma.Web.RichMedia.Parser.TTL do
@callback ttl(map(), String.t()) :: integer() | nil @callback ttl(map(), String.t()) :: integer() | nil
def get_from_image(data, url) do @spec process(map(), String.t()) :: {:ok, integer() | nil}
def process(data, url) do
[:rich_media, :ttl_setters] [:rich_media, :ttl_setters]
|> Pleroma.Config.get() |> Pleroma.Config.get()
|> Enum.reduce({:ok, nil}, fn |> Enum.reduce_while({:ok, nil}, fn
module, {:ok, _ttl} -> module, acc ->
module.ttl(data, url) case module.ttl(data, url) do
ttl when is_number(ttl) -> {:halt, {:ok, ttl}}
_, error -> _ -> {:cont, acc}
error end
end) end)
end end
end end

View File

@ -15,7 +15,7 @@ def ttl(data, _url) do
|> format_query_params() |> format_query_params()
|> get_expiration_timestamp() |> get_expiration_timestamp()
else else
{:error, "Not aws signed url #{inspect(image)}"} nil
end end
end end

View File

@ -0,0 +1,19 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.RichMedia.Parser.TTL.Opengraph do
@behaviour Pleroma.Web.RichMedia.Parser.TTL
@impl true
def ttl(%{"ttl" => ttl_string}, _url) do
with ttl <- String.to_integer(ttl_string) do
now = DateTime.utc_now() |> DateTime.to_unix()
now + ttl
else
_ -> nil
end
end
def ttl(_, _), do: nil
end

392
test/fixtures/rich_media/reddit.html vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,41 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.RichMedia.Parser.TTL.OpengraphTest do
use Pleroma.DataCase
use Oban.Testing, repo: Pleroma.Repo
import Mox
alias Pleroma.UnstubbedConfigMock, as: ConfigMock
alias Pleroma.Web.RichMedia.Card
setup do
ConfigMock
|> stub_with(Pleroma.Test.StaticConfig)
clear_config([:rich_media, :enabled], true)
:ok
end
test "OpenGraph TTL value is honored" do
url = "https://reddit.com/r/somepost"
Tesla.Mock.mock(fn
%{
method: :get,
url: ^url
} ->
%Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/reddit.html")}
%{method: :head} ->
%Tesla.Env{status: 200}
end)
Card.get_or_backfill_by_url(url)
assert_enqueued(worker: Pleroma.Workers.RichMediaExpirationWorker, args: %{"url" => url})
end
end