SideEffects: Builed out Announce effects.
authorlain <lain@soykaf.club>
Thu, 21 May 2020 10:43:09 +0000 (12:43 +0200)
committerlain <lain@soykaf.club>
Thu, 21 May 2020 10:43:09 +0000 (12:43 +0200)
lib/pleroma/web/activity_pub/side_effects.ex
test/web/activity_pub/side_effects_test.exs

index bc0d31c456770a180e7db9ed0cd5109a400f92eb..66aa1f628f4e985b1fa4d4a3096859815c11b4ea 100644 (file)
@@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
   alias Pleroma.Repo
   alias Pleroma.User
   alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.ActivityPub.Visibility
   alias Pleroma.Web.ActivityPub.Utils
 
   def handle(object, meta \\ [])
@@ -30,11 +31,16 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
   # Tasks this handles:
   # - Add announce to object
   # - Set up notification
+  # - Stream out the announce
   def handle(%{data: %{"type" => "Announce"}} = object, meta) do
     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)
+    ActivityPub.stream_out(object)
 
     {:ok, object, meta}
   end
index 5dede3957f73361c74e31e8179e4ebfd613cfc14..db8bf2b05c9d4ac323dc93f82d3002db6ea85c6d 100644 (file)
@@ -172,7 +172,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
       {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
       {:ok, like} = CommonAPI.favorite(user, post.id)
       {: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)
       User.block(user, poster)
 
@@ -295,23 +295,63 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
       poster = insert(:user)
       user = insert(:user)
       {: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, 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
 
-    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)
       object = Object.get_by_ap_id(announce.data["object"])
       assert object.data["announcement_count"] == 1
       assert user.ap_id in object.data["announcements"]
     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
       {:ok, announce, _} = SideEffects.handle(announce)
       assert Repo.get_by(Notification, user_id: poster.id, activity_id: announce.id)
     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