def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
def filter_tags(html), do: filter_tags(html, nil)
def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags)
+
+ def get_cached_scrubbed_html_for_object(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_for_object(content, object) do
+ get_cached_scrubbed_html_for_object(content, HtmlSanitizeEx.Scrubber.StripTags, object)
+ end
+
+ def ensure_scrubbed_html(
+ content,
+ scrubbers
+ ) do
+ {:commit, 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.StripTags) 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
defmodule Pleroma.HTML.Scrubber.TwitterText do
defmodule Pleroma.Object do
use Ecto.Schema
- alias Pleroma.{Repo, Object, User, Activity, HTML, ObjectTombstone}
alias Pleroma.{Repo, Object, User, Activity, ObjectTombstone}
import Ecto.{Query, Changeset}
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.StripTags) 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
alias Pleroma.HTML
alias Pleroma.Repo
alias Pleroma.User
- alias Pleroma.Object
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MediaProxy
alias Pleroma.Web.MastodonAPI.AccountView
content =
object
|> render_content()
- |> Object.get_cached_scrubbed_html(User.html_filter_policy(opts[:for]), activity)
+ |> HTML.get_cached_scrubbed_html_for_object(User.html_filter_policy(opts[:for]), activity)
%{
id: to_string(activity.id),
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
alias Pleroma.Activity
+ alias Pleroma.HTML
alias Pleroma.Object
alias Pleroma.User
alias Pleroma.Repo
html =
content
- |> Object.get_cached_scrubbed_html(User.html_filter_policy(opts[:for]), activity)
+ |> HTML.get_cached_scrubbed_html_for_object(User.html_filter_policy(opts[:for]), activity)
|> Formatter.emojify(object["emoji"])
text =
if content do
content
|> String.replace(~r/<br\s?\/?>/, "\n")
- |> Object.get_cached_stripped_html(activity)
+ |> HTML.get_cached_stripped_html_for_object(activity)
end
reply_parent = Activity.get_in_reply_to_activity(activity)