aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Pitcock <nenolod@dereferenced.org>2018-05-25 06:09:01 +0000
committerWilliam Pitcock <nenolod@dereferenced.org>2018-05-25 06:14:18 +0000
commit502ba33d01bc73cc40fc6734c086fa4b58a76634 (patch)
tree4932075e513002d8320386e07f4a2f60a80ed384
parentc0ca9f82b991d89524a8f0f770f4b7b08da59e2f (diff)
downloadpleroma-502ba33d01bc73cc40fc6734c086fa4b58a76634.tar.gz
activitypub: fix up accept/reject semantics for following
fixes #175
-rw-r--r--lib/pleroma/web/activity_pub/transmogrifier.ex43
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex2
2 files changed, 43 insertions, 2 deletions
diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex
index 803445011..eaa716cea 100644
--- a/lib/pleroma/web/activity_pub/transmogrifier.ex
+++ b/lib/pleroma/web/activity_pub/transmogrifier.ex
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
alias Pleroma.Activity
alias Pleroma.Repo
alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.ActivityPub.Utils
import Ecto.Query
@@ -145,6 +146,48 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
end
end
+ defp get_follow_activity(follow_object) do
+ cond do
+ is_map(follow_object) ->
+ {:ok, follow_object}
+
+ is_binary(follow_object) ->
+ object = get_obj_helper(follow_object) || ActivityPub.fetch_object_from_id(follow_object)
+ if object do
+ {:ok, object}
+ else
+ {:error, nil}
+ end
+
+ true ->
+ {:error, nil}
+ end
+ end
+
+ 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"]) do
+ User.follow(follower, followed)
+
+ {:ok, data}
+ end
+ end
+
+ 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"]),
+ {:ok, follow_activity} <- Utils.fetch_latest_follow(follower, followed),
+ {:ok, activity} <- ActivityPub.delete(follow_activity, false) do
+ {:ok, activity}
+ end
+ end
+
def handle_incoming(
%{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = _data
) do
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index 460942f1a..d50d2d9b5 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -429,7 +429,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
with %User{} = followed <- Repo.get(User, id),
- {:ok, follower} <- User.follow(follower, followed),
{:ok, _activity} <- ActivityPub.follow(follower, followed) do
render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
else
@@ -442,7 +441,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
with %User{} = followed <- Repo.get_by(User, nickname: uri),
- {:ok, follower} <- User.follow(follower, followed),
{:ok, _activity} <- ActivityPub.follow(follower, followed) do
render(conn, AccountView, "account.json", %{user: followed})
else