Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel
[akkoma] / test / web / streamer / streamer_test.exs
index b8fcd41fa31abb73588162f504c2d54125fbad61..5a5b3514793f423b97b93f6e07eece6a4fbf9612 100644 (file)
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.StreamerTest do
 
   import Pleroma.Factory
 
+  alias Pleroma.Conversation.Participation
   alias Pleroma.List
   alias Pleroma.User
   alias Pleroma.Web.CommonAPI
@@ -14,7 +15,7 @@ defmodule Pleroma.Web.StreamerTest do
   alias Pleroma.Web.Streamer.StreamerSocket
   alias Pleroma.Web.Streamer.Worker
 
-  @moduletag needs_streamer: true
+  @moduletag needs_streamer: true, capture_log: true
   clear_config_all([:instance, :skip_thread_containment])
 
   describe "user streams" do
@@ -68,7 +69,7 @@ defmodule Pleroma.Web.StreamerTest do
       )
 
       {:ok, activity} = CommonAPI.post(user, %{"status" => ":("})
-      {:ok, notif, _} = CommonAPI.favorite(activity.id, blocked)
+      {:ok, notif} = CommonAPI.favorite(blocked, activity.id)
 
       Streamer.stream("user:notification", notif)
       Task.await(task)
@@ -87,7 +88,7 @@ defmodule Pleroma.Web.StreamerTest do
 
       {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
       {:ok, activity} = CommonAPI.add_mute(user, activity)
-      {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
+      {:ok, notif} = CommonAPI.favorite(user2, activity.id)
       Streamer.stream("user:notification", notif)
       Task.await(task)
     end
@@ -105,11 +106,29 @@ defmodule Pleroma.Web.StreamerTest do
 
       {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
       {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
-      {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
+      {:ok, notif} = CommonAPI.favorite(user2, activity.id)
 
       Streamer.stream("user:notification", notif)
       Task.await(task)
     end
+
+    test "it sends follow activities to the 'user:notification' stream", %{
+      user: user
+    } do
+      user2 = insert(:user)
+      task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
+
+      Streamer.add_socket(
+        "user:notification",
+        %{transport_pid: task.pid, assigns: %{user: user}}
+      )
+
+      {:ok, _follower, _followed, _activity} = CommonAPI.follow(user2, user)
+
+      # We don't directly pipe the notification to the streamer as it's already
+      # generated as a side effect of CommonAPI.follow().
+      Task.await(task)
+    end
   end
 
   test "it sends to public" do
@@ -169,7 +188,8 @@ defmodule Pleroma.Web.StreamerTest do
     test "it doesn't send to user if recipients invalid and thread containment is enabled" do
       Pleroma.Config.put([:instance, :skip_thread_containment], false)
       author = insert(:user)
-      user = insert(:user, following: [author.ap_id])
+      user = insert(:user)
+      User.follow(user, author, "accept")
 
       activity =
         insert(:note_activity,
@@ -191,7 +211,8 @@ defmodule Pleroma.Web.StreamerTest do
     test "it sends message if recipients invalid and thread containment is disabled" do
       Pleroma.Config.put([:instance, :skip_thread_containment], true)
       author = insert(:user)
-      user = insert(:user, following: [author.ap_id])
+      user = insert(:user)
+      User.follow(user, author, "accept")
 
       activity =
         insert(:note_activity,
@@ -213,7 +234,8 @@ defmodule Pleroma.Web.StreamerTest do
     test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
       Pleroma.Config.put([:instance, :skip_thread_containment], false)
       author = insert(:user)
-      user = insert(:user, following: [author.ap_id], info: %{skip_thread_containment: true})
+      user = insert(:user, skip_thread_containment: true)
+      User.follow(user, author, "accept")
 
       activity =
         insert(:note_activity,
@@ -233,30 +255,68 @@ defmodule Pleroma.Web.StreamerTest do
     end
   end
 
-  test "it doesn't send to blocked users" do
-    user = insert(:user)
-    blocked_user = insert(:user)
-    {:ok, user} = User.block(user, blocked_user)
+  describe "blocks" do
+    test "it doesn't send messages involving blocked users" do
+      user = insert(:user)
+      blocked_user = insert(:user)
+      {:ok, user} = User.block(user, blocked_user)
 
-    task =
-      Task.async(fn ->
-        refute_receive {:text, _}, 1_000
-      end)
+      task =
+        Task.async(fn ->
+          refute_receive {:text, _}, 1_000
+        end)
 
-    fake_socket = %StreamerSocket{
-      transport_pid: task.pid,
-      user: user
-    }
+      fake_socket = %StreamerSocket{
+        transport_pid: task.pid,
+        user: user
+      }
 
-    {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
+      {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
 
-    topics = %{
-      "public" => [fake_socket]
-    }
+      topics = %{
+        "public" => [fake_socket]
+      }
 
-    Worker.push_to_socket(topics, "public", activity)
+      Worker.push_to_socket(topics, "public", activity)
 
-    Task.await(task)
+      Task.await(task)
+    end
+
+    test "it doesn't send messages transitively involving blocked users" do
+      blocker = insert(:user)
+      blockee = insert(:user)
+      friend = insert(:user)
+
+      task =
+        Task.async(fn ->
+          refute_receive {:text, _}, 1_000
+        end)
+
+      fake_socket = %StreamerSocket{
+        transport_pid: task.pid,
+        user: blocker
+      }
+
+      topics = %{
+        "public" => [fake_socket]
+      }
+
+      {:ok, blocker} = User.block(blocker, blockee)
+
+      {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
+
+      Worker.push_to_socket(topics, "public", activity_one)
+
+      {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
+
+      Worker.push_to_socket(topics, "public", activity_two)
+
+      {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
+
+      Worker.push_to_socket(topics, "public", activity_three)
+
+      Task.await(task)
+    end
   end
 
   test "it doesn't send unwanted DMs to list" do
@@ -422,7 +482,14 @@ defmodule Pleroma.Web.StreamerTest do
 
       task =
         Task.async(fn ->
-          assert_receive {:text, _received_event}, 4_000
+          assert_receive {:text, received_event}, 4_000
+
+          assert %{"event" => "conversation", "payload" => received_payload} =
+                   Jason.decode!(received_event)
+
+          assert %{"last_status" => last_status} = Jason.decode!(received_payload)
+          [participation] = Participation.for_user(user)
+          assert last_status["pleroma"]["direct_conversation_id"] == participation.id
         end)
 
       Streamer.add_socket(
@@ -439,7 +506,7 @@ defmodule Pleroma.Web.StreamerTest do
       Task.await(task)
     end
 
-    test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do
+    test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
       user = insert(:user)
       another_user = insert(:user)