1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.StreamerTest do
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.Streamer
14 alias Pleroma.Web.Streamer.StreamerSocket
15 alias Pleroma.Web.Streamer.Worker
17 @moduletag needs_streamer: true
18 clear_config_all([:instance, :skip_thread_containment])
20 describe "user streams" do
23 notify = insert(:notification, user: user, activity: build(:note_activity))
24 {:ok, %{user: user, notify: notify}}
27 test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
30 assert_receive {:text, _}, 4_000
35 %{transport_pid: task.pid, assigns: %{user: user}}
38 Streamer.stream("user", notify)
42 test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
45 assert_receive {:text, _}, 4_000
50 %{transport_pid: task.pid, assigns: %{user: user}}
53 Streamer.stream("user:notification", notify)
57 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
60 blocked = insert(:user)
61 {:ok, user} = User.block(user, blocked)
63 {:ok, activity} = CommonAPI.post(user, %{"status" => ":("})
64 {:ok, notif, _} = CommonAPI.favorite(activity.id, blocked)
66 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
70 %{transport_pid: task.pid, assigns: %{user: user}}
73 Streamer.stream("user:notification", notif)
77 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
82 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
83 {:ok, activity} = CommonAPI.add_mute(user, activity)
84 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
86 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
90 %{transport_pid: task.pid, assigns: %{user: user}}
93 Streamer.stream("user:notification", notif)
97 test "it doesn't send notify to the 'user:notification' stream' when a domain is blocked", %{
100 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
102 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
103 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
104 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
106 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
110 %{transport_pid: task.pid, assigns: %{user: user}}
113 Streamer.stream("user:notification", notif)
117 test "it sends follow activities to the 'user:notification' stream", %{
120 user2 = insert(:user)
121 task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)
125 %{transport_pid: task.pid, assigns: %{user: user}}
128 {:ok, _follower, _followed, _activity} = CommonAPI.follow(user2, user)
130 # We don't directly pipe the notification to the streamer as it's already
131 # generated as a side effect of CommonAPI.follow().
136 test "it sends to public" do
138 other_user = insert(:user)
142 assert_receive {:text, _}, 4_000
145 fake_socket = %StreamerSocket{
146 transport_pid: task.pid,
150 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
153 "public" => [fake_socket]
156 Worker.push_to_socket(topics, "public", activity)
165 "payload" => activity.id
169 assert_receive {:text, received_event}, 4_000
170 assert received_event == expected_event
173 fake_socket = %StreamerSocket{
174 transport_pid: task.pid,
178 {:ok, activity} = CommonAPI.delete(activity.id, other_user)
181 "public" => [fake_socket]
184 Worker.push_to_socket(topics, "public", activity)
189 describe "thread_containment" do
190 test "it doesn't send to user if recipients invalid and thread containment is enabled" do
191 Pleroma.Config.put([:instance, :skip_thread_containment], false)
192 author = insert(:user)
193 user = insert(:user, following: [author.ap_id])
196 insert(:note_activity,
200 data: %{"to" => ["TEST-FFF"]}
204 task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
205 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
206 topics = %{"public" => [fake_socket]}
207 Worker.push_to_socket(topics, "public", activity)
212 test "it sends message if recipients invalid and thread containment is disabled" do
213 Pleroma.Config.put([:instance, :skip_thread_containment], true)
214 author = insert(:user)
215 user = insert(:user, following: [author.ap_id])
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, following: [author.ap_id], info: %{skip_thread_containment: true})
240 insert(:note_activity,
244 data: %{"to" => ["TEST-FFF"]}
248 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
249 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
250 topics = %{"public" => [fake_socket]}
251 Worker.push_to_socket(topics, "public", activity)
258 test "it doesn't send messages involving blocked users" do
260 blocked_user = insert(:user)
261 {:ok, user} = User.block(user, blocked_user)
263 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
267 refute_receive {:text, _}, 1_000
270 fake_socket = %StreamerSocket{
271 transport_pid: task.pid,
276 "public" => [fake_socket]
279 Worker.push_to_socket(topics, "public", activity)
284 test "it doesn't send messages transitively involving blocked users" do
285 blocker = insert(:user)
286 blockee = insert(:user)
287 friend = insert(:user)
291 refute_receive {:text, _}, 1_000
294 fake_socket = %StreamerSocket{
295 transport_pid: task.pid,
300 "public" => [fake_socket]
303 {:ok, blocker} = User.block(blocker, blockee)
305 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
307 Worker.push_to_socket(topics, "public", activity_one)
309 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
311 Worker.push_to_socket(topics, "public", activity_two)
313 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
315 Worker.push_to_socket(topics, "public", activity_three)
321 test "it doesn't send unwanted DMs to list" do
322 user_a = insert(:user)
323 user_b = insert(:user)
324 user_c = insert(:user)
326 {:ok, user_a} = User.follow(user_a, user_b)
328 {:ok, list} = List.create("Test", user_a)
329 {:ok, list} = List.follow(list, user_b)
332 CommonAPI.post(user_b, %{
333 "status" => "@#{user_c.nickname} Test",
334 "visibility" => "direct"
339 refute_receive {:text, _}, 1_000
342 fake_socket = %StreamerSocket{
343 transport_pid: task.pid,
348 "list:#{list.id}" => [fake_socket]
351 Worker.handle_call({:stream, "list", activity}, self(), topics)
356 test "it doesn't send unwanted private posts to list" do
357 user_a = insert(:user)
358 user_b = insert(:user)
360 {:ok, list} = List.create("Test", user_a)
361 {:ok, list} = List.follow(list, user_b)
364 CommonAPI.post(user_b, %{
366 "visibility" => "private"
371 refute_receive {:text, _}, 1_000
374 fake_socket = %StreamerSocket{
375 transport_pid: task.pid,
380 "list:#{list.id}" => [fake_socket]
383 Worker.handle_call({:stream, "list", activity}, self(), topics)
388 test "it sends wanted private posts to list" do
389 user_a = insert(:user)
390 user_b = insert(:user)
392 {:ok, user_a} = User.follow(user_a, user_b)
394 {:ok, list} = List.create("Test", user_a)
395 {:ok, list} = List.follow(list, user_b)
398 CommonAPI.post(user_b, %{
400 "visibility" => "private"
405 assert_receive {:text, _}, 1_000
408 fake_socket = %StreamerSocket{
409 transport_pid: task.pid,
418 Worker.handle_call({:stream, "list", activity}, self(), %{})
423 test "it doesn't send muted reblogs" do
424 user1 = insert(:user)
425 user2 = insert(:user)
426 user3 = insert(:user)
427 CommonAPI.hide_reblogs(user1, user2)
429 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
430 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
434 refute_receive {:text, _}, 1_000
437 fake_socket = %StreamerSocket{
438 transport_pid: task.pid,
443 "public" => [fake_socket]
446 Worker.push_to_socket(topics, "public", announce_activity)
451 test "it does send non-reblog notification for reblog-muted actors" do
452 user1 = insert(:user)
453 user2 = insert(:user)
454 user3 = insert(:user)
455 CommonAPI.hide_reblogs(user1, user2)
457 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
458 {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, user2)
462 assert_receive {:text, _}, 1_000
465 fake_socket = %StreamerSocket{
466 transport_pid: task.pid,
471 "public" => [fake_socket]
474 Worker.push_to_socket(topics, "public", favorite_activity)
479 test "it doesn't send posts from muted threads" do
481 user2 = insert(:user)
482 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
484 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
486 {:ok, activity} = CommonAPI.add_mute(user2, activity)
488 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
494 %{transport_pid: task.pid, assigns: %{user: user2}}
497 Streamer.stream("user", activity)
501 describe "direct streams" do
506 test "it sends conversation update to the 'direct' stream", %{} do
508 another_user = insert(:user)
512 assert_receive {:text, _received_event}, 4_000
517 %{transport_pid: task.pid, assigns: %{user: user}}
520 {:ok, _create_activity} =
521 CommonAPI.post(another_user, %{
522 "status" => "hey @#{user.nickname}",
523 "visibility" => "direct"
529 test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do
531 another_user = insert(:user)
533 {:ok, create_activity} =
534 CommonAPI.post(another_user, %{
535 "status" => "hi @#{user.nickname}",
536 "visibility" => "direct"
541 assert_receive {:text, received_event}, 4_000
542 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
544 refute_receive {:text, _}, 4_000
551 %{transport_pid: task.pid, assigns: %{user: user}}
554 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
559 test "it sends conversation update to the 'direct' stream when a message is deleted" do
561 another_user = insert(:user)
563 {:ok, create_activity} =
564 CommonAPI.post(another_user, %{
565 "status" => "hi @#{user.nickname}",
566 "visibility" => "direct"
569 {:ok, create_activity2} =
570 CommonAPI.post(another_user, %{
571 "status" => "hi @#{user.nickname}",
572 "in_reply_to_status_id" => create_activity.id,
573 "visibility" => "direct"
578 assert_receive {:text, received_event}, 4_000
579 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
581 assert_receive {:text, received_event}, 4_000
583 assert %{"event" => "conversation", "payload" => received_payload} =
584 Jason.decode!(received_event)
586 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
587 assert last_status["id"] == to_string(create_activity.id)
594 %{transport_pid: task.pid, assigns: %{user: user}}
597 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)