1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.StreamerTest do
11 alias Pleroma.Chat.MessageReference
12 alias Pleroma.Conversation.Participation
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.Streamer
18 alias Pleroma.Web.StreamerView
20 @moduletag needs_streamer: true, capture_log: true
22 setup do: clear_config([:instance, :skip_thread_containment])
24 describe "get_topic without an user" do
25 test "allows public" do
26 assert {:ok, "public"} = Streamer.get_topic("public", nil)
27 assert {:ok, "public:local"} = Streamer.get_topic("public:local", nil)
28 assert {:ok, "public:media"} = Streamer.get_topic("public:media", nil)
29 assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", nil)
32 test "allows hashtag streams" do
33 assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", nil, %{"tag" => "cofe"})
36 test "disallows user streams" do
37 assert {:error, _} = Streamer.get_topic("user", nil)
38 assert {:error, _} = Streamer.get_topic("user:notification", nil)
39 assert {:error, _} = Streamer.get_topic("direct", nil)
42 test "disallows list streams" do
43 assert {:error, _} = Streamer.get_topic("list", nil, %{"list" => 42})
47 describe "get_topic with an user" do
53 test "allows public streams", %{user: user} do
54 assert {:ok, "public"} = Streamer.get_topic("public", user)
55 assert {:ok, "public:local"} = Streamer.get_topic("public:local", user)
56 assert {:ok, "public:media"} = Streamer.get_topic("public:media", user)
57 assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", user)
60 test "allows user streams", %{user: user} do
61 expected_user_topic = "user:#{user.id}"
62 expected_notif_topic = "user:notification:#{user.id}"
63 expected_direct_topic = "direct:#{user.id}"
64 assert {:ok, ^expected_user_topic} = Streamer.get_topic("user", user)
65 assert {:ok, ^expected_notif_topic} = Streamer.get_topic("user:notification", user)
66 assert {:ok, ^expected_direct_topic} = Streamer.get_topic("direct", user)
69 test "allows hashtag streams", %{user: user} do
70 assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", user, %{"tag" => "cofe"})
73 test "disallows registering to an user stream", %{user: user} do
74 another_user = insert(:user)
75 assert {:error, _} = Streamer.get_topic("user:#{another_user.id}", user)
76 assert {:error, _} = Streamer.get_topic("user:notification:#{another_user.id}", user)
77 assert {:error, _} = Streamer.get_topic("direct:#{another_user.id}", user)
80 test "allows list stream that are owned by the user", %{user: user} do
81 {:ok, list} = List.create("Test", user)
82 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user)
83 assert {:ok, _} = Streamer.get_topic("list", user, %{"list" => list.id})
86 test "disallows list stream that are not owned by the user", %{user: user} do
87 another_user = insert(:user)
88 {:ok, list} = List.create("Test", another_user)
89 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user)
90 assert {:error, _} = Streamer.get_topic("list", user, %{"list" => list.id})
94 describe "user streams" do
97 notify = insert(:notification, user: user, activity: build(:note_activity))
98 {:ok, %{user: user, notify: notify}}
101 test "it streams the user's post in the 'user' stream", %{user: user} do
102 Streamer.get_topic_and_add_socket("user", user)
103 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
104 assert_receive {:render_with_user, _, _, ^activity}
105 refute Streamer.filtered_by_user?(user, activity)
108 test "it streams boosts of the user in the 'user' stream", %{user: user} do
109 Streamer.get_topic_and_add_socket("user", user)
111 other_user = insert(:user)
112 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
113 {:ok, announce} = CommonAPI.repeat(activity.id, user)
115 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
116 refute Streamer.filtered_by_user?(user, announce)
119 test "it does not stream announces of the user's own posts in the 'user' stream", %{
122 Streamer.get_topic_and_add_socket("user", user)
124 other_user = insert(:user)
125 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
126 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
128 assert Streamer.filtered_by_user?(user, announce)
131 test "it does stream notifications announces of the user's own posts in the 'user' stream", %{
134 Streamer.get_topic_and_add_socket("user", user)
136 other_user = insert(:user)
137 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
138 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
142 |> Repo.get_by(%{user_id: user.id, activity_id: announce.id})
143 |> Repo.preload(:activity)
145 refute Streamer.filtered_by_user?(user, notification)
148 test "it streams boosts of mastodon user in the 'user' stream", %{user: user} do
149 Streamer.get_topic_and_add_socket("user", user)
151 other_user = insert(:user)
152 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
155 File.read!("test/fixtures/mastodon-announce.json")
157 |> Map.put("object", activity.data["object"])
158 |> Map.put("actor", user.ap_id)
160 {:ok, %Pleroma.Activity{data: _data, local: false} = announce} =
161 Pleroma.Web.ActivityPub.Transmogrifier.handle_incoming(data)
163 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
164 refute Streamer.filtered_by_user?(user, announce)
167 test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
168 Streamer.get_topic_and_add_socket("user", user)
169 Streamer.stream("user", notify)
170 assert_receive {:render_with_user, _, _, ^notify}
171 refute Streamer.filtered_by_user?(user, notify)
174 test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
175 Streamer.get_topic_and_add_socket("user:notification", user)
176 Streamer.stream("user:notification", notify)
177 assert_receive {:render_with_user, _, _, ^notify}
178 refute Streamer.filtered_by_user?(user, notify)
181 test "it sends chat messages to the 'user:pleroma_chat' stream", %{user: user} do
182 other_user = insert(:user)
184 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
185 object = Object.normalize(create_activity, false)
186 chat = Chat.get(user.id, other_user.ap_id)
187 cm_ref = MessageReference.for_chat_and_object(chat, object)
188 cm_ref = %{cm_ref | chat: chat, object: object}
190 Streamer.get_topic_and_add_socket("user:pleroma_chat", user)
191 Streamer.stream("user:pleroma_chat", {user, cm_ref})
193 text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
195 assert text =~ "hey cirno"
196 assert_receive {:text, ^text}
199 test "it sends chat messages to the 'user' stream", %{user: user} do
200 other_user = insert(:user)
202 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
203 object = Object.normalize(create_activity, false)
204 chat = Chat.get(user.id, other_user.ap_id)
205 cm_ref = MessageReference.for_chat_and_object(chat, object)
206 cm_ref = %{cm_ref | chat: chat, object: object}
208 Streamer.get_topic_and_add_socket("user", user)
209 Streamer.stream("user", {user, cm_ref})
211 text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
213 assert text =~ "hey cirno"
214 assert_receive {:text, ^text}
217 test "it sends chat message notifications to the 'user:notification' stream", %{user: user} do
218 other_user = insert(:user)
220 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey")
223 Repo.get_by(Pleroma.Notification, user_id: user.id, activity_id: create_activity.id)
224 |> Repo.preload(:activity)
226 Streamer.get_topic_and_add_socket("user:notification", user)
227 Streamer.stream("user:notification", notify)
228 assert_receive {:render_with_user, _, _, ^notify}
229 refute Streamer.filtered_by_user?(user, notify)
232 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
235 blocked = insert(:user)
236 {:ok, _user_relationship} = User.block(user, blocked)
238 Streamer.get_topic_and_add_socket("user:notification", user)
240 {:ok, activity} = CommonAPI.post(user, %{status: ":("})
241 {:ok, _} = CommonAPI.favorite(blocked, activity.id)
246 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
249 user2 = insert(:user)
251 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
252 {:ok, _} = CommonAPI.add_mute(user, activity)
254 Streamer.get_topic_and_add_socket("user:notification", user)
256 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
259 assert Streamer.filtered_by_user?(user, favorite_activity)
262 test "it sends favorite to 'user:notification' stream'", %{
265 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
267 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
268 Streamer.get_topic_and_add_socket("user:notification", user)
269 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
271 assert_receive {:render_with_user, _, "notification.json", notif}
272 assert notif.activity.id == favorite_activity.id
273 refute Streamer.filtered_by_user?(user, notif)
276 test "it doesn't send the 'user:notification' stream' when a domain is blocked", %{
279 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
281 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
282 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
283 Streamer.get_topic_and_add_socket("user:notification", user)
284 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
287 assert Streamer.filtered_by_user?(user, favorite_activity)
290 test "it sends follow activities to the 'user:notification' stream", %{
293 user_url = user.ap_id
294 user2 = insert(:user)
297 File.read!("test/fixtures/users_mock/localhost.json")
298 |> String.replace("{{nickname}}", user.nickname)
301 Tesla.Mock.mock_global(fn
302 %{method: :get, url: ^user_url} ->
303 %Tesla.Env{status: 200, body: body}
306 Streamer.get_topic_and_add_socket("user:notification", user)
307 {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)
309 assert_receive {:render_with_user, _, "notification.json", notif}
310 assert notif.activity.id == follow_activity.id
311 refute Streamer.filtered_by_user?(user, notif)
315 test "it sends to public authenticated" do
317 other_user = insert(:user)
319 Streamer.get_topic_and_add_socket("public", other_user)
321 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
322 assert_receive {:render_with_user, _, _, ^activity}
323 refute Streamer.filtered_by_user?(user, activity)
326 test "works for deletions" do
328 other_user = insert(:user)
329 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
331 Streamer.get_topic_and_add_socket("public", user)
333 {:ok, _} = CommonAPI.delete(activity.id, other_user)
334 activity_id = activity.id
335 assert_receive {:text, event}
336 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
339 test "it sends to public unauthenticated" do
342 Streamer.get_topic_and_add_socket("public", nil)
344 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
345 activity_id = activity.id
346 assert_receive {:text, event}
347 assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
348 assert %{"id" => ^activity_id} = Jason.decode!(payload)
350 {:ok, _} = CommonAPI.delete(activity.id, user)
351 assert_receive {:text, event}
352 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
355 describe "thread_containment" do
356 test "it filters to user if recipients invalid and thread containment is enabled" do
357 Pleroma.Config.put([:instance, :skip_thread_containment], false)
358 author = insert(:user)
360 User.follow(user, author, :follow_accept)
363 insert(:note_activity,
367 data: %{"to" => ["TEST-FFF"]}
371 Streamer.get_topic_and_add_socket("public", user)
372 Streamer.stream("public", activity)
373 assert_receive {:render_with_user, _, _, ^activity}
374 assert Streamer.filtered_by_user?(user, activity)
377 test "it sends message if recipients invalid and thread containment is disabled" do
378 Pleroma.Config.put([:instance, :skip_thread_containment], true)
379 author = insert(:user)
381 User.follow(user, author, :follow_accept)
384 insert(:note_activity,
388 data: %{"to" => ["TEST-FFF"]}
392 Streamer.get_topic_and_add_socket("public", user)
393 Streamer.stream("public", activity)
395 assert_receive {:render_with_user, _, _, ^activity}
396 refute Streamer.filtered_by_user?(user, activity)
399 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
400 Pleroma.Config.put([:instance, :skip_thread_containment], false)
401 author = insert(:user)
402 user = insert(:user, skip_thread_containment: true)
403 User.follow(user, author, :follow_accept)
406 insert(:note_activity,
410 data: %{"to" => ["TEST-FFF"]}
414 Streamer.get_topic_and_add_socket("public", user)
415 Streamer.stream("public", activity)
417 assert_receive {:render_with_user, _, _, ^activity}
418 refute Streamer.filtered_by_user?(user, activity)
423 test "it filters messages involving blocked users" do
425 blocked_user = insert(:user)
426 {:ok, _user_relationship} = User.block(user, blocked_user)
428 Streamer.get_topic_and_add_socket("public", user)
429 {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
430 assert_receive {:render_with_user, _, _, ^activity}
431 assert Streamer.filtered_by_user?(user, activity)
434 test "it filters messages transitively involving blocked users" do
435 blocker = insert(:user)
436 blockee = insert(:user)
437 friend = insert(:user)
439 Streamer.get_topic_and_add_socket("public", blocker)
441 {:ok, _user_relationship} = User.block(blocker, blockee)
443 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
445 assert_receive {:render_with_user, _, _, ^activity_one}
446 assert Streamer.filtered_by_user?(blocker, activity_one)
448 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
450 assert_receive {:render_with_user, _, _, ^activity_two}
451 assert Streamer.filtered_by_user?(blocker, activity_two)
453 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
455 assert_receive {:render_with_user, _, _, ^activity_three}
456 assert Streamer.filtered_by_user?(blocker, activity_three)
461 test "it doesn't send unwanted DMs to list" do
462 user_a = insert(:user)
463 user_b = insert(:user)
464 user_c = insert(:user)
466 {:ok, user_a} = User.follow(user_a, user_b)
468 {:ok, list} = List.create("Test", user_a)
469 {:ok, list} = List.follow(list, user_b)
471 Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})
474 CommonAPI.post(user_b, %{
475 status: "@#{user_c.nickname} Test",
482 test "it doesn't send unwanted private posts to list" do
483 user_a = insert(:user)
484 user_b = insert(:user)
486 {:ok, list} = List.create("Test", user_a)
487 {:ok, list} = List.follow(list, user_b)
489 Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})
492 CommonAPI.post(user_b, %{
494 visibility: "private"
500 test "it sends wanted private posts to list" do
501 user_a = insert(:user)
502 user_b = insert(:user)
504 {:ok, user_a} = User.follow(user_a, user_b)
506 {:ok, list} = List.create("Test", user_a)
507 {:ok, list} = List.follow(list, user_b)
509 Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})
512 CommonAPI.post(user_b, %{
514 visibility: "private"
517 assert_receive {:render_with_user, _, _, ^activity}
518 refute Streamer.filtered_by_user?(user_a, activity)
522 describe "muted reblogs" do
523 test "it filters muted reblogs" do
524 user1 = insert(:user)
525 user2 = insert(:user)
526 user3 = insert(:user)
527 CommonAPI.follow(user1, user2)
528 CommonAPI.hide_reblogs(user1, user2)
530 {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
532 Streamer.get_topic_and_add_socket("user", user1)
533 {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
534 assert_receive {:render_with_user, _, _, ^announce_activity}
535 assert Streamer.filtered_by_user?(user1, announce_activity)
538 test "it filters reblog notification for reblog-muted actors" do
539 user1 = insert(:user)
540 user2 = insert(:user)
541 CommonAPI.follow(user1, user2)
542 CommonAPI.hide_reblogs(user1, user2)
544 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
545 Streamer.get_topic_and_add_socket("user", user1)
546 {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)
548 assert_receive {:render_with_user, _, "notification.json", notif}
549 assert Streamer.filtered_by_user?(user1, notif)
552 test "it send non-reblog notification for reblog-muted actors" do
553 user1 = insert(:user)
554 user2 = insert(:user)
555 CommonAPI.follow(user1, user2)
556 CommonAPI.hide_reblogs(user1, user2)
558 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
559 Streamer.get_topic_and_add_socket("user", user1)
560 {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)
562 assert_receive {:render_with_user, _, "notification.json", notif}
563 refute Streamer.filtered_by_user?(user1, notif)
567 test "it filters posts from muted threads" do
569 user2 = insert(:user)
570 Streamer.get_topic_and_add_socket("user", user2)
571 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
572 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
573 {:ok, _} = CommonAPI.add_mute(user2, activity)
574 assert_receive {:render_with_user, _, _, ^activity}
575 assert Streamer.filtered_by_user?(user2, activity)
578 describe "direct streams" do
583 test "it sends conversation update to the 'direct' stream", %{} do
585 another_user = insert(:user)
587 Streamer.get_topic_and_add_socket("direct", user)
589 {:ok, _create_activity} =
590 CommonAPI.post(another_user, %{
591 status: "hey @#{user.nickname}",
595 assert_receive {:text, received_event}
597 assert %{"event" => "conversation", "payload" => received_payload} =
598 Jason.decode!(received_event)
600 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
601 [participation] = Participation.for_user(user)
602 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
605 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
607 another_user = insert(:user)
609 Streamer.get_topic_and_add_socket("direct", user)
611 {:ok, create_activity} =
612 CommonAPI.post(another_user, %{
613 status: "hi @#{user.nickname}",
617 create_activity_id = create_activity.id
618 assert_receive {:render_with_user, _, _, ^create_activity}
619 assert_receive {:text, received_conversation1}
620 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
622 {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
624 assert_receive {:text, received_event}
626 assert %{"event" => "delete", "payload" => ^create_activity_id} =
627 Jason.decode!(received_event)
632 test "it sends conversation update to the 'direct' stream when a message is deleted" do
634 another_user = insert(:user)
635 Streamer.get_topic_and_add_socket("direct", user)
637 {:ok, create_activity} =
638 CommonAPI.post(another_user, %{
639 status: "hi @#{user.nickname}",
643 {:ok, create_activity2} =
644 CommonAPI.post(another_user, %{
645 status: "hi @#{user.nickname} 2",
646 in_reply_to_status_id: create_activity.id,
650 assert_receive {:render_with_user, _, _, ^create_activity}
651 assert_receive {:render_with_user, _, _, ^create_activity2}
652 assert_receive {:text, received_conversation1}
653 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
654 assert_receive {:text, received_conversation1}
655 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
657 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
659 assert_receive {:text, received_event}
660 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
662 assert_receive {:text, received_event}
664 assert %{"event" => "conversation", "payload" => received_payload} =
665 Jason.decode!(received_event)
667 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
668 assert last_status["id"] == to_string(create_activity.id)