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