1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.StreamerTest do
10 alias Pleroma.Conversation.Participation
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Streamer
15 alias Pleroma.Web.Streamer.StreamerSocket
16 alias Pleroma.Web.Streamer.Worker
18 @moduletag needs_streamer: true, capture_log: true
21 @streamer_start_wait 10
23 clear_config([:instance, :skip_thread_containment])
25 describe "get_topic without an user" do
26 test "allows public" do
27 assert {:ok, "public"} = Streamer.get_topic("public", nil)
28 assert {:ok, "public:local"} = Streamer.get_topic("public:local", nil)
29 assert {:ok, "public:media"} = Streamer.get_topic("public:media", nil)
30 assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", nil)
33 test "allows hashtag streams" do
34 assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", nil, %{"tag" => "cofe"})
37 test "disallows user streams" do
38 assert {:error, _} = Streamer.get_topic("user", nil)
39 assert {:error, _} = Streamer.get_topic("user:notification", nil)
40 assert {:error, _} = Streamer.get_topic("direct", nil)
43 test "disallows list streams" do
44 assert {:error, _} = Streamer.get_topic("list", nil, %{"list" => 42})
48 describe "get_topic with an user" do
54 test "allows public streams", %{user: user} do
55 assert {:ok, "public"} = Streamer.get_topic("public", user)
56 assert {:ok, "public:local"} = Streamer.get_topic("public:local", user)
57 assert {:ok, "public:media"} = Streamer.get_topic("public:media", user)
58 assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", user)
61 test "allows user streams", %{user: user} do
62 expected_user_topic = "user:#{user.id}"
63 expected_notif_topic = "user:notification:#{user.id}"
64 expected_direct_topic = "direct:#{user.id}"
65 assert {:ok, ^expected_user_topic} = Streamer.get_topic("user", user)
66 assert {:ok, ^expected_notif_topic} = Streamer.get_topic("user:notification", user)
67 assert {:ok, ^expected_direct_topic} = Streamer.get_topic("direct", user)
70 test "allows hashtag streams", %{user: user} do
71 assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", user, %{"tag" => "cofe"})
74 test "disallows registering to an user stream", %{user: user} do
75 another_user = insert(:user)
76 assert {:error, _} = Streamer.get_topic("user:#{another_user.id}", user)
77 assert {:error, _} = Streamer.get_topic("user:notification:#{another_user.id}", user)
78 assert {:error, _} = Streamer.get_topic("direct:#{another_user.id}", user)
81 test "allows list stream that are owned by the user", %{user: user} do
82 {:ok, list} = List.create("Test", user)
83 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user)
84 assert {:ok, _} = Streamer.get_topic("list", user, %{"list" => list.id})
87 test "disallows list stream that are not owned by the user", %{user: user} do
88 another_user = insert(:user)
89 {:ok, list} = List.create("Test", another_user)
90 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user)
91 assert {:error, _} = Streamer.get_topic("list", user, %{"list" => list.id})
95 describe "user streams" do
98 notify = insert(:notification, user: user, activity: build(:note_activity))
99 {:ok, %{user: user, notify: notify}}
102 test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
105 assert_receive {:text, _}, @streamer_timeout
108 Streamer.get_topic_and_add_socket(
110 %{transport_pid: task.pid, assigns: %{user: user}}
113 Streamer.stream("user", notify)
117 test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
120 assert_receive {:text, _}, @streamer_timeout
123 Streamer.get_topic_and_add_socket(
125 %{transport_pid: task.pid, assigns: %{user: user}}
128 Streamer.stream("user:notification", notify)
132 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
135 blocked = insert(:user)
136 {:ok, _user_relationship} = User.block(user, blocked)
138 {:ok, activity} = CommonAPI.post(user, %{"status" => ":("})
139 {:ok, notif, _} = CommonAPI.favorite(activity.id, blocked)
141 task = Task.async(fn -> refute_receive {:text, _}, @streamer_timeout end)
143 Streamer.get_topic_and_add_socket(
145 %{transport_pid: task.pid, assigns: %{user: user}}
148 Streamer.stream("user:notification", notif)
152 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
155 user2 = insert(:user)
157 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
158 {:ok, activity} = CommonAPI.add_mute(user, activity)
159 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
161 task = Task.async(fn -> refute_receive {:text, _}, @streamer_timeout end)
163 Streamer.get_topic_and_add_socket(
165 %{transport_pid: task.pid, assigns: %{user: user}}
168 Streamer.stream("user:notification", notif)
172 test "it doesn't send notify to the 'user:notification' stream' when a domain is blocked", %{
175 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
177 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
178 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
179 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
181 task = Task.async(fn -> refute_receive {:text, _}, @streamer_timeout end)
183 Streamer.get_topic_and_add_socket(
185 %{transport_pid: task.pid, assigns: %{user: user}}
188 Streamer.stream("user:notification", notif)
192 test "it sends follow activities to the 'user:notification' stream", %{
195 user2 = insert(:user)
196 task = Task.async(fn -> assert_receive {:text, _}, @streamer_timeout end)
198 Process.sleep(@streamer_start_wait)
200 Streamer.get_topic_and_add_socket(
202 %{transport_pid: task.pid, assigns: %{user: user}}
205 {:ok, _follower, _followed, _activity} = CommonAPI.follow(user2, user)
207 # We don't directly pipe the notification to the streamer as it's already
208 # generated as a side effect of CommonAPI.follow().
213 test "it sends to public" do
215 other_user = insert(:user)
219 assert_receive {:text, _}, @streamer_timeout
222 fake_socket = %StreamerSocket{
223 transport_pid: task.pid,
227 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
230 "public" => [fake_socket]
233 Worker.push_to_socket(topics, "public", activity)
242 "payload" => activity.id
246 assert_receive {:text, received_event}, @streamer_timeout
247 assert received_event == expected_event
250 fake_socket = %StreamerSocket{
251 transport_pid: task.pid,
255 {:ok, activity} = CommonAPI.delete(activity.id, other_user)
258 "public" => [fake_socket]
261 Worker.push_to_socket(topics, "public", activity)
266 describe "thread_containment" do
267 test "it doesn't send to user if recipients invalid and thread containment is enabled" do
268 Pleroma.Config.put([:instance, :skip_thread_containment], false)
269 author = insert(:user)
271 User.follow(user, author, :follow_accept)
274 insert(:note_activity,
278 data: %{"to" => ["TEST-FFF"]}
282 task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
283 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
284 topics = %{"public" => [fake_socket]}
285 Worker.push_to_socket(topics, "public", activity)
290 test "it sends message if recipients invalid and thread containment is disabled" do
291 Pleroma.Config.put([:instance, :skip_thread_containment], true)
292 author = insert(:user)
294 User.follow(user, author, :follow_accept)
297 insert(:note_activity,
301 data: %{"to" => ["TEST-FFF"]}
305 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
306 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
307 topics = %{"public" => [fake_socket]}
308 Worker.push_to_socket(topics, "public", activity)
313 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
314 Pleroma.Config.put([:instance, :skip_thread_containment], false)
315 author = insert(:user)
316 user = insert(:user, skip_thread_containment: true)
317 User.follow(user, author, :follow_accept)
320 insert(:note_activity,
324 data: %{"to" => ["TEST-FFF"]}
328 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
329 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
330 topics = %{"public" => [fake_socket]}
331 Worker.push_to_socket(topics, "public", activity)
338 test "it doesn't send messages involving blocked users" do
340 blocked_user = insert(:user)
341 {:ok, _user_relationship} = User.block(user, blocked_user)
343 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
347 refute_receive {:text, _}, 1_000
350 fake_socket = %StreamerSocket{
351 transport_pid: task.pid,
356 "public" => [fake_socket]
359 Worker.push_to_socket(topics, "public", activity)
364 test "it doesn't send messages transitively involving blocked users" do
365 blocker = insert(:user)
366 blockee = insert(:user)
367 friend = insert(:user)
371 refute_receive {:text, _}, 1_000
374 fake_socket = %StreamerSocket{
375 transport_pid: task.pid,
380 "public" => [fake_socket]
383 {:ok, _user_relationship} = User.block(blocker, blockee)
385 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
387 Worker.push_to_socket(topics, "public", activity_one)
389 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
391 Worker.push_to_socket(topics, "public", activity_two)
393 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
395 Worker.push_to_socket(topics, "public", activity_three)
401 test "it doesn't send unwanted DMs to list" do
402 user_a = insert(:user)
403 user_b = insert(:user)
404 user_c = insert(:user)
406 {:ok, user_a} = User.follow(user_a, user_b)
408 {:ok, list} = List.create("Test", user_a)
409 {:ok, list} = List.follow(list, user_b)
412 CommonAPI.post(user_b, %{
413 "status" => "@#{user_c.nickname} Test",
414 "visibility" => "direct"
419 refute_receive {:text, _}, 1_000
422 fake_socket = %StreamerSocket{
423 transport_pid: task.pid,
428 "list:#{list.id}" => [fake_socket]
431 Worker.handle_call({:stream, "list", activity}, self(), topics)
436 test "it doesn't send unwanted private posts to list" do
437 user_a = insert(:user)
438 user_b = insert(:user)
440 {:ok, list} = List.create("Test", user_a)
441 {:ok, list} = List.follow(list, user_b)
444 CommonAPI.post(user_b, %{
446 "visibility" => "private"
451 refute_receive {:text, _}, 1_000
454 fake_socket = %StreamerSocket{
455 transport_pid: task.pid,
460 "list:#{list.id}" => [fake_socket]
463 Worker.handle_call({:stream, "list", activity}, self(), topics)
468 test "it sends wanted private posts to list" do
469 user_a = insert(:user)
470 user_b = insert(:user)
472 {:ok, user_a} = User.follow(user_a, user_b)
474 {:ok, list} = List.create("Test", user_a)
475 {:ok, list} = List.follow(list, user_b)
478 CommonAPI.post(user_b, %{
480 "visibility" => "private"
485 assert_receive {:text, _}, 1_000
488 Streamer.get_topic_and_add_socket(
490 %{transport_pid: task.pid, assigns: %{user: user_a}},
494 Worker.handle_call({:stream, "list", activity}, self(), %{})
499 test "it doesn't send muted reblogs" do
500 user1 = insert(:user)
501 user2 = insert(:user)
502 user3 = insert(:user)
503 CommonAPI.hide_reblogs(user1, user2)
505 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
506 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
510 refute_receive {:text, _}, 1_000
513 fake_socket = %StreamerSocket{
514 transport_pid: task.pid,
519 "public" => [fake_socket]
522 Worker.push_to_socket(topics, "public", announce_activity)
527 test "it does send non-reblog notification for reblog-muted actors" do
528 user1 = insert(:user)
529 user2 = insert(:user)
530 user3 = insert(:user)
531 CommonAPI.hide_reblogs(user1, user2)
533 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
534 {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, user2)
538 assert_receive {:text, _}, 1_000
541 fake_socket = %StreamerSocket{
542 transport_pid: task.pid,
547 "public" => [fake_socket]
550 Worker.push_to_socket(topics, "public", favorite_activity)
555 test "it doesn't send posts from muted threads" do
557 user2 = insert(:user)
558 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
560 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
562 {:ok, activity} = CommonAPI.add_mute(user2, activity)
564 task = Task.async(fn -> refute_receive {:text, _}, @streamer_timeout end)
566 Streamer.get_topic_and_add_socket(
568 %{transport_pid: task.pid, assigns: %{user: user2}}
571 Streamer.stream("user", activity)
575 describe "direct streams" do
580 test "it sends conversation update to the 'direct' stream", %{} do
582 another_user = insert(:user)
586 assert_receive {:text, received_event}, @streamer_timeout
588 assert %{"event" => "conversation", "payload" => received_payload} =
589 Jason.decode!(received_event)
591 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
592 [participation] = Participation.for_user(user)
593 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
596 Streamer.get_topic_and_add_socket(
598 %{transport_pid: task.pid, assigns: %{user: user}}
601 {:ok, _create_activity} =
602 CommonAPI.post(another_user, %{
603 "status" => "hey @#{user.nickname}",
604 "visibility" => "direct"
610 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
612 another_user = insert(:user)
614 {:ok, create_activity} =
615 CommonAPI.post(another_user, %{
616 "status" => "hi @#{user.nickname}",
617 "visibility" => "direct"
622 assert_receive {:text, received_event}, @streamer_timeout
623 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
625 refute_receive {:text, _}, @streamer_timeout
628 Process.sleep(@streamer_start_wait)
630 Streamer.get_topic_and_add_socket(
632 %{transport_pid: task.pid, assigns: %{user: user}}
635 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
640 test "it sends conversation update to the 'direct' stream when a message is deleted" do
642 another_user = insert(:user)
644 {:ok, create_activity} =
645 CommonAPI.post(another_user, %{
646 "status" => "hi @#{user.nickname}",
647 "visibility" => "direct"
650 {:ok, create_activity2} =
651 CommonAPI.post(another_user, %{
652 "status" => "hi @#{user.nickname}",
653 "in_reply_to_status_id" => create_activity.id,
654 "visibility" => "direct"
659 assert_receive {:text, received_event}, @streamer_timeout
660 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
662 assert_receive {:text, received_event}, @streamer_timeout
664 assert %{"event" => "conversation", "payload" => received_payload} =
665 Jason.decode!(received_event)
667 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
668 assert last_status["id"] == to_string(create_activity.id)
671 Process.sleep(@streamer_start_wait)
673 Streamer.get_topic_and_add_socket(
675 %{transport_pid: task.pid, assigns: %{user: user}}
678 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)