summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfeld <feld@feld.me>2021-05-29 21:19:25 +0000
committerfeld <feld@feld.me>2021-05-29 21:19:25 +0000
commitb5f3a5c979ca0c06502b48e5930d6f8b8d686bd9 (patch)
tree1a5e2b7eb177f5eba321e29b96c3f097a0b3f494
parent317fe240a8629caa445d74ca54826d5d379d6209 (diff)
parent1a69f59221b7009ab76df9abf24403d2711dbaf3 (diff)
Merge branch 'cycles-html' into 'develop'
Recompilation speedup: Break out activity-specific HTML functions into Pleroma.Activity.HTML See merge request pleroma/pleroma!3426
-rw-r--r--lib/pleroma/activity/html.ex45
-rw-r--r--lib/pleroma/html.ex35
-rw-r--r--lib/pleroma/web/mastodon_api/views/status_view.ex4
-rw-r--r--lib/pleroma/web/metadata/utils.ex3
4 files changed, 49 insertions, 38 deletions
diff --git a/lib/pleroma/activity/html.ex b/lib/pleroma/activity/html.ex
new file mode 100644
index 000000000..0bf393836
--- /dev/null
+++ b/lib/pleroma/activity/html.ex
@@ -0,0 +1,45 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Activity.HTML do
+ alias Pleroma.HTML
+ alias Pleroma.Object
+
+ @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
+
+ def get_cached_scrubbed_html_for_activity(
+ content,
+ scrubbers,
+ activity,
+ key \\ "",
+ callback \\ fn x -> x end
+ ) do
+ key = "#{key}#{generate_scrubber_signature(scrubbers)}|#{activity.id}"
+
+ @cachex.fetch!(:scrubber_cache, key, fn _key ->
+ object = Object.normalize(activity, fetch: false)
+ HTML.ensure_scrubbed_html(content, scrubbers, object.data["fake"] || false, callback)
+ end)
+ end
+
+ def get_cached_stripped_html_for_activity(content, activity, key) do
+ get_cached_scrubbed_html_for_activity(
+ content,
+ FastSanitize.Sanitizer.StripTags,
+ activity,
+ key,
+ &HtmlEntities.decode/1
+ )
+ 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 ->
+ "#{signature}#{to_string(scrubber)}"
+ end)
+ end
+end
diff --git a/lib/pleroma/html.ex b/lib/pleroma/html.ex
index 2dfdca693..bee66169d 100644
--- a/lib/pleroma/html.ex
+++ b/lib/pleroma/html.ex
@@ -49,31 +49,6 @@ defmodule Pleroma.HTML do
def filter_tags(html), do: filter_tags(html, nil)
def strip_tags(html), do: filter_tags(html, FastSanitize.Sanitizer.StripTags)
- def get_cached_scrubbed_html_for_activity(
- content,
- scrubbers,
- activity,
- key \\ "",
- callback \\ fn x -> x end
- ) do
- key = "#{key}#{generate_scrubber_signature(scrubbers)}|#{activity.id}"
-
- @cachex.fetch!(:scrubber_cache, key, fn _key ->
- object = Pleroma.Object.normalize(activity, fetch: false)
- ensure_scrubbed_html(content, scrubbers, object.data["fake"] || false, callback)
- end)
- end
-
- def get_cached_stripped_html_for_activity(content, activity, key) do
- get_cached_scrubbed_html_for_activity(
- content,
- FastSanitize.Sanitizer.StripTags,
- activity,
- key,
- &HtmlEntities.decode/1
- )
- end
-
def ensure_scrubbed_html(
content,
scrubbers,
@@ -92,16 +67,6 @@ defmodule Pleroma.HTML do
end
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 ->
- "#{signature}#{to_string(scrubber)}"
- end)
- end
-
def extract_first_external_url_from_object(%{data: %{"content" => content}} = object)
when is_binary(content) do
unless object.data["fake"] do
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index 8fdf30883..45495ce8e 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -260,7 +260,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
content_html =
content
- |> HTML.get_cached_scrubbed_html_for_activity(
+ |> Activity.HTML.get_cached_scrubbed_html_for_activity(
User.html_filter_policy(opts[:for]),
activity,
"mastoapi:content"
@@ -268,7 +268,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
content_plaintext =
content
- |> HTML.get_cached_stripped_html_for_activity(
+ |> Activity.HTML.get_cached_stripped_html_for_activity(
activity,
"mastoapi:content"
)
diff --git a/lib/pleroma/web/metadata/utils.ex b/lib/pleroma/web/metadata/utils.ex
index de7195435..bc31d66b9 100644
--- a/lib/pleroma/web/metadata/utils.ex
+++ b/lib/pleroma/web/metadata/utils.ex
@@ -3,6 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Utils do
+ alias Pleroma.Activity
alias Pleroma.Emoji
alias Pleroma.Formatter
alias Pleroma.HTML
@@ -13,7 +14,7 @@ defmodule Pleroma.Web.Metadata.Utils do
# html content comes from DB already encoded, decode first and scrub after
|> HtmlEntities.decode()
|> String.replace(~r/<br\s?\/?>/, " ")
- |> HTML.get_cached_stripped_html_for_activity(object, "metadata")
+ |> Activity.HTML.get_cached_stripped_html_for_activity(object, "metadata")
|> Emoji.Formatter.demojify()
|> HtmlEntities.decode()
|> Formatter.truncate()