Merge remote-tracking branch 'origin/develop' into password-reset
[akkoma] / lib / pleroma / activity / html.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity.HTML do
6 alias Pleroma.HTML
7 alias Pleroma.Object
8
9 @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
10
11 def get_cached_scrubbed_html_for_activity(
12 content,
13 scrubbers,
14 activity,
15 key \\ "",
16 callback \\ fn x -> x end
17 ) do
18 key = "#{key}#{generate_scrubber_signature(scrubbers)}|#{activity.id}"
19
20 @cachex.fetch!(:scrubber_cache, key, fn _key ->
21 object = Object.normalize(activity, fetch: false)
22 HTML.ensure_scrubbed_html(content, scrubbers, object.data["fake"] || false, callback)
23 end)
24 end
25
26 def get_cached_stripped_html_for_activity(content, activity, key) do
27 get_cached_scrubbed_html_for_activity(
28 content,
29 FastSanitize.Sanitizer.StripTags,
30 activity,
31 key,
32 &HtmlEntities.decode/1
33 )
34 end
35
36 defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do
37 generate_scrubber_signature([scrubber])
38 end
39
40 defp generate_scrubber_signature(scrubbers) do
41 Enum.reduce(scrubbers, "", fn scrubber, signature ->
42 "#{signature}#{to_string(scrubber)}"
43 end)
44 end
45 end