Make Mastodon follow hack more explicit.

This commit is contained in:
lain 2018-05-26 15:07:21 +02:00
parent 3839a11ef5
commit bfce29866f
2 changed files with 30 additions and 26 deletions

View File

@ -146,21 +146,27 @@ def handle_incoming(
end end
end end
defp get_follow_activity(follow_object) do defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
cond do with true <- id =~ "follows",
is_map(follow_object) -> %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
{:ok, follow_object} %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
{:ok, activity}
is_binary(follow_object) ->
object = Activity.get_by_ap_id(follow_object)
if object do
{:ok, object.data}
else else
{:error, nil} _ -> {:error, nil}
end
end end
true -> defp mastodon_follow_hack(_), do: {:error, nil}
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} {:error, nil}
end end
end end
@ -169,10 +175,8 @@ def handle_incoming(
%{"type" => "Accept", "object" => follow_object, "actor" => actor, "id" => id} = data %{"type" => "Accept", "object" => follow_object, "actor" => actor, "id" => id} = data
) do ) do
with %User{} = followed <- User.get_or_fetch_by_ap_id(actor), with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
{:ok, follow_activity} <- get_follow_activity(follow_object), {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
%User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]), %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
follow_activity <- Utils.fetch_latest_follow(follower, followed),
false <- is_nil(follow_activity),
{:ok, activity} <- ActivityPub.accept(%{to: follow_activity.data["to"], type: "Accept", actor: followed.ap_id, object: follow_activity.data["id"], local: false}) do {: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 if not User.following?(follower, followed) do
{:ok, follower} = User.follow(follower, followed) {:ok, follower} = User.follow(follower, followed)
@ -188,10 +192,8 @@ def handle_incoming(
%{"type" => "Reject", "object" => follow_object, "actor" => actor, "id" => id} = data %{"type" => "Reject", "object" => follow_object, "actor" => actor, "id" => id} = data
) do ) do
with %User{} = followed <- User.get_or_fetch_by_ap_id(actor), with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
{:ok, follow_activity} <- get_follow_activity(follow_object), {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
%User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]), %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
follow_activity <- Utils.fetch_latest_follow(follower, followed),
false <- is_nil(follow_activity),
{:ok, activity} <- ActivityPub.accept(%{to: follow_activity.data["to"], type: "Accept", actor: followed.ap_id, object: follow_activity.data["id"], local: false}) do {: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) User.unfollow(follower, followed)

View File

@ -8,8 +8,6 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
alias Pleroma.User alias Pleroma.User
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Web.Websub.WebsubClientSubscription alias Pleroma.Web.Websub.WebsubClientSubscription
alias Pleroma.Web.Websub.WebsubServerSubscription
import Ecto.Query
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -284,7 +282,7 @@ test "it works for incoming deletes" do
|> Map.put("object", object) |> Map.put("object", object)
|> Map.put("actor", activity.data["actor"]) |> 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) refute Repo.get(Activity, activity.id)
end end
@ -401,8 +399,12 @@ test "it works for incoming accepts which were pre-accepted" do
|> Poison.decode!() |> Poison.decode!()
|> Map.put("actor", followed.ap_id) |> 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 = 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) {:ok, activity} = Transmogrifier.handle_incoming(accept_data)
refute activity.local refute activity.local
@ -499,7 +501,7 @@ test "it works for incoming rejects which are orphaned" do
followed = insert(:user, %{info: %{"locked" => true}}) followed = insert(:user, %{info: %{"locked" => true}})
{:ok, follower} = User.follow(follower, followed) {: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 assert User.following?(follower, followed) == true