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
11 require Pleroma.Constants
13 @moduledoc "Filter activities depending on their age"
16 defp check_date(%{"published" => published} = message) do
17 with %DateTime{} = now <- DateTime.utc_now(),
18 {:ok, %DateTime{} = then, _} <- DateTime.from_iso8601(published),
19 max_ttl <- Config.get([:mrf_object_age, :threshold]),
20 {:ttl, false} <- {:ttl, DateTime.diff(now, then) > max_ttl} do
31 defp check_reject(message, actions) do
32 if :reject in actions do
39 defp check_delist(message, actions) do
40 if :delist in actions do
41 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
42 to = List.delete(message["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
43 cc = List.delete(message["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
52 # Unhandleable error: somebody is messing around, just drop the message.
54 Logger.error("ERROR: #{inspect(e)}")
62 defp check_strip_followers(message, actions) do
63 if :strip_followers in actions do
64 with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
65 to = List.delete(message["to"], user.follower_address)
66 cc = List.delete(message["cc"], user.follower_address)
75 # Unhandleable error: somebody is messing around, just drop the message.
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}
102 def describe, do: {:ok, %{}}