logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

utils.ex (2030B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Metadata.Utils do
  5. alias Pleroma.Activity
  6. alias Pleroma.Emoji
  7. alias Pleroma.Formatter
  8. alias Pleroma.HTML
  9. defp scrub_html_and_truncate_object_field(field, object) do
  10. field
  11. # html content comes from DB already encoded, decode first and scrub after
  12. |> HtmlEntities.decode()
  13. |> String.replace(~r/<br\s?\/?>/, " ")
  14. |> Activity.HTML.get_cached_stripped_html_for_activity(object, "metadata")
  15. |> Emoji.Formatter.demojify()
  16. |> HtmlEntities.decode()
  17. |> Formatter.truncate()
  18. end
  19. def scrub_html_and_truncate(%{data: %{"summary" => summary}} = object)
  20. when is_binary(summary) and summary != "" do
  21. summary
  22. |> scrub_html_and_truncate_object_field(object)
  23. end
  24. def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
  25. content
  26. |> scrub_html_and_truncate_object_field(object)
  27. end
  28. def scrub_html_and_truncate(content, max_length \\ 200, omission \\ "...")
  29. when is_binary(content) do
  30. content
  31. |> scrub_html
  32. |> Emoji.Formatter.demojify()
  33. |> HtmlEntities.decode()
  34. |> Formatter.truncate(max_length, omission)
  35. end
  36. def scrub_html(content) when is_binary(content) do
  37. content
  38. # html content comes from DB already encoded, decode first and scrub after
  39. |> HtmlEntities.decode()
  40. |> String.replace(~r/<br\s?\/?>/, " ")
  41. |> HTML.strip_tags()
  42. end
  43. def scrub_html(content), do: content
  44. def user_name_string(user) do
  45. "#{user.name} " <>
  46. if user.local do
  47. "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
  48. else
  49. "(@#{user.nickname})"
  50. end
  51. end
  52. @spec fetch_media_type(list(String.t()), String.t()) :: String.t() | nil
  53. def fetch_media_type(supported_types, media_type) do
  54. Enum.find(supported_types, fn support_type ->
  55. String.starts_with?(media_type, support_type)
  56. end)
  57. end
  58. end