2022-01-24 22:44:35 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy do
|
|
|
|
@moduledoc "Force a quote line into the message content."
|
|
|
|
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
|
|
|
|
|
2023-07-12 13:30:43 +00:00
|
|
|
defp build_inline_quote(template, url) do
|
|
|
|
quote_line = String.replace(template, "{url}", "<a href=\"#{url}\">#{url}</a>")
|
|
|
|
|
|
|
|
"<span class=\"quote-inline\"><br/><br/>#{quote_line}</span>"
|
2022-01-24 22:44:35 +00:00
|
|
|
end
|
|
|
|
|
2022-01-30 16:57:29 +00:00
|
|
|
defp has_inline_quote?(content, quote_url) do
|
|
|
|
cond do
|
|
|
|
# Does the quote URL exist in the content?
|
|
|
|
content =~ quote_url -> true
|
|
|
|
# Does the content already have a .quote-inline span?
|
|
|
|
content =~ "<span class=\"quote-inline\">" -> true
|
|
|
|
# No inline quote found
|
|
|
|
true -> false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-24 22:44:35 +00:00
|
|
|
defp filter_object(%{"quoteUrl" => quote_url} = object) do
|
|
|
|
content = object["content"] || ""
|
|
|
|
|
2022-01-30 16:57:29 +00:00
|
|
|
if has_inline_quote?(content, quote_url) do
|
2022-01-24 22:44:35 +00:00
|
|
|
object
|
|
|
|
else
|
2023-07-12 13:30:43 +00:00
|
|
|
template = Pleroma.Config.get([:mrf_inline_quote, :template])
|
2022-01-27 20:28:06 +00:00
|
|
|
|
2022-01-28 22:07:17 +00:00
|
|
|
content =
|
2022-01-27 20:28:06 +00:00
|
|
|
if String.ends_with?(content, "</p>"),
|
2022-01-28 22:07:17 +00:00
|
|
|
do:
|
|
|
|
String.trim_trailing(content, "</p>") <>
|
2023-07-12 13:30:43 +00:00
|
|
|
build_inline_quote(template, quote_url) <> "</p>",
|
|
|
|
else: content <> build_inline_quote(template, quote_url)
|
2022-01-27 20:28:06 +00:00
|
|
|
|
2022-01-24 22:44:35 +00:00
|
|
|
Map.put(object, "content", content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def filter(%{"object" => %{"quoteUrl" => _} = object} = activity) do
|
|
|
|
{:ok, Map.put(activity, "object", filter_object(object))}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def filter(object), do: {:ok, object}
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def describe, do: {:ok, %{}}
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def config_description do
|
|
|
|
%{
|
|
|
|
key: :mrf_inline_quote,
|
|
|
|
related_policy: "Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy",
|
|
|
|
label: "MRF Inline Quote",
|
|
|
|
description: "Force quote post URLs inline",
|
|
|
|
children: [
|
|
|
|
%{
|
|
|
|
key: :prefix,
|
|
|
|
type: :string,
|
|
|
|
description: "Prefix before the link",
|
|
|
|
suggestions: ["RT", "QT", "RE", "RN"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|