Merge remote-tracking branch 'pleroma/develop' into errorview-json-fix
[akkoma] / lib / pleroma / web / metadata / utils.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Metadata.Utils do
6 alias Pleroma.Activity
7 alias Pleroma.Emoji
8 alias Pleroma.Formatter
9 alias Pleroma.HTML
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 |> Activity.HTML.get_cached_stripped_html_for_activity(object, "metadata")
17 |> Emoji.Formatter.demojify()
18 |> HtmlEntities.decode()
19 |> Formatter.truncate()
20 end
21
22 def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do
23 content
24 |> scrub_html
25 |> Emoji.Formatter.demojify()
26 |> HtmlEntities.decode()
27 |> Formatter.truncate(max_length)
28 end
29
30 def scrub_html(content) when is_binary(content) do
31 content
32 # html content comes from DB already encoded, decode first and scrub after
33 |> HtmlEntities.decode()
34 |> String.replace(~r/<br\s?\/?>/, " ")
35 |> HTML.strip_tags()
36 end
37
38 def scrub_html(content), do: content
39
40 def user_name_string(user) do
41 "#{user.name} " <>
42 if user.local do
43 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
44 else
45 "(@#{user.nickname})"
46 end
47 end
48
49 @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
50 def fetch_media_type(supported_types, media_type) do
51 Enum.find(supported_types, fn support_type ->
52 String.starts_with?(media_type, support_type)
53 end)
54 end
55 end