Exclude persons from being followed

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-01-22 19:44:37 +01:00
parent e6da458b49
commit a4545bcf67
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
4 changed files with 31 additions and 11 deletions

View File

@ -338,7 +338,7 @@ defmodule Mobilizon.Federation.ActivityPub do
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
{:ok, activity, follower} {:ok, activity, follower}
else else
{:error, err, msg} when err in [:already_following, :suspended] -> {:error, err, msg} when err in [:already_following, :suspended, :no_person] ->
{:error, msg} {:error, msg}
{:different_actors, _} -> {:different_actors, _} ->

View File

@ -131,7 +131,8 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Actors do
end end
end end
def follow(%Actor{} = follower_actor, %Actor{} = followed, _local, additional) do def follow(%Actor{} = follower_actor, %Actor{type: type} = followed, _local, additional)
when type != :Person do
with {:ok, %Follower{} = follower} <- with {:ok, %Follower{} = follower} <-
Mobilizon.Actors.follow(followed, follower_actor, additional["activity_id"], false), Mobilizon.Actors.follow(followed, follower_actor, additional["activity_id"], false),
:ok <- FollowMailer.send_notification_to_admins(follower), :ok <- FollowMailer.send_notification_to_admins(follower),
@ -140,6 +141,8 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Actors do
end end
end end
def follow(_, _, _, _), do: {:error, :no_person, "Only group and instances can be followed"}
defp prepare_args_for_actor(args) do defp prepare_args_for_actor(args) do
args args
|> maybe_sanitize_username() |> maybe_sanitize_username()

View File

@ -1,6 +1,7 @@
defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
use Mobilizon.DataCase use Mobilizon.DataCase
import ExUnit.CaptureLog
import Mobilizon.Factory import Mobilizon.Factory
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Actors.Follower alias Mobilizon.Actors.Follower
@ -8,9 +9,25 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier} alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier}
describe "handle incoming follow requests" do describe "handle incoming follow requests" do
test "it works for incoming follow requests" do test "it works only for groups" do
actor = insert(:actor) actor = insert(:actor)
data =
File.read!("test/fixtures/mastodon-follow-activity.json")
|> Jason.decode!()
|> Map.put("object", actor.url)
assert capture_log(fn ->
:error = Transmogrifier.handle_incoming(data)
end) =~ "Only group and instances can be followed"
actor = Actors.get_actor_with_preload(actor.id)
refute Actors.is_following(Actors.get_actor_by_url!(data["actor"], true), actor)
end
test "it works for incoming follow requests" do
actor = insert(:group)
data = data =
File.read!("test/fixtures/mastodon-follow-activity.json") File.read!("test/fixtures/mastodon-follow-activity.json")
|> Jason.decode!() |> Jason.decode!()
@ -27,7 +44,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
end end
test "it rejects activities without a valid ID" do test "it rejects activities without a valid ID" do
actor = insert(:actor) actor = insert(:group)
data = data =
File.read!("test/fixtures/mastodon-follow-activity.json") File.read!("test/fixtures/mastodon-follow-activity.json")
@ -59,7 +76,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
describe "handle incoming follow accept activities" do describe "handle incoming follow accept activities" do
test "it works for incoming accepts" do test "it works for incoming accepts" do
follower = insert(:actor) follower = insert(:actor)
followed = insert(:actor, manually_approves_followers: false) followed = insert(:group, manually_approves_followers: false)
refute Actors.is_following(follower, followed) refute Actors.is_following(follower, followed)
@ -91,7 +108,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
test "it works for incoming accepts which were pre-accepted" do test "it works for incoming accepts which were pre-accepted" do
follower = insert(:actor) follower = insert(:actor)
followed = insert(:actor, manually_approves_followers: true) followed = insert(:group, manually_approves_followers: true)
refute Actors.is_following(follower, followed) refute Actors.is_following(follower, followed)
@ -126,7 +143,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
test "it works for incoming accepts which are referenced by IRI only" do test "it works for incoming accepts which are referenced by IRI only" do
follower = insert(:actor) follower = insert(:actor)
followed = insert(:actor, manually_approves_followers: true) followed = insert(:group, manually_approves_followers: true)
{:ok, follow_activity, _} = ActivityPub.follow(follower, followed) {:ok, follow_activity, _} = ActivityPub.follow(follower, followed)
@ -148,7 +165,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
test "it fails for incoming accepts which cannot be correlated" do test "it fails for incoming accepts which cannot be correlated" do
follower = insert(:actor) follower = insert(:actor)
followed = insert(:actor) followed = insert(:group)
accept_data = accept_data =
File.read!("test/fixtures/mastodon-accept-activity.json") File.read!("test/fixtures/mastodon-accept-activity.json")
@ -169,7 +186,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
describe "handle incoming follow reject activities" do describe "handle incoming follow reject activities" do
test "it fails for incoming rejects which cannot be correlated" do test "it fails for incoming rejects which cannot be correlated" do
follower = insert(:actor) follower = insert(:actor)
followed = insert(:actor) followed = insert(:group)
accept_data = accept_data =
File.read!("test/fixtures/mastodon-reject-activity.json") File.read!("test/fixtures/mastodon-reject-activity.json")
@ -188,7 +205,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.FollowTest do
test "it works for incoming rejects which are referenced by IRI only" do test "it works for incoming rejects which are referenced by IRI only" do
follower = insert(:actor) follower = insert(:actor)
followed = insert(:actor) followed = insert(:group)
{:ok, follow_activity, _} = ActivityPub.follow(follower, followed) {:ok, follow_activity, _} = ActivityPub.follow(follower, followed)

View File

@ -46,7 +46,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.UndoTest do
end end
test "it works for incomming unfollows with an existing follow" do test "it works for incomming unfollows with an existing follow" do
actor = insert(:actor) actor = insert(:group)
follow_data = follow_data =
File.read!("test/fixtures/mastodon-follow-activity.json") File.read!("test/fixtures/mastodon-follow-activity.json")