logo

pleroma

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

html.ex (2599B)


  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.Activity.HTML do
  5. alias Pleroma.HTML
  6. alias Pleroma.Object
  7. @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
  8. # We store a list of cache keys related to an activity in a
  9. # separate cache, scrubber_management_cache. It has the same
  10. # size as scrubber_cache (see application.ex). Every time we add
  11. # a cache to scrubber_cache, we update scrubber_management_cache.
  12. #
  13. # The most recent write of a certain key in the management cache
  14. # is the same as the most recent write of any record related to that
  15. # key in the main cache.
  16. # Assuming LRW ( https://hexdocs.pm/cachex/Cachex.Policy.LRW.html ),
  17. # this means when the management cache is evicted by cachex, all
  18. # related records in the main cache will also have been evicted.
  19. defp get_cache_keys_for(activity_id) do
  20. with {:ok, list} when is_list(list) <- @cachex.get(:scrubber_management_cache, activity_id) do
  21. list
  22. else
  23. _ -> []
  24. end
  25. end
  26. def add_cache_key_for(activity_id, additional_key) do
  27. current = get_cache_keys_for(activity_id)
  28. unless additional_key in current do
  29. @cachex.put(:scrubber_management_cache, activity_id, [additional_key | current])
  30. end
  31. end
  32. def invalidate_cache_for(activity_id) do
  33. keys = get_cache_keys_for(activity_id)
  34. Enum.map(keys, &@cachex.del(:scrubber_cache, &1))
  35. @cachex.del(:scrubber_management_cache, activity_id)
  36. end
  37. def get_cached_scrubbed_html_for_activity(
  38. content,
  39. scrubbers,
  40. activity,
  41. key \\ "",
  42. callback \\ fn x -> x end
  43. ) do
  44. key = "#{key}#{generate_scrubber_signature(scrubbers)}|#{activity.id}"
  45. @cachex.fetch!(:scrubber_cache, key, fn _key ->
  46. object = Object.normalize(activity, fetch: false)
  47. add_cache_key_for(activity.id, key)
  48. HTML.ensure_scrubbed_html(content, scrubbers, object.data["fake"] || false, callback)
  49. end)
  50. end
  51. def get_cached_stripped_html_for_activity(content, activity, key) do
  52. get_cached_scrubbed_html_for_activity(
  53. content,
  54. FastSanitize.Sanitizer.StripTags,
  55. activity,
  56. key,
  57. &HtmlEntities.decode/1
  58. )
  59. end
  60. defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
  61. generate_scrubber_signature([scrubber])
  62. end
  63. defp generate_scrubber_signature(scrubbers) do
  64. Enum.reduce(scrubbers, "", fn scrubber, signature ->
  65. "#{signature}#{to_string(scrubber)}"
  66. end)
  67. end
  68. end