1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy do
9 require Pleroma.Constants
11 @moduledoc "Filter activities depending on their age"
12 @behaviour Pleroma.Web.ActivityPub.MRF
14 defp check_date(%{"object" => %{"published" => published}} = message) do
15 with %DateTime{} = now <- DateTime.utc_now(),
16 {:ok, %DateTime{} = then, _} <- DateTime.from_iso8601(published),
17 max_ttl <- Config.get([:mrf_object_age, :threshold]),
18 {:ttl, false} <- {:ttl, DateTime.diff(now, then) > max_ttl} do
29 defp check_reject(message, actions) do
30 if :reject in actions do
31 {:reject, "[ObjectAgePolicy]"}
37 defp check_delist(message, actions) do
38 if :delist in actions do
39 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
41 List.delete(message["to"] || [], Pleroma.Constants.as_public()) ++
42 [user.follower_address]
45 List.delete(message["cc"] || [], user.follower_address) ++
46 [Pleroma.Constants.as_public()]
56 {:reject, "[ObjectAgePolicy] Unhandled error"}
63 defp check_strip_followers(message, actions) do
64 if :strip_followers in actions do
65 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
66 to = List.delete(message["to"] || [], user.follower_address)
67 cc = List.delete(message["cc"] || [], user.follower_address)
77 {:reject, "[ObjectAgePolicy] Unhandled error"}
85 def filter(%{"type" => "Create", "published" => _} = message) do
86 with actions <- Config.get([:mrf_object_age, :actions]),
87 {:reject, _} <- check_date(message),
88 {:ok, message} <- check_reject(message, actions),
89 {:ok, message} <- check_delist(message, actions),
90 {:ok, message} <- check_strip_followers(message, actions) do
93 # check_date() is allowed to short-circuit the pipeline
99 def filter(message), do: {:ok, message}
104 Config.get(:mrf_object_age)
107 {:ok, %{mrf_object_age: mrf_object_age}}
111 def config_description do
113 key: :mrf_object_age,
114 related_policy: "Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy",
115 label: "MRF Object Age",
117 "Rejects or delists posts based on their timestamp deviance from your server's clock.",
122 description: "Required age (in seconds) of a post before actions are taken.",
123 suggestions: [172_800]
127 type: {:list, :atom},
129 "A list of actions to apply to the post. `:delist` removes the post from public timelines; " <>
130 "`:strip_followers` removes followers from the ActivityPub recipient list ensuring they won't be delivered to home timelines; " <>
131 "`:reject` rejects the message entirely",
132 suggestions: [:delist, :strip_followers, :reject]