1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
40 to = List.delete(message["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
41 cc = List.delete(message["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
51 {:reject, "[ObjectAgePolicy] Unhandled error"}
58 defp check_strip_followers(message, actions) do
59 if :strip_followers in actions do
60 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
61 to = List.delete(message["to"], user.follower_address)
62 cc = List.delete(message["cc"], user.follower_address)
72 {:reject, "[ObjectAgePolicy] Unhandled error"}
80 def filter(%{"type" => "Create", "published" => _} = message) do
81 with actions <- Config.get([:mrf_object_age, :actions]),
82 {:reject, _} <- check_date(message),
83 {:ok, message} <- check_reject(message, actions),
84 {:ok, message} <- check_delist(message, actions),
85 {:ok, message} <- check_strip_followers(message, actions) do
88 # check_date() is allowed to short-circuit the pipeline
94 def filter(message), do: {:ok, message}
99 Config.get(:mrf_object_age)
102 {:ok, %{mrf_object_age: mrf_object_age}}