SideEffects: Builed out Announce effects.

This commit is contained in:
lain 2020-05-21 12:43:09 +02:00
parent 39031f4860
commit d9d425708e
2 changed files with 51 additions and 5 deletions

View file

@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Visibility
alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Utils
def handle(object, meta \\ []) def handle(object, meta \\ [])
@ -30,11 +31,16 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
# Tasks this handles: # Tasks this handles:
# - Add announce to object # - Add announce to object
# - Set up notification # - Set up notification
# - Stream out the announce
def handle(%{data: %{"type" => "Announce"}} = object, meta) do def handle(%{data: %{"type" => "Announce"}} = object, meta) do
announced_object = Object.get_by_ap_id(object.data["object"]) announced_object = Object.get_by_ap_id(object.data["object"])
Utils.add_announce_to_object(object, announced_object)
if Visibility.is_public?(object) do
Utils.add_announce_to_object(object, announced_object)
end
Notification.create_notifications(object) Notification.create_notifications(object)
ActivityPub.stream_out(object)
{:ok, object, meta} {:ok, object, meta}
end end

View file

@ -172,7 +172,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
{:ok, post} = CommonAPI.post(poster, %{status: "hey"}) {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
{:ok, like} = CommonAPI.favorite(user, post.id) {:ok, like} = CommonAPI.favorite(user, post.id)
{:ok, reaction} = CommonAPI.react_with_emoji(post.id, user, "👍") {:ok, reaction} = CommonAPI.react_with_emoji(post.id, user, "👍")
{:ok, announce, _} = CommonAPI.repeat(post.id, user) {:ok, announce} = CommonAPI.repeat(post.id, user)
{:ok, block} = ActivityPub.block(user, poster) {:ok, block} = ActivityPub.block(user, poster)
User.block(user, poster) User.block(user, poster)
@ -295,23 +295,63 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
poster = insert(:user) poster = insert(:user)
user = insert(:user) user = insert(:user)
{:ok, post} = CommonAPI.post(poster, %{status: "hey"}) {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
{:ok, private_post} = CommonAPI.post(poster, %{status: "hey", visibility: "private"})
{:ok, announce_data, _meta} = Builder.announce(user, post.object, public: true)
{:ok, private_announce_data, _meta} =
Builder.announce(user, private_post.object, public: false)
{:ok, relay_announce_data, _meta} =
Builder.announce(Pleroma.Web.ActivityPub.Relay.get_actor(), post.object, public: true)
{:ok, announce_data, _meta} = Builder.announce(user, post.object)
{:ok, announce, _meta} = ActivityPub.persist(announce_data, local: true) {:ok, announce, _meta} = ActivityPub.persist(announce_data, local: true)
{:ok, private_announce, _meta} = ActivityPub.persist(private_announce_data, local: true)
{:ok, relay_announce, _meta} = ActivityPub.persist(relay_announce_data, local: true)
%{announce: announce, user: user, poster: poster} %{
announce: announce,
user: user,
poster: poster,
private_announce: private_announce,
relay_announce: relay_announce
}
end end
test "add the announce to the original object", %{announce: announce, user: user} do test "adds the announce to the original object", %{announce: announce, user: user} do
{:ok, announce, _} = SideEffects.handle(announce) {:ok, announce, _} = SideEffects.handle(announce)
object = Object.get_by_ap_id(announce.data["object"]) object = Object.get_by_ap_id(announce.data["object"])
assert object.data["announcement_count"] == 1 assert object.data["announcement_count"] == 1
assert user.ap_id in object.data["announcements"] assert user.ap_id in object.data["announcements"]
end end
test "does not add the announce to the original object if the announce is private", %{
private_announce: announce
} do
{:ok, announce, _} = SideEffects.handle(announce)
object = Object.get_by_ap_id(announce.data["object"])
assert object.data["announcement_count"] == nil
end
test "does not add the announce to the original object if the actor is a service actor", %{
relay_announce: announce
} do
{:ok, announce, _} = SideEffects.handle(announce)
object = Object.get_by_ap_id(announce.data["object"])
assert object.data["announcement_count"] == nil
end
test "creates a notification", %{announce: announce, poster: poster} do test "creates a notification", %{announce: announce, poster: poster} do
{:ok, announce, _} = SideEffects.handle(announce) {:ok, announce, _} = SideEffects.handle(announce)
assert Repo.get_by(Notification, user_id: poster.id, activity_id: announce.id) assert Repo.get_by(Notification, user_id: poster.id, activity_id: announce.id)
end end
test "it streams out the announce", %{announce: announce} do
with_mock Pleroma.Web.ActivityPub.ActivityPub, [:passthrough], stream_out: fn _ -> nil end do
{:ok, announce, _} = SideEffects.handle(announce)
assert called(Pleroma.Web.ActivityPub.ActivityPub.stream_out(announce))
end
end
end end
end end