logo

pleroma

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

object_age_policy.ex (4212B)


  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.ObjectAgePolicy do
  5. alias Pleroma.Config
  6. alias Pleroma.User
  7. require Pleroma.Constants
  8. @moduledoc "Filter activities depending on their age"
  9. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  10. defp check_date(%{"object" => %{"published" => published}} = message) do
  11. with %DateTime{} = now <- DateTime.utc_now(),
  12. {:ok, %DateTime{} = then, _} <- DateTime.from_iso8601(published),
  13. max_ttl <- Config.get([:mrf_object_age, :threshold]),
  14. {:ttl, false} <- {:ttl, DateTime.diff(now, then) > max_ttl} do
  15. {:ok, message}
  16. else
  17. {:ttl, true} ->
  18. {:reject, nil}
  19. e ->
  20. {:error, e}
  21. end
  22. end
  23. defp check_reject(message, actions) do
  24. if :reject in actions do
  25. {:reject, "[ObjectAgePolicy]"}
  26. else
  27. {:ok, message}
  28. end
  29. end
  30. defp check_delist(message, actions) do
  31. if :delist in actions do
  32. with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
  33. to =
  34. List.delete(message["to"] || [], Pleroma.Constants.as_public()) ++
  35. [user.follower_address]
  36. cc =
  37. List.delete(message["cc"] || [], user.follower_address) ++
  38. [Pleroma.Constants.as_public()]
  39. message =
  40. message
  41. |> Map.put("to", to)
  42. |> Map.put("cc", cc)
  43. |> Kernel.put_in(["object", "to"], to)
  44. |> Kernel.put_in(["object", "cc"], cc)
  45. {:ok, message}
  46. else
  47. _e ->
  48. {:reject, "[ObjectAgePolicy] Unhandled error"}
  49. end
  50. else
  51. {:ok, message}
  52. end
  53. end
  54. defp check_strip_followers(message, actions) do
  55. if :strip_followers in actions do
  56. with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
  57. to = List.delete(message["to"] || [], user.follower_address)
  58. cc = List.delete(message["cc"] || [], user.follower_address)
  59. message =
  60. message
  61. |> Map.put("to", to)
  62. |> Map.put("cc", cc)
  63. |> Kernel.put_in(["object", "to"], to)
  64. |> Kernel.put_in(["object", "cc"], cc)
  65. {:ok, message}
  66. else
  67. _e ->
  68. {:reject, "[ObjectAgePolicy] Unhandled error"}
  69. end
  70. else
  71. {:ok, message}
  72. end
  73. end
  74. @impl true
  75. def filter(%{"type" => "Create", "object" => %{"published" => _}} = message) do
  76. with actions <- Config.get([:mrf_object_age, :actions]),
  77. {:reject, _} <- check_date(message),
  78. {:ok, message} <- check_reject(message, actions),
  79. {:ok, message} <- check_delist(message, actions),
  80. {:ok, message} <- check_strip_followers(message, actions) do
  81. {:ok, message}
  82. else
  83. # check_date() is allowed to short-circuit the pipeline
  84. e -> e
  85. end
  86. end
  87. @impl true
  88. def filter(message), do: {:ok, message}
  89. @impl true
  90. def describe do
  91. mrf_object_age =
  92. Config.get(:mrf_object_age)
  93. |> Enum.into(%{})
  94. {:ok, %{mrf_object_age: mrf_object_age}}
  95. end
  96. @impl true
  97. def config_description do
  98. %{
  99. key: :mrf_object_age,
  100. related_policy: "Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy",
  101. label: "MRF Object Age",
  102. description:
  103. "Rejects or delists posts based on their timestamp deviance from your server's clock.",
  104. children: [
  105. %{
  106. key: :threshold,
  107. type: :integer,
  108. description: "Required age (in seconds) of a post before actions are taken.",
  109. suggestions: [172_800]
  110. },
  111. %{
  112. key: :actions,
  113. type: {:list, :atom},
  114. description:
  115. "A list of actions to apply to the post. `:delist` removes the post from public timelines; " <>
  116. "`:strip_followers` removes followers from the ActivityPub recipient list ensuring they won't be delivered to home timelines, additionally for followers-only it degrades to a direct message; " <>
  117. "`:reject` rejects the message entirely",
  118. suggestions: [:delist, :strip_followers, :reject]
  119. }
  120. ]
  121. }
  122. end
  123. end