Merge pull request '[#37] fix frienderica pinned collection fetching' (#42) from...
[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
7
8 import Pleroma.Factory
9
10 alias Pleroma.Chat
11 alias Pleroma.Chat.MessageReference
12 alias Pleroma.Conversation.Participation
13 alias Pleroma.List
14 alias Pleroma.Object
15 alias Pleroma.User
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.Streamer
18 alias Pleroma.Web.StreamerView
19
20 @moduletag needs_streamer: true, capture_log: true
21
22 setup do: clear_config([:instance, :skip_thread_containment])
23
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)
30 end
31
32 test "allows instance streams" do
33 assert {:ok, "public:remote:lain.com"} =
34 Streamer.get_topic("public:remote", nil, nil, %{"instance" => "lain.com"})
35
36 assert {:ok, "public:remote:media:lain.com"} =
37 Streamer.get_topic("public:remote:media", nil, nil, %{"instance" => "lain.com"})
38 end
39
40 test "allows hashtag streams" do
41 assert {:ok, "hashtag:cofe"} = Streamer.get_topic("hashtag", nil, nil, %{"tag" => "cofe"})
42 end
43
44 test "disallows user streams" do
45 assert {:error, _} = Streamer.get_topic("user", nil, nil)
46 assert {:error, _} = Streamer.get_topic("user:notification", nil, nil)
47 assert {:error, _} = Streamer.get_topic("direct", nil, nil)
48 end
49
50 test "disallows list streams" do
51 assert {:error, _} = Streamer.get_topic("list", nil, nil, %{"list" => 42})
52 end
53 end
54
55 describe "get_topic/_ (authenticated)" do
56 setup do: oauth_access(["read"])
57
58 test "allows public streams (regardless of OAuth token scopes)", %{
59 user: user,
60 token: read_oauth_token
61 } do
62 with oauth_token <- [nil, read_oauth_token] do
63 assert {:ok, "public"} = Streamer.get_topic("public", user, oauth_token)
64 assert {:ok, "public:local"} = Streamer.get_topic("public:local", user, oauth_token)
65 assert {:ok, "public:media"} = Streamer.get_topic("public:media", user, oauth_token)
66
67 assert {:ok, "public:local:media"} =
68 Streamer.get_topic("public:local:media", user, oauth_token)
69 end
70 end
71
72 test "allows user streams (with proper OAuth token scopes)", %{
73 user: user,
74 token: read_oauth_token
75 } do
76 %{token: read_notifications_token} = oauth_access(["read:notifications"], user: user)
77 %{token: read_statuses_token} = oauth_access(["read:statuses"], user: user)
78 %{token: badly_scoped_token} = oauth_access(["irrelevant:scope"], user: user)
79
80 expected_user_topic = "user:#{user.id}"
81 expected_notification_topic = "user:notification:#{user.id}"
82 expected_direct_topic = "direct:#{user.id}"
83 expected_pleroma_chat_topic = "user:pleroma_chat:#{user.id}"
84
85 for valid_user_token <- [read_oauth_token, read_statuses_token] do
86 assert {:ok, ^expected_user_topic} = Streamer.get_topic("user", user, valid_user_token)
87
88 assert {:ok, ^expected_direct_topic} =
89 Streamer.get_topic("direct", user, valid_user_token)
90
91 assert {:ok, ^expected_pleroma_chat_topic} =
92 Streamer.get_topic("user:pleroma_chat", user, valid_user_token)
93 end
94
95 for invalid_user_token <- [read_notifications_token, badly_scoped_token],
96 user_topic <- ["user", "direct", "user:pleroma_chat"] do
97 assert {:error, :unauthorized} = Streamer.get_topic(user_topic, user, invalid_user_token)
98 end
99
100 for valid_notification_token <- [read_oauth_token, read_notifications_token] do
101 assert {:ok, ^expected_notification_topic} =
102 Streamer.get_topic("user:notification", user, valid_notification_token)
103 end
104
105 for invalid_notification_token <- [read_statuses_token, badly_scoped_token] do
106 assert {:error, :unauthorized} =
107 Streamer.get_topic("user:notification", user, invalid_notification_token)
108 end
109 end
110
111 test "allows hashtag streams (regardless of OAuth token scopes)", %{
112 user: user,
113 token: read_oauth_token
114 } do
115 for oauth_token <- [nil, read_oauth_token] do
116 assert {:ok, "hashtag:cofe"} =
117 Streamer.get_topic("hashtag", user, oauth_token, %{"tag" => "cofe"})
118 end
119 end
120
121 test "disallows registering to another user's stream", %{user: user, token: read_oauth_token} do
122 another_user = insert(:user)
123 assert {:error, _} = Streamer.get_topic("user:#{another_user.id}", user, read_oauth_token)
124
125 assert {:error, _} =
126 Streamer.get_topic("user:notification:#{another_user.id}", user, read_oauth_token)
127
128 assert {:error, _} = Streamer.get_topic("direct:#{another_user.id}", user, read_oauth_token)
129 end
130
131 test "allows list stream that are owned by the user (with `read` or `read:lists` scopes)", %{
132 user: user,
133 token: read_oauth_token
134 } do
135 %{token: read_lists_token} = oauth_access(["read:lists"], user: user)
136 %{token: invalid_token} = oauth_access(["irrelevant:scope"], user: user)
137 {:ok, list} = List.create("Test", user)
138
139 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, read_oauth_token)
140
141 for valid_token <- [read_oauth_token, read_lists_token] do
142 assert {:ok, _} = Streamer.get_topic("list", user, valid_token, %{"list" => list.id})
143 end
144
145 assert {:error, _} = Streamer.get_topic("list", user, invalid_token, %{"list" => list.id})
146 end
147
148 test "disallows list stream that are not owned by the user", %{user: user, token: oauth_token} do
149 another_user = insert(:user)
150 {:ok, list} = List.create("Test", another_user)
151
152 assert {:error, _} = Streamer.get_topic("list:#{list.id}", user, oauth_token)
153 assert {:error, _} = Streamer.get_topic("list", user, oauth_token, %{"list" => list.id})
154 end
155 end
156
157 describe "user streams" do
158 setup do
159 %{user: user, token: token} = oauth_access(["read"])
160 notify = insert(:notification, user: user, activity: build(:note_activity))
161 {:ok, %{user: user, notify: notify, token: token}}
162 end
163
164 test "it streams the user's post in the 'user' stream", %{user: user, token: oauth_token} do
165 Streamer.get_topic_and_add_socket("user", user, oauth_token)
166 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
167
168 assert_receive {:render_with_user, _, _, ^activity}
169 refute Streamer.filtered_by_user?(user, activity)
170 end
171
172 test "it streams boosts of the user in the 'user' stream", %{user: user, token: oauth_token} do
173 Streamer.get_topic_and_add_socket("user", user, oauth_token)
174
175 other_user = insert(:user)
176 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
177 {:ok, announce} = CommonAPI.repeat(activity.id, user)
178
179 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
180 refute Streamer.filtered_by_user?(user, announce)
181 end
182
183 test "it does not stream announces of the user's own posts in the 'user' stream", %{
184 user: user,
185 token: oauth_token
186 } do
187 Streamer.get_topic_and_add_socket("user", user, oauth_token)
188
189 other_user = insert(:user)
190 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
191 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
192
193 assert Streamer.filtered_by_user?(user, announce)
194 end
195
196 test "it does stream notifications announces of the user's own posts in the 'user' stream", %{
197 user: user,
198 token: oauth_token
199 } do
200 Streamer.get_topic_and_add_socket("user", user, oauth_token)
201
202 other_user = insert(:user)
203 {:ok, activity} = CommonAPI.post(user, %{status: "hey"})
204 {:ok, announce} = CommonAPI.repeat(activity.id, other_user)
205
206 notification =
207 Pleroma.Notification
208 |> Repo.get_by(%{user_id: user.id, activity_id: announce.id})
209 |> Repo.preload(:activity)
210
211 refute Streamer.filtered_by_user?(user, notification)
212 end
213
214 test "it streams boosts of mastodon user in the 'user' stream", %{
215 user: user,
216 token: oauth_token
217 } do
218 Streamer.get_topic_and_add_socket("user", user, oauth_token)
219
220 other_user = insert(:user)
221 {:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
222
223 data =
224 File.read!("test/fixtures/mastodon-announce.json")
225 |> Jason.decode!()
226 |> Map.put("object", activity.data["object"])
227 |> Map.put("actor", user.ap_id)
228
229 {:ok, %Pleroma.Activity{data: _data, local: false} = announce} =
230 Pleroma.Web.ActivityPub.Transmogrifier.handle_incoming(data)
231
232 assert_receive {:render_with_user, Pleroma.Web.StreamerView, "update.json", ^announce}
233 refute Streamer.filtered_by_user?(user, announce)
234 end
235
236 test "it sends notify to in the 'user' stream", %{
237 user: user,
238 token: oauth_token,
239 notify: notify
240 } do
241 Streamer.get_topic_and_add_socket("user", user, oauth_token)
242 Streamer.stream("user", notify)
243
244 assert_receive {:render_with_user, _, _, ^notify}
245 refute Streamer.filtered_by_user?(user, notify)
246 end
247
248 test "it sends notify to in the 'user:notification' stream", %{
249 user: user,
250 token: oauth_token,
251 notify: notify
252 } do
253 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
254 Streamer.stream("user:notification", notify)
255
256 assert_receive {:render_with_user, _, _, ^notify}
257 refute Streamer.filtered_by_user?(user, notify)
258 end
259
260 test "it sends chat messages to the 'user:pleroma_chat' stream", %{
261 user: user,
262 token: oauth_token
263 } do
264 other_user = insert(:user)
265
266 {:ok, create_activity} =
267 CommonAPI.post_chat_message(other_user, user, "hey cirno", idempotency_key: "123")
268
269 object = Object.normalize(create_activity, fetch: false)
270 chat = Chat.get(user.id, other_user.ap_id)
271 cm_ref = MessageReference.for_chat_and_object(chat, object)
272 cm_ref = %{cm_ref | chat: chat, object: object}
273
274 Streamer.get_topic_and_add_socket("user:pleroma_chat", user, oauth_token)
275 Streamer.stream("user:pleroma_chat", {user, cm_ref})
276
277 text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
278
279 assert text =~ "hey cirno"
280 assert_receive {:text, ^text}
281 end
282
283 test "it sends chat messages to the 'user' stream", %{user: user, token: oauth_token} do
284 other_user = insert(:user)
285
286 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey cirno")
287 object = Object.normalize(create_activity, fetch: false)
288 chat = Chat.get(user.id, other_user.ap_id)
289 cm_ref = MessageReference.for_chat_and_object(chat, object)
290 cm_ref = %{cm_ref | chat: chat, object: object}
291
292 Streamer.get_topic_and_add_socket("user", user, oauth_token)
293 Streamer.stream("user", {user, cm_ref})
294
295 text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
296
297 assert text =~ "hey cirno"
298 assert_receive {:text, ^text}
299 end
300
301 test "it sends chat message notifications to the 'user:notification' stream", %{
302 user: user,
303 token: oauth_token
304 } do
305 other_user = insert(:user)
306
307 {:ok, create_activity} = CommonAPI.post_chat_message(other_user, user, "hey")
308
309 notify =
310 Repo.get_by(Pleroma.Notification, user_id: user.id, activity_id: create_activity.id)
311 |> Repo.preload(:activity)
312
313 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
314 Streamer.stream("user:notification", notify)
315
316 assert_receive {:render_with_user, _, _, ^notify}
317 refute Streamer.filtered_by_user?(user, notify)
318 end
319
320 test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
321 user: user,
322 token: oauth_token
323 } do
324 blocked = insert(:user)
325 {:ok, _user_relationship} = User.block(user, blocked)
326
327 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
328
329 {:ok, activity} = CommonAPI.post(user, %{status: ":("})
330 {:ok, _} = CommonAPI.favorite(blocked, activity.id)
331
332 refute_receive _
333 end
334
335 test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
336 user: user,
337 token: oauth_token
338 } do
339 user2 = insert(:user)
340
341 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
342 {:ok, _} = CommonAPI.add_mute(user, activity)
343
344 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
345
346 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
347
348 refute_receive _
349 assert Streamer.filtered_by_user?(user, favorite_activity)
350 end
351
352 test "it sends favorite to 'user:notification' stream'", %{
353 user: user,
354 token: oauth_token
355 } do
356 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
357
358 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
359 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
360 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
361
362 assert_receive {:render_with_user, _, "notification.json", notif}
363 assert notif.activity.id == favorite_activity.id
364 refute Streamer.filtered_by_user?(user, notif)
365 end
366
367 test "it doesn't send the 'user:notification' stream' when a domain is blocked", %{
368 user: user,
369 token: oauth_token
370 } do
371 user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
372
373 {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
374 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
375 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
376 {:ok, favorite_activity} = CommonAPI.favorite(user2, activity.id)
377
378 refute_receive _
379 assert Streamer.filtered_by_user?(user, favorite_activity)
380 end
381
382 test "it sends follow activities to the 'user:notification' stream", %{
383 user: user,
384 token: oauth_token
385 } do
386 user2 = insert(:user)
387
388 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
389 {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)
390
391 assert_receive {:render_with_user, _, "notification.json", notif}
392 assert notif.activity.id == follow_activity.id
393 refute Streamer.filtered_by_user?(user, notif)
394 end
395
396 test "it sends follow relationships updates to the 'user' stream", %{
397 user: user,
398 token: oauth_token
399 } do
400 user_id = user.id
401 other_user = insert(:user)
402 other_user_id = other_user.id
403
404 Streamer.get_topic_and_add_socket("user", user, oauth_token)
405 {:ok, _follower, _followed, _follow_activity} = CommonAPI.follow(user, other_user)
406
407 assert_receive {:text, event}
408
409 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
410 Jason.decode!(event)
411
412 assert %{
413 "follower" => %{
414 "follower_count" => 0,
415 "following_count" => 0,
416 "id" => ^user_id
417 },
418 "following" => %{
419 "follower_count" => 0,
420 "following_count" => 0,
421 "id" => ^other_user_id
422 },
423 "state" => "follow_pending"
424 } = Jason.decode!(payload)
425
426 assert_receive {:text, event}
427
428 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
429 Jason.decode!(event)
430
431 assert %{
432 "follower" => %{
433 "follower_count" => 0,
434 "following_count" => 1,
435 "id" => ^user_id
436 },
437 "following" => %{
438 "follower_count" => 1,
439 "following_count" => 0,
440 "id" => ^other_user_id
441 },
442 "state" => "follow_accept"
443 } = Jason.decode!(payload)
444 end
445 end
446
447 describe "public streams" do
448 test "it sends to public (authenticated)" do
449 %{user: user, token: oauth_token} = oauth_access(["read"])
450 other_user = insert(:user)
451
452 Streamer.get_topic_and_add_socket("public", user, oauth_token)
453
454 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
455 assert_receive {:render_with_user, _, _, ^activity}
456 refute Streamer.filtered_by_user?(other_user, activity)
457 end
458
459 test "it sends to public (unauthenticated)" do
460 user = insert(:user)
461
462 Streamer.get_topic_and_add_socket("public", nil, nil)
463
464 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
465 activity_id = activity.id
466 assert_receive {:text, event}
467 assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
468 assert %{"id" => ^activity_id} = Jason.decode!(payload)
469
470 {:ok, _} = CommonAPI.delete(activity.id, user)
471 assert_receive {:text, event}
472 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
473 end
474
475 test "handles deletions" do
476 %{user: user, token: oauth_token} = oauth_access(["read"])
477 other_user = insert(:user)
478 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
479
480 Streamer.get_topic_and_add_socket("public", user, oauth_token)
481
482 {:ok, _} = CommonAPI.delete(activity.id, other_user)
483 activity_id = activity.id
484 assert_receive {:text, event}
485 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
486 end
487 end
488
489 describe "thread_containment/2" do
490 test "it filters to user if recipients invalid and thread containment is enabled" do
491 clear_config([:instance, :skip_thread_containment], false)
492 author = insert(:user)
493 %{user: user, token: oauth_token} = oauth_access(["read"])
494 User.follow(user, author, :follow_accept)
495
496 activity =
497 insert(:note_activity,
498 note:
499 insert(:note,
500 user: author,
501 data: %{"to" => ["TEST-FFF"]}
502 )
503 )
504
505 Streamer.get_topic_and_add_socket("public", user, oauth_token)
506 Streamer.stream("public", activity)
507 assert_receive {:render_with_user, _, _, ^activity}
508 assert Streamer.filtered_by_user?(user, activity)
509 end
510
511 test "it sends message if recipients invalid and thread containment is disabled" do
512 clear_config([:instance, :skip_thread_containment], true)
513 author = insert(:user)
514 %{user: user, token: oauth_token} = oauth_access(["read"])
515 User.follow(user, author, :follow_accept)
516
517 activity =
518 insert(:note_activity,
519 note:
520 insert(:note,
521 user: author,
522 data: %{"to" => ["TEST-FFF"]}
523 )
524 )
525
526 Streamer.get_topic_and_add_socket("public", user, oauth_token)
527 Streamer.stream("public", activity)
528
529 assert_receive {:render_with_user, _, _, ^activity}
530 refute Streamer.filtered_by_user?(user, activity)
531 end
532
533 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
534 clear_config([:instance, :skip_thread_containment], false)
535 author = insert(:user)
536 user = insert(:user, skip_thread_containment: true)
537 %{token: oauth_token} = oauth_access(["read"], user: user)
538 User.follow(user, author, :follow_accept)
539
540 activity =
541 insert(:note_activity,
542 note:
543 insert(:note,
544 user: author,
545 data: %{"to" => ["TEST-FFF"]}
546 )
547 )
548
549 Streamer.get_topic_and_add_socket("public", user, oauth_token)
550 Streamer.stream("public", activity)
551
552 assert_receive {:render_with_user, _, _, ^activity}
553 refute Streamer.filtered_by_user?(user, activity)
554 end
555 end
556
557 describe "blocks" do
558 setup do: oauth_access(["read"])
559
560 test "it filters messages involving blocked users", %{user: user, token: oauth_token} do
561 blocked_user = insert(:user)
562 {:ok, _user_relationship} = User.block(user, blocked_user)
563
564 Streamer.get_topic_and_add_socket("public", user, oauth_token)
565 {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
566 assert_receive {:render_with_user, _, _, ^activity}
567 assert Streamer.filtered_by_user?(user, activity)
568 end
569
570 test "it filters messages transitively involving blocked users", %{
571 user: blocker,
572 token: blocker_token
573 } do
574 blockee = insert(:user)
575 friend = insert(:user)
576
577 Streamer.get_topic_and_add_socket("public", blocker, blocker_token)
578
579 {:ok, _user_relationship} = User.block(blocker, blockee)
580
581 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
582
583 assert_receive {:render_with_user, _, _, ^activity_one}
584 assert Streamer.filtered_by_user?(blocker, activity_one)
585
586 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
587
588 assert_receive {:render_with_user, _, _, ^activity_two}
589 assert Streamer.filtered_by_user?(blocker, activity_two)
590
591 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
592
593 assert_receive {:render_with_user, _, _, ^activity_three}
594 assert Streamer.filtered_by_user?(blocker, activity_three)
595 end
596 end
597
598 describe "lists" do
599 setup do: oauth_access(["read"])
600
601 test "it doesn't send unwanted DMs to list", %{user: user_a, token: user_a_token} do
602 user_b = insert(:user)
603 user_c = insert(:user)
604
605 {:ok, user_a, user_b} = User.follow(user_a, user_b)
606
607 {:ok, list} = List.create("Test", user_a)
608 {:ok, list} = List.follow(list, user_b)
609
610 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
611
612 {:ok, _activity} =
613 CommonAPI.post(user_b, %{
614 status: "@#{user_c.nickname} Test",
615 visibility: "direct"
616 })
617
618 refute_receive _
619 end
620
621 test "it doesn't send unwanted private posts to list", %{user: user_a, token: user_a_token} do
622 user_b = insert(:user)
623
624 {:ok, list} = List.create("Test", user_a)
625 {:ok, list} = List.follow(list, user_b)
626
627 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
628
629 {:ok, _activity} =
630 CommonAPI.post(user_b, %{
631 status: "Test",
632 visibility: "private"
633 })
634
635 refute_receive _
636 end
637
638 test "it sends wanted private posts to list", %{user: user_a, token: user_a_token} do
639 user_b = insert(:user)
640
641 {:ok, user_a, user_b} = User.follow(user_a, user_b)
642
643 {:ok, list} = List.create("Test", user_a)
644 {:ok, list} = List.follow(list, user_b)
645
646 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
647
648 {:ok, activity} =
649 CommonAPI.post(user_b, %{
650 status: "Test",
651 visibility: "private"
652 })
653
654 assert_receive {:render_with_user, _, _, ^activity}
655 refute Streamer.filtered_by_user?(user_a, activity)
656 end
657 end
658
659 describe "muted reblogs" do
660 setup do: oauth_access(["read"])
661
662 test "it filters muted reblogs", %{user: user1, token: user1_token} do
663 user2 = insert(:user)
664 user3 = insert(:user)
665 CommonAPI.follow(user1, user2)
666 CommonAPI.hide_reblogs(user1, user2)
667
668 {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
669
670 Streamer.get_topic_and_add_socket("user", user1, user1_token)
671 {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
672 assert_receive {:render_with_user, _, _, ^announce_activity}
673 assert Streamer.filtered_by_user?(user1, announce_activity)
674 end
675
676 test "it filters reblog notification for reblog-muted actors", %{
677 user: user1,
678 token: user1_token
679 } do
680 user2 = insert(:user)
681 CommonAPI.follow(user1, user2)
682 CommonAPI.hide_reblogs(user1, user2)
683
684 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
685 Streamer.get_topic_and_add_socket("user", user1, user1_token)
686 {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)
687
688 assert_receive {:render_with_user, _, "notification.json", notif}
689 assert Streamer.filtered_by_user?(user1, notif)
690 end
691
692 test "it send non-reblog notification for reblog-muted actors", %{
693 user: user1,
694 token: user1_token
695 } do
696 user2 = insert(:user)
697 CommonAPI.follow(user1, user2)
698 CommonAPI.hide_reblogs(user1, user2)
699
700 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
701 Streamer.get_topic_and_add_socket("user", user1, user1_token)
702 {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)
703
704 assert_receive {:render_with_user, _, "notification.json", notif}
705 refute Streamer.filtered_by_user?(user1, notif)
706 end
707 end
708
709 describe "muted threads" do
710 test "it filters posts from muted threads" do
711 user = insert(:user)
712 %{user: user2, token: user2_token} = oauth_access(["read"])
713 Streamer.get_topic_and_add_socket("user", user2, user2_token)
714
715 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
716 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
717 {:ok, _} = CommonAPI.add_mute(user2, activity)
718
719 assert_receive {:render_with_user, _, _, ^activity}
720 assert Streamer.filtered_by_user?(user2, activity)
721 end
722 end
723
724 describe "direct streams" do
725 setup do: oauth_access(["read"])
726
727 test "it sends conversation update to the 'direct' stream", %{user: user, token: oauth_token} do
728 another_user = insert(:user)
729
730 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
731
732 {:ok, _create_activity} =
733 CommonAPI.post(another_user, %{
734 status: "hey @#{user.nickname}",
735 visibility: "direct"
736 })
737
738 assert_receive {:text, received_event}
739
740 assert %{"event" => "conversation", "payload" => received_payload} =
741 Jason.decode!(received_event)
742
743 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
744 [participation] = Participation.for_user(user)
745 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
746 end
747
748 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted",
749 %{user: user, token: oauth_token} do
750 another_user = insert(:user)
751
752 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
753
754 {:ok, create_activity} =
755 CommonAPI.post(another_user, %{
756 status: "hi @#{user.nickname}",
757 visibility: "direct"
758 })
759
760 create_activity_id = create_activity.id
761 assert_receive {:render_with_user, _, _, ^create_activity}
762 assert_receive {:text, received_conversation1}
763 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
764
765 {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
766
767 assert_receive {:text, received_event}
768
769 assert %{"event" => "delete", "payload" => ^create_activity_id} =
770 Jason.decode!(received_event)
771
772 refute_receive _
773 end
774
775 @tag :erratic
776 test "it sends conversation update to the 'direct' stream when a message is deleted", %{
777 user: user,
778 token: oauth_token
779 } do
780 another_user = insert(:user)
781 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
782
783 {:ok, create_activity} =
784 CommonAPI.post(another_user, %{
785 status: "hi @#{user.nickname}",
786 visibility: "direct"
787 })
788
789 {:ok, create_activity2} =
790 CommonAPI.post(another_user, %{
791 status: "hi @#{user.nickname} 2",
792 in_reply_to_status_id: create_activity.id,
793 visibility: "direct"
794 })
795
796 assert_receive {:render_with_user, _, _, ^create_activity}
797 assert_receive {:render_with_user, _, _, ^create_activity2}
798 assert_receive {:text, received_conversation1}
799 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
800 assert_receive {:text, received_conversation1}
801 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
802
803 {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)
804
805 assert_receive {:text, received_event}
806 assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)
807
808 assert_receive {:text, received_event}
809
810 assert %{"event" => "conversation", "payload" => received_payload} =
811 Jason.decode!(received_event)
812
813 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
814 assert last_status["id"] == to_string(create_activity.id)
815 end
816 end
817 end