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 streams boosts of mastodon user in the 'user' stream", %{user: user} do
132 Streamer.get_topic_and_add_socket("user", user)
134 other_user = insert(:user)
135 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
138 File.read!("test/fixtures/mastodon-announce.json")
140 |> Map.put("object", activity.data["object"])
141 |> Map.put("actor", user.ap_id)
143 {:ok, %Pleroma.Activity{data: _data, local: false} = announce} =
144 Pleroma.Web.ActivityPub.Transmogrifier.handle_incoming(data)
146 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
147 refute Streamer.filtered_by_user?(user, announce)
150 test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
151 Streamer.get_topic_and_add_socket("user", user)
152 Streamer.stream("user", notify)
153 assert_receive {:render_with_user, _, _, ^notify}
154 refute Streamer.filtered_by_user?(user, notify)
157 test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
158 Streamer.get_topic_and_add_socket("user:notification", user)
159 Streamer.stream("user:notification", notify)
160 assert_receive {:render_with_user, _, _, ^notify}
161 refute Streamer.filtered_by_user?(user, notify)
164 test "it sends chat messages to the 'user:pleroma_chat' stream", %{user: user} do
165 other_user = insert(:user)
167 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
168 object = Object.normalize(create_activity, false)
169 chat = Chat.get(user.id, other_user.ap_id)
170 cm_ref = MessageReference.for_chat_and_object(chat, object)
171 cm_ref = %{cm_ref | chat: chat, object: object}
173 Streamer.get_topic_and_add_socket("user:pleroma_chat", user)
174 Streamer.stream("user:pleroma_chat", {user, cm_ref})
176 text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
178 assert text =~ "hey cirno"
179 assert_receive {:text, ^text}
182 test "it sends chat messages to the 'user' stream", %{user: user} do
183 other_user = insert(:user)
185 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
186 object = Object.normalize(create_activity, false)
187 chat = Chat.get(user.id, other_user.ap_id)
188 cm_ref = MessageReference.for_chat_and_object(chat, object)
189 cm_ref = %{cm_ref | chat: chat, object: object}
191 Streamer.get_topic_and_add_socket("user", user)
192 Streamer.stream("user", {user, cm_ref})
194 text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
196 assert text =~ "hey cirno"
197 assert_receive {:text, ^text}
200 test "it sends chat message notifications to the 'user:notification' stream", %{user: user} do
201 other_user = insert(:user)
203 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey")
206 Repo.get_by(Pleroma.Notification, user_id: user.id, activity_id: create_activity.id)
207 |> Repo.preload(:activity)
209 Streamer.get_topic_and_add_socket("user:notification", user)
210 Streamer.stream("user:notification", notify)
211 assert_receive {:render_with_user, _, _, ^notify}
212 refute Streamer.filtered_by_user?(user, notify)
215 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
218 blocked = insert(:user)
219 {:ok, _user_relationship} = User.block(user, blocked)
221 Streamer.get_topic_and_add_socket("user:notification", user)
223 {:ok, activity} = CommonAPI.post(user, %{status: ":("})
224 {:ok, _} = CommonAPI.favorite(blocked, activity.id)
229 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
232 user2 = insert(:user)
234 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
235 {:ok, _} = CommonAPI.add_mute(user, activity)
237 Streamer.get_topic_and_add_socket("user:notification", user)
239 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
242 assert Streamer.filtered_by_user?(user, favorite_activity)
245 test "it sends favorite to 'user:notification' stream'", %{
248 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
250 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
251 Streamer.get_topic_and_add_socket("user:notification", user)
252 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
254 assert_receive {:render_with_user, _, "notification.json", notif}
255 assert notif.activity.id == favorite_activity.id
256 refute Streamer.filtered_by_user?(user, notif)
259 test "it doesn't send the 'user:notification' stream' when a domain is blocked", %{
262 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
264 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
265 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
266 Streamer.get_topic_and_add_socket("user:notification", user)
267 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
270 assert Streamer.filtered_by_user?(user, favorite_activity)
273 test "it sends follow activities to the 'user:notification' stream", %{
276 user_url = user.ap_id
277 user2 = insert(:user)
280 File.read!("test/fixtures/users_mock/localhost.json")
281 |> String.replace("{{nickname}}", user.nickname)
284 Tesla.Mock.mock_global(fn
285 %{method: :get, url: ^user_url} ->
286 %Tesla.Env{status: 200, body: body}
289 Streamer.get_topic_and_add_socket("user:notification", user)
290 {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)
292 assert_receive {:render_with_user, _, "notification.json", notif}
293 assert notif.activity.id == follow_activity.id
294 refute Streamer.filtered_by_user?(user, notif)
298 test "it sends to public authenticated" do
300 other_user = insert(:user)
302 Streamer.get_topic_and_add_socket("public", other_user)
304 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
305 assert_receive {:render_with_user, _, _, ^activity}
306 refute Streamer.filtered_by_user?(user, activity)
309 test "works for deletions" do
311 other_user = insert(:user)
312 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
314 Streamer.get_topic_and_add_socket("public", user)
316 {:ok, _} = CommonAPI.delete(activity.id, other_user)
317 activity_id = activity.id
318 assert_receive {:text, event}
319 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
322 test "it sends to public unauthenticated" do
325 Streamer.get_topic_and_add_socket("public", nil)
327 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
328 activity_id = activity.id
329 assert_receive {:text, event}
330 assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
331 assert %{"id" => ^activity_id} = Jason.decode!(payload)
333 {:ok, _} = CommonAPI.delete(activity.id, user)
334 assert_receive {:text, event}
335 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
338 describe "thread_containment" do
339 test "it filters to user if recipients invalid and thread containment is enabled" do
340 Pleroma.Config.put([:instance, :skip_thread_containment], false)
341 author = insert(:user)
343 User.follow(user, author, :follow_accept)
346 insert(:note_activity,
350 data: %{"to" => ["TEST-FFF"]}
354 Streamer.get_topic_and_add_socket("public", user)
355 Streamer.stream("public", activity)
356 assert_receive {:render_with_user, _, _, ^activity}
357 assert Streamer.filtered_by_user?(user, activity)
360 test "it sends message if recipients invalid and thread containment is disabled" do
361 Pleroma.Config.put([:instance, :skip_thread_containment], true)
362 author = insert(:user)
364 User.follow(user, author, :follow_accept)
367 insert(:note_activity,
371 data: %{"to" => ["TEST-FFF"]}
375 Streamer.get_topic_and_add_socket("public", user)
376 Streamer.stream("public", activity)
378 assert_receive {:render_with_user, _, _, ^activity}
379 refute Streamer.filtered_by_user?(user, activity)
382 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
383 Pleroma.Config.put([:instance, :skip_thread_containment], false)
384 author = insert(:user)
385 user = insert(:user, skip_thread_containment: true)
386 User.follow(user, author, :follow_accept)
389 insert(:note_activity,
393 data: %{"to" => ["TEST-FFF"]}
397 Streamer.get_topic_and_add_socket("public", user)
398 Streamer.stream("public", activity)
400 assert_receive {:render_with_user, _, _, ^activity}
401 refute Streamer.filtered_by_user?(user, activity)
406 test "it filters messages involving blocked users" do
408 blocked_user = insert(:user)
409 {:ok, _user_relationship} = User.block(user, blocked_user)
411 Streamer.get_topic_and_add_socket("public", user)
412 {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
413 assert_receive {:render_with_user, _, _, ^activity}
414 assert Streamer.filtered_by_user?(user, activity)
417 test "it filters messages transitively involving blocked users" do
418 blocker = insert(:user)
419 blockee = insert(:user)
420 friend = insert(:user)
422 Streamer.get_topic_and_add_socket("public", blocker)
424 {:ok, _user_relationship} = User.block(blocker, blockee)
426 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
428 assert_receive {:render_with_user, _, _, ^activity_one}
429 assert Streamer.filtered_by_user?(blocker, activity_one)
431 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
433 assert_receive {:render_with_user, _, _, ^activity_two}
434 assert Streamer.filtered_by_user?(blocker, activity_two)
436 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
438 assert_receive {:render_with_user, _, _, ^activity_three}
439 assert Streamer.filtered_by_user?(blocker, activity_three)
444 test "it doesn't send unwanted DMs to list" do
445 user_a = insert(:user)
446 user_b = insert(:user)
447 user_c = insert(:user)
449 {:ok, user_a} = User.follow(user_a, user_b)
451 {:ok, list} = List.create("Test", user_a)
452 {:ok, list} = List.follow(list, user_b)
454 Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})
457 CommonAPI.post(user_b, %{
458 status: "@#{user_c.nickname} Test",
465 test "it doesn't send unwanted private posts to list" do
466 user_a = insert(:user)
467 user_b = insert(:user)
469 {:ok, list} = List.create("Test", user_a)
470 {:ok, list} = List.follow(list, user_b)
472 Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})
475 CommonAPI.post(user_b, %{
477 visibility: "private"
483 test "it sends wanted private posts to list" do
484 user_a = insert(:user)
485 user_b = insert(:user)
487 {:ok, user_a} = User.follow(user_a, user_b)
489 {:ok, list} = List.create("Test", user_a)
490 {:ok, list} = List.follow(list, user_b)
492 Streamer.get_topic_and_add_socket("list", user_a, %{"list" => list.id})
495 CommonAPI.post(user_b, %{
497 visibility: "private"
500 assert_receive {:render_with_user, _, _, ^activity}
501 refute Streamer.filtered_by_user?(user_a, activity)
505 describe "muted reblogs" do
506 test "it filters muted reblogs" do
507 user1 = insert(:user)
508 user2 = insert(:user)
509 user3 = insert(:user)
510 CommonAPI.follow(user1, user2)
511 CommonAPI.hide_reblogs(user1, user2)
513 {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
515 Streamer.get_topic_and_add_socket("user", user1)
516 {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
517 assert_receive {:render_with_user, _, _, ^announce_activity}
518 assert Streamer.filtered_by_user?(user1, announce_activity)
521 test "it filters reblog notification for reblog-muted actors" do
522 user1 = insert(:user)
523 user2 = insert(:user)
524 CommonAPI.follow(user1, user2)
525 CommonAPI.hide_reblogs(user1, user2)
527 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
528 Streamer.get_topic_and_add_socket("user", user1)
529 {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)
531 assert_receive {:render_with_user, _, "notification.json", notif}
532 assert Streamer.filtered_by_user?(user1, notif)
535 test "it send non-reblog notification for reblog-muted actors" do
536 user1 = insert(:user)
537 user2 = insert(:user)
538 CommonAPI.follow(user1, user2)
539 CommonAPI.hide_reblogs(user1, user2)
541 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
542 Streamer.get_topic_and_add_socket("user", user1)
543 {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)
545 assert_receive {:render_with_user, _, "notification.json", notif}
546 refute Streamer.filtered_by_user?(user1, notif)
550 test "it filters posts from muted threads" do
552 user2 = insert(:user)
553 Streamer.get_topic_and_add_socket("user", user2)
554 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
555 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
556 {:ok, _} = CommonAPI.add_mute(user2, activity)
557 assert_receive {:render_with_user, _, _, ^activity}
558 assert Streamer.filtered_by_user?(user2, activity)
561 describe "direct streams" do
566 test "it sends conversation update to the 'direct' stream", %{} do
568 another_user = insert(:user)
570 Streamer.get_topic_and_add_socket("direct", user)
572 {:ok, _create_activity} =
573 CommonAPI.post(another_user, %{
574 status: "hey @#{user.nickname}",
578 assert_receive {:text, received_event}
580 assert %{"event" => "conversation", "payload" => received_payload} =
581 Jason.decode!(received_event)
583 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
584 [participation] = Participation.for_user(user)
585 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
588 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
590 another_user = insert(:user)
592 Streamer.get_topic_and_add_socket("direct", user)
594 {:ok, create_activity} =
595 CommonAPI.post(another_user, %{
596 status: "hi @#{user.nickname}",
600 create_activity_id = create_activity.id
601 assert_receive {:render_with_user, _, _, ^create_activity}
602 assert_receive {:text, received_conversation1}
603 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
605 {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
607 assert_receive {:text, received_event}
609 assert %{"event" => "delete", "payload" => ^create_activity_id} =
610 Jason.decode!(received_event)
615 test "it sends conversation update to the 'direct' stream when a message is deleted" do
617 another_user = insert(:user)
618 Streamer.get_topic_and_add_socket("direct", user)
620 {:ok, create_activity} =
621 CommonAPI.post(another_user, %{
622 status: "hi @#{user.nickname}",
626 {:ok, create_activity2} =
627 CommonAPI.post(another_user, %{
628 status: "hi @#{user.nickname} 2",
629 in_reply_to_status_id: create_activity.id,
633 assert_receive {:render_with_user, _, _, ^create_activity}
634 assert_receive {:render_with_user, _, _, ^create_activity2}
635 assert_receive {:text, received_conversation1}
636 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
637 assert_receive {:text, received_conversation1}
638 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
640 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
642 assert_receive {:text, received_event}
643 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
645 assert_receive {:text, received_event}
647 assert %{"event" => "conversation", "payload" => received_payload} =
648 Jason.decode!(received_event)
650 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
651 assert last_status["id"] == to_string(create_activity.id)