SimpleMRF: Let instances be silenced

This commit is contained in:
Alex Gleason 2020-07-28 18:55:29 -05:00
parent 93dbba9b8a
commit 3c90f7f715
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 82 additions and 0 deletions

View File

@ -1524,6 +1524,12 @@
description: "List of instances to only accept activities from (except deletes)",
suggestions: ["example.com", "*.example.com"]
},
%{
key: :silence,
type: {:list, :string},
description: "Force posts from the given instances to be visible by followers only",
suggestions: ["example.com", "*.example.com"]
},
%{
key: :report_removal,
type: {:list, :string},

View File

@ -122,6 +122,7 @@ To add configuration to your config file, you can copy it from the base config.
* `federated_timeline_removal`: List of instances to remove from Federated (aka The Whole Known Network) Timeline.
* `reject`: List of instances to reject any activities from.
* `accept`: List of instances to accept any activities from.
* `silence`: List of instances to force posts as followers-only.
* `report_removal`: List of instances to reject reports from.
* `avatar_removal`: List of instances to strip avatars from.
* `banner_removal`: List of instances to strip banners from.

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
@behaviour Pleroma.Web.ActivityPub.MRF
alias Pleroma.Config
alias Pleroma.FollowingRelationship
alias Pleroma.User
alias Pleroma.Web.ActivityPub.MRF
@ -108,6 +109,32 @@ defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
{:ok, object}
end
defp check_silence(%{host: actor_host} = _actor_info, object) do
silence =
Config.get([:mrf_simple, :silence])
|> MRF.subdomains_regex()
object =
with true <- MRF.subdomain_match?(silence, actor_host),
user <- User.get_cached_by_ap_id(object["actor"]) do
to =
FollowingRelationship.followers_ap_ids(user, Map.get(object, "to", [])) ++
[user.follower_address]
cc = FollowingRelationship.followers_ap_ids(user, Map.get(object, "cc", []))
object
|> Map.put("to", to)
|> Map.put("cc", cc)
else
_ -> object
end
{:ok, object}
end
defp check_silence(_actor_info, object), do: {:ok, object}
defp check_report_removal(%{host: actor_host} = _actor_info, %{"type" => "Flag"} = object) do
report_removal =
Config.get([:mrf_simple, :report_removal])
@ -174,6 +201,7 @@ def filter(%{"actor" => actor} = object) do
{:ok, object} <- check_media_removal(actor_info, object),
{:ok, object} <- check_media_nsfw(actor_info, object),
{:ok, object} <- check_ftl_removal(actor_info, object),
{:ok, object} <- check_silence(actor_info, object),
{:ok, object} <- check_report_removal(actor_info, object) do
{:ok, object}
else

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do
import Pleroma.Factory
alias Pleroma.Config
alias Pleroma.Web.ActivityPub.MRF.SimplePolicy
alias Pleroma.Web.CommonAPI
setup do:
clear_config(:mrf_simple,
@ -15,6 +16,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do
federated_timeline_removal: [],
report_removal: [],
reject: [],
silence: [],
accept: [],
avatar_removal: [],
banner_removal: [],
@ -261,6 +263,51 @@ test "actor has a matching host" do
end
end
describe "when :silence" do
test "is empty" do
Config.put([:mrf_simple, :silence], [])
{_, ftl_message} = build_ftl_actor_and_message()
local_message = build_local_message()
assert SimplePolicy.filter(ftl_message) == {:ok, ftl_message}
assert SimplePolicy.filter(local_message) == {:ok, local_message}
end
test "has a matching host" do
actor = insert(:user)
following_user = insert(:user)
non_following_user = insert(:user)
{:ok, _, _, _} = CommonAPI.follow(following_user, actor)
activity = %{
"actor" => actor.ap_id,
"to" => [
"https://www.w3.org/ns/activitystreams#Public",
following_user.ap_id,
non_following_user.ap_id
],
"cc" => [actor.follower_address, "http://foo.bar/qux"]
}
actor_domain =
activity
|> Map.fetch!("actor")
|> URI.parse()
|> Map.fetch!(:host)
Config.put([:mrf_simple, :silence], [actor_domain])
assert {:ok, new_activity} = SimplePolicy.filter(activity)
assert actor.follower_address in new_activity["to"]
assert following_user.ap_id in new_activity["to"]
refute "https://www.w3.org/ns/activitystreams#Public" in new_activity["to"]
refute "https://www.w3.org/ns/activitystreams#Public" in new_activity["cc"]
refute non_following_user.ap_id in new_activity["to"]
refute non_following_user.ap_id in new_activity["cc"]
end
end
describe "when :accept" do
test "is empty" do
Config.put([:mrf_simple, :accept], [])