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, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
119 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
120 {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
121 Streamer.stream("user:notification", notif)
126 test "it sends to public" do
128 other_user = insert(:user)
132 assert_receive {:text, _}, 4_000
136 transport_pid: task.pid,
142 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
145 "public" => [fake_socket]
148 Streamer.push_to_socket(topics, "public", activity)
157 "payload" => activity.id
161 assert_receive {:text, received_event}, 4_000
162 assert received_event == expected_event
166 transport_pid: task.pid,
172 {:ok, activity} = CommonAPI.delete(activity.id, other_user)
175 "public" => [fake_socket]
178 Streamer.push_to_socket(topics, "public", activity)
183 describe "thread_containment" do
184 test "it doesn't send to user if recipients invalid and thread containment is enabled" do
185 Pleroma.Config.put([:instance, :skip_thread_containment], false)
186 author = insert(:user)
187 user = insert(:user, following: [author.ap_id])
190 insert(:note_activity,
194 data: %{"to" => ["TEST-FFF"]}
198 task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
199 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
200 topics = %{"public" => [fake_socket]}
201 Streamer.push_to_socket(topics, "public", activity)
206 test "it sends message if recipients invalid and thread containment is disabled" do
207 Pleroma.Config.put([:instance, :skip_thread_containment], true)
208 author = insert(:user)
209 user = insert(:user, following: [author.ap_id])
212 insert(:note_activity,
216 data: %{"to" => ["TEST-FFF"]}
220 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
221 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
222 topics = %{"public" => [fake_socket]}
223 Streamer.push_to_socket(topics, "public", activity)
228 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
229 Pleroma.Config.put([:instance, :skip_thread_containment], false)
230 author = insert(:user)
231 user = insert(:user, following: [author.ap_id], info: %{skip_thread_containment: true})
234 insert(:note_activity,
238 data: %{"to" => ["TEST-FFF"]}
242 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
243 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
244 topics = %{"public" => [fake_socket]}
245 Streamer.push_to_socket(topics, "public", activity)
251 test "it doesn't send to blocked users" do
253 blocked_user = insert(:user)
254 {:ok, user} = User.block(user, blocked_user)
258 refute_receive {:text, _}, 1_000
262 transport_pid: task.pid,
268 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
271 "public" => [fake_socket]
274 Streamer.push_to_socket(topics, "public", activity)
279 test "it doesn't send unwanted DMs to list" do
280 user_a = insert(:user)
281 user_b = insert(:user)
282 user_c = insert(:user)
284 {:ok, user_a} = User.follow(user_a, user_b)
286 {:ok, list} = List.create("Test", user_a)
287 {:ok, list} = List.follow(list, user_b)
291 refute_receive {:text, _}, 1_000
295 transport_pid: task.pid,
302 CommonAPI.post(user_b, %{
303 "status" => "@#{user_c.nickname} Test",
304 "visibility" => "direct"
308 "list:#{list.id}" => [fake_socket]
311 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
316 test "it doesn't send unwanted private posts to list" do
317 user_a = insert(:user)
318 user_b = insert(:user)
320 {:ok, list} = List.create("Test", user_a)
321 {:ok, list} = List.follow(list, user_b)
325 refute_receive {:text, _}, 1_000
329 transport_pid: task.pid,
336 CommonAPI.post(user_b, %{
338 "visibility" => "private"
342 "list:#{list.id}" => [fake_socket]
345 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
350 test "it send wanted private posts to list" do
351 user_a = insert(:user)
352 user_b = insert(:user)
354 {:ok, user_a} = User.follow(user_a, user_b)
356 {:ok, list} = List.create("Test", user_a)
357 {:ok, list} = List.follow(list, user_b)
361 assert_receive {:text, _}, 1_000
365 transport_pid: task.pid,
372 CommonAPI.post(user_b, %{
374 "visibility" => "private"
378 "list:#{list.id}" => [fake_socket]
381 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
386 test "it doesn't send muted reblogs" do
387 user1 = insert(:user)
388 user2 = insert(:user)
389 user3 = insert(:user)
390 CommonAPI.hide_reblogs(user1, user2)
394 refute_receive {:text, _}, 1_000
398 transport_pid: task.pid,
404 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
405 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
408 "public" => [fake_socket]
411 Streamer.push_to_socket(topics, "public", announce_activity)
416 describe "direct streams" do
418 GenServer.start(Streamer, %{}, name: Streamer)
421 if pid = Process.whereis(Streamer) do
422 Process.exit(pid, :kill)
429 test "it sends conversation update to the 'direct' stream", %{} do
431 another_user = insert(:user)
435 assert_receive {:text, _received_event}, 4_000
440 %{transport_pid: task.pid, assigns: %{user: user}}
443 {:ok, _create_activity} =
444 CommonAPI.post(another_user, %{
445 "status" => "hey @#{user.nickname}",
446 "visibility" => "direct"
452 test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do
454 another_user = insert(:user)
456 {:ok, create_activity} =
457 CommonAPI.post(another_user, %{
458 "status" => "hi @#{user.nickname}",
459 "visibility" => "direct"
464 assert_receive {:text, received_event}, 4_000
465 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
467 refute_receive {:text, _}, 4_000
472 %{transport_pid: task.pid, assigns: %{user: user}}
475 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
480 test "it sends conversation update to the 'direct' stream when a message 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"
490 {:ok, create_activity2} =
491 CommonAPI.post(another_user, %{
492 "status" => "hi @#{user.nickname}",
493 "in_reply_to_status_id" => create_activity.id,
494 "visibility" => "direct"
499 assert_receive {:text, received_event}, 4_000
500 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
502 assert_receive {:text, received_event}, 4_000
504 assert %{"event" => "conversation", "payload" => received_payload} =
505 Jason.decode!(received_event)
507 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
508 assert last_status["id"] == to_string(create_activity.id)
513 %{transport_pid: task.pid, assigns: %{user: user}}
516 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)