Merge pull request 'docs: Update links to list of akkoma instances' (#278) from norm...
[akkoma] / test / pleroma / web / streamer_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.StreamerTest do
6 use Pleroma.DataCase, async: false
7
8 import Pleroma.Factory
9
10 alias Pleroma.Conversation.Participation
11 alias Pleroma.List
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.Streamer
15
16 @moduletag needs_streamer: true, capture_log: true
17
18 setup do: clear_config([:instance, :skip_thread_containment])
19
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)
26 end
27
28 test "allows instance streams" do
29 assert {:ok, "public:remote:lain.com"} =
30 Streamer.get_topic("public:remote", nil, nil, %{"instance" => "lain.com"})
31
32 assert {:ok, "public:remote:media:lain.com"} =
33 Streamer.get_topic("public:remote:media", nil, nil, %{"instance" => "lain.com"})
34 end
35
36 test "allows hashtag streams" do
37 assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", nil, nil, %{"tag" => "cofe"})
38 end
39
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)
44 end
45
46 test "disallows list streams" do
47 assert {:error, _} = Streamer.get_topic("list", nil, nil, %{"list" => 42})
48 end
49 end
50
51 describe "get_topic/_ (authenticated)" do
52 setup do: oauth_access(["read"])
53
54 test "allows public streams (regardless of OAuth token scopes)", %{
55 user: user,
56 token: read_oauth_token
57 } do
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)
62
63 assert {:ok, "public:local:media"} =
64 Streamer.get_topic("public:local:media", user, oauth_token)
65 end
66 end
67
68 test "allows user streams (with proper OAuth token scopes)", %{
69 user: user,
70 token: read_oauth_token
71 } do
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)
75
76 expected_user_topic = "user:#{user.id}"
77 expected_notification_topic = "user:notification:#{user.id}"
78 expected_direct_topic = "direct:#{user.id}"
79
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)
82
83 assert {:ok, ^expected_direct_topic} =
84 Streamer.get_topic("direct", user, valid_user_token)
85 end
86
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)
90 end
91
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)
95 end
96
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)
100 end
101 end
102
103 test "allows hashtag streams (regardless of OAuth token scopes)", %{
104 user: user,
105 token: read_oauth_token
106 } do
107 for oauth_token <- [nil, read_oauth_token] do
108 assert {:ok, "hashtag:cofe"} =
109 Streamer.get_topic("hashtag", user, oauth_token, %{"tag" => "cofe"})
110 end
111 end
112
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)
116
117 assert {:error, _} =
118 Streamer.get_topic("user:notification:#{another_user.id}", user, read_oauth_token)
119
120 assert {:error, _} = Streamer.get_topic("direct:#{another_user.id}", user, read_oauth_token)
121 end
122
123 test "allows list stream that are owned by the user (with `read` or `read:lists` scopes)", %{
124 user: user,
125 token: read_oauth_token
126 } do
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)
130
131 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, read_oauth_token)
132
133 for valid_token <- [read_oauth_token, read_lists_token] do
134 assert {:ok, _} = Streamer.get_topic("list", user, valid_token, %{"list" => list.id})
135 end
136
137 assert {:error, _} = Streamer.get_topic("list", user, invalid_token, %{"list" => list.id})
138 end
139
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)
143
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})
146 end
147 end
148
149 describe "user streams" do
150 setup 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}}
154 end
155
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"})
159
160 stream_name = "user:#{user.id}"
161 assert_receive {:render_with_user, _, _, ^activity, ^stream_name}
162 refute Streamer.filtered_by_user?(user, activity)
163 end
164
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)
167
168 other_user = insert(:user)
169 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
170 {:ok, announce} = CommonAPI.repeat(activity.id, user)
171
172 stream_name = "user:#{user.id}"
173
174 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce,
175 ^stream_name}
176
177 refute Streamer.filtered_by_user?(user, announce)
178 end
179
180 test "it does not stream announces of the user's own posts in the 'user' stream", %{
181 user: user,
182 token: oauth_token
183 } do
184 Streamer.get_topic_and_add_socket("user", user, oauth_token)
185
186 other_user = insert(:user)
187 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
188 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
189
190 assert Streamer.filtered_by_user?(user, announce)
191 end
192
193 test "it does stream notifications announces of the user's own posts in the 'user' stream", %{
194 user: user,
195 token: oauth_token
196 } do
197 Streamer.get_topic_and_add_socket("user", user, oauth_token)
198
199 other_user = insert(:user)
200 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
201 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
202
203 notification =
204 Pleroma.Notification
205 |> Repo.get_by(%{user_id: user.id, activity_id: announce.id})
206 |> Repo.preload(:activity)
207
208 refute Streamer.filtered_by_user?(user, notification)
209 end
210
211 test "it streams boosts of mastodon user in the 'user' stream", %{
212 user: user,
213 token: oauth_token
214 } do
215 Streamer.get_topic_and_add_socket("user", user, oauth_token)
216
217 other_user = insert(:user)
218 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
219
220 data =
221 File.read!("test/fixtures/mastodon-announce.json")
222 |> Jason.decode!()
223 |> Map.put("object", activity.data["object"])
224 |> Map.put("actor", user.ap_id)
225
226 {:ok, %Pleroma.Activity{data: _data, local: false} = announce} =
227 Pleroma.Web.ActivityPub.Transmogrifier.handle_incoming(data)
228
229 stream_name = "user:#{user.id}"
230
231 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce,
232 ^stream_name}
233
234 refute Streamer.filtered_by_user?(user, announce)
235 end
236
237 test "it sends notify to in the 'user' stream", %{
238 user: user,
239 token: oauth_token,
240 notify: notify
241 } do
242 Streamer.get_topic_and_add_socket("user", user, oauth_token)
243 Streamer.stream("user", notify)
244
245 assert_receive {:render_with_user, _, _, ^notify, "user"}
246 refute Streamer.filtered_by_user?(user, notify)
247 end
248
249 test "it sends notify to in the 'user:notification' stream", %{
250 user: user,
251 token: oauth_token,
252 notify: notify
253 } do
254 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
255 Streamer.stream("user:notification", notify)
256
257 assert_receive {:render_with_user, _, _, ^notify, "user:notification"}
258 refute Streamer.filtered_by_user?(user, notify)
259 end
260
261 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
262 user: user,
263 token: oauth_token
264 } do
265 blocked = insert(:user)
266 {:ok, _user_relationship} = User.block(user, blocked)
267
268 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
269
270 {:ok, activity} = CommonAPI.post(user, %{status: ":("})
271 {:ok, _} = CommonAPI.favorite(blocked, activity.id)
272
273 refute_receive _
274 end
275
276 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
277 user: user,
278 token: oauth_token
279 } do
280 user2 = insert(:user)
281
282 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
283 {:ok, _} = CommonAPI.add_mute(user, activity)
284
285 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
286
287 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
288
289 refute_receive _
290 assert Streamer.filtered_by_user?(user, favorite_activity)
291 end
292
293 test "it sends favorite to 'user:notification' stream'", %{
294 user: user,
295 token: oauth_token
296 } do
297 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
298
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)
302
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)
306 end
307
308 test "it doesn't send the 'user:notification' stream' when a domain is blocked", %{
309 user: user,
310 token: oauth_token
311 } do
312 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
313
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)
318
319 refute_receive _
320 assert Streamer.filtered_by_user?(user, favorite_activity)
321 end
322
323 test "it sends follow activities to the 'user:notification' stream", %{
324 user: user,
325 token: oauth_token
326 } do
327 user2 = insert(:user)
328
329 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
330 {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)
331
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)
335 end
336
337 test "it sends follow relationships updates to the 'user' stream", %{
338 user: user,
339 token: oauth_token
340 } do
341 user_id = user.id
342 other_user = insert(:user)
343 other_user_id = other_user.id
344
345 Streamer.get_topic_and_add_socket("user", user, oauth_token)
346 {:ok, _follower, _followed, _follow_activity} = CommonAPI.follow(user, other_user)
347
348 assert_receive {:text, event}
349
350 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
351 Jason.decode!(event)
352
353 assert %{
354 "follower" => %{
355 "follower_count" => 0,
356 "following_count" => 0,
357 "id" => ^user_id
358 },
359 "following" => %{
360 "follower_count" => 0,
361 "following_count" => 0,
362 "id" => ^other_user_id
363 },
364 "state" => "follow_pending"
365 } = Jason.decode!(payload)
366
367 assert_receive {:text, event}
368
369 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
370 Jason.decode!(event)
371
372 assert %{
373 "follower" => %{
374 "follower_count" => 0,
375 "following_count" => 1,
376 "id" => ^user_id
377 },
378 "following" => %{
379 "follower_count" => 1,
380 "following_count" => 0,
381 "id" => ^other_user_id
382 },
383 "state" => "follow_accept"
384 } = Jason.decode!(payload)
385 end
386
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)
390
391 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
392
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"])
396
397 stream = "user:#{user.id}"
398 assert_receive {:render_with_user, _, "status_update.json", ^create, ^stream}
399 refute Streamer.filtered_by_user?(user, edited)
400 end
401
402 test "it streams own edits in the 'user' stream", %{user: user, token: oauth_token} do
403 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
404
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"])
408
409 stream = "user:#{user.id}"
410 assert_receive {:render_with_user, _, "status_update.json", ^create, ^stream}
411 refute Streamer.filtered_by_user?(user, edited)
412 end
413 end
414
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)
419
420 Streamer.get_topic_and_add_socket("public", user, oauth_token)
421
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)
425 end
426
427 test "it sends to public (unauthenticated)" do
428 user = insert(:user)
429
430 Streamer.get_topic_and_add_socket("public", nil, nil)
431
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)
437
438 {:ok, _} = CommonAPI.delete(activity.id, user)
439 assert_receive {:text, event}
440 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
441 end
442
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"})
447
448 Streamer.get_topic_and_add_socket("public", user, oauth_token)
449
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)
454 end
455
456 test "it streams edits in the 'public' stream" do
457 sender = insert(:user)
458
459 Streamer.get_topic_and_add_socket("public", nil, nil)
460 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
461 assert_receive {:text, _}
462
463 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
464
465 edited = Pleroma.Activity.normalize(edited)
466
467 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
468
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)
473 end
474
475 test "it streams multiple edits in the 'public' stream correctly" do
476 sender = insert(:user)
477
478 Streamer.get_topic_and_add_socket("public", nil, nil)
479 {:ok, activity} = CommonAPI.post(sender, %{status: "hey"})
480 assert_receive {:text, _}
481
482 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew"})
483
484 edited = Pleroma.Activity.normalize(edited)
485
486 %{id: activity_id} = Pleroma.Activity.get_create_by_object_ap_id(edited.object.data["id"])
487
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)
492
493 {:ok, edited} = CommonAPI.update(sender, activity, %{status: "mew mew 2"})
494
495 edited = Pleroma.Activity.normalize(edited)
496
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)
502 end
503 end
504
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)
511
512 activity =
513 insert(:note_activity,
514 note:
515 insert(:note,
516 user: author,
517 data: %{"to" => ["TEST-FFF"]}
518 )
519 )
520
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)
525 end
526
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)
532
533 activity =
534 insert(:note_activity,
535 note:
536 insert(:note,
537 user: author,
538 data: %{"to" => ["TEST-FFF"]}
539 )
540 )
541
542 Streamer.get_topic_and_add_socket("public", user, oauth_token)
543 Streamer.stream("public", activity)
544
545 assert_receive {:render_with_user, _, _, ^activity, "public"}
546 refute Streamer.filtered_by_user?(user, activity)
547 end
548
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)
555
556 activity =
557 insert(:note_activity,
558 note:
559 insert(:note,
560 user: author,
561 data: %{"to" => ["TEST-FFF"]}
562 )
563 )
564
565 Streamer.get_topic_and_add_socket("public", user, oauth_token)
566 Streamer.stream("public", activity)
567
568 assert_receive {:render_with_user, _, _, ^activity, "public"}
569 refute Streamer.filtered_by_user?(user, activity)
570 end
571 end
572
573 describe "blocks" do
574 setup do: oauth_access(["read"])
575
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)
579
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)
584 end
585
586 test "it filters messages transitively involving blocked users", %{
587 user: blocker,
588 token: blocker_token
589 } do
590 blockee = insert(:user)
591 friend = insert(:user)
592
593 Streamer.get_topic_and_add_socket("public", blocker, blocker_token)
594
595 {:ok, _user_relationship} = User.block(blocker, blockee)
596
597 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
598
599 assert_receive {:render_with_user, _, _, ^activity_one, "public"}
600 assert Streamer.filtered_by_user?(blocker, activity_one)
601
602 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
603
604 assert_receive {:render_with_user, _, _, ^activity_two, "public"}
605 assert Streamer.filtered_by_user?(blocker, activity_two)
606
607 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
608
609 assert_receive {:render_with_user, _, _, ^activity_three, "public"}
610 assert Streamer.filtered_by_user?(blocker, activity_three)
611 end
612 end
613
614 describe "lists" do
615 setup do: oauth_access(["read"])
616
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)
620
621 {:ok, user_a, user_b} = User.follow(user_a, user_b)
622
623 {:ok, list} = List.create("Test", user_a)
624 {:ok, list} = List.follow(list, user_b)
625
626 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
627
628 {:ok, _activity} =
629 CommonAPI.post(user_b, %{
630 status: "@#{user_c.nickname} Test",
631 visibility: "direct"
632 })
633
634 refute_receive _
635 end
636
637 test "it doesn't send unwanted private posts to list", %{user: user_a, token: user_a_token} do
638 user_b = insert(:user)
639
640 {:ok, list} = List.create("Test", user_a)
641 {:ok, list} = List.follow(list, user_b)
642
643 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
644
645 {:ok, _activity} =
646 CommonAPI.post(user_b, %{
647 status: "Test",
648 visibility: "private"
649 })
650
651 refute_receive _
652 end
653
654 test "it sends wanted private posts to list", %{user: user_a, token: user_a_token} do
655 user_b = insert(:user)
656
657 {:ok, user_a, user_b} = User.follow(user_a, user_b)
658
659 {:ok, list} = List.create("Test", user_a)
660 {:ok, list} = List.follow(list, user_b)
661
662 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
663
664 {:ok, activity} =
665 CommonAPI.post(user_b, %{
666 status: "Test",
667 visibility: "private"
668 })
669
670 stream_name = "list:#{list.id}"
671 assert_receive {:render_with_user, _, _, ^activity, ^stream_name}
672 refute Streamer.filtered_by_user?(user_a, activity)
673 end
674 end
675
676 describe "muted reblogs" do
677 setup do: oauth_access(["read"])
678
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)
684
685 {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
686
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)
692 end
693
694 test "it filters reblog notification for reblog-muted actors", %{
695 user: user1,
696 token: user1_token
697 } do
698 user2 = insert(:user)
699 CommonAPI.follow(user1, user2)
700 CommonAPI.hide_reblogs(user1, user2)
701
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)
705
706 assert_receive {:render_with_user, _, "notification.json", notif, "user"}
707 assert Streamer.filtered_by_user?(user1, notif)
708 end
709
710 test "it send non-reblog notification for reblog-muted actors", %{
711 user: user1,
712 token: user1_token
713 } do
714 user2 = insert(:user)
715 CommonAPI.follow(user1, user2)
716 CommonAPI.hide_reblogs(user1, user2)
717
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)
721
722 assert_receive {:render_with_user, _, "notification.json", notif, "user"}
723 refute Streamer.filtered_by_user?(user1, notif)
724 end
725 end
726
727 describe "muted threads" do
728 test "it filters posts from muted threads" do
729 user = insert(:user)
730 %{user: user2, token: user2_token} = oauth_access(["read"])
731 Streamer.get_topic_and_add_socket("user", user2, user2_token)
732
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)
736
737 stream_name = "user:#{user2.id}"
738 assert_receive {:render_with_user, _, _, ^activity, ^stream_name}
739 assert Streamer.filtered_by_user?(user2, activity)
740 end
741 end
742
743 describe "direct streams" do
744 setup do: oauth_access(["read"])
745
746 test "it sends conversation update to the 'direct' stream", %{user: user, token: oauth_token} do
747 another_user = insert(:user)
748
749 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
750
751 {:ok, _create_activity} =
752 CommonAPI.post(another_user, %{
753 status: "hey @#{user.nickname}",
754 visibility: "direct"
755 })
756
757 assert_receive {:text, received_event}
758
759 assert %{"event" => "conversation", "payload" => received_payload} =
760 Jason.decode!(received_event)
761
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
765 end
766
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)
770
771 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
772
773 {:ok, create_activity} =
774 CommonAPI.post(another_user, %{
775 status: "hi @#{user.nickname}",
776 visibility: "direct"
777 })
778
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)
784
785 {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
786
787 assert_receive {:text, received_event}
788
789 assert %{"event" => "delete", "payload" => ^create_activity_id} =
790 Jason.decode!(received_event)
791
792 refute_receive _
793 end
794
795 @tag :erratic
796 test "it sends conversation update to the 'direct' stream when a message is deleted", %{
797 user: user,
798 token: oauth_token
799 } do
800 another_user = insert(:user)
801 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
802
803 {:ok, create_activity} =
804 CommonAPI.post(another_user, %{
805 status: "hi @#{user.nickname}",
806 visibility: "direct"
807 })
808
809 {:ok, create_activity2} =
810 CommonAPI.post(another_user, %{
811 status: "hi @#{user.nickname} 2",
812 in_reply_to_status_id: create_activity.id,
813 visibility: "direct"
814 })
815
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)
823
824 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
825
826 assert_receive {:text, received_event}
827 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
828
829 assert_receive {:text, received_event}
830
831 assert %{"event" => "conversation", "payload" => received_payload} =
832 Jason.decode!(received_event)
833
834 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
835 assert last_status["id"] == to_string(create_activity.id)
836 end
837 end
838
839 describe "stop streaming if token got revoked" do
840 setup do
841 child_proc = fn start, finalize ->
842 fn ->
843 start.()
844
845 receive do
846 {StreamerTest, :ready} ->
847 assert_receive {:render_with_user, _, "update.json", _, _}
848
849 receive do
850 {StreamerTest, :revoked} -> finalize.()
851 end
852 end
853 end
854 end
855
856 starter = fn user, token ->
857 fn -> Streamer.get_topic_and_add_socket("user", user, token) end
858 end
859
860 hit = fn -> assert_receive :close end
861 miss = fn -> refute_receive :close end
862
863 send_all = fn tasks, thing -> Enum.each(tasks, &send(&1.pid, thing)) end
864
865 %{
866 child_proc: child_proc,
867 starter: starter,
868 hit: hit,
869 miss: miss,
870 send_all: send_all
871 }
872 end
873
874 test "do not revoke other tokens", %{
875 child_proc: child_proc,
876 starter: starter,
877 hit: hit,
878 miss: miss,
879 send_all: send_all
880 } do
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"])
884
885 post_user = insert(:user)
886 CommonAPI.follow(user, post_user)
887 CommonAPI.follow(user2, post_user)
888
889 tasks = [
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))
893 ]
894
895 {:ok, _} =
896 CommonAPI.post(post_user, %{
897 status: "hi"
898 })
899
900 send_all.(tasks, {StreamerTest, :ready})
901
902 Pleroma.Web.OAuth.Token.Strategy.Revoke.revoke(token)
903
904 send_all.(tasks, {StreamerTest, :revoked})
905
906 Enum.each(tasks, &Task.await/1)
907 end
908
909 test "revoke all streams for this token", %{
910 child_proc: child_proc,
911 starter: starter,
912 hit: hit,
913 send_all: send_all
914 } do
915 %{user: user, token: token} = oauth_access(["read"])
916
917 post_user = insert(:user)
918 CommonAPI.follow(user, post_user)
919
920 tasks = [
921 Task.async(child_proc.(starter.(user, token), hit)),
922 Task.async(child_proc.(starter.(user, token), hit))
923 ]
924
925 {:ok, _} =
926 CommonAPI.post(post_user, %{
927 status: "hi"
928 })
929
930 send_all.(tasks, {StreamerTest, :ready})
931
932 Pleroma.Web.OAuth.Token.Strategy.Revoke.revoke(token)
933
934 send_all.(tasks, {StreamerTest, :revoked})
935
936 Enum.each(tasks, &Task.await/1)
937 end
938 end
939 end