giant massive dep upgrade and dialyxir-found error emporium (#371)
[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 defp scrub_html_and_truncate_object_field(field, object) do
12 field
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(%{data: %{"summary" => summary}} = object)
23 when is_binary(summary) and summary != "" do
24 summary
25 |> scrub_html_and_truncate_object_field(object)
26 end
27
28 def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
29 content
30 |> scrub_html_and_truncate_object_field(object)
31 end
32
33 def scrub_html_and_truncate(%{data: _}) do
34 ""
35 end
36
37 def scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do
38 content
39 |> scrub_html
40 |> Emoji.Formatter.demojify()
41 |> HtmlEntities.decode()
42 |> Formatter.truncate(max_length)
43 end
44
45 def scrub_html(content) when is_binary(content) do
46 content
47 # html content comes from DB already encoded, decode first and scrub after
48 |> HtmlEntities.decode()
49 |> String.replace(~r/<br\s?\/?>/, " ")
50 |> HTML.strip_tags()
51 end
52
53 def scrub_html(content), do: content
54
55 def user_name_string(user) do
56 "#{user.name} " <>
57 if user.local do
58 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
59 else
60 "(@#{user.nickname})"
61 end
62 end
63
64 @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
65 def fetch_media_type(supported_types, media_type) do
66 Enum.find(supported_types, fn support_type ->
67 String.starts_with?(media_type, support_type)
68 end)
69 end
70 end