1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Metadata.Utils do
6 alias Pleroma.Formatter
8 alias Pleroma.Web.MediaProxy
10 def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
12 # html content comes from DB already encoded, decode first and scrub after
13 |> HtmlEntities.decode()
14 |> String.replace(~r/<br\s?\/?>/, " ")
15 |> HTML.get_cached_stripped_html_for_activity(object, "metadata")
16 |> Formatter.demojify()
17 |> Formatter.truncate()
20 def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do
22 # html content comes from DB already encoded, decode first and scrub after
23 |> HtmlEntities.decode()
24 |> String.replace(~r/<br\s?\/?>/, " ")
26 |> Formatter.demojify()
27 |> Formatter.truncate(max_length)
30 def attachment_url(url) do
34 def user_name_string(user) do
37 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
43 @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
44 def fetch_media_type(supported_types, media_type) do
45 Enum.find(supported_types, fn support_type ->
46 String.starts_with?(media_type, support_type)