extend reject MRF to check if originating instance is blocked
[akkoma] / lib / pleroma / web / activity_pub / mrf / inline_quote_policy.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.Web.ActivityPub.MRF.InlineQuotePolicy do
6 @moduledoc "Force a quote line into the message content."
7 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
8
9 defp build_inline_quote(prefix, url) do
10 "<span class=\"quote-inline\"><br/><br/>#{prefix}: <a href=\"#{url}\">#{url}</a></span>"
11 end
12
13 defp has_inline_quote?(content, quote_url) do
14 cond do
15 # Does the quote URL exist in the content?
16 content =~ quote_url -> true
17 # Does the content already have a .quote-inline span?
18 content =~ "<span class=\"quote-inline\">" -> true
19 # No inline quote found
20 true -> false
21 end
22 end
23
24 defp filter_object(%{"quoteUri" => quote_url} = object) do
25 content = object["content"] || ""
26
27 if has_inline_quote?(content, quote_url) do
28 object
29 else
30 prefix = Pleroma.Config.get([:mrf_inline_quote, :prefix])
31
32 content =
33 if String.ends_with?(content, "</p>") do
34 String.trim_trailing(content, "</p>") <> build_inline_quote(prefix, quote_url) <> "</p>"
35 else
36 content <> build_inline_quote(prefix, quote_url)
37 end
38
39 Map.put(object, "content", content)
40 end
41 end
42
43 @impl true
44 def filter(%{"object" => %{"quoteUri" => _} = object} = activity) do
45 {:ok, Map.put(activity, "object", filter_object(object))}
46 end
47
48 @impl true
49 def filter(object), do: {:ok, object}
50
51 @impl true
52 def describe, do: {:ok, %{}}
53
54 @impl true
55 def config_description do
56 %{
57 key: :mrf_inline_quote,
58 related_policy: "Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy",
59 label: "MRF Inline Quote",
60 description: "Force quote post URLs inline",
61 children: [
62 %{
63 key: :prefix,
64 type: :string,
65 description: "Prefix before the link",
66 suggestions: ["RE", "QT", "RT", "RN"]
67 }
68 ]
69 }
70 end
71 end