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)
415 describe "public streams" do
416 test "it sends to public (authenticated)" do
417 %{user: user, token: oauth_token} = oauth_access(["read"])
418 other_user = insert(:user)
420 Streamer.get_topic_and_add_socket("public", user, oauth_token)
422 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
423 assert_receive {:render_with_user, _, _, ^activity, "public"}
424 refute Streamer.filtered_by_user?(other_user, activity)
427 test "it sends to public (unauthenticated)" do
430 Streamer.get_topic_and_add_socket("public", nil, nil)
432 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
433 activity_id = activity.id
434 assert_receive {:text, event}
435 assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
436 assert %{"id" => ^activity_id} = Jason.decode!(payload)
438 {:ok, _} = CommonAPI.delete(activity.id, user)
439 assert_receive {:text, event}
440 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
443 test "handles deletions" do
444 %{user: user, token: oauth_token} = oauth_access(["read"])
445 other_user = insert(:user)
446 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
448 Streamer.get_topic_and_add_socket("public", user, oauth_token)
450 {:ok, _} = CommonAPI.delete(activity.id, other_user)
451 activity_id = activity.id
452 assert_receive {:text, event}
453 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
456 test "it streams edits in the 'public' stream" do
457 sender = insert(:user)
459 Streamer.get_topic_and_add_socket("public", nil, nil)
460 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
461 assert_receive {:text, _}
463 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
465 edited = Pleroma.Activity.normalize(edited)
467 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
469 assert_receive {:text, event}
470 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
471 assert %{"id" => ^activity_id} = Jason.decode!(payload)
472 refute Streamer.filtered_by_user?(sender, edited)
475 test "it streams multiple edits in the 'public' stream correctly" do
476 sender = insert(:user)
478 Streamer.get_topic_and_add_socket("public", nil, nil)
479 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
480 assert_receive {:text, _}
482 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
484 edited = Pleroma.Activity.normalize(edited)
486 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
488 assert_receive {:text, event}
489 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
490 assert %{"id" => ^activity_id} = Jason.decode!(payload)
491 refute Streamer.filtered_by_user?(sender, edited)
493 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew 2"})
495 edited = Pleroma.Activity.normalize(edited)
497 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
498 assert_receive {:text, event}
499 assert %{"event" => "status.update", "payload" => payload} = Jason.decode!(event)
500 assert %{"id" => ^activity_id, "content" => "mew mew 2"} = Jason.decode!(payload)
501 refute Streamer.filtered_by_user?(sender, edited)
505 describe "thread_containment/2" do
506 test "it filters to user if recipients invalid and thread containment is enabled" do
507 clear_config([:instance, :skip_thread_containment], false)
508 author = insert(:user)
509 %{user: user, token: oauth_token} = oauth_access(["read"])
510 User.follow(user, author, :follow_accept)
513 insert(:note_activity,
517 data: %{"to" => ["TEST-FFF"]}
521 Streamer.get_topic_and_add_socket("public", user, oauth_token)
522 Streamer.stream("public", activity)
523 assert_receive {:render_with_user, _, _, ^activity, "public"}
524 assert Streamer.filtered_by_user?(user, activity)
527 test "it sends message if recipients invalid and thread containment is disabled" do
528 clear_config([:instance, :skip_thread_containment], true)
529 author = insert(:user)
530 %{user: user, token: oauth_token} = oauth_access(["read"])
531 User.follow(user, author, :follow_accept)
534 insert(:note_activity,
538 data: %{"to" => ["TEST-FFF"]}
542 Streamer.get_topic_and_add_socket("public", user, oauth_token)
543 Streamer.stream("public", activity)
545 assert_receive {:render_with_user, _, _, ^activity, "public"}
546 refute Streamer.filtered_by_user?(user, activity)
549 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
550 clear_config([:instance, :skip_thread_containment], false)
551 author = insert(:user)
552 user = insert(:user, skip_thread_containment: true)
553 %{token: oauth_token} = oauth_access(["read"], user: user)
554 User.follow(user, author, :follow_accept)
557 insert(:note_activity,
561 data: %{"to" => ["TEST-FFF"]}
565 Streamer.get_topic_and_add_socket("public", user, oauth_token)
566 Streamer.stream("public", activity)
568 assert_receive {:render_with_user, _, _, ^activity, "public"}
569 refute Streamer.filtered_by_user?(user, activity)
574 setup do: oauth_access(["read"])
576 test "it filters messages involving blocked users", %{user: user, token: oauth_token} do
577 blocked_user = insert(:user)
578 {:ok, _user_relationship} = User.block(user, blocked_user)
580 Streamer.get_topic_and_add_socket("public", user, oauth_token)
581 {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
582 assert_receive {:render_with_user, _, _, ^activity, "public"}
583 assert Streamer.filtered_by_user?(user, activity)
586 test "it filters messages transitively involving blocked users", %{
590 blockee = insert(:user)
591 friend = insert(:user)
593 Streamer.get_topic_and_add_socket("public", blocker, blocker_token)
595 {:ok, _user_relationship} = User.block(blocker, blockee)
597 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
599 assert_receive {:render_with_user, _, _, ^activity_one, "public"}
600 assert Streamer.filtered_by_user?(blocker, activity_one)
602 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
604 assert_receive {:render_with_user, _, _, ^activity_two, "public"}
605 assert Streamer.filtered_by_user?(blocker, activity_two)
607 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
609 assert_receive {:render_with_user, _, _, ^activity_three, "public"}
610 assert Streamer.filtered_by_user?(blocker, activity_three)
615 setup do: oauth_access(["read"])
617 test "it doesn't send unwanted DMs to list", %{user: user_a, token: user_a_token} do
618 user_b = insert(:user)
619 user_c = insert(:user)
621 {:ok, user_a, user_b} = User.follow(user_a, user_b)
623 {:ok, list} = List.create("Test", user_a)
624 {:ok, list} = List.follow(list, user_b)
626 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
629 CommonAPI.post(user_b, %{
630 status: "@#{user_c.nickname} Test",
637 test "it doesn't send unwanted private posts to list", %{user: user_a, token: user_a_token} do
638 user_b = insert(:user)
640 {:ok, list} = List.create("Test", user_a)
641 {:ok, list} = List.follow(list, user_b)
643 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
646 CommonAPI.post(user_b, %{
648 visibility: "private"
654 test "it sends wanted private posts to list", %{user: user_a, token: user_a_token} do
655 user_b = insert(:user)
657 {:ok, user_a, user_b} = User.follow(user_a, user_b)
659 {:ok, list} = List.create("Test", user_a)
660 {:ok, list} = List.follow(list, user_b)
662 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
665 CommonAPI.post(user_b, %{
667 visibility: "private"
670 stream_name = "list:#{list.id}"
671 assert_receive {:render_with_user, _, _, ^activity, ^stream_name}
672 refute Streamer.filtered_by_user?(user_a, activity)
676 describe "muted reblogs" do
677 setup do: oauth_access(["read"])
679 test "it filters muted reblogs", %{user: user1, token: user1_token} do
680 user2 = insert(:user)
681 user3 = insert(:user)
682 CommonAPI.follow(user1, user2)
683 CommonAPI.hide_reblogs(user1, user2)
685 {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
687 Streamer.get_topic_and_add_socket("user", user1, user1_token)
688 {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
689 stream_name = "user:#{user1.id}"
690 assert_receive {:render_with_user, _, _, ^announce_activity, ^stream_name}
691 assert Streamer.filtered_by_user?(user1, announce_activity)
694 test "it filters reblog notification for reblog-muted actors", %{
698 user2 = insert(:user)
699 CommonAPI.follow(user1, user2)
700 CommonAPI.hide_reblogs(user1, user2)
702 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
703 Streamer.get_topic_and_add_socket("user", user1, user1_token)
704 {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)
706 assert_receive {:render_with_user, _, "notification.json", notif, "user"}
707 assert Streamer.filtered_by_user?(user1, notif)
710 test "it send non-reblog notification for reblog-muted actors", %{
714 user2 = insert(:user)
715 CommonAPI.follow(user1, user2)
716 CommonAPI.hide_reblogs(user1, user2)
718 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
719 Streamer.get_topic_and_add_socket("user", user1, user1_token)
720 {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)
722 assert_receive {:render_with_user, _, "notification.json", notif, "user"}
723 refute Streamer.filtered_by_user?(user1, notif)
727 describe "muted threads" do
728 test "it filters posts from muted threads" do
730 %{user: user2, token: user2_token} = oauth_access(["read"])
731 Streamer.get_topic_and_add_socket("user", user2, user2_token)
733 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
734 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
735 {:ok, _} = CommonAPI.add_mute(user2, activity)
737 stream_name = "user:#{user2.id}"
738 assert_receive {:render_with_user, _, _, ^activity, ^stream_name}
739 assert Streamer.filtered_by_user?(user2, activity)
743 describe "direct streams" do
744 setup do: oauth_access(["read"])
746 test "it sends conversation update to the 'direct' stream", %{user: user, token: oauth_token} do
747 another_user = insert(:user)
749 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
751 {:ok, _create_activity} =
752 CommonAPI.post(another_user, %{
753 status: "hey @#{user.nickname}",
757 assert_receive {:text, received_event}
759 assert %{"event" => "conversation", "payload" => received_payload} =
760 Jason.decode!(received_event)
762 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
763 [participation] = Participation.for_user(user)
764 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
767 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted",
768 %{user: user, token: oauth_token} do
769 another_user = insert(:user)
771 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
773 {:ok, create_activity} =
774 CommonAPI.post(another_user, %{
775 status: "hi @#{user.nickname}",
779 create_activity_id = create_activity.id
780 stream_name = "direct:#{user.id}"
781 assert_receive {:render_with_user, _, _, ^create_activity, ^stream_name}
782 assert_receive {:text, received_conversation1}
783 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
785 {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
787 assert_receive {:text, received_event}
789 assert %{"event" => "delete", "payload" => ^create_activity_id} =
790 Jason.decode!(received_event)
796 test "it sends conversation update to the 'direct' stream when a message is deleted", %{
800 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 {:ok, create_activity2} =
810 CommonAPI.post(another_user, %{
811 status: "hi @#{user.nickname} 2",
812 in_reply_to_status_id: create_activity.id,
816 stream_name = "direct:#{user.id}"
817 assert_receive {:render_with_user, _, _, ^create_activity, ^stream_name}
818 assert_receive {:render_with_user, _, _, ^create_activity2, ^stream_name}
819 assert_receive {:text, received_conversation1}
820 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
821 assert_receive {:text, received_conversation1}
822 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
824 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
826 assert_receive {:text, received_event}
827 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
829 assert_receive {:text, received_event}
831 assert %{"event" => "conversation", "payload" => received_payload} =
832 Jason.decode!(received_event)
834 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
835 assert last_status["id"] == to_string(create_activity.id)
839 describe "stop streaming if token got revoked" do
841 child_proc = fn start, finalize ->
846 {StreamerTest, :ready} ->
847 assert_receive {:render_with_user, _, "update.json", _, _}
850 {StreamerTest, :revoked} -> finalize.()
856 starter = fn user, token ->
857 fn -> Streamer.get_topic_and_add_socket("user", user, token) end
860 hit = fn -> assert_receive :close end
861 miss = fn -> refute_receive :close end
863 send_all = fn tasks, thing -> Enum.each(tasks, &send(&1.pid, thing)) end
866 child_proc: child_proc,
874 test "do not revoke other tokens", %{
875 child_proc: child_proc,
881 %{user: user, token: token} = oauth_access(["read"])
882 %{token: token2} = oauth_access(["read"], user: user)
883 %{user: user2, token: user2_token} = oauth_access(["read"])
885 post_user = insert(:user)
886 CommonAPI.follow(user, post_user)
887 CommonAPI.follow(user2, post_user)
890 Task.async(child_proc.(starter.(user, token), hit)),
891 Task.async(child_proc.(starter.(user, token2), miss)),
892 Task.async(child_proc.(starter.(user2, user2_token), miss))
896 CommonAPI.post(post_user, %{
900 send_all.(tasks, {StreamerTest, :ready})
902 Pleroma.Web.OAuth.Token.Strategy.Revoke.revoke(token)
904 send_all.(tasks, {StreamerTest, :revoked})
906 Enum.each(tasks, &Task.await/1)
909 test "revoke all streams for this token", %{
910 child_proc: child_proc,
915 %{user: user, token: token} = oauth_access(["read"])
917 post_user = insert(:user)
918 CommonAPI.follow(user, post_user)
921 Task.async(child_proc.(starter.(user, token), hit)),
922 Task.async(child_proc.(starter.(user, token), hit))
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)