Fix uploaded media plug test
[akkoma] / lib / pleroma / web / metadata / utils.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright \xc2\xa9 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.HTML
7 alias Pleroma.Formatter
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_object(object, __MODULE__)
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 end