Merge branch 'translation/errors-french' into 'develop'
[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.Emoji
7 alias Pleroma.Formatter
8 alias Pleroma.HTML
9 alias Pleroma.Web.MediaProxy
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 |> 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 # html content comes from DB already encoded, decode first and scrub after
25 |> HtmlEntities.decode()
26 |> String.replace(~r/<br\s?\/?>/, " ")
27 |> HTML.strip_tags()
28 |> Emoji.Formatter.demojify()
29 |> HtmlEntities.decode()
30 |> Formatter.truncate(max_length)
31 end
32
33 def attachment_url(url) do
34 MediaProxy.url(url)
35 end
36
37 def user_name_string(user) do
38 "#{user.name} " <>
39 if user.local do
40 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
41 else
42 "(@#{user.nickname})"
43 end
44 end
45
46 @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
47 def fetch_media_type(supported_types, media_type) do
48 Enum.find(supported_types, fn support_type ->
49 String.starts_with?(media_type, support_type)
50 end)
51 end
52 end