logo

pleroma

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

hashtag_policy.ex (4046B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.MRF.HashtagPolicy do
  5. require Pleroma.Constants
  6. alias Pleroma.Config
  7. alias Pleroma.Object
  8. @moduledoc """
  9. Reject, TWKN-remove or Set-Sensitive messages with specific hashtags (without the leading #)
  10. Note: This MRF Policy is always enabled, if you want to disable it you have to set empty lists.
  11. """
  12. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  13. @impl true
  14. def history_awareness, do: :manual
  15. defp check_reject(message, hashtags) do
  16. if Enum.any?(Config.get([:mrf_hashtag, :reject]), fn match -> match in hashtags end) do
  17. {:reject, "[HashtagPolicy] Matches with rejected keyword"}
  18. else
  19. {:ok, message}
  20. end
  21. end
  22. defp check_ftl_removal(%{"to" => to} = message, hashtags) do
  23. if Pleroma.Constants.as_public() in to and
  24. Enum.any?(Config.get([:mrf_hashtag, :federated_timeline_removal]), fn match ->
  25. match in hashtags
  26. end) do
  27. to = List.delete(to, Pleroma.Constants.as_public())
  28. cc = [Pleroma.Constants.as_public() | message["cc"] || []]
  29. message =
  30. message
  31. |> Map.put("to", to)
  32. |> Map.put("cc", cc)
  33. |> Kernel.put_in(["object", "to"], to)
  34. |> Kernel.put_in(["object", "cc"], cc)
  35. {:ok, message}
  36. else
  37. {:ok, message}
  38. end
  39. end
  40. defp check_ftl_removal(message, _hashtags), do: {:ok, message}
  41. defp check_sensitive(message) do
  42. {:ok, new_object} =
  43. Object.Updater.do_with_history(message["object"], fn object ->
  44. hashtags = Object.hashtags(%Object{data: object})
  45. if Enum.any?(Config.get([:mrf_hashtag, :sensitive]), fn match -> match in hashtags end) do
  46. {:ok, Map.put(object, "sensitive", true)}
  47. else
  48. {:ok, object}
  49. end
  50. end)
  51. {:ok, Map.put(message, "object", new_object)}
  52. end
  53. @impl true
  54. def filter(%{"type" => type, "object" => object} = message) when type in ["Create", "Update"] do
  55. history_items =
  56. with %{"formerRepresentations" => %{"orderedItems" => items}} <- object do
  57. items
  58. else
  59. _ -> []
  60. end
  61. historical_hashtags =
  62. Enum.reduce(history_items, [], fn item, acc ->
  63. acc ++ Object.hashtags(%Object{data: item})
  64. end)
  65. hashtags = Object.hashtags(%Object{data: object}) ++ historical_hashtags
  66. if hashtags != [] do
  67. with {:ok, message} <- check_reject(message, hashtags),
  68. {:ok, message} <-
  69. (if type == "Create" do
  70. check_ftl_removal(message, hashtags)
  71. else
  72. {:ok, message}
  73. end),
  74. {:ok, message} <- check_sensitive(message) do
  75. {:ok, message}
  76. end
  77. else
  78. {:ok, message}
  79. end
  80. end
  81. @impl true
  82. def filter(message), do: {:ok, message}
  83. @impl true
  84. def describe do
  85. mrf_hashtag =
  86. Config.get(:mrf_hashtag)
  87. |> Enum.into(%{})
  88. {:ok, %{mrf_hashtag: mrf_hashtag}}
  89. end
  90. @impl true
  91. def config_description do
  92. %{
  93. key: :mrf_hashtag,
  94. related_policy: "Pleroma.Web.ActivityPub.MRF.HashtagPolicy",
  95. label: "MRF Hashtag",
  96. description: @moduledoc,
  97. children: [
  98. %{
  99. key: :reject,
  100. type: {:list, :string},
  101. description: "A list of hashtags which result in message being rejected.",
  102. suggestions: ["foo"]
  103. },
  104. %{
  105. key: :federated_timeline_removal,
  106. type: {:list, :string},
  107. description:
  108. "A list of hashtags which result in message being removed from federated timelines (a.k.a unlisted).",
  109. suggestions: ["foo"]
  110. },
  111. %{
  112. key: :sensitive,
  113. type: {:list, :string},
  114. description:
  115. "A list of hashtags which result in message being set as sensitive (a.k.a NSFW/R-18)",
  116. suggestions: ["nsfw", "r18"]
  117. }
  118. ]
  119. }
  120. end
  121. end