ActivityPub: Remove follow and fix issues.

This commit is contained in:
lain 2020-07-08 17:07:24 +02:00
parent 9dda8b5427
commit 00e54f8fe7
10 changed files with 43 additions and 77 deletions

View file

@ -1543,7 +1543,7 @@ def perform(:follow_import, %User{} = follower, followed_identifiers)
fn followed_identifier -> fn followed_identifier ->
with {:ok, %User{} = followed} <- get_or_fetch(followed_identifier), with {:ok, %User{} = followed} <- get_or_fetch(followed_identifier),
{:ok, follower} <- maybe_direct_follow(follower, followed), {:ok, follower} <- maybe_direct_follow(follower, followed),
{:ok, _} <- ActivityPub.follow(follower, followed) do {:ok, _, _, _} <- CommonAPI.follow(follower, followed) do
followed followed
else else
err -> err ->

View file

@ -322,28 +322,6 @@ defp accept_or_reject(type, %{to: to, actor: actor, object: object} = params) do
end end
end end
@spec follow(User.t(), User.t(), String.t() | nil, boolean(), keyword()) ::
{:ok, Activity.t()} | {:error, any()}
def follow(follower, followed, activity_id \\ nil, local \\ true, opts \\ []) do
with {:ok, result} <-
Repo.transaction(fn -> do_follow(follower, followed, activity_id, local, opts) end) do
result
end
end
defp do_follow(follower, followed, activity_id, local, opts) do
skip_notify_and_stream = Keyword.get(opts, :skip_notify_and_stream, false)
data = make_follow_data(follower, followed, activity_id)
with {:ok, activity} <- insert(data, local),
_ <- skip_notify_and_stream || notify_and_stream(activity),
:ok <- maybe_federate(activity) do
{:ok, activity}
else
{:error, error} -> Repo.rollback(error)
end
end
@spec unfollow(User.t(), User.t(), String.t() | nil, boolean()) :: @spec unfollow(User.t(), User.t(), String.t() | nil, boolean()) ::
{:ok, Activity.t()} | nil | {:error, any()} {:ok, Activity.t()} | nil | {:error, any()}
def unfollow(follower, followed, activity_id \\ nil, local \\ true) do def unfollow(follower, followed, activity_id \\ nil, local \\ true) do

View file

@ -28,7 +28,7 @@ def relay_ap_id do
def follow(target_instance) do def follow(target_instance) do
with %User{} = local_user <- get_actor(), with %User{} = local_user <- get_actor(),
{:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance), {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
{:ok, activity} <- ActivityPub.follow(local_user, target_user) do {:ok, _, _, activity} <- CommonAPI.follow(local_user, target_user) do
Logger.info("relay: followed instance: #{target_instance}; id=#{activity.data["id"]}") Logger.info("relay: followed instance: #{target_instance}; id=#{activity.data["id"]}")
{:ok, activity} {:ok, activity}
else else

View file

@ -10,6 +10,8 @@ defmodule Mix.Tasks.Pleroma.RelayTest do
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory
setup_all do setup_all do
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
@ -46,7 +48,8 @@ test "relay is followed" do
describe "running unfollow" do describe "running unfollow" do
test "relay is unfollowed" do test "relay is unfollowed" do
target_instance = "http://mastodon.example.org/users/admin" user = insert(:user)
target_instance = user.ap_id
Mix.Tasks.Pleroma.Relay.run(["follow", target_instance]) Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
@ -71,7 +74,7 @@ test "relay is unfollowed" do
assert undo_activity.data["type"] == "Undo" assert undo_activity.data["type"] == "Undo"
assert undo_activity.data["actor"] == local_user.ap_id assert undo_activity.data["actor"] == local_user.ap_id
assert undo_activity.data["object"] == cancelled_activity.data assert undo_activity.data["object"]["id"] == cancelled_activity.data["id"]
refute "#{target_instance}/followers" in User.following(local_user) refute "#{target_instance}/followers" in User.following(local_user)
end end
end end

View file

@ -669,7 +669,7 @@ test "doesn't return activities from blocked domains" do
refute activity in activities refute activity in activities
followed_user = insert(:user) followed_user = insert(:user)
ActivityPub.follow(user, followed_user) CommonAPI.follow(user, followed_user)
{:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user) {:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user)
activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true}) activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
@ -1013,24 +1013,12 @@ test "fetches the latest Follow activity" do
end end
end end
describe "following / unfollowing" do describe "unfollowing" do
test "it reverts follow activity" do
follower = insert(:user)
followed = insert(:user)
with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
assert {:error, :reverted} = ActivityPub.follow(follower, followed)
end
assert Repo.aggregate(Activity, :count, :id) == 0
assert Repo.aggregate(Object, :count, :id) == 0
end
test "it reverts unfollow activity" do test "it reverts unfollow activity" do
follower = insert(:user) follower = insert(:user)
followed = insert(:user) followed = insert(:user)
{:ok, follow_activity} = ActivityPub.follow(follower, followed) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
assert {:error, :reverted} = ActivityPub.unfollow(follower, followed) assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
@ -1043,21 +1031,11 @@ test "it reverts unfollow activity" do
assert activity.data["object"] == followed.ap_id assert activity.data["object"] == followed.ap_id
end end
test "creates a follow activity" do
follower = insert(:user)
followed = insert(:user)
{:ok, activity} = ActivityPub.follow(follower, followed)
assert activity.data["type"] == "Follow"
assert activity.data["actor"] == follower.ap_id
assert activity.data["object"] == followed.ap_id
end
test "creates an undo activity for the last follow" do test "creates an undo activity for the last follow" do
follower = insert(:user) follower = insert(:user)
followed = insert(:user) followed = insert(:user)
{:ok, follow_activity} = ActivityPub.follow(follower, followed) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
{:ok, activity} = ActivityPub.unfollow(follower, followed) {:ok, activity} = ActivityPub.unfollow(follower, followed)
assert activity.data["type"] == "Undo" assert activity.data["type"] == "Undo"
@ -1074,7 +1052,7 @@ test "creates an undo activity for a pending follow request" do
follower = insert(:user) follower = insert(:user)
followed = insert(:user, %{locked: true}) followed = insert(:user, %{locked: true})
{:ok, follow_activity} = ActivityPub.follow(follower, followed) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
{:ok, activity} = ActivityPub.unfollow(follower, followed) {:ok, activity} = ActivityPub.unfollow(follower, followed)
assert activity.data["type"] == "Undo" assert activity.data["type"] == "Undo"

View file

@ -7,8 +7,8 @@ defmodule Pleroma.Web.ActivityPub.RelayTest do
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Relay alias Pleroma.Web.ActivityPub.Relay
alias Pleroma.Web.CommonAPI
import ExUnit.CaptureLog import ExUnit.CaptureLog
import Pleroma.Factory import Pleroma.Factory
@ -53,8 +53,7 @@ test "returns errors when user not found" do
test "returns activity" do test "returns activity" do
user = insert(:user) user = insert(:user)
service_actor = Relay.get_actor() service_actor = Relay.get_actor()
ActivityPub.follow(service_actor, user) CommonAPI.follow(service_actor, user)
Pleroma.User.follow(service_actor, user)
assert "#{user.ap_id}/followers" in User.following(service_actor) assert "#{user.ap_id}/followers" in User.following(service_actor)
assert {:ok, %Activity{} = activity} = Relay.unfollow(user.ap_id) assert {:ok, %Activity{} = activity} = Relay.unfollow(user.ap_id)
assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay" assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
@ -74,6 +73,7 @@ test "returns error when activity not `Create` type" do
assert Relay.publish(activity) == {:error, "Not implemented"} assert Relay.publish(activity) == {:error, "Not implemented"}
end end
@tag capture_log: true
test "returns error when activity not public" do test "returns error when activity not public" do
activity = insert(:direct_note_activity) activity = insert(:direct_note_activity)
assert Relay.publish(activity) == {:error, false} assert Relay.publish(activity) == {:error, false}

View file

@ -11,7 +11,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
alias Pleroma.Object.Fetcher alias Pleroma.Object.Fetcher
alias Pleroma.Tests.ObanHelpers alias Pleroma.Tests.ObanHelpers
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.AdminAPI.AccountView alias Pleroma.Web.AdminAPI.AccountView
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -452,7 +451,7 @@ test "it works for incoming accepts which were pre-accepted" do
{:ok, follower} = User.follow(follower, followed) {:ok, follower} = User.follow(follower, followed)
assert User.following?(follower, followed) == true assert User.following?(follower, followed) == true
{:ok, follow_activity} = ActivityPub.follow(follower, followed) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
accept_data = accept_data =
File.read!("test/fixtures/mastodon-accept-activity.json") File.read!("test/fixtures/mastodon-accept-activity.json")
@ -482,7 +481,7 @@ test "it works for incoming accepts which were orphaned" do
follower = insert(:user) follower = insert(:user)
followed = insert(:user, locked: true) followed = insert(:user, locked: true)
{:ok, follow_activity} = ActivityPub.follow(follower, followed) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
accept_data = accept_data =
File.read!("test/fixtures/mastodon-accept-activity.json") File.read!("test/fixtures/mastodon-accept-activity.json")
@ -504,7 +503,7 @@ test "it works for incoming accepts which are referenced by IRI only" do
follower = insert(:user) follower = insert(:user)
followed = insert(:user, locked: true) followed = insert(:user, locked: true)
{:ok, follow_activity} = ActivityPub.follow(follower, followed) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
accept_data = accept_data =
File.read!("test/fixtures/mastodon-accept-activity.json") File.read!("test/fixtures/mastodon-accept-activity.json")
@ -569,7 +568,7 @@ test "it works for incoming rejects which are orphaned" do
followed = insert(:user, locked: true) followed = insert(:user, locked: true)
{:ok, follower} = User.follow(follower, followed) {:ok, follower} = User.follow(follower, followed)
{:ok, _follow_activity} = ActivityPub.follow(follower, followed) {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, followed)
assert User.following?(follower, followed) == true assert User.following?(follower, followed) == true
@ -595,7 +594,7 @@ test "it works for incoming rejects which are referenced by IRI only" do
followed = insert(:user, locked: true) followed = insert(:user, locked: true)
{:ok, follower} = User.follow(follower, followed) {:ok, follower} = User.follow(follower, followed)
{:ok, follow_activity} = ActivityPub.follow(follower, followed) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
assert User.following?(follower, followed) == true assert User.following?(follower, followed) == true

View file

@ -8,7 +8,6 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.AdminAPI.AccountView alias Pleroma.Web.AdminAPI.AccountView
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -197,8 +196,8 @@ test "updates the state of all Follow activities with the same actor and object"
user = insert(:user, locked: true) user = insert(:user, locked: true)
follower = insert(:user) follower = insert(:user)
{:ok, follow_activity} = ActivityPub.follow(follower, user) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
{:ok, follow_activity_two} = ActivityPub.follow(follower, user) {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
data = data =
follow_activity_two.data follow_activity_two.data
@ -221,8 +220,8 @@ test "updates the state of the given follow activity" do
user = insert(:user, locked: true) user = insert(:user, locked: true)
follower = insert(:user) follower = insert(:user)
{:ok, follow_activity} = ActivityPub.follow(follower, user) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
{:ok, follow_activity_two} = ActivityPub.follow(follower, user) {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
data = data =
follow_activity_two.data follow_activity_two.data

View file

@ -934,6 +934,15 @@ test "remove a reblog mute", %{muter: muter, muted: muted} do
end end
end end
describe "follow/2" do
test "directly follows a non-locked local user" do
[follower, followed] = insert_pair(:user)
{:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
assert User.following?(follower, followed)
end
end
describe "unfollow/2" do describe "unfollow/2" do
test "also unsubscribes a user" do test "also unsubscribes a user" do
[follower, followed] = insert_pair(:user) [follower, followed] = insert_pair(:user)
@ -998,9 +1007,9 @@ test "after acceptance, it sets all existing pending follow request states to 'a
follower = insert(:user) follower = insert(:user)
follower_two = insert(:user) follower_two = insert(:user)
{:ok, follow_activity} = ActivityPub.follow(follower, user) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
{:ok, follow_activity_two} = ActivityPub.follow(follower, user) {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
{:ok, follow_activity_three} = ActivityPub.follow(follower_two, user) {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
assert follow_activity.data["state"] == "pending" assert follow_activity.data["state"] == "pending"
assert follow_activity_two.data["state"] == "pending" assert follow_activity_two.data["state"] == "pending"
@ -1018,9 +1027,9 @@ test "after rejection, it sets all existing pending follow request states to 're
follower = insert(:user) follower = insert(:user)
follower_two = insert(:user) follower_two = insert(:user)
{:ok, follow_activity} = ActivityPub.follow(follower, user) {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
{:ok, follow_activity_two} = ActivityPub.follow(follower, user) {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
{:ok, follow_activity_three} = ActivityPub.follow(follower_two, user) {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
assert follow_activity.data["state"] == "pending" assert follow_activity.data["state"] == "pending"
assert follow_activity_two.data["state"] == "pending" assert follow_activity_two.data["state"] == "pending"

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do
use Pleroma.Web.ConnCase use Pleroma.Web.ConnCase
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.CommonAPI
import Pleroma.Factory import Pleroma.Factory
@ -20,7 +20,7 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do
test "/api/v1/follow_requests works", %{user: user, conn: conn} do test "/api/v1/follow_requests works", %{user: user, conn: conn} do
other_user = insert(:user) other_user = insert(:user)
{:ok, _activity} = ActivityPub.follow(other_user, user) {:ok, _, _, _activity} = CommonAPI.follow(other_user, user)
{:ok, other_user} = User.follow(other_user, user, :follow_pending) {:ok, other_user} = User.follow(other_user, user, :follow_pending)
assert User.following?(other_user, user) == false assert User.following?(other_user, user) == false
@ -34,7 +34,7 @@ test "/api/v1/follow_requests works", %{user: user, conn: conn} do
test "/api/v1/follow_requests/:id/authorize works", %{user: user, conn: conn} do test "/api/v1/follow_requests/:id/authorize works", %{user: user, conn: conn} do
other_user = insert(:user) other_user = insert(:user)
{:ok, _activity} = ActivityPub.follow(other_user, user) {:ok, _, _, _activity} = CommonAPI.follow(other_user, user)
{:ok, other_user} = User.follow(other_user, user, :follow_pending) {:ok, other_user} = User.follow(other_user, user, :follow_pending)
user = User.get_cached_by_id(user.id) user = User.get_cached_by_id(user.id)
@ -56,7 +56,7 @@ test "/api/v1/follow_requests/:id/authorize works", %{user: user, conn: conn} do
test "/api/v1/follow_requests/:id/reject works", %{user: user, conn: conn} do test "/api/v1/follow_requests/:id/reject works", %{user: user, conn: conn} do
other_user = insert(:user) other_user = insert(:user)
{:ok, _activity} = ActivityPub.follow(other_user, user) {:ok, _, _, _activity} = CommonAPI.follow(other_user, user)
user = User.get_cached_by_id(user.id) user = User.get_cached_by_id(user.id)