Move scrubber cache-related functions to Pleroma.HTML
This commit is contained in:
parent
05743e2000
commit
7e09c2bd7d
|
@ -27,6 +27,40 @@ def filter_tags(html, scrubbers) when is_list(scrubbers) do
|
||||||
def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
|
def filter_tags(html, scrubber), do: Scrubber.scrub(html, scrubber)
|
||||||
def filter_tags(html), do: filter_tags(html, nil)
|
def filter_tags(html), do: filter_tags(html, nil)
|
||||||
def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags)
|
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
|
end
|
||||||
|
|
||||||
defmodule Pleroma.HTML.Scrubber.TwitterText do
|
defmodule Pleroma.HTML.Scrubber.TwitterText do
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
|
|
||||||
defmodule Pleroma.Object do
|
defmodule Pleroma.Object do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
alias Pleroma.{Repo, Object, User, Activity, HTML, ObjectTombstone}
|
|
||||||
alias Pleroma.{Repo, Object, User, Activity, ObjectTombstone}
|
alias Pleroma.{Repo, Object, User, Activity, ObjectTombstone}
|
||||||
import Ecto.{Query, Changeset}
|
import Ecto.{Query, Changeset}
|
||||||
|
|
||||||
|
@ -92,37 +91,4 @@ def delete(%Object{data: %{"id" => id}} = object) do
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -9,7 +9,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
alias Pleroma.HTML
|
alias Pleroma.HTML
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
alias Pleroma.Object
|
|
||||||
alias Pleroma.Web.CommonAPI.Utils
|
alias Pleroma.Web.CommonAPI.Utils
|
||||||
alias Pleroma.Web.MediaProxy
|
alias Pleroma.Web.MediaProxy
|
||||||
alias Pleroma.Web.MastodonAPI.AccountView
|
alias Pleroma.Web.MastodonAPI.AccountView
|
||||||
|
@ -121,7 +120,7 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity}
|
||||||
content =
|
content =
|
||||||
object
|
object
|
||||||
|> render_content()
|
|> 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),
|
id: to_string(activity.id),
|
||||||
|
|
|
@ -11,6 +11,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
|
||||||
alias Pleroma.Web.TwitterAPI.TwitterAPI
|
alias Pleroma.Web.TwitterAPI.TwitterAPI
|
||||||
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
|
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
|
||||||
alias Pleroma.Activity
|
alias Pleroma.Activity
|
||||||
|
alias Pleroma.HTML
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
|
@ -244,14 +245,14 @@ def render(
|
||||||
|
|
||||||
html =
|
html =
|
||||||
content
|
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"])
|
|> Formatter.emojify(object["emoji"])
|
||||||
|
|
||||||
text =
|
text =
|
||||||
if content do
|
if content do
|
||||||
content
|
content
|
||||||
|> String.replace(~r/<br\s?\/?>/, "\n")
|
|> String.replace(~r/<br\s?\/?>/, "\n")
|
||||||
|> Object.get_cached_stripped_html(activity)
|
|> HTML.get_cached_stripped_html_for_object(activity)
|
||||||
end
|
end
|
||||||
|
|
||||||
reply_parent = Activity.get_in_reply_to_activity(activity)
|
reply_parent = Activity.get_in_reply_to_activity(activity)
|
||||||
|
|
Loading…
Reference in New Issue