commit: 5459bbc1efabc373e5619990be8d811df072e5e6
parent 7a58ddfa486048d17fa653662914e0f02d11fadb
Author: tusooa <tusooa@kazv.moe>
Date: Mon, 6 Nov 2023 19:59:05 -0500
Allow group actors to boost posts
Diffstat:
4 files changed, 69 insertions(+), 0 deletions(-)
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -319,6 +319,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
{:ok, _actor} <- update_last_status_at_if_public(actor, activity),
_ <- notify_and_stream(activity),
:ok <- maybe_schedule_poll_notifications(activity),
+ :ok <- maybe_handle_group_posts(activity),
:ok <- maybe_federate(activity) do
{:ok, activity}
else
diff --git a/lib/pleroma/web/activity_pub/side_effects.ex b/lib/pleroma/web/activity_pub/side_effects.ex
@@ -233,6 +233,8 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
Pleroma.Search.add_to_index(Map.put(activity, :object, object))
+ Utils.maybe_handle_group_posts(activity)
+
meta =
meta
|> add_notifications(notifications)
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
@@ -935,4 +935,21 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|> where([a, object: o], fragment("(?)->>'type' = 'Answer'", o.data))
|> Repo.all()
end
+
+ def maybe_handle_group_posts(activity) do
+ mentions =
+ activity.data["to"]
+ |> Enum.filter(&(&1 != activity.actor))
+
+ mentioned_local_groups =
+ User.get_all_by_ap_id(mentions)
+ |> Enum.filter(&(&1.actor_type == "Group" and &1.local))
+
+ mentioned_local_groups
+ |> Enum.each(fn group ->
+ Pleroma.Web.CommonAPI.repeat(activity.id, group)
+ end)
+
+ :ok
+ end
end
diff --git a/test/pleroma/web/common_api_test.exs b/test/pleroma/web/common_api_test.exs
@@ -26,8 +26,15 @@ defmodule Pleroma.Web.CommonAPITest do
import Mox
import Pleroma.Factory
+ require Pleroma.Activity.Queries
require Pleroma.Constants
+ defp get_announces_of_object(%{data: %{"id" => id}} = _object) do
+ Pleroma.Activity.Queries.by_type("Announce")
+ |> Pleroma.Activity.Queries.by_object_id(id)
+ |> Pleroma.Repo.all()
+ end
+
setup_all do
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
:ok
@@ -1835,4 +1842,46 @@ defmodule Pleroma.Web.CommonAPITest do
assert Map.has_key?(updated_object.data, "updated")
end
end
+
+ describe "Group actors" do
+ setup do
+ poster = insert(:user)
+ group = insert(:user, actor_type: "Group")
+ other_group = insert(:user, actor_type: "Group")
+ %{poster: poster, group: group, other_group: other_group}
+ end
+
+ test "it boosts public posts", %{poster: poster, group: group} do
+ {:ok, post} = CommonAPI.post(poster, %{status: "hey @#{group.nickname}"})
+
+ announces = get_announces_of_object(post.object)
+ assert [_] = announces
+ end
+
+ test "it does not boost private posts", %{poster: poster, group: group} do
+ {:ok, private_post} =
+ CommonAPI.post(poster, %{status: "hey @#{group.nickname}", visibility: "private"})
+
+ assert [] = get_announces_of_object(private_post.object)
+ end
+
+ test "remote groups do not boost any posts", %{poster: poster} do
+ remote_group =
+ insert(:user, actor_type: "Group", local: false, nickname: "remote@example.com")
+
+ {:ok, post} = CommonAPI.post(poster, %{status: "hey @#{User.full_nickname(remote_group)}"})
+ assert remote_group.ap_id in post.data["to"]
+
+ announces = get_announces_of_object(post.object)
+ assert [] = announces
+ end
+
+ test "multiple groups mentioned", %{poster: poster, group: group, other_group: other_group} do
+ {:ok, post} =
+ CommonAPI.post(poster, %{status: "hey @#{group.nickname} @#{other_group.nickname}"})
+
+ announces = get_announces_of_object(post.object)
+ assert [_, _] = announces
+ end
+ end
end