2019-11-15 01:48:10 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-11-15 01:48:10 +00: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"
|
2021-06-07 19:22:08 +00:00
|
|
|
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
|
2019-11-15 01:48:10 +00:00
|
|
|
|
2020-04-20 12:59:16 +00:00
|
|
|
defp check_date(%{"object" => %{"published" => published}} = message) do
|
2019-11-15 01:48:10 +00: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
|
2020-07-13 13:47:13 +00:00
|
|
|
{:reject, "[ObjectAgePolicy]"}
|
2019-11-15 01:48:10 +00:00
|
|
|
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
|
2020-08-03 12:12:32 +00:00
|
|
|
to =
|
|
|
|
List.delete(message["to"] || [], Pleroma.Constants.as_public()) ++
|
|
|
|
[user.follower_address]
|
|
|
|
|
|
|
|
cc =
|
|
|
|
List.delete(message["cc"] || [], user.follower_address) ++
|
|
|
|
[Pleroma.Constants.as_public()]
|
2019-11-15 01:48:10 +00:00
|
|
|
|
|
|
|
message =
|
|
|
|
message
|
|
|
|
|> Map.put("to", to)
|
|
|
|
|> Map.put("cc", cc)
|
2021-08-10 05:41:06 +00:00
|
|
|
|> Kernel.put_in(["object", "to"], to)
|
|
|
|
|> Kernel.put_in(["object", "cc"], cc)
|
2019-11-15 01:48:10 +00:00
|
|
|
|
|
|
|
{:ok, message}
|
|
|
|
else
|
2019-11-15 01:56:14 +00:00
|
|
|
_e ->
|
2020-07-13 13:47:13 +00:00
|
|
|
{:reject, "[ObjectAgePolicy] Unhandled error"}
|
2019-11-15 01:48:10 +00:00
|
|
|
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
|
2020-08-03 12:12:32 +00:00
|
|
|
to = List.delete(message["to"] || [], user.follower_address)
|
|
|
|
cc = List.delete(message["cc"] || [], user.follower_address)
|
2019-11-15 01:48:10 +00:00
|
|
|
|
|
|
|
message =
|
|
|
|
message
|
|
|
|
|> Map.put("to", to)
|
|
|
|
|> Map.put("cc", cc)
|
2021-08-10 05:41:06 +00:00
|
|
|
|> Kernel.put_in(["object", "to"], to)
|
|
|
|
|> Kernel.put_in(["object", "cc"], cc)
|
2019-11-15 01:48:10 +00:00
|
|
|
|
|
|
|
{:ok, message}
|
|
|
|
else
|
|
|
|
_e ->
|
2020-07-13 13:47:13 +00:00
|
|
|
{:reject, "[ObjectAgePolicy] Unhandled error"}
|
2019-11-15 01:48:10 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
{:ok, message}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2021-08-10 05:41:06 +00:00
|
|
|
def filter(%{"type" => "Create", "object" => %{"published" => _}} = message) do
|
2019-11-15 01:48:10 +00:00
|
|
|
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 12:59:16 +00:00
|
|
|
def describe do
|
|
|
|
mrf_object_age =
|
2020-07-09 15:53:51 +00:00
|
|
|
Config.get(:mrf_object_age)
|
2020-04-20 12:59:16 +00:00
|
|
|
|> Enum.into(%{})
|
|
|
|
|
|
|
|
{:ok, %{mrf_object_age: mrf_object_age}}
|
|
|
|
end
|
2020-11-10 16:18:53 +00:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def config_description do
|
|
|
|
%{
|
|
|
|
key: :mrf_object_age,
|
|
|
|
related_policy: "Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy",
|
|
|
|
label: "MRF Object Age",
|
|
|
|
description:
|
|
|
|
"Rejects or delists posts based on their timestamp deviance from your server's clock.",
|
|
|
|
children: [
|
|
|
|
%{
|
|
|
|
key: :threshold,
|
|
|
|
type: :integer,
|
|
|
|
description: "Required age (in seconds) of a post before actions are taken.",
|
|
|
|
suggestions: [172_800]
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
key: :actions,
|
|
|
|
type: {:list, :atom},
|
|
|
|
description:
|
|
|
|
"A list of actions to apply to the post. `:delist` removes the post from public timelines; " <>
|
|
|
|
"`:strip_followers` removes followers from the ActivityPub recipient list ensuring they won't be delivered to home timelines; " <>
|
|
|
|
"`:reject` rejects the message entirely",
|
|
|
|
suggestions: [:delist, :strip_followers, :reject]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
2019-11-15 01:48:10 +00:00
|
|
|
end
|