From 591c82620eeb74a012d4614b9f16db74a7fd84f9 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 18 Jun 2018 04:33:41 +0000 Subject: [PATCH 1/3] activitypub: filter destination list for announce activities differently than normal (closes #164) --- lib/pleroma/user.ex | 28 +++++++++++++++----- lib/pleroma/web/activity_pub/activity_pub.ex | 18 +++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index b27397e13..856f27e10 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -449,13 +449,29 @@ def update_follower_count(%User{} = user) do update_and_set_cache(cs) end + def get_notified_from_activity_query(to) do + from( + u in User, + where: u.ap_id in ^to, + where: u.local == true + ) + end + + def get_notified_from_activity(%Activity{recipients: to, data: %{"type" => "Announce"} = data}) do + object = Object.get_by_ap_id(data["object"]) + + # ensure that the actor who published the announced object appears only once + to = + (to ++ [object.data["actor"]]) + |> Enum.uniq() + + query = get_notified_from_activity_query(to) + + Repo.all(query) + end + def get_notified_from_activity(%Activity{recipients: to}) do - query = - from( - u in User, - where: u.ap_id in ^to, - where: u.local == true - ) + query = get_notified_from_activity_query(to) Repo.all(query) end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 267427a23..a4b49e73c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -12,6 +12,24 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do @instance Application.get_env(:pleroma, :instance) + # For Announce activities, we filter the recipients based on following status for any actors + # that match actual users. See issue #164 for more information about why this is necessary. + def get_recipients(%{"type" => "Announce"} = data) do + recipients = (data["to"] || []) ++ (data["cc"] || []) + actor = User.get_cached_by_ap_id(data["actor"]) + + recipients + |> Enum.filter(fn recipient -> + case User.get_cached_by_ap_id(recipient) do + nil -> + true + + user -> + User.following?(user, actor) + end + end) + end + def get_recipients(data) do (data["to"] || []) ++ (data["cc"] || []) end From 6f384d35ddc4682e3b845b0224ca620fbdd93785 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 18 Jun 2018 05:11:05 +0000 Subject: [PATCH 2/3] ostatus: we need to include the original author of a message as a mention this is due to the use of activity.recipients which may not necessarily contain the original author if the actors are not following each other. --- lib/pleroma/web/ostatus/activity_representer.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index 4179d86c9..4c4a0c233 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -184,7 +184,10 @@ def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_autho retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true) - mentions = activity.recipients |> get_mentions + mentions = + ([retweeted_user.ap_id] ++ activity.recipients) + |> Enum.uniq() + |> get_mentions() [ {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']}, From 7e0f62acee35a9e2738caab97deffdaae6a822c4 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 18 Jun 2018 05:23:54 +0000 Subject: [PATCH 3/3] object: add helper functions to handle various forms of a given object and return a normalized one --- lib/pleroma/object.ex | 4 ++++ lib/pleroma/user.ex | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index ff2af4a6f..1bcff5a7b 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -27,6 +27,10 @@ def get_by_ap_id(ap_id) do Repo.one(from(object in Object, where: fragment("(?)->>'id' = ?", object.data, ^ap_id))) end + def normalize(obj) when is_map(obj), do: Object.get_by_ap_id(obj["id"]) + def normalize(ap_id) when is_binary(ap_id), do: Object.get_by_ap_id(ap_id) + def normalize(_), do: nil + def get_cached_by_ap_id(ap_id) do if Mix.env() == :test do get_by_ap_id(ap_id) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 856f27e10..d68aef52a 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -458,7 +458,7 @@ def get_notified_from_activity_query(to) do end def get_notified_from_activity(%Activity{recipients: to, data: %{"type" => "Announce"} = data}) do - object = Object.get_by_ap_id(data["object"]) + object = Object.normalize(data["object"]) # ensure that the actor who published the announced object appears only once to =