logo

pleroma

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

html.ex (2208B)


  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.HTML do
  5. # Scrubbers are compiled on boot so they can be configured in OTP releases
  6. # @on_load :compile_scrubbers
  7. def compile_scrubbers do
  8. dir = Path.join(:code.priv_dir(:pleroma), "scrubbers")
  9. dir
  10. |> Pleroma.Utils.compile_dir()
  11. |> case do
  12. {:error, _errors, _warnings} ->
  13. raise "Compiling scrubbers failed"
  14. {:ok, _modules, _warnings} ->
  15. :ok
  16. end
  17. end
  18. defp get_scrubbers(scrubber) when is_atom(scrubber), do: [scrubber]
  19. defp get_scrubbers(scrubbers) when is_list(scrubbers), do: scrubbers
  20. defp get_scrubbers(_), do: [Pleroma.HTML.Scrubber.Default]
  21. def get_scrubbers do
  22. Pleroma.Config.get([:markup, :scrub_policy])
  23. |> get_scrubbers
  24. end
  25. def filter_tags(html, nil) do
  26. filter_tags(html, get_scrubbers())
  27. end
  28. def filter_tags(html, scrubbers) when is_list(scrubbers) do
  29. Enum.reduce(scrubbers, html, fn scrubber, html ->
  30. filter_tags(html, scrubber)
  31. end)
  32. end
  33. def filter_tags(html, scrubber) do
  34. {:ok, content} = FastSanitize.Sanitizer.scrub(html, scrubber)
  35. content
  36. end
  37. def filter_tags(html), do: filter_tags(html, nil)
  38. def strip_tags(html), do: filter_tags(html, FastSanitize.Sanitizer.StripTags)
  39. def ensure_scrubbed_html(
  40. content,
  41. scrubbers,
  42. fake,
  43. callback
  44. ) do
  45. content =
  46. content
  47. |> filter_tags(scrubbers)
  48. |> callback.()
  49. if fake do
  50. {:ignore, content}
  51. else
  52. {:commit, content}
  53. end
  54. end
  55. @spec extract_first_external_url_from_object(Pleroma.Object.t()) ::
  56. {:ok, String.t()} | {:error, :no_content}
  57. def extract_first_external_url_from_object(%{data: %{"content" => content}})
  58. when is_binary(content) do
  59. url =
  60. content
  61. |> Floki.parse_fragment!()
  62. |> Floki.find("a:not(.mention,.hashtag,.attachment,[rel~=\"tag\"])")
  63. |> Enum.take(1)
  64. |> Floki.attribute("href")
  65. |> Enum.at(0)
  66. {:ok, url}
  67. end
  68. def extract_first_external_url_from_object(_), do: {:error, :no_content}
  69. end