1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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)
236 test "it doesn't send to blocked users" do
238 blocked_user = insert(:user)
239 {:ok, user} = User.block(user, blocked_user)
243 refute_receive {:text, _}, 1_000
246 fake_socket = %StreamerSocket{
247 transport_pid: task.pid,
251 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
254 "public" => [fake_socket]
257 Worker.push_to_socket(topics, "public", activity)
262 test "it doesn't send unwanted DMs to list" do
263 user_a = insert(:user)
264 user_b = insert(:user)
265 user_c = insert(:user)
267 {:ok, user_a} = User.follow(user_a, user_b)
269 {:ok, list} = List.create("Test", user_a)
270 {:ok, list} = List.follow(list, user_b)
274 refute_receive {:text, _}, 1_000
277 fake_socket = %StreamerSocket{
278 transport_pid: task.pid,
283 CommonAPI.post(user_b, %{
284 "status" => "@#{user_c.nickname} Test",
285 "visibility" => "direct"
289 "list:#{list.id}" => [fake_socket]
292 Worker.handle_call({:stream, "list", activity}, self(), topics)
297 test "it doesn't send unwanted private posts to list" do
298 user_a = insert(:user)
299 user_b = insert(:user)
301 {:ok, list} = List.create("Test", user_a)
302 {:ok, list} = List.follow(list, user_b)
306 refute_receive {:text, _}, 1_000
309 fake_socket = %StreamerSocket{
310 transport_pid: task.pid,
315 CommonAPI.post(user_b, %{
317 "visibility" => "private"
321 "list:#{list.id}" => [fake_socket]
324 Worker.handle_call({:stream, "list", activity}, self(), topics)
329 test "it sends wanted private posts to list" do
330 user_a = insert(:user)
331 user_b = insert(:user)
333 {:ok, user_a} = User.follow(user_a, user_b)
335 {:ok, list} = List.create("Test", user_a)
336 {:ok, list} = List.follow(list, user_b)
340 assert_receive {:text, _}, 1_000
343 fake_socket = %StreamerSocket{
344 transport_pid: task.pid,
349 CommonAPI.post(user_b, %{
351 "visibility" => "private"
359 Worker.handle_call({:stream, "list", activity}, self(), %{})
364 test "it doesn't send muted reblogs" do
365 user1 = insert(:user)
366 user2 = insert(:user)
367 user3 = insert(:user)
368 CommonAPI.hide_reblogs(user1, user2)
372 refute_receive {:text, _}, 1_000
375 fake_socket = %StreamerSocket{
376 transport_pid: task.pid,
380 {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
381 {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)
384 "public" => [fake_socket]
387 Worker.push_to_socket(topics, "public", announce_activity)
392 test "it doesn't send posts from muted threads" do
394 user2 = insert(:user)
395 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
397 {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
399 {:ok, activity} = CommonAPI.add_mute(user2, activity)
401 task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)
407 %{transport_pid: task.pid, assigns: %{user: user2}}
410 Streamer.stream("user", activity)
414 describe "direct streams" do
419 test "it sends conversation update to the 'direct' stream", %{} do
421 another_user = insert(:user)
425 assert_receive {:text, _received_event}, 4_000
430 %{transport_pid: task.pid, assigns: %{user: user}}
433 {:ok, _create_activity} =
434 CommonAPI.post(another_user, %{
435 "status" => "hey @#{user.nickname}",
436 "visibility" => "direct"
442 test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do
444 another_user = insert(:user)
446 {:ok, create_activity} =
447 CommonAPI.post(another_user, %{
448 "status" => "hi @#{user.nickname}",
449 "visibility" => "direct"
454 assert_receive {:text, received_event}, 4_000
455 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
457 refute_receive {:text, _}, 4_000
464 %{transport_pid: task.pid, assigns: %{user: user}}
467 {:ok, _} = CommonAPI.delete(create_activity.id, another_user)
472 test "it sends conversation update to the 'direct' stream when a message is deleted" do
474 another_user = insert(:user)
476 {:ok, create_activity} =
477 CommonAPI.post(another_user, %{
478 "status" => "hi @#{user.nickname}",
479 "visibility" => "direct"
482 {:ok, create_activity2} =
483 CommonAPI.post(another_user, %{
484 "status" => "hi @#{user.nickname}",
485 "in_reply_to_status_id" => create_activity.id,
486 "visibility" => "direct"
491 assert_receive {:text, received_event}, 4_000
492 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
494 assert_receive {:text, received_event}, 4_000
496 assert %{"event" => "conversation", "payload" => received_payload} =
497 Jason.decode!(received_event)
499 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
500 assert last_status["id"] == to_string(create_activity.id)
507 %{transport_pid: task.pid, assigns: %{user: user}}
510 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)