2022-07-25 10:30:06 -06: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
|
|
|
|
|
2024-04-02 15:57:27 -06:00
|
|
|
alias Pleroma.Object
|
|
|
|
|
2022-07-25 10:30:06 -06:00
|
|
|
defp build_inline_quote(prefix, url) do
|
|
|
|
"<span class=\"quote-inline\"><br/><br/>#{prefix}: <a href=\"#{url}\">#{url}</a></span>"
|
|
|
|
end
|
|
|
|
|
2024-04-02 15:57:27 -06:00
|
|
|
defp resolve_urls(quote_url) do
|
|
|
|
# Fetching here can cause infinite recursion as we run this logic on inbound objects too
|
|
|
|
# This is probably not a problem - its an exceptional corner case for a local user to quote
|
|
|
|
# a post which doesn't exist
|
|
|
|
with %Object{} = obj <- Object.normalize(quote_url, fetch: false) do
|
|
|
|
id = obj.data["id"]
|
|
|
|
url = Map.get(obj.data, "url", id)
|
|
|
|
{id, url, [id, url, quote_url]}
|
|
|
|
else
|
|
|
|
_ -> {quote_url, quote_url, [quote_url]}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp has_inline_quote?(content, urls) do
|
2022-07-25 10:30:06 -06:00
|
|
|
cond do
|
|
|
|
# Does the quote URL exist in the content?
|
2024-04-02 15:57:27 -06:00
|
|
|
Enum.any?(urls, fn url -> content =~ url end) -> true
|
2022-07-25 10:30:06 -06:00
|
|
|
# Does the content already have a .quote-inline span?
|
|
|
|
content =~ "<span class=\"quote-inline\">" -> true
|
|
|
|
# No inline quote found
|
|
|
|
true -> false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp filter_object(%{"quoteUri" => quote_url} = object) do
|
2024-04-02 15:57:27 -06:00
|
|
|
{id, preferred_url, all_urls} = resolve_urls(quote_url)
|
|
|
|
object = Map.put(object, "quoteUri", id)
|
|
|
|
|
2022-07-25 10:30:06 -06:00
|
|
|
content = object["content"] || ""
|
|
|
|
|
2024-04-02 15:57:27 -06:00
|
|
|
if has_inline_quote?(content, all_urls) do
|
2022-07-25 10:30:06 -06:00
|
|
|
object
|
|
|
|
else
|
|
|
|
prefix = Pleroma.Config.get([:mrf_inline_quote, :prefix])
|
|
|
|
|
|
|
|
content =
|
|
|
|
if String.ends_with?(content, "</p>") do
|
2024-04-02 15:57:27 -06:00
|
|
|
String.trim_trailing(content, "</p>") <>
|
|
|
|
build_inline_quote(prefix, preferred_url) <> "</p>"
|
2022-07-25 10:30:06 -06:00
|
|
|
else
|
2024-04-02 15:57:27 -06:00
|
|
|
content <> build_inline_quote(prefix, preferred_url)
|
2022-07-25 10:30:06 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
Map.put(object, "content", content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def filter(%{"object" => %{"quoteUri" => _} = 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: ["RE", "QT", "RT", "RN"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|