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)
108 test "it sends to public" do
110 other_user = insert(:user)
114 assert_receive {:text, _}, 4_000
118 transport_pid: task.pid,
124 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
127 "public" => [fake_socket]
130 Streamer.push_to_socket(topics, "public", activity)
139 "payload" => activity.id
143 assert_receive {:text, received_event}, 4_000
144 assert received_event == expected_event
148 transport_pid: task.pid,
154 {:ok, activity} = CommonAPI.delete(activity.id, other_user)
157 "public" => [fake_socket]
160 Streamer.push_to_socket(topics, "public", activity)
165 describe "thread_containment" do
166 test "it doesn't send to user if recipients invalid and thread containment is enabled" do
167 Pleroma.Config.put([:instance, :skip_thread_containment], false)
168 author = insert(:user)
169 user = insert(:user, following: [author.ap_id])
172 insert(:note_activity,
176 data: %{"to" => ["TEST-FFF"]}
180 task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
181 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
182 topics = %{"public" => [fake_socket]}
183 Streamer.push_to_socket(topics, "public", activity)
188 test "it sends message if recipients invalid and thread containment is disabled" do
189 Pleroma.Config.put([:instance, :skip_thread_containment], true)
190 author = insert(:user)
191 user = insert(:user, following: [author.ap_id])
194 insert(:note_activity,
198 data: %{"to" => ["TEST-FFF"]}
202 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
203 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
204 topics = %{"public" => [fake_socket]}
205 Streamer.push_to_socket(topics, "public", activity)
210 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
211 Pleroma.Config.put([:instance, :skip_thread_containment], false)
212 author = insert(:user)
213 user = insert(:user, following: [author.ap_id], info: %{skip_thread_containment: true})
216 insert(:note_activity,
220 data: %{"to" => ["TEST-FFF"]}
224 task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
225 fake_socket = %{transport_pid: task.pid, assigns: %{user: user}}
226 topics = %{"public" => [fake_socket]}
227 Streamer.push_to_socket(topics, "public", activity)
233 test "it doesn't send to blocked users" do
235 blocked_user = insert(:user)
236 {:ok, user} = User.block(user, blocked_user)
240 refute_receive {:text, _}, 1_000
244 transport_pid: task.pid,
250 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
253 "public" => [fake_socket]
256 Streamer.push_to_socket(topics, "public", activity)
261 test "it doesn't send unwanted DMs to list" do
262 user_a = insert(:user)
263 user_b = insert(:user)
264 user_c = insert(:user)
266 {:ok, user_a} = User.follow(user_a, user_b)
268 {:ok, list} = List.create("Test", user_a)
269 {:ok, list} = List.follow(list, user_b)
273 refute_receive {:text, _}, 1_000
277 transport_pid: task.pid,
284 CommonAPI.post(user_b, %{
285 "status" => "@#{user_c.nickname} Test",
286 "visibility" => "direct"
290 "list:#{list.id}" => [fake_socket]
293 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
298 test "it doesn't send unwanted private posts to list" do
299 user_a = insert(:user)
300 user_b = insert(:user)
302 {:ok, list} = List.create("Test", user_a)
303 {:ok, list} = List.follow(list, user_b)
307 refute_receive {:text, _}, 1_000
311 transport_pid: task.pid,
318 CommonAPI.post(user_b, %{
320 "visibility" => "private"
324 "list:#{list.id}" => [fake_socket]
327 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
332 test "it send wanted private posts to list" do
333 user_a = insert(:user)
334 user_b = insert(:user)
336 {:ok, user_a} = User.follow(user_a, user_b)
338 {:ok, list} = List.create("Test", user_a)
339 {:ok, list} = List.follow(list, user_b)
343 assert_receive {:text, _}, 1_000
347 transport_pid: task.pid,
354 CommonAPI.post(user_b, %{
356 "visibility" => "private"
360 "list:#{list.id}" => [fake_socket]
363 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
368 test "it doesn't send muted reblogs" do
369 user1 = insert(:user)
370 user2 = insert(:user)
371 user3 = insert(:user)
372 CommonAPI.hide_reblogs(user1, user2)
376 refute_receive {:text, _}, 1_000
380 transport_pid: task.pid,
386 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
387 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
390 "public" => [fake_socket]
393 Streamer.push_to_socket(topics, "public", announce_activity)
398 describe "direct streams" do
400 GenServer.start(Streamer, %{}, name: Streamer)
403 if pid = Process.whereis(Streamer) do
404 Process.exit(pid, :kill)
411 test "it sends conversation update to the 'direct' stream", %{} do
413 another_user = insert(:user)
417 assert_receive {:text, _received_event}, 4_000
422 %{transport_pid: task.pid, assigns: %{user: user}}
425 {:ok, _create_activity} =
426 CommonAPI.post(another_user, %{
427 "status" => "hey @#{user.nickname}",
428 "visibility" => "direct"
434 test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do
436 another_user = insert(:user)
438 {:ok, create_activity} =
439 CommonAPI.post(another_user, %{
440 "status" => "hi @#{user.nickname}",
441 "visibility" => "direct"
446 assert_receive {:text, received_event}, 4_000
447 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
449 refute_receive {:text, _}, 4_000
454 %{transport_pid: task.pid, assigns: %{user: user}}
457 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
462 test "it sends conversation update to the 'direct' stream when a message is deleted" do
464 another_user = insert(:user)
466 {:ok, create_activity} =
467 CommonAPI.post(another_user, %{
468 "status" => "hi @#{user.nickname}",
469 "visibility" => "direct"
472 {:ok, create_activity2} =
473 CommonAPI.post(another_user, %{
474 "status" => "hi @#{user.nickname}",
475 "in_reply_to_status_id" => create_activity.id,
476 "visibility" => "direct"
481 assert_receive {:text, received_event}, 4_000
482 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
484 assert_receive {:text, received_event}, 4_000
486 assert %{"event" => "conversation", "payload" => received_payload} =
487 Jason.decode!(received_event)
489 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
490 assert last_status["id"] == to_string(create_activity.id)
495 %{transport_pid: task.pid, assigns: %{user: user}}
498 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)