1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Metadata.Utils do
8 alias Pleroma.Formatter
11 defp scrub_html_and_truncate_object_field(field, object) do
13 # html content comes from DB already encoded, decode first and scrub after
14 |> HtmlEntities.decode()
15 |> String.replace(~r/<br\s?\/?>/, " ")
16 |> Activity.HTML.get_cached_stripped_html_for_activity(object, "metadata")
17 |> Emoji.Formatter.demojify()
18 |> HtmlEntities.decode()
19 |> Formatter.truncate()
22 def scrub_html_and_truncate(%{data: %{"summary" => summary}} = object)
23 when is_binary(summary) and summary != "" do
25 |> scrub_html_and_truncate_object_field(object)
28 def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
30 |> scrub_html_and_truncate_object_field(object)
33 def scrub_html_and_truncate(content, max_length \\ 200, omission \\ "...")
34 when is_binary(content) do
37 |> Emoji.Formatter.demojify()
38 |> HtmlEntities.decode()
39 |> Formatter.truncate(max_length, omission)
42 def scrub_html(content) when is_binary(content) do
44 # html content comes from DB already encoded, decode first and scrub after
45 |> HtmlEntities.decode()
46 |> String.replace(~r/<br\s?\/?>/, " ")
50 def scrub_html(content), do: content
52 def user_name_string(user) do
55 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
61 @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
62 def fetch_media_type(supported_types, media_type) do
63 Enum.find(supported_types, fn support_type ->
64 String.starts_with?(media_type, support_type)