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
7 alias Pleroma.Formatter
9 alias Pleroma.Web.MediaProxy
11 def scrub_html_and_truncate(%{data: %{"content" => content}} = 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 |> HTML.get_cached_stripped_html_for_activity(object, "metadata")
17 |> Emoji.Formatter.demojify()
18 |> Formatter.truncate()
21 def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do
23 # html content comes from DB already encoded, decode first and scrub after
24 |> HtmlEntities.decode()
25 |> String.replace(~r/<br\s?\/?>/, " ")
27 |> Emoji.Formatter.demojify()
28 |> Formatter.truncate(max_length)
31 def attachment_url(url) do
35 def user_name_string(user) do
38 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
44 @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
45 def fetch_media_type(supported_types, media_type) do
46 Enum.find(supported_types, fn support_type ->
47 String.starts_with?(media_type, support_type)