aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/object.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/object.ex')
-rw-r--r--lib/pleroma/object.ex34
1 files changed, 33 insertions, 1 deletions
diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex
index cc4a2181a..e148c1d75 100644
--- a/lib/pleroma/object.ex
+++ b/lib/pleroma/object.ex
@@ -4,7 +4,7 @@
defmodule Pleroma.Object do
use Ecto.Schema
- alias Pleroma.{Repo, Object, User, Activity}
+ alias Pleroma.{Repo, Object, User, Activity, HTML}
import Ecto.{Query, Changeset}
schema "objects" do
@@ -73,4 +73,36 @@ defmodule Pleroma.Object do
{:ok, object}
end
end
+
+ def get_cached_scrubbed_html(content, scrubbers, object) do
+ key = "#{generate_scrubber_signature(scrubbers)}|#{object.id}"
+ Cachex.fetch!(:scrubber_cache, key, fn(_key) -> ensure_scrubbed_html(content, scrubbers) end )
+ end
+
+ def get_cached_stripped_html(content, object) do
+ get_cached_scrubbed_html(content, HtmlSanitizeEx.Scrubber.StripTags, object)
+ end
+
+ def ensure_scrubbed_html(
+ content,
+ scrubbers
+ ) do
+ {:commit, HTML.filter_tags(content, scrubbers)}
+ end
+
+ defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
+ generate_scrubber_signature([scrubber])
+ end
+
+ defp generate_scrubber_signature(scrubbers) do
+ Enum.reduce(scrubbers, "", fn scrubber, signature ->
+ # If a scrubber does not have a version(e.g HtmlSanitizeEx.Scrubber) it is assumed it is always 0)
+ version = if Kernel.function_exported?(scrubber, :version, 0) do
+ scrubber.version
+ else
+ 0
+ end
+ "#{signature}#{to_string(scrubber)}#{version}"
+ end)
+ end
end