eva
[akkoma] / lib / pleroma / web / metadata / utils.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Metadata.Utils do
6 alias Pleroma.Formatter
7 alias Pleroma.HTML
8 alias Pleroma.Web.MediaProxy
9
10 def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
11 content
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()
18 end
19
20 def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do
21 content
22 # html content comes from DB already encoded, decode first and scrub after
23 |> HtmlEntities.decode()
24 |> String.replace(~r/<br\s?\/?>/, " ")
25 |> HTML.strip_tags()
26 |> Formatter.demojify()
27 |> Formatter.truncate(max_length)
28 end
29
30 def attachment_url(url) do
31 MediaProxy.url(url)
32 end
33
34 def user_name_string(user) do
35 "#{user.name} " <>
36 if user.local do
37 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
38 else
39 "(@#{user.nickname})"
40 end
41 end
42
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)
47 end)
48 end
49 end