From bfce29866fea3ec7ab91a6b1f20a845248fa0130 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 26 May 2018 15:07:21 +0200 Subject: [PATCH] Make Mastodon follow hack more explicit. --- .../web/activity_pub/transmogrifier.ex | 44 ++++++++++--------- test/web/activity_pub/transmogrifier_test.exs | 12 ++--- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index b2224514c..fee0b5859 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -146,21 +146,27 @@ def handle_incoming( end end - defp get_follow_activity(follow_object) do - cond do - is_map(follow_object) -> - {:ok, follow_object} + defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do + with true <- id =~ "follows", + %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id), + %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do + {:ok, activity} + else + _ -> {:error, nil} + end + end - is_binary(follow_object) -> - object = Activity.get_by_ap_id(follow_object) + defp mastodon_follow_hack(_), do: {:error, nil} - if object do - {:ok, object.data} - else - {:error, nil} - end - - true -> + defp get_follow_activity(follow_object, followed) do + with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object), + {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do + {:ok, activity} + else + # Can't find the activity. This might a Mastodon 2.3 "Accept" + {:activity, nil} -> + mastodon_follow_hack(follow_object, followed) + _ -> {:error, nil} end end @@ -169,10 +175,8 @@ def handle_incoming( %{"type" => "Accept", "object" => follow_object, "actor" => actor, "id" => id} = data ) do with %User{} = followed <- User.get_or_fetch_by_ap_id(actor), - {:ok, follow_activity} <- get_follow_activity(follow_object), - %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]), - follow_activity <- Utils.fetch_latest_follow(follower, followed), - false <- is_nil(follow_activity), + {:ok, follow_activity} <- get_follow_activity(follow_object, followed), + %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]), {:ok, activity} <- ActivityPub.accept(%{to: follow_activity.data["to"], type: "Accept", actor: followed.ap_id, object: follow_activity.data["id"], local: false}) do if not User.following?(follower, followed) do {:ok, follower} = User.follow(follower, followed) @@ -188,10 +192,8 @@ def handle_incoming( %{"type" => "Reject", "object" => follow_object, "actor" => actor, "id" => id} = data ) do with %User{} = followed <- User.get_or_fetch_by_ap_id(actor), - {:ok, follow_activity} <- get_follow_activity(follow_object), - %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]), - follow_activity <- Utils.fetch_latest_follow(follower, followed), - false <- is_nil(follow_activity), + {:ok, follow_activity} <- get_follow_activity(follow_object, followed), + %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]), {:ok, activity} <- ActivityPub.accept(%{to: follow_activity.data["to"], type: "Accept", actor: followed.ap_id, object: follow_activity.data["id"], local: false}) do User.unfollow(follower, followed) diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 761d9d992..43395eef1 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -8,8 +8,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do alias Pleroma.User alias Pleroma.Repo alias Pleroma.Web.Websub.WebsubClientSubscription - alias Pleroma.Web.Websub.WebsubServerSubscription - import Ecto.Query import Pleroma.Factory alias Pleroma.Web.CommonAPI @@ -284,7 +282,7 @@ test "it works for incoming deletes" do |> Map.put("object", object) |> Map.put("actor", activity.data["actor"]) - {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data) refute Repo.get(Activity, activity.id) end @@ -401,8 +399,12 @@ test "it works for incoming accepts which were pre-accepted" do |> Poison.decode!() |> Map.put("actor", followed.ap_id) + object = accept_data["object"] + |> Map.put("actor", follower.ap_id) + |> Map.put("id", follow_activity.data["id"]) + accept_data = - Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id)) + Map.put(accept_data, "object", object) {:ok, activity} = Transmogrifier.handle_incoming(accept_data) refute activity.local @@ -499,7 +501,7 @@ test "it works for incoming rejects which are orphaned" do followed = insert(:user, %{info: %{"locked" => true}}) {:ok, follower} = User.follow(follower, followed) - {:ok, follow_activity} = ActivityPub.follow(follower, followed) + {:ok, _follow_activity} = ActivityPub.follow(follower, followed) assert User.following?(follower, followed) == true