1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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/_ (unauthenticated)" do
25 test "allows public" do
26 assert {:ok, "public"} = Streamer.get_topic("public", nil, nil)
27 assert {:ok, "public:local"} = Streamer.get_topic("public:local", nil, nil)
28 assert {:ok, "public:media"} = Streamer.get_topic("public:media", nil, nil)
29 assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", nil, nil)
32 test "rejects local public streams if restricted_unauthenticated is on" do
33 clear_config([:restrict_unauthenticated, :timelines, :local], true)
35 assert {:error, :unauthorized} = Streamer.get_topic("public:local", nil, nil)
36 assert {:error, :unauthorized} = Streamer.get_topic("public:local:media", nil, nil)
39 test "rejects remote public streams if restricted_unauthenticated is on" do
40 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
42 assert {:error, :unauthorized} = Streamer.get_topic("public", nil, nil)
43 assert {:error, :unauthorized} = Streamer.get_topic("public:media", nil, nil)
45 assert {:error, :unauthorized} =
46 Streamer.get_topic("public:remote", nil, nil, %{"instance" => "lain.com"})
48 assert {:error, :unauthorized} =
49 Streamer.get_topic("public:remote:media", nil, nil, %{"instance" => "lain.com"})
52 test "allows instance streams" do
53 assert {:ok, "public:remote:lain.com"} =
54 Streamer.get_topic("public:remote", nil, nil, %{"instance" => "lain.com"})
56 assert {:ok, "public:remote:media:lain.com"} =
57 Streamer.get_topic("public:remote:media", nil, nil, %{"instance" => "lain.com"})
60 test "allows hashtag streams" do
61 assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", nil, nil, %{"tag" => "cofe"})
64 test "disallows user streams" do
65 assert {:error, _} = Streamer.get_topic("user", nil, nil)
66 assert {:error, _} = Streamer.get_topic("user:notification", nil, nil)
67 assert {:error, _} = Streamer.get_topic("direct", nil, nil)
70 test "disallows list streams" do
71 assert {:error, _} = Streamer.get_topic("list", nil, nil, %{"list" => 42})
75 describe "get_topic/_ (authenticated)" do
76 setup do: oauth_access(["read"])
78 test "allows public streams (regardless of OAuth token scopes)", %{
80 token: read_oauth_token
82 with oauth_token <- [nil, read_oauth_token] do
83 assert {:ok, "public"} = Streamer.get_topic("public", user, oauth_token)
84 assert {:ok, "public:local"} = Streamer.get_topic("public:local", user, oauth_token)
85 assert {:ok, "public:media"} = Streamer.get_topic("public:media", user, oauth_token)
87 assert {:ok, "public:local:media"} =
88 Streamer.get_topic("public:local:media", user, oauth_token)
92 test "allows local public streams if restricted_unauthenticated is on", %{
96 clear_config([:restrict_unauthenticated, :timelines, :local], true)
98 %{token: read_notifications_token} = oauth_access(["read:notifications"], user: user)
99 %{token: badly_scoped_token} = oauth_access(["irrelevant:scope"], user: user)
101 assert {:ok, "public:local"} = Streamer.get_topic("public:local", user, oauth_token)
103 assert {:ok, "public:local:media"} =
104 Streamer.get_topic("public:local:media", user, oauth_token)
106 for token <- [read_notifications_token, badly_scoped_token] do
107 assert {:error, :unauthorized} = Streamer.get_topic("public:local", user, token)
109 assert {:error, :unauthorized} = Streamer.get_topic("public:local:media", user, token)
113 test "allows remote public streams if restricted_unauthenticated is on", %{
117 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
119 %{token: read_notifications_token} = oauth_access(["read:notifications"], user: user)
120 %{token: badly_scoped_token} = oauth_access(["irrelevant:scope"], user: user)
122 assert {:ok, "public"} = Streamer.get_topic("public", user, oauth_token)
123 assert {:ok, "public:media"} = Streamer.get_topic("public:media", user, oauth_token)
125 assert {:ok, "public:remote:lain.com"} =
126 Streamer.get_topic("public:remote", user, oauth_token, %{"instance" => "lain.com"})
128 assert {:ok, "public:remote:media:lain.com"} =
129 Streamer.get_topic("public:remote:media", user, oauth_token, %{
130 "instance" => "lain.com"
133 for token <- [read_notifications_token, badly_scoped_token] do
134 assert {:error, :unauthorized} = Streamer.get_topic("public", user, token)
135 assert {:error, :unauthorized} = Streamer.get_topic("public:media", user, token)
137 assert {:error, :unauthorized} =
138 Streamer.get_topic("public:remote", user, token, %{
139 "instance" => "lain.com"
142 assert {:error, :unauthorized} =
143 Streamer.get_topic("public:remote:media", user, token, %{
144 "instance" => "lain.com"
149 test "allows user streams (with proper OAuth token scopes)", %{
151 token: read_oauth_token
153 %{token: read_notifications_token} = oauth_access(["read:notifications"], user: user)
154 %{token: read_statuses_token} = oauth_access(["read:statuses"], user: user)
155 %{token: badly_scoped_token} = oauth_access(["irrelevant:scope"], user: user)
157 expected_user_topic = "user:#{user.id}"
158 expected_notification_topic = "user:notification:#{user.id}"
159 expected_direct_topic = "direct:#{user.id}"
160 expected_pleroma_chat_topic = "user:pleroma_chat:#{user.id}"
162 for valid_user_token <- [read_oauth_token, read_statuses_token] do
163 assert {:ok, ^expected_user_topic} = Streamer.get_topic("user", user, valid_user_token)
165 assert {:ok, ^expected_direct_topic} =
166 Streamer.get_topic("direct", user, valid_user_token)
168 assert {:ok, ^expected_pleroma_chat_topic} =
169 Streamer.get_topic("user:pleroma_chat", user, valid_user_token)
172 for invalid_user_token <- [read_notifications_token, badly_scoped_token],
173 user_topic <- ["user", "direct", "user:pleroma_chat"] do
174 assert {:error, :unauthorized} = Streamer.get_topic(user_topic, user, invalid_user_token)
177 for valid_notification_token <- [read_oauth_token, read_notifications_token] do
178 assert {:ok, ^expected_notification_topic} =
179 Streamer.get_topic("user:notification", user, valid_notification_token)
182 for invalid_notification_token <- [read_statuses_token, badly_scoped_token] do
183 assert {:error, :unauthorized} =
184 Streamer.get_topic("user:notification", user, invalid_notification_token)
188 test "allows hashtag streams (regardless of OAuth token scopes)", %{
190 token: read_oauth_token
192 for oauth_token <- [nil, read_oauth_token] do
193 assert {:ok, "hashtag:cofe"} =
194 Streamer.get_topic("hashtag", user, oauth_token, %{"tag" => "cofe"})
198 test "disallows registering to another user's stream", %{user: user, token: read_oauth_token} do
199 another_user = insert(:user)
200 assert {:error, _} = Streamer.get_topic("user:#{another_user.id}", user, read_oauth_token)
203 Streamer.get_topic("user:notification:#{another_user.id}", user, read_oauth_token)
205 assert {:error, _} = Streamer.get_topic("direct:#{another_user.id}", user, read_oauth_token)
208 test "allows list stream that are owned by the user (with `read` or `read:lists` scopes)", %{
210 token: read_oauth_token
212 %{token: read_lists_token} = oauth_access(["read:lists"], user: user)
213 %{token: invalid_token} = oauth_access(["irrelevant:scope"], user: user)
214 {:ok, list} = List.create("Test", user)
216 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, read_oauth_token)
218 for valid_token <- [read_oauth_token, read_lists_token] do
219 assert {:ok, _} = Streamer.get_topic("list", user, valid_token, %{"list" => list.id})
222 assert {:error, _} = Streamer.get_topic("list", user, invalid_token, %{"list" => list.id})
225 test "disallows list stream that are not owned by the user", %{user: user, token: oauth_token} do
226 another_user = insert(:user)
227 {:ok, list} = List.create("Test", another_user)
229 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, oauth_token)
230 assert {:error, _} = Streamer.get_topic("list", user, oauth_token, %{"list" => list.id})
234 describe "user streams" do
236 %{user: user, token: token} = oauth_access(["read"])
237 notify = insert(:notification, user: user, activity: build(:note_activity))
238 {:ok, %{user: user, notify: notify, token: token}}
241 test "it streams the user's post in the 'user' stream", %{user: user, token: oauth_token} do
242 Streamer.get_topic_and_add_socket("user", user, oauth_token)
243 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
245 assert_receive {:render_with_user, _, _, ^activity}
246 refute Streamer.filtered_by_user?(user, activity)
249 test "it streams boosts of the user in the 'user' stream", %{user: user, token: oauth_token} do
250 Streamer.get_topic_and_add_socket("user", user, oauth_token)
252 other_user = insert(:user)
253 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
254 {:ok, announce} = CommonAPI.repeat(activity.id, user)
256 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
257 refute Streamer.filtered_by_user?(user, announce)
260 test "it does not stream announces of the user's own posts in the 'user' stream", %{
264 Streamer.get_topic_and_add_socket("user", user, oauth_token)
266 other_user = insert(:user)
267 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
268 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
270 assert Streamer.filtered_by_user?(user, announce)
273 test "it does stream notifications announces of the user's own posts in the 'user' stream", %{
277 Streamer.get_topic_and_add_socket("user", user, oauth_token)
279 other_user = insert(:user)
280 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
281 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
285 |> Repo.get_by(%{user_id: user.id, activity_id: announce.id})
286 |> Repo.preload(:activity)
288 refute Streamer.filtered_by_user?(user, notification)
291 test "it streams boosts of mastodon user in the 'user' stream", %{
295 Streamer.get_topic_and_add_socket("user", user, oauth_token)
297 other_user = insert(:user)
298 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
301 File.read!("test/fixtures/mastodon-announce.json")
303 |> Map.put("object", activity.data["object"])
304 |> Map.put("actor", user.ap_id)
306 {:ok, %Pleroma.Activity{data: _data, local: false} = announce} =
307 Pleroma.Web.ActivityPub.Transmogrifier.handle_incoming(data)
309 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
310 refute Streamer.filtered_by_user?(user, announce)
313 test "it sends notify to in the 'user' stream", %{
318 Streamer.get_topic_and_add_socket("user", user, oauth_token)
319 Streamer.stream("user", notify)
321 assert_receive {:render_with_user, _, _, ^notify}
322 refute Streamer.filtered_by_user?(user, notify)
325 test "it sends notify to in the 'user:notification' stream", %{
330 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
331 Streamer.stream("user:notification", notify)
333 assert_receive {:render_with_user, _, _, ^notify}
334 refute Streamer.filtered_by_user?(user, notify)
337 test "it sends chat messages to the 'user:pleroma_chat' stream", %{
341 other_user = insert(:user)
343 {:ok, create_activity} =
344 CommonAPI.post_chat_message(other_user, user, "hey cirno", idempotency_key: "123")
346 object = Object.normalize(create_activity, fetch: false)
347 chat = Chat.get(user.id, other_user.ap_id)
348 cm_ref = MessageReference.for_chat_and_object(chat, object)
349 cm_ref = %{cm_ref | chat: chat, object: object}
351 Streamer.get_topic_and_add_socket("user:pleroma_chat", user, oauth_token)
352 Streamer.stream("user:pleroma_chat", {user, cm_ref})
354 text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
356 assert text =~ "hey cirno"
357 assert_receive {:text, ^text}
360 test "it sends chat messages to the 'user' stream", %{user: user, token: oauth_token} do
361 other_user = insert(:user)
363 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
364 object = Object.normalize(create_activity, fetch: false)
365 chat = Chat.get(user.id, other_user.ap_id)
366 cm_ref = MessageReference.for_chat_and_object(chat, object)
367 cm_ref = %{cm_ref | chat: chat, object: object}
369 Streamer.get_topic_and_add_socket("user", user, oauth_token)
370 Streamer.stream("user", {user, cm_ref})
372 text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
374 assert text =~ "hey cirno"
375 assert_receive {:text, ^text}
378 test "it sends chat message notifications to the 'user:notification' stream", %{
382 other_user = insert(:user)
384 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey")
387 Repo.get_by(Pleroma.Notification, user_id: user.id, activity_id: create_activity.id)
388 |> Repo.preload(:activity)
390 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
391 Streamer.stream("user:notification", notify)
393 assert_receive {:render_with_user, _, _, ^notify}
394 refute Streamer.filtered_by_user?(user, notify)
397 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
401 blocked = insert(:user)
402 {:ok, _user_relationship} = User.block(user, blocked)
404 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
406 {:ok, activity} = CommonAPI.post(user, %{status: ":("})
407 {:ok, _} = CommonAPI.favorite(blocked, activity.id)
412 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
416 user2 = insert(:user)
418 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
419 {:ok, _} = CommonAPI.add_mute(user, activity)
421 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
423 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
426 assert Streamer.filtered_by_user?(user, favorite_activity)
429 test "it sends favorite to 'user:notification' stream'", %{
433 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
435 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
436 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
437 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
439 assert_receive {:render_with_user, _, "notification.json", notif}
440 assert notif.activity.id == favorite_activity.id
441 refute Streamer.filtered_by_user?(user, notif)
444 test "it doesn't send the 'user:notification' stream' when a domain is blocked", %{
448 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
450 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
451 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
452 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
453 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
456 assert Streamer.filtered_by_user?(user, favorite_activity)
459 test "it sends follow activities to the 'user:notification' stream", %{
463 user2 = insert(:user)
465 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
466 {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)
468 assert_receive {:render_with_user, _, "notification.json", notif}
469 assert notif.activity.id == follow_activity.id
470 refute Streamer.filtered_by_user?(user, notif)
473 test "it sends follow relationships updates to the 'user' stream", %{
478 other_user = insert(:user)
479 other_user_id = other_user.id
481 Streamer.get_topic_and_add_socket("user", user, oauth_token)
482 {:ok, _follower, _followed, _follow_activity} = CommonAPI.follow(user, other_user)
484 assert_receive {:text, event}
486 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
491 "follower_count" => 0,
492 "following_count" => 0,
496 "follower_count" => 0,
497 "following_count" => 0,
498 "id" => ^other_user_id
500 "state" => "follow_pending"
501 } = Jason.decode!(payload)
503 assert_receive {:text, event}
505 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
510 "follower_count" => 0,
511 "following_count" => 1,
515 "follower_count" => 1,
516 "following_count" => 0,
517 "id" => ^other_user_id
519 "state" => "follow_accept"
520 } = Jason.decode!(payload)
523 test "it streams edits in the 'user' stream", %{user: user, token: oauth_token} do
524 sender = insert(:user)
525 {:ok, _, _, _} = CommonAPI.follow(user, sender)
527 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
529 Streamer.get_topic_and_add_socket("user", user, oauth_token)
530 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
531 create = Pleroma.Activity.get_create_by_object_ap_id_with_object(activity.object.data["id"])
533 assert_receive {:render_with_user, _, "status_update.json", ^create}
534 refute Streamer.filtered_by_user?(user, edited)
537 test "it streams own edits in the 'user' stream", %{user: user, token: oauth_token} do
538 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
540 Streamer.get_topic_and_add_socket("user", user, oauth_token)
541 {:ok, edited} = CommonAPI.update(user, activity, %{status: "mew mew"})
542 create = Pleroma.Activity.get_create_by_object_ap_id_with_object(activity.object.data["id"])
544 assert_receive {:render_with_user, _, "status_update.json", ^create}
545 refute Streamer.filtered_by_user?(user, edited)
549 describe "public streams" do
550 test "it sends to public (authenticated)" do
551 %{user: user, token: oauth_token} = oauth_access(["read"])
552 other_user = insert(:user)
554 Streamer.get_topic_and_add_socket("public", user, oauth_token)
556 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
557 assert_receive {:render_with_user, _, _, ^activity}
558 refute Streamer.filtered_by_user?(other_user, activity)
561 test "it sends to public (unauthenticated)" do
564 Streamer.get_topic_and_add_socket("public", nil, nil)
566 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
567 activity_id = activity.id
568 assert_receive {:text, event}
569 assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
570 assert %{"id" => ^activity_id} = Jason.decode!(payload)
572 {:ok, _} = CommonAPI.delete(activity.id, user)
573 assert_receive {:text, event}
574 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
577 test "handles deletions" do
578 %{user: user, token: oauth_token} = oauth_access(["read"])
579 other_user = insert(:user)
580 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
582 Streamer.get_topic_and_add_socket("public", user, oauth_token)
584 {:ok, _} = CommonAPI.delete(activity.id, other_user)
585 activity_id = activity.id
586 assert_receive {:text, event}
587 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
590 test "it streams edits in the 'public' stream" do
591 sender = insert(:user)
593 Streamer.get_topic_and_add_socket("public", nil, nil)
594 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
595 assert_receive {:text, _}
597 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
599 edited = Pleroma.Activity.normalize(edited)
601 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
603 assert_receive {:text, event}
604 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
605 assert %{"id" => ^activity_id} = Jason.decode!(payload)
606 refute Streamer.filtered_by_user?(sender, edited)
609 test "it streams multiple edits in the 'public' stream correctly" do
610 sender = insert(:user)
612 Streamer.get_topic_and_add_socket("public", nil, nil)
613 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
614 assert_receive {:text, _}
616 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
618 edited = Pleroma.Activity.normalize(edited)
620 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
622 assert_receive {:text, event}
623 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
624 assert %{"id" => ^activity_id} = Jason.decode!(payload)
625 refute Streamer.filtered_by_user?(sender, edited)
627 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew 2"})
629 edited = Pleroma.Activity.normalize(edited)
631 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
632 assert_receive {:text, event}
633 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
634 assert %{"id" => ^activity_id, "content" => "mew mew 2"} = Jason.decode!(payload)
635 refute Streamer.filtered_by_user?(sender, edited)
639 describe "thread_containment/2" do
640 test "it filters to user if recipients invalid and thread containment is enabled" do
641 clear_config([:instance, :skip_thread_containment], false)
642 author = insert(:user)
643 %{user: user, token: oauth_token} = oauth_access(["read"])
644 User.follow(user, author, :follow_accept)
647 insert(:note_activity,
651 data: %{"to" => ["TEST-FFF"]}
655 Streamer.get_topic_and_add_socket("public", user, oauth_token)
656 Streamer.stream("public", activity)
657 assert_receive {:render_with_user, _, _, ^activity}
658 assert Streamer.filtered_by_user?(user, activity)
661 test "it sends message if recipients invalid and thread containment is disabled" do
662 clear_config([:instance, :skip_thread_containment], true)
663 author = insert(:user)
664 %{user: user, token: oauth_token} = oauth_access(["read"])
665 User.follow(user, author, :follow_accept)
668 insert(:note_activity,
672 data: %{"to" => ["TEST-FFF"]}
676 Streamer.get_topic_and_add_socket("public", user, oauth_token)
677 Streamer.stream("public", activity)
679 assert_receive {:render_with_user, _, _, ^activity}
680 refute Streamer.filtered_by_user?(user, activity)
683 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
684 clear_config([:instance, :skip_thread_containment], false)
685 author = insert(:user)
686 user = insert(:user, skip_thread_containment: true)
687 %{token: oauth_token} = oauth_access(["read"], user: user)
688 User.follow(user, author, :follow_accept)
691 insert(:note_activity,
695 data: %{"to" => ["TEST-FFF"]}
699 Streamer.get_topic_and_add_socket("public", user, oauth_token)
700 Streamer.stream("public", activity)
702 assert_receive {:render_with_user, _, _, ^activity}
703 refute Streamer.filtered_by_user?(user, activity)
708 setup do: oauth_access(["read"])
710 test "it filters messages involving blocked users", %{user: user, token: oauth_token} do
711 blocked_user = insert(:user)
712 {:ok, _user_relationship} = User.block(user, blocked_user)
714 Streamer.get_topic_and_add_socket("public", user, oauth_token)
715 {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
716 assert_receive {:render_with_user, _, _, ^activity}
717 assert Streamer.filtered_by_user?(user, activity)
720 test "it filters messages transitively involving blocked users", %{
724 blockee = insert(:user)
725 friend = insert(:user)
727 Streamer.get_topic_and_add_socket("public", blocker, blocker_token)
729 {:ok, _user_relationship} = User.block(blocker, blockee)
731 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
733 assert_receive {:render_with_user, _, _, ^activity_one}
734 assert Streamer.filtered_by_user?(blocker, activity_one)
736 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
738 assert_receive {:render_with_user, _, _, ^activity_two}
739 assert Streamer.filtered_by_user?(blocker, activity_two)
741 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
743 assert_receive {:render_with_user, _, _, ^activity_three}
744 assert Streamer.filtered_by_user?(blocker, activity_three)
749 setup do: oauth_access(["read"])
751 test "it doesn't send unwanted DMs to list", %{user: user_a, token: user_a_token} do
752 user_b = insert(:user)
753 user_c = insert(:user)
755 {:ok, user_a, user_b} = User.follow(user_a, user_b)
757 {:ok, list} = List.create("Test", user_a)
758 {:ok, list} = List.follow(list, user_b)
760 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
763 CommonAPI.post(user_b, %{
764 status: "@#{user_c.nickname} Test",
771 test "it doesn't send unwanted private posts to list", %{user: user_a, token: user_a_token} do
772 user_b = insert(:user)
774 {:ok, list} = List.create("Test", user_a)
775 {:ok, list} = List.follow(list, user_b)
777 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
780 CommonAPI.post(user_b, %{
782 visibility: "private"
788 test "it sends wanted private posts to list", %{user: user_a, token: user_a_token} do
789 user_b = insert(:user)
791 {:ok, user_a, user_b} = User.follow(user_a, user_b)
793 {:ok, list} = List.create("Test", user_a)
794 {:ok, list} = List.follow(list, user_b)
796 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
799 CommonAPI.post(user_b, %{
801 visibility: "private"
804 assert_receive {:render_with_user, _, _, ^activity}
805 refute Streamer.filtered_by_user?(user_a, activity)
809 describe "muted reblogs" do
810 setup do: oauth_access(["read"])
812 test "it filters muted reblogs", %{user: user1, token: user1_token} do
813 user2 = insert(:user)
814 user3 = insert(:user)
815 CommonAPI.follow(user1, user2)
816 CommonAPI.hide_reblogs(user1, user2)
818 {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
820 Streamer.get_topic_and_add_socket("user", user1, user1_token)
821 {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
822 assert_receive {:render_with_user, _, _, ^announce_activity}
823 assert Streamer.filtered_by_user?(user1, announce_activity)
826 test "it filters reblog notification for reblog-muted actors", %{
830 user2 = insert(:user)
831 CommonAPI.follow(user1, user2)
832 CommonAPI.hide_reblogs(user1, user2)
834 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
835 Streamer.get_topic_and_add_socket("user", user1, user1_token)
836 {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)
838 assert_receive {:render_with_user, _, "notification.json", notif}
839 assert Streamer.filtered_by_user?(user1, notif)
842 test "it send non-reblog notification for reblog-muted actors", %{
846 user2 = insert(:user)
847 CommonAPI.follow(user1, user2)
848 CommonAPI.hide_reblogs(user1, user2)
850 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
851 Streamer.get_topic_and_add_socket("user", user1, user1_token)
852 {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)
854 assert_receive {:render_with_user, _, "notification.json", notif}
855 refute Streamer.filtered_by_user?(user1, notif)
859 describe "muted threads" do
860 test "it filters posts from muted threads" do
862 %{user: user2, token: user2_token} = oauth_access(["read"])
863 Streamer.get_topic_and_add_socket("user", user2, user2_token)
865 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
866 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
867 {:ok, _} = CommonAPI.add_mute(user2, activity)
869 assert_receive {:render_with_user, _, _, ^activity}
870 assert Streamer.filtered_by_user?(user2, activity)
874 describe "direct streams" do
875 setup do: oauth_access(["read"])
877 test "it sends conversation update to the 'direct' stream", %{user: user, token: oauth_token} do
878 another_user = insert(:user)
880 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
882 {:ok, _create_activity} =
883 CommonAPI.post(another_user, %{
884 status: "hey @#{user.nickname}",
888 assert_receive {:text, received_event}
890 assert %{"event" => "conversation", "payload" => received_payload} =
891 Jason.decode!(received_event)
893 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
894 [participation] = Participation.for_user(user)
895 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
898 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted",
899 %{user: user, token: oauth_token} do
900 another_user = insert(:user)
902 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
904 {:ok, create_activity} =
905 CommonAPI.post(another_user, %{
906 status: "hi @#{user.nickname}",
910 create_activity_id = create_activity.id
911 assert_receive {:render_with_user, _, _, ^create_activity}
912 assert_receive {:text, received_conversation1}
913 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
915 {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
917 assert_receive {:text, received_event}
919 assert %{"event" => "delete", "payload" => ^create_activity_id} =
920 Jason.decode!(received_event)
926 test "it sends conversation update to the 'direct' stream when a message is deleted", %{
930 another_user = insert(:user)
931 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
933 {:ok, create_activity} =
934 CommonAPI.post(another_user, %{
935 status: "hi @#{user.nickname}",
939 {:ok, create_activity2} =
940 CommonAPI.post(another_user, %{
941 status: "hi @#{user.nickname} 2",
942 in_reply_to_status_id: create_activity.id,
946 assert_receive {:render_with_user, _, _, ^create_activity}
947 assert_receive {:render_with_user, _, _, ^create_activity2}
948 assert_receive {:text, received_conversation1}
949 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
950 assert_receive {:text, received_conversation1}
951 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
953 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
955 assert_receive {:text, received_event}
956 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
958 assert_receive {:text, received_event}
960 assert %{"event" => "conversation", "payload" => received_payload} =
961 Jason.decode!(received_event)
963 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
964 assert last_status["id"] == to_string(create_activity.id)
968 describe "stop streaming if token got revoked" do
970 child_proc = fn start, finalize ->
975 {StreamerTest, :ready} ->
976 assert_receive {:render_with_user, _, "update.json", _}
979 {StreamerTest, :revoked} -> finalize.()
985 starter = fn user, token ->
986 fn -> Streamer.get_topic_and_add_socket("user", user, token) end
989 hit = fn -> assert_receive :close end
990 miss = fn -> refute_receive :close end
992 send_all = fn tasks, thing -> Enum.each(tasks, &send(&1.pid, thing)) end
995 child_proc: child_proc,
1003 test "do not revoke other tokens", %{
1004 child_proc: child_proc,
1010 %{user: user, token: token} = oauth_access(["read"])
1011 %{token: token2} = oauth_access(["read"], user: user)
1012 %{user: user2, token: user2_token} = oauth_access(["read"])
1014 post_user = insert(:user)
1015 CommonAPI.follow(user, post_user)
1016 CommonAPI.follow(user2, post_user)
1019 Task.async(child_proc.(starter.(user, token), hit)),
1020 Task.async(child_proc.(starter.(user, token2), miss)),
1021 Task.async(child_proc.(starter.(user2, user2_token), miss))
1025 CommonAPI.post(post_user, %{
1029 send_all.(tasks, {StreamerTest, :ready})
1031 Pleroma.Web.OAuth.Token.Strategy.Revoke.revoke(token)
1033 send_all.(tasks, {StreamerTest, :revoked})
1035 Enum.each(tasks, &Task.await/1)
1038 test "revoke all streams for this token", %{
1039 child_proc: child_proc,
1044 %{user: user, token: token} = oauth_access(["read"])
1046 post_user = insert(:user)
1047 CommonAPI.follow(user, post_user)
1050 Task.async(child_proc.(starter.(user, token), hit)),
1051 Task.async(child_proc.(starter.(user, token), hit))
1055 CommonAPI.post(post_user, %{
1059 send_all.(tasks, {StreamerTest, :ready})
1061 Pleroma.Web.OAuth.Token.Strategy.Revoke.revoke(token)
1063 send_all.(tasks, {StreamerTest, :revoked})
1065 Enum.each(tasks, &Task.await/1)