update tests
[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.Emoji
7 alias Pleroma.Formatter
8 alias Pleroma.HTML
9 alias Pleroma.Web.MediaProxy
10
11 def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
12 content
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()
19 end
20
21 def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do
22 content
23 |> scrub_html
24 |> Emoji.Formatter.demojify()
25 |> Formatter.truncate(max_length)
26 end
27
28 def scrub_html(content) when is_binary(content) do
29 content
30 # html content comes from DB already encoded, decode first and scrub after
31 |> HtmlEntities.decode()
32 |> String.replace(~r/<br\s?\/?>/, " ")
33 |> HTML.strip_tags()
34 end
35
36 def scrub_html(content), do: content
37
38 def attachment_url(url) do
39 MediaProxy.url(url)
40 end
41
42 def user_name_string(user) do
43 "#{user.name} " <>
44 if user.local do
45 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
46 else
47 "(@#{user.nickname})"
48 end
49 end
50
51 @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
52 def fetch_media_type(supported_types, media_type) do
53 Enum.find(supported_types, fn support_type ->
54 String.starts_with?(media_type, support_type)
55 end)
56 end
57 end