2019-11-14 18:48:10 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-01 22:08:45 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-11-14 18:48:10 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy do
|
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.User
|
|
|
|
|
|
|
|
require Pleroma.Constants
|
|
|
|
|
|
|
|
@moduledoc "Filter activities depending on their age"
|
2020-02-14 10:02:24 -07:00
|
|
|
@behaviour Pleroma.Web.ActivityPub.MRF
|
2019-11-14 18:48:10 -07:00
|
|
|
|
2020-04-20 06:59:16 -06:00
|
|
|
defp check_date(%{"object" => %{"published" => published}} = message) do
|
2019-11-14 18:48:10 -07:00
|
|
|
with %DateTime{} = now <- DateTime.utc_now(),
|
|
|
|
{:ok, %DateTime{} = then, _} <- DateTime.from_iso8601(published),
|
|
|
|
max_ttl <- Config.get([:mrf_object_age, :threshold]),
|
|
|
|
{:ttl, false} <- {:ttl, DateTime.diff(now, then) > max_ttl} do
|
|
|
|
{:ok, message}
|
|
|
|
else
|
|
|
|
{:ttl, true} ->
|
|
|
|
{:reject, nil}
|
|
|
|
|
|
|
|
e ->
|
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp check_reject(message, actions) do
|
|
|
|
if :reject in actions do
|
|
|
|
{:reject, nil}
|
|
|
|
else
|
|
|
|
{:ok, message}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp check_delist(message, actions) do
|
|
|
|
if :delist in actions do
|
|
|
|
with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
|
|
|
|
to = List.delete(message["to"], Pleroma.Constants.as_public()) ++ [user.follower_address]
|
|
|
|
cc = List.delete(message["cc"], user.follower_address) ++ [Pleroma.Constants.as_public()]
|
|
|
|
|
|
|
|
message =
|
|
|
|
message
|
|
|
|
|> Map.put("to", to)
|
|
|
|
|> Map.put("cc", cc)
|
|
|
|
|
|
|
|
{:ok, message}
|
|
|
|
else
|
|
|
|
# Unhandleable error: somebody is messing around, just drop the message.
|
2019-11-14 18:56:14 -07:00
|
|
|
_e ->
|
2019-11-14 18:48:10 -07:00
|
|
|
{:reject, nil}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:ok, message}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp check_strip_followers(message, actions) do
|
|
|
|
if :strip_followers in actions do
|
|
|
|
with %User{} = user <- User.get_cached_by_ap_id(message["actor"]) do
|
|
|
|
to = List.delete(message["to"], user.follower_address)
|
|
|
|
cc = List.delete(message["cc"], user.follower_address)
|
|
|
|
|
|
|
|
message =
|
|
|
|
message
|
|
|
|
|> Map.put("to", to)
|
|
|
|
|> Map.put("cc", cc)
|
|
|
|
|
|
|
|
{:ok, message}
|
|
|
|
else
|
|
|
|
# Unhandleable error: somebody is messing around, just drop the message.
|
|
|
|
_e ->
|
|
|
|
{:reject, nil}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:ok, message}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def filter(%{"type" => "Create", "published" => _} = message) do
|
|
|
|
with actions <- Config.get([:mrf_object_age, :actions]),
|
|
|
|
{:reject, _} <- check_date(message),
|
|
|
|
{:ok, message} <- check_reject(message, actions),
|
|
|
|
{:ok, message} <- check_delist(message, actions),
|
|
|
|
{:ok, message} <- check_strip_followers(message, actions) do
|
|
|
|
{:ok, message}
|
|
|
|
else
|
|
|
|
# check_date() is allowed to short-circuit the pipeline
|
|
|
|
e -> e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def filter(message), do: {:ok, message}
|
|
|
|
|
|
|
|
@impl true
|
2020-04-20 06:59:16 -06:00
|
|
|
def describe do
|
|
|
|
mrf_object_age =
|
|
|
|
Pleroma.Config.get(:mrf_object_age)
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
|
|
|
{:ok, %{mrf_object_age: mrf_object_age}}
|
|
|
|
end
|
2019-11-14 18:48:10 -07:00
|
|
|
end
|