logo

pleroma

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

utils.ex (2122B)


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