From 0ec1abb3b65d9885e81a470efcc5b7437c4f8d4c Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Fri, 18 May 2018 18:09:56 -0400 Subject: [PATCH] Add federated blocks --- lib/pleroma/web/activity_pub/activity_pub.ex | 23 ++++++++++++ .../web/activity_pub/transmogrifier.ex | 18 ++++++++++ lib/pleroma/web/activity_pub/utils.ex | 35 +++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 491ad3705..48eba36fd 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -199,6 +199,29 @@ def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ tru end end + def block(blocker, blocked, local \\ true) do + follow_activity = fetch_latest_follow(blocker, blocked) + + if follow_activity do + unfollow(blocker, blocked, local) + end + + with block_data <- make_block_data(blocker, blocked), + {:ok, activity} <- insert(block_data, local), + :ok <- maybe_federate(activity) do + {:ok, activity} + end + end + + def unblock(blocker, blocked, local \\ true) do + with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked), + unblock_data <- make_unblock_data(blocker, blocked, block_activity), + {:ok, activity} <- insert(unblock_data, local), + :ok <- maybe_federate(activity) do + {:ok, activity} + end + end + def fetch_activities_for_context(context, opts \\ %{}) do public = ["https://www.w3.org/ns/activitystreams#Public"] diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 0bec8c4dd..cc0033a5f 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -259,6 +259,24 @@ def handle_incoming( end end + def handle_incoming(%{"type" => "Undo", "object" => object_id} = data) do + object = Object.get_by_ap_id(object_id).data + data = Map.put(data, "object", object) + + handle_incoming(data) + end + + def handle_incoming(%{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = data) do + with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked), + %User{} = blocker = User.get_or_fetch_by_ap_id(blocker), + {:ok, activity} <- ActivityPub.block(blocker, blocked, false) do + User.unfollow(follower, followed) + User.block(blocker, blocked.ap_id) + {:ok, activity} + else + e -> :error + end + end # TODO # Accept # Undo for non-Announce diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index f98545336..c29fc6b07 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -322,6 +322,41 @@ def make_unfollow_data(follower, followed, follow_activity) do } end + #### Block-related helpers + def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do + query = + from( + activity in Activity, + where: + fragment( + "? @> ?", + activity.data, + ^%{type: "Block", actor: blocker_id, object: blocked_id} + ), + order_by: [desc: :id], + limit: 1 + ) + + Repo.one(query) + end + + def make_block_data(blocker, blocked) do + %{ + "type" => "Block", + "actor" => blocker.ap_id, + "to" => [blocked.ap_id], + "object" => blocked.ap_id + } + end + + def make_unblock_data(blocker, blocked, block_activity) do + %{ + "type" => "Undo", + "actor" => blocker.ap_id, + "to" => [blocked.ap_id], + "object" => block_activity.data + } + end #### Create-related helpers def make_create_data(params, additional) do