Truncate the length of Rich Media title and description fields

Some sites like Instagram are serving obnoxiously long metadata fields
This commit is contained in:
Mark Felder 2025-03-19 10:29:45 -07:00
commit 7763b9a87f
5 changed files with 118 additions and 1 deletions

View file

@ -4,6 +4,7 @@
defmodule Pleroma.Web.RichMedia.Parser do
alias Pleroma.Web.RichMedia.Helpers
import Pleroma.Web.Metadata.Utils, only: [scrub_html_and_truncate: 2]
require Logger
@config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config)
@ -63,8 +64,20 @@ defmodule Pleroma.Web.RichMedia.Parser do
not match?({:ok, _}, Jason.encode(%{key => val}))
end)
|> Map.new()
|> truncate_title()
|> truncate_desc()
end
defp truncate_title(%{"title" => title} = data) when is_binary(title),
do: %{data | "title" => scrub_html_and_truncate(title, 120)}
defp truncate_title(data), do: data
defp truncate_desc(%{"description" => desc} = data) when is_binary(desc),
do: %{data | "description" => scrub_html_and_truncate(desc, 200)}
defp truncate_desc(data), do: data
@spec validate_page_url(URI.t() | binary()) :: :ok | :error
defp validate_page_url(page_url) when is_binary(page_url) do
validate_tld = @config_impl.get([Pleroma.Formatter, :validate_tld])