1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy do
8 alias Pleroma.Web.ActivityPub.MRF
10 require Pleroma.Constants
12 @moduledoc "Filter activities depending on their age"
15 defp check_date(%{"published" => published} = message) do
16 with %DateTime{} = now <- DateTime.utc_now(),
17 {:ok, %DateTime{} = then, _} <- DateTime.from_iso8601(published),
18 max_ttl <- Config.get([:mrf_object_age, :threshold]),
19 {:ttl, false} <- {:ttl, DateTime.diff(now, then) > max_ttl} do
30 defp check_reject(message, actions) do
31 if :reject in actions do
38 defp check_delist(message, actions) do
39 if :delist in actions do
40 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
41 to = List.delete(message["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
42 cc = List.delete(message["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
51 # Unhandleable error: somebody is messing around, just drop the message.
60 defp check_strip_followers(message, actions) do
61 if :strip_followers in actions do
62 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
63 to = List.delete(message["to"], user.follower_address)
64 cc = List.delete(message["cc"], user.follower_address)
73 # Unhandleable error: somebody is messing around, just drop the message.
83 def filter(%{"type" => "Create", "published" => _} = message) do
84 with actions <- Config.get([:mrf_object_age, :actions]),
85 {:reject, _} <- check_date(message),
86 {:ok, message} <- check_reject(message, actions),
87 {:ok, message} <- check_delist(message, actions),
88 {:ok, message} <- check_strip_followers(message, actions) do
91 # check_date() is allowed to short-circuit the pipeline
97 def filter(message), do: {:ok, message}
100 def describe, do: {:ok, %{}}