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 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
67 %{transport_pid: task.pid, assigns: %{user: user}}
70 {:ok, activity} = CommonAPI.post(user, %{"status" => ":("})
71 {:ok, notif, _} = CommonAPI.favorite(activity.id, blocked)
73 Streamer.stream("user:notification", notif)
77 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
81 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
85 %{transport_pid: task.pid, assigns: %{user: user}}
88 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
89 {:ok, activity} = CommonAPI.add_mute(user, activity)
90 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
91 Streamer.stream("user:notification", notif)
95 test "it doesn't send notify to the 'user:notification' stream' when a domain is blocked", %{
98 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
99 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
103 %{transport_pid: task.pid, assigns: %{user: user}}
106 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
107 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
108 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
110 Streamer.stream("user:notification", notif)
115 test "it sends to public" do
117 other_user = insert(:user)
121 assert_receive {:text, _}, 4_000
124 fake_socket = %StreamerSocket{
125 transport_pid: task.pid,
129 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
132 "public" => [fake_socket]
135 Worker.push_to_socket(topics, "public", activity)
144 "payload" => activity.id
148 assert_receive {:text, received_event}, 4_000
149 assert received_event == expected_event
152 fake_socket = %StreamerSocket{
153 transport_pid: task.pid,
157 {:ok, activity} = CommonAPI.delete(activity.id, other_user)
160 "public" => [fake_socket]
163 Worker.push_to_socket(topics, "public", activity)
168 describe "thread_containment" do
169 test "it doesn't send to user if recipients invalid and thread containment is enabled" do
170 Pleroma.Config.put([:instance, :skip_thread_containment], false)
171 author = insert(:user)
172 user = insert(:user, following: [author.ap_id])
175 insert(:note_activity,
179 data: %{"to" => ["TEST-FFF"]}
183 task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
184 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
185 topics = %{"public" => [fake_socket]}
186 Worker.push_to_socket(topics, "public", activity)
191 test "it sends message if recipients invalid and thread containment is disabled" do
192 Pleroma.Config.put([:instance, :skip_thread_containment], true)
193 author = insert(:user)
194 user = insert(:user, following: [author.ap_id])
197 insert(:note_activity,
201 data: %{"to" => ["TEST-FFF"]}
205 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
206 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
207 topics = %{"public" => [fake_socket]}
208 Worker.push_to_socket(topics, "public", activity)
213 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
214 Pleroma.Config.put([:instance, :skip_thread_containment], false)
215 author = insert(:user)
216 user = insert(:user, following: [author.ap_id], info: %{skip_thread_containment: true})
219 insert(:note_activity,
223 data: %{"to" => ["TEST-FFF"]}
227 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
228 fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
229 topics = %{"public" => [fake_socket]}
230 Worker.push_to_socket(topics, "public", activity)
237 test "it doesn't send messages involving blocked users" do
239 blocked_user = insert(:user)
240 {:ok, user} = User.block(user, blocked_user)
244 refute_receive {:text, _}, 1_000
247 fake_socket = %StreamerSocket{
248 transport_pid: task.pid,
252 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
255 "public" => [fake_socket]
258 Worker.push_to_socket(topics, "public", activity)
263 test "it doesn't send messages transitively involving blocked users" do
264 blocker = insert(:user)
265 blockee = insert(:user)
266 friend = insert(:user)
270 refute_receive {:text, _}, 1_000
273 fake_socket = %StreamerSocket{
274 transport_pid: task.pid,
279 "public" => [fake_socket]
282 {:ok, blocker} = User.block(blocker, blockee)
284 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
286 Worker.push_to_socket(topics, "public", activity_one)
288 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
290 Worker.push_to_socket(topics, "public", activity_two)
292 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
294 Worker.push_to_socket(topics, "public", activity_three)
300 test "it doesn't send unwanted DMs to list" do
301 user_a = insert(:user)
302 user_b = insert(:user)
303 user_c = insert(:user)
305 {:ok, user_a} = User.follow(user_a, user_b)
307 {:ok, list} = List.create("Test", user_a)
308 {:ok, list} = List.follow(list, user_b)
312 refute_receive {:text, _}, 1_000
315 fake_socket = %StreamerSocket{
316 transport_pid: task.pid,
321 CommonAPI.post(user_b, %{
322 "status" => "@#{user_c.nickname} Test",
323 "visibility" => "direct"
327 "list:#{list.id}" => [fake_socket]
330 Worker.handle_call({:stream, "list", activity}, self(), topics)
335 test "it doesn't send unwanted private posts to list" do
336 user_a = insert(:user)
337 user_b = insert(:user)
339 {:ok, list} = List.create("Test", user_a)
340 {:ok, list} = List.follow(list, user_b)
344 refute_receive {:text, _}, 1_000
347 fake_socket = %StreamerSocket{
348 transport_pid: task.pid,
353 CommonAPI.post(user_b, %{
355 "visibility" => "private"
359 "list:#{list.id}" => [fake_socket]
362 Worker.handle_call({:stream, "list", activity}, self(), topics)
367 test "it sends wanted private posts to list" do
368 user_a = insert(:user)
369 user_b = insert(:user)
371 {:ok, user_a} = User.follow(user_a, user_b)
373 {:ok, list} = List.create("Test", user_a)
374 {:ok, list} = List.follow(list, user_b)
378 assert_receive {:text, _}, 1_000
381 fake_socket = %StreamerSocket{
382 transport_pid: task.pid,
387 CommonAPI.post(user_b, %{
389 "visibility" => "private"
397 Worker.handle_call({:stream, "list", activity}, self(), %{})
402 test "it doesn't send muted reblogs" do
403 user1 = insert(:user)
404 user2 = insert(:user)
405 user3 = insert(:user)
406 CommonAPI.hide_reblogs(user1, user2)
410 refute_receive {:text, _}, 1_000
413 fake_socket = %StreamerSocket{
414 transport_pid: task.pid,
418 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
419 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
422 "public" => [fake_socket]
425 Worker.push_to_socket(topics, "public", announce_activity)
430 test "it doesn't send posts from muted threads" do
432 user2 = insert(:user)
433 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
435 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
437 {:ok, activity} = CommonAPI.add_mute(user2, activity)
439 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
445 %{transport_pid: task.pid, assigns: %{user: user2}}
448 Streamer.stream("user", activity)
452 describe "direct streams" do
457 test "it sends conversation update to the 'direct' stream", %{} do
459 another_user = insert(:user)
463 assert_receive {:text, _received_event}, 4_000
468 %{transport_pid: task.pid, assigns: %{user: user}}
471 {:ok, _create_activity} =
472 CommonAPI.post(another_user, %{
473 "status" => "hey @#{user.nickname}",
474 "visibility" => "direct"
480 test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do
482 another_user = insert(:user)
484 {:ok, create_activity} =
485 CommonAPI.post(another_user, %{
486 "status" => "hi @#{user.nickname}",
487 "visibility" => "direct"
492 assert_receive {:text, received_event}, 4_000
493 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
495 refute_receive {:text, _}, 4_000
502 %{transport_pid: task.pid, assigns: %{user: user}}
505 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
510 test "it sends conversation update to the 'direct' stream when a message is deleted" do
512 another_user = insert(:user)
514 {:ok, create_activity} =
515 CommonAPI.post(another_user, %{
516 "status" => "hi @#{user.nickname}",
517 "visibility" => "direct"
520 {:ok, create_activity2} =
521 CommonAPI.post(another_user, %{
522 "status" => "hi @#{user.nickname}",
523 "in_reply_to_status_id" => create_activity.id,
524 "visibility" => "direct"
529 assert_receive {:text, received_event}, 4_000
530 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
532 assert_receive {:text, received_event}, 4_000
534 assert %{"event" => "conversation", "payload" => received_payload} =
535 Jason.decode!(received_event)
537 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
538 assert last_status["id"] == to_string(create_activity.id)
545 %{transport_pid: task.pid, assigns: %{user: user}}
548 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)