1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
19 clear_config_all([:instance, :skip_thread_containment])
21 describe "user streams" do
24 notify = insert(:notification, user: user, activity: build(:note_activity))
25 {:ok, %{user: user, notify: notify}}
28 test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
31 assert_receive {:text, _}, 4_000
36 %{transport_pid: task.pid, assigns: %{user: user}}
39 Streamer.stream("user", notify)
43 test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
46 assert_receive {:text, _}, 4_000
51 %{transport_pid: task.pid, assigns: %{user: user}}
54 Streamer.stream("user:notification", notify)
58 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
61 blocked = insert(:user)
62 {:ok, user} = User.block(user, blocked)
64 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
68 %{transport_pid: task.pid, assigns: %{user: user}}
71 {:ok, activity} = CommonAPI.post(user, %{"status" => ":("})
72 {:ok, notif, _} = CommonAPI.favorite(activity.id, blocked)
74 Streamer.stream("user:notification", notif)
78 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
82 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
86 %{transport_pid: task.pid, assigns: %{user: user}}
89 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
90 {:ok, activity} = CommonAPI.add_mute(user, activity)
91 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
92 Streamer.stream("user:notification", notif)
96 test "it doesn't send notify to the 'user:notification' stream' when a domain is blocked", %{
99 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
100 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
104 %{transport_pid: task.pid, assigns: %{user: user}}
107 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
108 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
109 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
111 Streamer.stream("user:notification", notif)
115 test "it sends follow activities to the 'user:notification' stream", %{
118 user2 = insert(:user)
119 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
123 %{transport_pid: task.pid, assigns: %{user: user}}
126 {:ok, _follower, _followed, _activity} = CommonAPI.follow(user2, user)
128 # We don't directly pipe the notification to the streamer as it's already
129 # generated as a side effect of CommonAPI.follow().
134 test "it sends to public" do
136 other_user = insert(:user)
140 assert_receive {:text, _}, 4_000
143 fake_socket = %StreamerSocket{
144 transport_pid: task.pid,
148 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
151 "public" => [fake_socket]
154 Worker.push_to_socket(topics, "public", activity)
163 "payload" => activity.id
167 assert_receive {:text, received_event}, 4_000
168 assert received_event == expected_event
171 fake_socket = %StreamerSocket{
172 transport_pid: task.pid,
176 {:ok, activity} = CommonAPI.delete(activity.id, other_user)
179 "public" => [fake_socket]
182 Worker.push_to_socket(topics, "public", activity)
187 describe "thread_containment" do
188 test "it doesn't send to user if recipients invalid and thread containment is enabled" do
189 Pleroma.Config.put([:instance, :skip_thread_containment], false)
190 author = insert(:user)
192 User.follow(user, author, "accept")
195 insert(:note_activity,
199 data: %{"to" => ["TEST-FFF"]}
203 task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
204 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
205 topics = %{"public" => [fake_socket]}
206 Worker.push_to_socket(topics, "public", activity)
211 test "it sends message if recipients invalid and thread containment is disabled" do
212 Pleroma.Config.put([:instance, :skip_thread_containment], true)
213 author = insert(:user)
215 User.follow(user, author, "accept")
218 insert(:note_activity,
222 data: %{"to" => ["TEST-FFF"]}
226 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
227 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
228 topics = %{"public" => [fake_socket]}
229 Worker.push_to_socket(topics, "public", activity)
234 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
235 Pleroma.Config.put([:instance, :skip_thread_containment], false)
236 author = insert(:user)
237 user = insert(:user, skip_thread_containment: true)
238 User.follow(user, author, "accept")
241 insert(:note_activity,
245 data: %{"to" => ["TEST-FFF"]}
249 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
250 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
251 topics = %{"public" => [fake_socket]}
252 Worker.push_to_socket(topics, "public", activity)
259 test "it doesn't send messages involving blocked users" do
261 blocked_user = insert(:user)
262 {:ok, user} = User.block(user, blocked_user)
266 refute_receive {:text, _}, 1_000
269 fake_socket = %StreamerSocket{
270 transport_pid: task.pid,
274 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
277 "public" => [fake_socket]
280 Worker.push_to_socket(topics, "public", activity)
285 test "it doesn't send messages transitively involving blocked users" do
286 blocker = insert(:user)
287 blockee = insert(:user)
288 friend = insert(:user)
292 refute_receive {:text, _}, 1_000
295 fake_socket = %StreamerSocket{
296 transport_pid: task.pid,
301 "public" => [fake_socket]
304 {:ok, blocker} = User.block(blocker, blockee)
306 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
308 Worker.push_to_socket(topics, "public", activity_one)
310 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
312 Worker.push_to_socket(topics, "public", activity_two)
314 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
316 Worker.push_to_socket(topics, "public", activity_three)
322 test "it doesn't send unwanted DMs to list" do
323 user_a = insert(:user)
324 user_b = insert(:user)
325 user_c = insert(:user)
327 {:ok, user_a} = User.follow(user_a, user_b)
329 {:ok, list} = List.create("Test", user_a)
330 {:ok, list} = List.follow(list, user_b)
334 refute_receive {:text, _}, 1_000
337 fake_socket = %StreamerSocket{
338 transport_pid: task.pid,
343 CommonAPI.post(user_b, %{
344 "status" => "@#{user_c.nickname} Test",
345 "visibility" => "direct"
349 "list:#{list.id}" => [fake_socket]
352 Worker.handle_call({:stream, "list", activity}, self(), topics)
357 test "it doesn't send unwanted private posts to list" do
358 user_a = insert(:user)
359 user_b = insert(:user)
361 {:ok, list} = List.create("Test", user_a)
362 {:ok, list} = List.follow(list, user_b)
366 refute_receive {:text, _}, 1_000
369 fake_socket = %StreamerSocket{
370 transport_pid: task.pid,
375 CommonAPI.post(user_b, %{
377 "visibility" => "private"
381 "list:#{list.id}" => [fake_socket]
384 Worker.handle_call({:stream, "list", activity}, self(), topics)
389 test "it sends wanted private posts to list" do
390 user_a = insert(:user)
391 user_b = insert(:user)
393 {:ok, user_a} = User.follow(user_a, user_b)
395 {:ok, list} = List.create("Test", user_a)
396 {:ok, list} = List.follow(list, user_b)
400 assert_receive {:text, _}, 1_000
403 fake_socket = %StreamerSocket{
404 transport_pid: task.pid,
409 CommonAPI.post(user_b, %{
411 "visibility" => "private"
419 Worker.handle_call({:stream, "list", activity}, self(), %{})
424 test "it doesn't send muted reblogs" do
425 user1 = insert(:user)
426 user2 = insert(:user)
427 user3 = insert(:user)
428 CommonAPI.hide_reblogs(user1, user2)
432 refute_receive {:text, _}, 1_000
435 fake_socket = %StreamerSocket{
436 transport_pid: task.pid,
440 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
441 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
444 "public" => [fake_socket]
447 Worker.push_to_socket(topics, "public", announce_activity)
452 test "it doesn't send posts from muted threads" do
454 user2 = insert(:user)
455 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
457 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
459 {:ok, activity} = CommonAPI.add_mute(user2, activity)
461 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
467 %{transport_pid: task.pid, assigns: %{user: user2}}
470 Streamer.stream("user", activity)
474 describe "direct streams" do
479 test "it sends conversation update to the 'direct' stream", %{} do
481 another_user = insert(:user)
485 assert_receive {:text, received_event}, 4_000
487 assert %{"event" => "conversation", "payload" => received_payload} =
488 Jason.decode!(received_event)
490 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
491 [participation] = Participation.for_user(user)
492 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
497 %{transport_pid: task.pid, assigns: %{user: user}}
500 {:ok, _create_activity} =
501 CommonAPI.post(another_user, %{
502 "status" => "hey @#{user.nickname}",
503 "visibility" => "direct"
509 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
511 another_user = insert(:user)
513 {:ok, create_activity} =
514 CommonAPI.post(another_user, %{
515 "status" => "hi @#{user.nickname}",
516 "visibility" => "direct"
521 assert_receive {:text, received_event}, 4_000
522 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
524 refute_receive {:text, _}, 4_000
531 %{transport_pid: task.pid, assigns: %{user: user}}
534 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
539 test "it sends conversation update to the 'direct' stream when a message is deleted" do
541 another_user = insert(:user)
543 {:ok, create_activity} =
544 CommonAPI.post(another_user, %{
545 "status" => "hi @#{user.nickname}",
546 "visibility" => "direct"
549 {:ok, create_activity2} =
550 CommonAPI.post(another_user, %{
551 "status" => "hi @#{user.nickname}",
552 "in_reply_to_status_id" => create_activity.id,
553 "visibility" => "direct"
558 assert_receive {:text, received_event}, 4_000
559 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
561 assert_receive {:text, received_event}, 4_000
563 assert %{"event" => "conversation", "payload" => received_payload} =
564 Jason.decode!(received_event)
566 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
567 assert last_status["id"] == to_string(create_activity.id)
574 %{transport_pid: task.pid, assigns: %{user: user}}
577 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)