Make Mastodon follow hack more explicit.
authorlain <lain@soykaf.club>
Sat, 26 May 2018 13:07:21 +0000 (15:07 +0200)
committerlain <lain@soykaf.club>
Sat, 26 May 2018 13:07:21 +0000 (15:07 +0200)
lib/pleroma/web/activity_pub/transmogrifier.ex
test/web/activity_pub/transmogrifier_test.exs

index b2224514c1679e35a41930948e6c02d327d29976..fee0b58596c186b82a4ccee37d589f161d96cc05 100644 (file)
@@ -146,21 +146,27 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     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 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
         %{"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 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
         %{"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)
 
index 761d9d99273dd8226553e7de450bf90bddec4c18..43395eef129be418ac0c56c0e06ac0bf4f94751a 100644 (file)
@@ -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 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest 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 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest 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 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest 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