1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.StreamerTest do
6 use Pleroma.DataCase, async: false
10 alias Pleroma.Conversation.Participation
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Streamer
16 @moduletag needs_streamer: true, capture_log: true
18 setup do: clear_config([:instance, :skip_thread_containment])
20 describe "get_topic/_ (unauthenticated)" do
21 test "allows public" do
22 assert {:ok, "public"} = Streamer.get_topic("public", nil, nil)
23 assert {:ok, "public:local"} = Streamer.get_topic("public:local", nil, nil)
24 assert {:ok, "public:media"} = Streamer.get_topic("public:media", nil, nil)
25 assert {:ok, "public:local:media"} = Streamer.get_topic("public:local:media", nil, nil)
28 test "allows instance streams" do
29 assert {:ok, "public:remote:lain.com"} =
30 Streamer.get_topic("public:remote", nil, nil, %{"instance" => "lain.com"})
32 assert {:ok, "public:remote:media:lain.com"} =
33 Streamer.get_topic("public:remote:media", nil, nil, %{"instance" => "lain.com"})
36 test "allows hashtag streams" do
37 assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", nil, nil, %{"tag" => "cofe"})
40 test "disallows user streams" do
41 assert {:error, _} = Streamer.get_topic("user", nil, nil)
42 assert {:error, _} = Streamer.get_topic("user:notification", nil, nil)
43 assert {:error, _} = Streamer.get_topic("direct", nil, nil)
46 test "disallows list streams" do
47 assert {:error, _} = Streamer.get_topic("list", nil, nil, %{"list" => 42})
51 describe "get_topic/_ (authenticated)" do
52 setup do: oauth_access(["read"])
54 test "allows public streams (regardless of OAuth token scopes)", %{
56 token: read_oauth_token
58 with oauth_token <- [nil, read_oauth_token] do
59 assert {:ok, "public"} = Streamer.get_topic("public", user, oauth_token)
60 assert {:ok, "public:local"} = Streamer.get_topic("public:local", user, oauth_token)
61 assert {:ok, "public:media"} = Streamer.get_topic("public:media", user, oauth_token)
63 assert {:ok, "public:local:media"} =
64 Streamer.get_topic("public:local:media", user, oauth_token)
68 test "allows user streams (with proper OAuth token scopes)", %{
70 token: read_oauth_token
72 %{token: read_notifications_token} = oauth_access(["read:notifications"], user: user)
73 %{token: read_statuses_token} = oauth_access(["read:statuses"], user: user)
74 %{token: badly_scoped_token} = oauth_access(["irrelevant:scope"], user: user)
76 expected_user_topic = "user:#{user.id}"
77 expected_notification_topic = "user:notification:#{user.id}"
78 expected_direct_topic = "direct:#{user.id}"
80 for valid_user_token <- [read_oauth_token, read_statuses_token] do
81 assert {:ok, ^expected_user_topic} = Streamer.get_topic("user", user, valid_user_token)
83 assert {:ok, ^expected_direct_topic} =
84 Streamer.get_topic("direct", user, valid_user_token)
87 for invalid_user_token <- [read_notifications_token, badly_scoped_token],
88 user_topic <- ["user", "direct"] do
89 assert {:error, :unauthorized} = Streamer.get_topic(user_topic, user, invalid_user_token)
92 for valid_notification_token <- [read_oauth_token, read_notifications_token] do
93 assert {:ok, ^expected_notification_topic} =
94 Streamer.get_topic("user:notification", user, valid_notification_token)
97 for invalid_notification_token <- [read_statuses_token, badly_scoped_token] do
98 assert {:error, :unauthorized} =
99 Streamer.get_topic("user:notification", user, invalid_notification_token)
103 test "allows hashtag streams (regardless of OAuth token scopes)", %{
105 token: read_oauth_token
107 for oauth_token <- [nil, read_oauth_token] do
108 assert {:ok, "hashtag:cofe"} =
109 Streamer.get_topic("hashtag", user, oauth_token, %{"tag" => "cofe"})
113 test "disallows registering to another user's stream", %{user: user, token: read_oauth_token} do
114 another_user = insert(:user)
115 assert {:error, _} = Streamer.get_topic("user:#{another_user.id}", user, read_oauth_token)
118 Streamer.get_topic("user:notification:#{another_user.id}", user, read_oauth_token)
120 assert {:error, _} = Streamer.get_topic("direct:#{another_user.id}", user, read_oauth_token)
123 test "allows list stream that are owned by the user (with `read` or `read:lists` scopes)", %{
125 token: read_oauth_token
127 %{token: read_lists_token} = oauth_access(["read:lists"], user: user)
128 %{token: invalid_token} = oauth_access(["irrelevant:scope"], user: user)
129 {:ok, list} = List.create("Test", user)
131 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, read_oauth_token)
133 for valid_token <- [read_oauth_token, read_lists_token] do
134 assert {:ok, _} = Streamer.get_topic("list", user, valid_token, %{"list" => list.id})
137 assert {:error, _} = Streamer.get_topic("list", user, invalid_token, %{"list" => list.id})
140 test "disallows list stream that are not owned by the user", %{user: user, token: oauth_token} do
141 another_user = insert(:user)
142 {:ok, list} = List.create("Test", another_user)
144 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, oauth_token)
145 assert {:error, _} = Streamer.get_topic("list", user, oauth_token, %{"list" => list.id})
149 describe "user streams" do
151 %{user: user, token: token} = oauth_access(["read"])
152 notify = insert(:notification, user: user, activity: build(:note_activity))
153 {:ok, %{user: user, notify: notify, token: token}}
156 test "it streams the user's post in the 'user' stream", %{user: user, token: oauth_token} do
157 Streamer.get_topic_and_add_socket("user", user, oauth_token)
158 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
160 stream_name = "user:#{user.id}"
161 assert_receive {:render_with_user, _, _, ^activity, ^stream_name}
162 refute Streamer.filtered_by_user?(user, activity)
165 test "it streams boosts of the user in the 'user' stream", %{user: user, token: oauth_token} do
166 Streamer.get_topic_and_add_socket("user", user, oauth_token)
168 other_user = insert(:user)
169 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
170 {:ok, announce} = CommonAPI.repeat(activity.id, user)
172 stream_name = "user:#{user.id}"
174 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce,
177 refute Streamer.filtered_by_user?(user, announce)
180 test "it does not stream announces of the user's own posts in the 'user' stream", %{
184 Streamer.get_topic_and_add_socket("user", user, oauth_token)
186 other_user = insert(:user)
187 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
188 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
190 assert Streamer.filtered_by_user?(user, announce)
193 test "it does stream notifications announces of the user's own posts in the 'user' stream", %{
197 Streamer.get_topic_and_add_socket("user", user, oauth_token)
199 other_user = insert(:user)
200 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
201 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
205 |> Repo.get_by(%{user_id: user.id, activity_id: announce.id})
206 |> Repo.preload(:activity)
208 refute Streamer.filtered_by_user?(user, notification)
211 test "it streams boosts of mastodon user in the 'user' stream", %{
215 Streamer.get_topic_and_add_socket("user", user, oauth_token)
217 other_user = insert(:user)
218 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
221 File.read!("test/fixtures/mastodon-announce.json")
223 |> Map.put("object", activity.data["object"])
224 |> Map.put("actor", user.ap_id)
226 {:ok, %Pleroma.Activity{data: _data, local: false} = announce} =
227 Pleroma.Web.ActivityPub.Transmogrifier.handle_incoming(data)
229 stream_name = "user:#{user.id}"
231 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce,
234 refute Streamer.filtered_by_user?(user, announce)
237 test "it sends notify to in the 'user' stream", %{
242 Streamer.get_topic_and_add_socket("user", user, oauth_token)
243 Streamer.stream("user", notify)
245 assert_receive {:render_with_user, _, _, ^notify, "user"}
246 refute Streamer.filtered_by_user?(user, notify)
249 test "it sends notify to in the 'user:notification' stream", %{
254 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
255 Streamer.stream("user:notification", notify)
257 assert_receive {:render_with_user, _, _, ^notify, "user:notification"}
258 refute Streamer.filtered_by_user?(user, notify)
261 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
265 blocked = insert(:user)
266 {:ok, _user_relationship} = User.block(user, blocked)
268 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
270 {:ok, activity} = CommonAPI.post(user, %{status: ":("})
271 {:ok, _} = CommonAPI.favorite(blocked, activity.id)
276 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
280 user2 = insert(:user)
282 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
283 {:ok, _} = CommonAPI.add_mute(user, activity)
285 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
287 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
290 assert Streamer.filtered_by_user?(user, favorite_activity)
293 test "it sends favorite to 'user:notification' stream'", %{
297 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
299 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
300 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
301 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
303 assert_receive {:render_with_user, _, "notification.json", notif, "user:notification"}
304 assert notif.activity.id == favorite_activity.id
305 refute Streamer.filtered_by_user?(user, notif)
308 test "it doesn't send the 'user:notification' stream' when a domain is blocked", %{
312 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
314 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
315 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
316 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
317 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
320 assert Streamer.filtered_by_user?(user, favorite_activity)
323 test "it sends follow activities to the 'user:notification' stream", %{
327 user2 = insert(:user)
329 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
330 {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)
332 assert_receive {:render_with_user, _, "notification.json", notif, "user:notification"}
333 assert notif.activity.id == follow_activity.id
334 refute Streamer.filtered_by_user?(user, notif)
337 test "it sends follow relationships updates to the 'user' stream", %{
342 other_user = insert(:user)
343 other_user_id = other_user.id
345 Streamer.get_topic_and_add_socket("user", user, oauth_token)
346 {:ok, _follower, _followed, _follow_activity} = CommonAPI.follow(user, other_user)
348 assert_receive {:text, event}
350 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
355 "follower_count" => 0,
356 "following_count" => 0,
360 "follower_count" => 0,
361 "following_count" => 0,
362 "id" => ^other_user_id
364 "state" => "follow_pending"
365 } = Jason.decode!(payload)
367 assert_receive {:text, event}
369 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
374 "follower_count" => 0,
375 "following_count" => 1,
379 "follower_count" => 1,
380 "following_count" => 0,
381 "id" => ^other_user_id
383 "state" => "follow_accept"
384 } = Jason.decode!(payload)
387 test "it streams edits in the 'user' stream", %{user: user, token: oauth_token} do
388 sender = insert(:user)
389 {:ok, _, _, _} = CommonAPI.follow(user, sender)
391 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
393 Streamer.get_topic_and_add_socket("user", user, oauth_token)
394 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
395 create = Pleroma.Activity.get_create_by_object_ap_id_with_object(activity.object.data["id"])
397 stream = "user:#{user.id}"
398 assert_receive {:render_with_user, _, "status_update.json", ^create, ^stream}
399 refute Streamer.filtered_by_user?(user, edited)
402 test "it streams own edits in the 'user' stream", %{user: user, token: oauth_token} do
403 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
405 Streamer.get_topic_and_add_socket("user", user, oauth_token)
406 {:ok, edited} = CommonAPI.update(user, activity, %{status: "mew mew"})
407 create = Pleroma.Activity.get_create_by_object_ap_id_with_object(activity.object.data["id"])
409 stream = "user:#{user.id}"
410 assert_receive {:render_with_user, _, "status_update.json", ^create, ^stream}
411 refute Streamer.filtered_by_user?(user, edited)
414 test "it streams posts containing followed hashtags on the 'user' stream", %{
418 hashtag = insert(:hashtag, %{name: "tenshi"})
419 other_user = insert(:user)
420 {:ok, user} = User.follow_hashtag(user, hashtag)
422 Streamer.get_topic_and_add_socket("user", user, oauth_token)
423 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey #tenshi"})
425 assert_receive {:render_with_user, _, "update.json", ^activity, _}
428 test "should not stream private posts containing followed hashtags on the 'user' stream", %{
432 hashtag = insert(:hashtag, %{name: "tenshi"})
433 other_user = insert(:user)
434 {:ok, user} = User.follow_hashtag(user, hashtag)
436 Streamer.get_topic_and_add_socket("user", user, oauth_token)
439 CommonAPI.post(other_user, %{status: "hey #tenshi", visibility: "private"})
441 refute_receive {:render_with_user, _, "update.json", ^activity, _}
445 describe "public streams" do
446 test "it sends to public (authenticated)" do
447 %{user: user, token: oauth_token} = oauth_access(["read"])
448 other_user = insert(:user)
450 Streamer.get_topic_and_add_socket("public", user, oauth_token)
452 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
453 assert_receive {:render_with_user, _, _, ^activity, "public"}
454 refute Streamer.filtered_by_user?(other_user, activity)
457 test "it sends to public (unauthenticated)" do
460 Streamer.get_topic_and_add_socket("public", nil, nil)
462 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
463 activity_id = activity.id
464 assert_receive {:text, event}
465 assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
466 assert %{"id" => ^activity_id} = Jason.decode!(payload)
468 {:ok, _} = CommonAPI.delete(activity.id, user)
469 assert_receive {:text, event}
470 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
473 test "handles deletions" do
474 %{user: user, token: oauth_token} = oauth_access(["read"])
475 other_user = insert(:user)
476 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
478 Streamer.get_topic_and_add_socket("public", user, oauth_token)
480 {:ok, _} = CommonAPI.delete(activity.id, other_user)
481 activity_id = activity.id
482 assert_receive {:text, event}
483 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
486 test "it streams edits in the 'public' stream" do
487 sender = insert(:user)
489 Streamer.get_topic_and_add_socket("public", nil, nil)
490 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
491 assert_receive {:text, _}
493 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
495 edited = Pleroma.Activity.normalize(edited)
497 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
499 assert_receive {:text, event}
500 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
501 assert %{"id" => ^activity_id} = Jason.decode!(payload)
502 refute Streamer.filtered_by_user?(sender, edited)
505 test "it streams multiple edits in the 'public' stream correctly" do
506 sender = insert(:user)
508 Streamer.get_topic_and_add_socket("public", nil, nil)
509 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
510 assert_receive {:text, _}
512 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
514 edited = Pleroma.Activity.normalize(edited)
516 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
518 assert_receive {:text, event}
519 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
520 assert %{"id" => ^activity_id} = Jason.decode!(payload)
521 refute Streamer.filtered_by_user?(sender, edited)
523 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew 2"})
525 edited = Pleroma.Activity.normalize(edited)
527 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
528 assert_receive {:text, event}
529 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
530 assert %{"id" => ^activity_id, "content" => "mew mew 2"} = Jason.decode!(payload)
531 refute Streamer.filtered_by_user?(sender, edited)
535 describe "thread_containment/2" do
536 test "it filters to user if recipients invalid and thread containment is enabled" do
537 clear_config([:instance, :skip_thread_containment], false)
538 author = insert(:user)
539 %{user: user, token: oauth_token} = oauth_access(["read"])
540 User.follow(user, author, :follow_accept)
543 insert(:note_activity,
547 data: %{"to" => ["TEST-FFF"]}
551 Streamer.get_topic_and_add_socket("public", user, oauth_token)
552 Streamer.stream("public", activity)
553 assert_receive {:render_with_user, _, _, ^activity, "public"}
554 assert Streamer.filtered_by_user?(user, activity)
557 test "it sends message if recipients invalid and thread containment is disabled" do
558 clear_config([:instance, :skip_thread_containment], true)
559 author = insert(:user)
560 %{user: user, token: oauth_token} = oauth_access(["read"])
561 User.follow(user, author, :follow_accept)
564 insert(:note_activity,
568 data: %{"to" => ["TEST-FFF"]}
572 Streamer.get_topic_and_add_socket("public", user, oauth_token)
573 Streamer.stream("public", activity)
575 assert_receive {:render_with_user, _, _, ^activity, "public"}
576 refute Streamer.filtered_by_user?(user, activity)
579 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
580 clear_config([:instance, :skip_thread_containment], false)
581 author = insert(:user)
582 user = insert(:user, skip_thread_containment: true)
583 %{token: oauth_token} = oauth_access(["read"], user: user)
584 User.follow(user, author, :follow_accept)
587 insert(:note_activity,
591 data: %{"to" => ["TEST-FFF"]}
595 Streamer.get_topic_and_add_socket("public", user, oauth_token)
596 Streamer.stream("public", activity)
598 assert_receive {:render_with_user, _, _, ^activity, "public"}
599 refute Streamer.filtered_by_user?(user, activity)
604 setup do: oauth_access(["read"])
606 test "it filters messages involving blocked users", %{user: user, token: oauth_token} do
607 blocked_user = insert(:user)
608 {:ok, _user_relationship} = User.block(user, blocked_user)
610 Streamer.get_topic_and_add_socket("public", user, oauth_token)
611 {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
612 assert_receive {:render_with_user, _, _, ^activity, "public"}
613 assert Streamer.filtered_by_user?(user, activity)
616 test "it filters messages transitively involving blocked users", %{
620 blockee = insert(:user)
621 friend = insert(:user)
623 Streamer.get_topic_and_add_socket("public", blocker, blocker_token)
625 {:ok, _user_relationship} = User.block(blocker, blockee)
627 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
629 assert_receive {:render_with_user, _, _, ^activity_one, "public"}
630 assert Streamer.filtered_by_user?(blocker, activity_one)
632 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
634 assert_receive {:render_with_user, _, _, ^activity_two, "public"}
635 assert Streamer.filtered_by_user?(blocker, activity_two)
637 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
639 assert_receive {:render_with_user, _, _, ^activity_three, "public"}
640 assert Streamer.filtered_by_user?(blocker, activity_three)
645 setup do: oauth_access(["read"])
647 test "it doesn't send unwanted DMs to list", %{user: user_a, token: user_a_token} do
648 user_b = insert(:user)
649 user_c = insert(:user)
651 {:ok, user_a, user_b} = User.follow(user_a, user_b)
653 {:ok, list} = List.create("Test", user_a)
654 {:ok, list} = List.follow(list, user_b)
656 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
659 CommonAPI.post(user_b, %{
660 status: "@#{user_c.nickname} Test",
667 test "it doesn't send unwanted private posts to list", %{user: user_a, token: user_a_token} do
668 user_b = insert(:user)
670 {:ok, list} = List.create("Test", user_a)
671 {:ok, list} = List.follow(list, user_b)
673 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
676 CommonAPI.post(user_b, %{
678 visibility: "private"
684 test "it sends wanted private posts to list", %{user: user_a, token: user_a_token} do
685 user_b = insert(:user)
687 {:ok, user_a, user_b} = User.follow(user_a, user_b)
689 {:ok, list} = List.create("Test", user_a)
690 {:ok, list} = List.follow(list, user_b)
692 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
695 CommonAPI.post(user_b, %{
697 visibility: "private"
700 stream_name = "list:#{list.id}"
701 assert_receive {:render_with_user, _, _, ^activity, ^stream_name}
702 refute Streamer.filtered_by_user?(user_a, activity)
706 describe "muted reblogs" do
707 setup do: oauth_access(["read"])
709 test "it filters muted reblogs", %{user: user1, token: user1_token} do
710 user2 = insert(:user)
711 user3 = insert(:user)
712 CommonAPI.follow(user1, user2)
713 CommonAPI.hide_reblogs(user1, user2)
715 {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
717 Streamer.get_topic_and_add_socket("user", user1, user1_token)
718 {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
719 stream_name = "user:#{user1.id}"
720 assert_receive {:render_with_user, _, _, ^announce_activity, ^stream_name}
721 assert Streamer.filtered_by_user?(user1, announce_activity)
724 test "it filters reblog notification for reblog-muted actors", %{
728 user2 = insert(:user)
729 CommonAPI.follow(user1, user2)
730 CommonAPI.hide_reblogs(user1, user2)
732 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
733 Streamer.get_topic_and_add_socket("user", user1, user1_token)
734 {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)
736 assert_receive {:render_with_user, _, "notification.json", notif, "user"}
737 assert Streamer.filtered_by_user?(user1, notif)
740 test "it send non-reblog notification for reblog-muted actors", %{
744 user2 = insert(:user)
745 CommonAPI.follow(user1, user2)
746 CommonAPI.hide_reblogs(user1, user2)
748 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
749 Streamer.get_topic_and_add_socket("user", user1, user1_token)
750 {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)
752 assert_receive {:render_with_user, _, "notification.json", notif, "user"}
753 refute Streamer.filtered_by_user?(user1, notif)
757 describe "muted threads" do
758 test "it filters posts from muted threads" do
760 %{user: user2, token: user2_token} = oauth_access(["read"])
761 Streamer.get_topic_and_add_socket("user", user2, user2_token)
763 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
764 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
765 {:ok, _} = CommonAPI.add_mute(user2, activity)
767 stream_name = "user:#{user2.id}"
768 assert_receive {:render_with_user, _, _, ^activity, ^stream_name}
769 assert Streamer.filtered_by_user?(user2, activity)
773 describe "direct streams" do
774 setup do: oauth_access(["read"])
776 test "it sends conversation update to the 'direct' stream", %{user: user, token: oauth_token} do
777 another_user = insert(:user)
779 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
781 {:ok, _create_activity} =
782 CommonAPI.post(another_user, %{
783 status: "hey @#{user.nickname}",
787 assert_receive {:text, received_event}
789 assert %{"event" => "conversation", "payload" => received_payload} =
790 Jason.decode!(received_event)
792 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
793 [participation] = Participation.for_user(user)
794 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
797 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted",
798 %{user: user, token: oauth_token} do
799 another_user = insert(:user)
801 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
803 {:ok, create_activity} =
804 CommonAPI.post(another_user, %{
805 status: "hi @#{user.nickname}",
809 create_activity_id = create_activity.id
810 stream_name = "direct:#{user.id}"
811 assert_receive {:render_with_user, _, _, ^create_activity, ^stream_name}
812 assert_receive {:text, received_conversation1}
813 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
815 {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
817 assert_receive {:text, received_event}
819 assert %{"event" => "delete", "payload" => ^create_activity_id} =
820 Jason.decode!(received_event)
826 test "it sends conversation update to the 'direct' stream when a message is deleted", %{
830 another_user = insert(:user)
831 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
833 {:ok, create_activity} =
834 CommonAPI.post(another_user, %{
835 status: "hi @#{user.nickname}",
839 {:ok, create_activity2} =
840 CommonAPI.post(another_user, %{
841 status: "hi @#{user.nickname} 2",
842 in_reply_to_status_id: create_activity.id,
846 stream_name = "direct:#{user.id}"
847 assert_receive {:render_with_user, _, _, ^create_activity, ^stream_name}
848 assert_receive {:render_with_user, _, _, ^create_activity2, ^stream_name}
849 assert_receive {:text, received_conversation1}
850 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
851 assert_receive {:text, received_conversation1}
852 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
854 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
856 assert_receive {:text, received_event}
857 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
859 assert_receive {:text, received_event}
861 assert %{"event" => "conversation", "payload" => received_payload} =
862 Jason.decode!(received_event)
864 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
865 assert last_status["id"] == to_string(create_activity.id)
869 describe "stop streaming if token got revoked" do
871 child_proc = fn start, finalize ->
876 {StreamerTest, :ready} ->
877 assert_receive {:render_with_user, _, "update.json", _, _}
880 {StreamerTest, :revoked} -> finalize.()
886 starter = fn user, token ->
887 fn -> Streamer.get_topic_and_add_socket("user", user, token) end
890 hit = fn -> assert_receive :close end
891 miss = fn -> refute_receive :close end
893 send_all = fn tasks, thing -> Enum.each(tasks, &send(&1.pid, thing)) end
896 child_proc: child_proc,
904 test "do not revoke other tokens", %{
905 child_proc: child_proc,
911 %{user: user, token: token} = oauth_access(["read"])
912 %{token: token2} = oauth_access(["read"], user: user)
913 %{user: user2, token: user2_token} = oauth_access(["read"])
915 post_user = insert(:user)
916 CommonAPI.follow(user, post_user)
917 CommonAPI.follow(user2, post_user)
920 Task.async(child_proc.(starter.(user, token), hit)),
921 Task.async(child_proc.(starter.(user, token2), miss)),
922 Task.async(child_proc.(starter.(user2, user2_token), miss))
926 CommonAPI.post(post_user, %{
930 send_all.(tasks, {StreamerTest, :ready})
932 Pleroma.Web.OAuth.Token.Strategy.Revoke.revoke(token)
934 send_all.(tasks, {StreamerTest, :revoked})
936 Enum.each(tasks, &Task.await/1)
939 test "revoke all streams for this token", %{
940 child_proc: child_proc,
945 %{user: user, token: token} = oauth_access(["read"])
947 post_user = insert(:user)
948 CommonAPI.follow(user, post_user)
951 Task.async(child_proc.(starter.(user, token), hit)),
952 Task.async(child_proc.(starter.(user, token), hit))
956 CommonAPI.post(post_user, %{
960 send_all.(tasks, {StreamerTest, :ready})
962 Pleroma.Web.OAuth.Token.Strategy.Revoke.revoke(token)
964 send_all.(tasks, {StreamerTest, :revoked})
966 Enum.each(tasks, &Task.await/1)