Merge branch 'hex-flake-id' 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 |> 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 |> Emoji.Formatter.demojify()
28 |> Formatter.truncate(max_length)
29 end
30
31 def attachment_url(url) do
32 MediaProxy.url(url)
33 end
34
35 def user_name_string(user) do
36 "#{user.name} " <>
37 if user.local do
38 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
39 else
40 "(@#{user.nickname})"
41 end
42 end
43
44 @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
45 def fetch_media_type(supported_types, media_type) do
46 Enum.find(supported_types, fn support_type ->
47 String.starts_with?(media_type, support_type)
48 end)
49 end
50 end