1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
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
9 defp build_inline_quote(prefix, url) do
10 "<span class=\"quote-inline\"><br/><br/>#{prefix}: <a href=\"#{url}\">#{url}</a></span>"
13 defp has_inline_quote?(content, quote_url) 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
24 defp filter_object(%{"quoteUri" => quote_url} = object) do
25 content = object["content"] || ""
27 if has_inline_quote?(content, quote_url) do
30 prefix = Pleroma.Config.get([:mrf_inline_quote, :prefix])
33 if String.ends_with?(content, "</p>") do
34 String.trim_trailing(content, "</p>") <> build_inline_quote(prefix, quote_url) <> "</p>"
36 content <> build_inline_quote(prefix, quote_url)
39 Map.put(object, "content", content)
44 def filter(%{"object" => %{"quoteUri" => _} = object} = activity) do
45 {:ok, Map.put(activity, "object", filter_object(object))}
49 def filter(object), do: {:ok, object}
52 def describe, do: {:ok, %{}}
55 def config_description do
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",
65 description: "Prefix before the link",
66 suggestions: ["RE", "QT", "RT", "RN"]