logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

inline_quote_policy.ex (2301B)


  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. defmodule Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy do
  5. @moduledoc "Force a quote line into the message content."
  6. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  7. defp build_inline_quote(template, url) do
  8. quote_line = String.replace(template, "{url}", "<a href=\"#{url}\">#{url}</a>")
  9. "<span class=\"quote-inline\"><br/><br/>#{quote_line}</span>"
  10. end
  11. defp has_inline_quote?(content, quote_url) do
  12. cond do
  13. # Does the quote URL exist in the content?
  14. content =~ quote_url -> true
  15. # Does the content already have a .quote-inline span?
  16. content =~ "<span class=\"quote-inline\">" -> true
  17. # No inline quote found
  18. true -> false
  19. end
  20. end
  21. defp filter_object(%{"quoteUrl" => quote_url} = object) do
  22. content = object["content"] || ""
  23. if has_inline_quote?(content, quote_url) do
  24. object
  25. else
  26. template = Pleroma.Config.get([:mrf_inline_quote, :template])
  27. content =
  28. if String.ends_with?(content, "</p>"),
  29. do:
  30. String.trim_trailing(content, "</p>") <>
  31. build_inline_quote(template, quote_url) <> "</p>",
  32. else: content <> build_inline_quote(template, quote_url)
  33. Map.put(object, "content", content)
  34. end
  35. end
  36. @impl true
  37. def filter(%{"object" => %{"quoteUrl" => _} = object} = activity) do
  38. {:ok, Map.put(activity, "object", filter_object(object))}
  39. end
  40. @impl true
  41. def filter(object), do: {:ok, object}
  42. @impl true
  43. def describe, do: {:ok, %{}}
  44. @impl Pleroma.Web.ActivityPub.MRF.Policy
  45. def history_awareness, do: :auto
  46. @impl true
  47. def config_description do
  48. %{
  49. key: :mrf_inline_quote,
  50. related_policy: "Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy",
  51. label: "MRF Inline Quote Policy",
  52. description: "Force quote url to appear in post content.",
  53. children: [
  54. %{
  55. key: :template,
  56. type: :string,
  57. description:
  58. "The template to append to the post. `{url}` will be replaced with the actual link to the quoted post.",
  59. suggestions: ["<bdi>RT:</bdi> {url}"]
  60. }
  61. ]
  62. }
  63. end
  64. end