1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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.Policy
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()]
52 |> Kernel.put_in(["object", "to"], to)
53 |> Kernel.put_in(["object", "cc"], cc)
58 {:reject, "[ObjectAgePolicy] Unhandled error"}
65 defp check_strip_followers(message, actions) do
66 if :strip_followers in actions do
67 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
68 to = List.delete(message["to"] || [], user.follower_address)
69 cc = List.delete(message["cc"] || [], user.follower_address)
75 |> Kernel.put_in(["object", "to"], to)
76 |> Kernel.put_in(["object", "cc"], cc)
81 {:reject, "[ObjectAgePolicy] Unhandled error"}
89 def filter(%{"type" => "Create", "object" => %{"published" => _}} = message) do
90 with actions <- Config.get([:mrf_object_age, :actions]),
91 {:reject, _} <- check_date(message),
92 {:ok, message} <- check_reject(message, actions),
93 {:ok, message} <- check_delist(message, actions),
94 {:ok, message} <- check_strip_followers(message, actions) do
97 # check_date() is allowed to short-circuit the pipeline
103 def filter(message), do: {:ok, message}
108 Config.get(:mrf_object_age)
111 {:ok, %{mrf_object_age: mrf_object_age}}
115 def config_description do
117 key: :mrf_object_age,
118 related_policy: "Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy",
119 label: "MRF Object Age",
121 "Rejects or delists posts based on their timestamp deviance from your server's clock.",
126 description: "Required age (in seconds) of a post before actions are taken.",
127 suggestions: [172_800]
131 type: {:list, :atom},
133 "A list of actions to apply to the post. `:delist` removes the post from public timelines; " <>
134 "`: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; " <>
135 "`:reject` rejects the message entirely",
136 suggestions: [:delist, :strip_followers, :reject]