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
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.Streamer
12 import Pleroma.Factory
15 skip_thread_containment = Pleroma.Config.get([:instance, :skip_thread_containment])
18 Pleroma.Config.put([:instance, :skip_thread_containment], skip_thread_containment)
24 describe "user streams" do
26 GenServer.start(Streamer, %{}, name: Streamer)
29 if pid = Process.whereis(Streamer) do
30 Process.exit(pid, :kill)
35 notify = insert(:notification, user: user, activity: build(:note_activity))
36 {:ok, %{user: user, notify: notify}}
39 test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
42 assert_receive {:text, _}, 4_000
47 %{transport_pid: task.pid, assigns: %{user: user}}
50 Streamer.stream("user", notify)
54 test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
57 assert_receive {:text, _}, 4_000
62 %{transport_pid: task.pid, assigns: %{user: user}}
65 Streamer.stream("user:notification", notify)
69 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
72 blocked = insert(:user)
73 {:ok, user} = User.block(user, blocked)
75 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
79 %{transport_pid: task.pid, assigns: %{user: user}}
82 {:ok, activity} = CommonAPI.post(user, %{"status" => ":("})
83 {:ok, notif, _} = CommonAPI.favorite(activity.id, blocked)
85 Streamer.stream("user:notification", notif)
89 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
93 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
97 %{transport_pid: task.pid, assigns: %{user: user}}
100 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
101 {:ok, activity} = CommonAPI.add_mute(user, activity)
102 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
103 Streamer.stream("user:notification", notif)
107 test "it doesn't send notify to the 'user:notification' stream' when a domain is blocked", %{
110 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
111 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
115 %{transport_pid: task.pid, assigns: %{user: user}}
118 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
119 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
120 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
122 Streamer.stream("user:notification", notif)
127 test "it sends to public" do
129 other_user = insert(:user)
133 assert_receive {:text, _}, 4_000
137 transport_pid: task.pid,
143 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
146 "public" => [fake_socket]
149 Streamer.push_to_socket(topics, "public", activity)
158 "payload" => activity.id
162 assert_receive {:text, received_event}, 4_000
163 assert received_event == expected_event
167 transport_pid: task.pid,
173 {:ok, activity} = CommonAPI.delete(activity.id, other_user)
176 "public" => [fake_socket]
179 Streamer.push_to_socket(topics, "public", activity)
184 describe "thread_containment" do
185 test "it doesn't send to user if recipients invalid and thread containment is enabled" do
186 Pleroma.Config.put([:instance, :skip_thread_containment], false)
187 author = insert(:user)
188 user = insert(:user, following: [author.ap_id])
191 insert(:note_activity,
195 data: %{"to" => ["TEST-FFF"]}
199 task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
200 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
201 topics = %{"public" => [fake_socket]}
202 Streamer.push_to_socket(topics, "public", activity)
207 test "it sends message if recipients invalid and thread containment is disabled" do
208 Pleroma.Config.put([:instance, :skip_thread_containment], true)
209 author = insert(:user)
210 user = insert(:user, following: [author.ap_id])
213 insert(:note_activity,
217 data: %{"to" => ["TEST-FFF"]}
221 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
222 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
223 topics = %{"public" => [fake_socket]}
224 Streamer.push_to_socket(topics, "public", activity)
229 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
230 Pleroma.Config.put([:instance, :skip_thread_containment], false)
231 author = insert(:user)
232 user = insert(:user, following: [author.ap_id], info: %{skip_thread_containment: true})
235 insert(:note_activity,
239 data: %{"to" => ["TEST-FFF"]}
243 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
244 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
245 topics = %{"public" => [fake_socket]}
246 Streamer.push_to_socket(topics, "public", activity)
252 test "it doesn't send to blocked users" do
254 blocked_user = insert(:user)
255 {:ok, user} = User.block(user, blocked_user)
259 refute_receive {:text, _}, 1_000
263 transport_pid: task.pid,
269 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
272 "public" => [fake_socket]
275 Streamer.push_to_socket(topics, "public", activity)
280 test "it doesn't send unwanted DMs to list" do
281 user_a = insert(:user)
282 user_b = insert(:user)
283 user_c = insert(:user)
285 {:ok, user_a} = User.follow(user_a, user_b)
287 {:ok, list} = List.create("Test", user_a)
288 {:ok, list} = List.follow(list, user_b)
292 refute_receive {:text, _}, 1_000
296 transport_pid: task.pid,
303 CommonAPI.post(user_b, %{
304 "status" => "@#{user_c.nickname} Test",
305 "visibility" => "direct"
309 "list:#{list.id}" => [fake_socket]
312 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
317 test "it doesn't send unwanted private posts to list" do
318 user_a = insert(:user)
319 user_b = insert(:user)
321 {:ok, list} = List.create("Test", user_a)
322 {:ok, list} = List.follow(list, user_b)
326 refute_receive {:text, _}, 1_000
330 transport_pid: task.pid,
337 CommonAPI.post(user_b, %{
339 "visibility" => "private"
343 "list:#{list.id}" => [fake_socket]
346 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
351 test "it send wanted private posts to list" do
352 user_a = insert(:user)
353 user_b = insert(:user)
355 {:ok, user_a} = User.follow(user_a, user_b)
357 {:ok, list} = List.create("Test", user_a)
358 {:ok, list} = List.follow(list, user_b)
362 assert_receive {:text, _}, 1_000
366 transport_pid: task.pid,
373 CommonAPI.post(user_b, %{
375 "visibility" => "private"
379 "list:#{list.id}" => [fake_socket]
382 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
387 test "it doesn't send muted reblogs" do
388 user1 = insert(:user)
389 user2 = insert(:user)
390 user3 = insert(:user)
391 CommonAPI.hide_reblogs(user1, user2)
395 refute_receive {:text, _}, 1_000
399 transport_pid: task.pid,
405 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
406 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
409 "public" => [fake_socket]
412 Streamer.push_to_socket(topics, "public", announce_activity)
417 describe "direct streams" do
419 GenServer.start(Streamer, %{}, name: Streamer)
422 if pid = Process.whereis(Streamer) do
423 Process.exit(pid, :kill)
430 test "it sends conversation update to the 'direct' stream", %{} do
432 another_user = insert(:user)
436 assert_receive {:text, _received_event}, 4_000
441 %{transport_pid: task.pid, assigns: %{user: user}}
444 {:ok, _create_activity} =
445 CommonAPI.post(another_user, %{
446 "status" => "hey @#{user.nickname}",
447 "visibility" => "direct"
453 test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do
455 another_user = insert(:user)
457 {:ok, create_activity} =
458 CommonAPI.post(another_user, %{
459 "status" => "hi @#{user.nickname}",
460 "visibility" => "direct"
465 assert_receive {:text, received_event}, 4_000
466 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
468 refute_receive {:text, _}, 4_000
473 %{transport_pid: task.pid, assigns: %{user: user}}
476 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
481 test "it sends conversation update to the 'direct' stream when a message is deleted" do
483 another_user = insert(:user)
485 {:ok, create_activity} =
486 CommonAPI.post(another_user, %{
487 "status" => "hi @#{user.nickname}",
488 "visibility" => "direct"
491 {:ok, create_activity2} =
492 CommonAPI.post(another_user, %{
493 "status" => "hi @#{user.nickname}",
494 "in_reply_to_status_id" => create_activity.id,
495 "visibility" => "direct"
500 assert_receive {:text, received_event}, 4_000
501 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
503 assert_receive {:text, received_event}, 4_000
505 assert %{"event" => "conversation", "payload" => received_payload} =
506 Jason.decode!(received_event)
508 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
509 assert last_status["id"] == to_string(create_activity.id)
514 %{transport_pid: task.pid, assigns: %{user: user}}
517 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)