mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-11-11 03:21:12 +00:00
Respect the TTL returned in OpenGraph tags
This commit is contained in:
parent
f40084e019
commit
d21aa1a77c
7 changed files with 467 additions and 11 deletions
|
@ -428,7 +428,10 @@ config :pleroma, :rich_media,
|
|||
Pleroma.Web.RichMedia.Parsers.OEmbed
|
||||
],
|
||||
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
|
||||
|
||||
config :pleroma, :media_proxy,
|
||||
|
|
|
@ -78,8 +78,8 @@ defmodule Pleroma.Web.RichMedia.Backfill do
|
|||
end
|
||||
|
||||
defp maybe_schedule_expiration(url, fields) do
|
||||
case TTL.get_from_image(fields, url) do
|
||||
ttl when is_number(ttl) ->
|
||||
case TTL.process(fields, url) do
|
||||
{:ok, ttl} when is_number(ttl) ->
|
||||
timestamp = DateTime.from_unix!(ttl)
|
||||
|
||||
RichMediaExpirationWorker.new(%{"url" => url}, scheduled_at: timestamp)
|
||||
|
|
|
@ -5,15 +5,16 @@
|
|||
defmodule Pleroma.Web.RichMedia.Parser.TTL do
|
||||
@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]
|
||||
|> Pleroma.Config.get()
|
||||
|> Enum.reduce({:ok, nil}, fn
|
||||
module, {:ok, _ttl} ->
|
||||
module.ttl(data, url)
|
||||
|
||||
_, error ->
|
||||
error
|
||||
|> Enum.reduce_while({:ok, nil}, fn
|
||||
module, acc ->
|
||||
case module.ttl(data, url) do
|
||||
ttl when is_number(ttl) -> {:halt, {:ok, ttl}}
|
||||
_ -> {:cont, acc}
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,7 +15,7 @@ defmodule Pleroma.Web.RichMedia.Parser.TTL.AwsSignedUrl do
|
|||
|> format_query_params()
|
||||
|> get_expiration_timestamp()
|
||||
else
|
||||
{:error, "Not aws signed url #{inspect(image)}"}
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
|
|
19
lib/pleroma/web/rich_media/parser/ttl/opengraph.ex
Normal file
19
lib/pleroma/web/rich_media/parser/ttl/opengraph.ex
Normal 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
392
test/fixtures/rich_media/reddit.html
vendored
Normal file
File diff suppressed because one or more lines are too long
41
test/pleroma/web/rich_media/parser/ttl/opengraph_test.exs
Normal file
41
test/pleroma/web/rich_media/parser/ttl/opengraph_test.exs
Normal 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
|
Loading…
Reference in a new issue