Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Activity.HTML do
  5. alias Pleroma.HTML
  6. alias Pleroma.Object
  7. @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
  8. def get_cached_scrubbed_html_for_activity(
  9. content,
  10. scrubbers,
  11. activity,
  12. key \\ "",
  13. callback \\ fn x -> x end
  14. ) do
  15. key = "#{key}#{generate_scrubber_signature(scrubbers)}|#{activity.id}"
  16. @cachex.fetch!(:scrubber_cache, key, fn _key ->
  17. object = Object.normalize(activity, fetch: false)
  18. HTML.ensure_scrubbed_html(content, scrubbers, object.data["fake"] || false, callback)
  19. end)
  20. end
  21. def get_cached_stripped_html_for_activity(content, activity, key) do
  22. get_cached_scrubbed_html_for_activity(
  23. content,
  24. FastSanitize.Sanitizer.StripTags,
  25. activity,
  26. key,
  27. &HtmlEntities.decode/1
  28. )
  29. end
  30. defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
  31. generate_scrubber_signature([scrubber])
  32. end
  33. defp generate_scrubber_signature(scrubbers) do
  34. Enum.reduce(scrubbers, "", fn scrubber, signature ->
  35. "#{signature}#{to_string(scrubber)}"
  36. end)
  37. end
  38. end