From 414d4b80883a37b0d323522112680d11b49cbb28 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 14 Mar 2019 19:04:33 +0000 Subject: [PATCH 1/8] test: add a test for accepting inbound Flag activities --- test/web/activity_pub/transmogrifier_test.exs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 8184dbbae..afb931934 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -5,6 +5,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do use Pleroma.DataCase alias Pleroma.Activity + alias Pleroma.Object alias Pleroma.Repo alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub @@ -764,6 +765,30 @@ test "it remaps video URLs as attachments if necessary" do assert object.data["attachment"] == [attachment] end + + test "it accepts Flag activities" do + user = insert(:user) + other_user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"}) + object = Object.normalize(activity.data["object"]) + + message = %{ + "@context" => "https://www.w3.org/ns/activitystreams", + "cc" => [user.ap_id], + "object" => [user.ap_id, object.data["id"]], + "type" => "Flag", + "content" => "blocked AND reported!!!", + "actor" => other_user.ap_id + } + + assert {:ok, activity} = Transmogrifier.handle_incoming(message) + + assert activity.data["object"] == [user.ap_id, object.data["id"]] + assert activity.data["content"] == "blocked AND reported!!!" + assert activity.data["actor"] == other_user.ap_id + assert activity.data["cc"] == [user.ap_id] + end end describe "prepare outgoing" do From f86f7dbb8f7bd72bd4037b03224b2840de4a4292 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 14 Mar 2019 19:04:52 +0000 Subject: [PATCH 2/8] activitypub: utils: rework make_flag_data to accept either activity payloads or IRIs --- lib/pleroma/web/activity_pub/utils.ex | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 182f9cacb..9881b7bbb 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -621,7 +621,12 @@ def make_create_data(params, additional) do #### Flag-related helpers def make_flag_data(params, additional) do - status_ap_ids = Enum.map(params.statuses || [], & &1.data["id"]) + status_ap_ids = + Enum.map(params.statuses || [], fn + act when is_map(act) -> act["id"] + act when is_binary(act) -> act + end) + object = [params.account.ap_id] ++ status_ap_ids %{ From 0f3ecb2bfbb42ce16eb3144d62a932bfc32ce656 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 14 Mar 2019 19:06:02 +0000 Subject: [PATCH 3/8] activitypub: transmogrifier: accept remote Flag activities --- .../web/activity_pub/transmogrifier.ex | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 1247e4b61..8e4bf7b47 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -355,6 +355,40 @@ defp get_follow_activity(follow_object, followed) do end end + # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them + # with nil ID. + def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data) do + with context <- data["context"] || Utils.generate_context_id(), + content <- data["content"] || "", + %User{} = actor <- User.get_cached_by_ap_id(actor), + + # Reduce the object list to find the reported user. + %User{} = account <- + Enum.reduce_while(objects, nil, fn ap_id, _ -> + with %User{} = user <- User.get_cached_by_ap_id(ap_id) do + {:halt, user} + else + _ -> {:cont, nil} + end + end), + + # Remove the reported user from the object list. + statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do + params = %{ + actor: actor, + context: context, + account: account, + statuses: statuses, + content: content, + additional: %{ + "cc" => [account.ap_id] + } + } + + ActivityPub.flag(params) + end + end + # disallow objects with bogus IDs def handle_incoming(%{"id" => nil}), do: :error def handle_incoming(%{"id" => ""}), do: :error From 379442ad17cab9c27863be2c885d10f8b6eadeca Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 14 Mar 2019 19:29:04 +0000 Subject: [PATCH 4/8] activitypub: utils: also match Activity objects --- lib/pleroma/web/activity_pub/utils.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 9881b7bbb..af317245f 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -623,6 +623,7 @@ def make_create_data(params, additional) do def make_flag_data(params, additional) do status_ap_ids = Enum.map(params.statuses || [], fn + %Activity{} = act -> act.data["id"] act when is_map(act) -> act["id"] act when is_binary(act) -> act end) From 3b48d5f0c27b42a6ea409fffbdc6b831da2ff4ca Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 14 Mar 2019 19:29:33 +0000 Subject: [PATCH 5/8] common api: add support for forwarding reports --- lib/pleroma/web/common_api/common_api.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index de0759fb0..ead4928b5 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -284,7 +284,8 @@ def report(user, data) do actor: user, account: account, statuses: statuses, - content: content_html + content: content_html, + forward: data["forward"] || false }) do Enum.each(User.all_superusers(), fn superuser -> superuser From 64b0120d678b106f33d5c903749fcac5ed5a1ed7 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 14 Mar 2019 19:29:47 +0000 Subject: [PATCH 6/8] activitypub: add support for forwarding reports --- lib/pleroma/web/activity_pub/activity_pub.ex | 22 +++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 70db419ca..7d21fe65f 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -370,20 +370,32 @@ def flag( content: content } = params ) do - additional = params[:additional] || %{} - # only accept false as false value local = !(params[:local] == false) + forward = !(params[:forward] == false) - %{ + additional = params[:additional] || %{} + + params = %{ actor: actor, context: context, account: account, statuses: statuses, content: content } - |> make_flag_data(additional) - |> insert(local) + + additional = + if forward do + Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]}) + else + additional + end + + with flag_data <- make_flag_data(params, additional), + {:ok, activity} <- insert(flag_data, local), + :ok <- maybe_federate(activity) do + {:ok, activity} + end end def fetch_activities_for_context(context, opts \\ %{}) do From 5c7b774f09d86414e6a8ef6494ccd2e6a76c1396 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 14 Mar 2019 19:38:46 +0000 Subject: [PATCH 7/8] reports: unify sending e-mail for both remote and local reports --- lib/pleroma/web/activity_pub/activity_pub.ex | 6 ++++++ lib/pleroma/web/common_api/common_api.ex | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 7d21fe65f..32f69e0fa 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -394,6 +394,12 @@ def flag( with flag_data <- make_flag_data(params, additional), {:ok, activity} <- insert(flag_data, local), :ok <- maybe_federate(activity) do + Enum.each(User.all_superusers(), fn superuser -> + superuser + |> Pleroma.AdminEmail.report(actor, account, statuses, content) + |> Pleroma.Mailer.deliver_async() + end) + {:ok, activity} end end diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index ead4928b5..edbcd3397 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -287,12 +287,6 @@ def report(user, data) do content: content_html, forward: data["forward"] || false }) do - Enum.each(User.all_superusers(), fn superuser -> - superuser - |> Pleroma.AdminEmail.report(user, account, statuses, content_html) - |> Pleroma.Mailer.deliver_async() - end) - {:ok, activity} else {:error, err} -> {:error, err} From 423fd07928d64dd7810ac408265a37ae1274956f Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 14 Mar 2019 19:52:08 +0000 Subject: [PATCH 8/8] activitypub: inject to/cc fields on non-forwarded reports since Flag activities are now Forwardable --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 32f69e0fa..45a030ca3 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -388,7 +388,7 @@ def flag( if forward do Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]}) else - additional + Map.merge(additional, %{"to" => [], "cc" => []}) end with flag_data <- make_flag_data(params, additional),