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