Merge branch 'develop' into docs/apache-config
[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 user_url = user.ap_id
387 user2 = insert(:user)
388
389 body =
390 File.read!("test/fixtures/users_mock/localhost.json")
391 |> String.replace("{{nickname}}", user.nickname)
392 |> Jason.encode!()
393
394 Tesla.Mock.mock_global(fn
395 %{method: :get, url: ^user_url} ->
396 %Tesla.Env{status: 200, body: body}
397 end)
398
399 Streamer.get_topic_and_add_socket("user:notification", user, oauth_token)
400 {:ok, _follower, _followed, follow_activity} = CommonAPI.follow(user2, user)
401
402 assert_receive {:render_with_user, _, "notification.json", notif}
403 assert notif.activity.id == follow_activity.id
404 refute Streamer.filtered_by_user?(user, notif)
405 end
406
407 test "it sends follow relationships updates to the 'user' stream", %{
408 user: user,
409 token: oauth_token
410 } do
411 user_id = user.id
412 user_url = user.ap_id
413 other_user = insert(:user)
414 other_user_id = other_user.id
415
416 body =
417 File.read!("test/fixtures/users_mock/localhost.json")
418 |> String.replace("{{nickname}}", user.nickname)
419 |> Jason.encode!()
420
421 Tesla.Mock.mock_global(fn
422 %{method: :get, url: ^user_url} ->
423 %Tesla.Env{status: 200, body: body}
424 end)
425
426 Streamer.get_topic_and_add_socket("user", user, oauth_token)
427 {:ok, _follower, _followed, _follow_activity} = CommonAPI.follow(user, other_user)
428
429 assert_receive {:text, event}
430
431 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
432 Jason.decode!(event)
433
434 assert %{
435 "follower" => %{
436 "follower_count" => 0,
437 "following_count" => 0,
438 "id" => ^user_id
439 },
440 "following" => %{
441 "follower_count" => 0,
442 "following_count" => 0,
443 "id" => ^other_user_id
444 },
445 "state" => "follow_pending"
446 } = Jason.decode!(payload)
447
448 assert_receive {:text, event}
449
450 assert %{"event" => "pleroma:follow_relationships_update", "payload" => payload} =
451 Jason.decode!(event)
452
453 assert %{
454 "follower" => %{
455 "follower_count" => 0,
456 "following_count" => 1,
457 "id" => ^user_id
458 },
459 "following" => %{
460 "follower_count" => 1,
461 "following_count" => 0,
462 "id" => ^other_user_id
463 },
464 "state" => "follow_accept"
465 } = Jason.decode!(payload)
466 end
467 end
468
469 describe "public streams" do
470 test "it sends to public (authenticated)" do
471 %{user: user, token: oauth_token} = oauth_access(["read"])
472 other_user = insert(:user)
473
474 Streamer.get_topic_and_add_socket("public", user, oauth_token)
475
476 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
477 assert_receive {:render_with_user, _, _, ^activity}
478 refute Streamer.filtered_by_user?(other_user, activity)
479 end
480
481 test "it sends to public (unauthenticated)" do
482 user = insert(:user)
483
484 Streamer.get_topic_and_add_socket("public", nil, nil)
485
486 {:ok, activity} = CommonAPI.post(user, %{status: "Test"})
487 activity_id = activity.id
488 assert_receive {:text, event}
489 assert %{"event" => "update", "payload" => payload} = Jason.decode!(event)
490 assert %{"id" => ^activity_id} = Jason.decode!(payload)
491
492 {:ok, _} = CommonAPI.delete(activity.id, user)
493 assert_receive {:text, event}
494 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
495 end
496
497 test "handles deletions" do
498 %{user: user, token: oauth_token} = oauth_access(["read"])
499 other_user = insert(:user)
500 {:ok, activity} = CommonAPI.post(other_user, %{status: "Test"})
501
502 Streamer.get_topic_and_add_socket("public", user, oauth_token)
503
504 {:ok, _} = CommonAPI.delete(activity.id, other_user)
505 activity_id = activity.id
506 assert_receive {:text, event}
507 assert %{"event" => "delete", "payload" => ^activity_id} = Jason.decode!(event)
508 end
509 end
510
511 describe "thread_containment/2" do
512 test "it filters to user if recipients invalid and thread containment is enabled" do
513 Pleroma.Config.put([:instance, :skip_thread_containment], false)
514 author = insert(:user)
515 %{user: user, token: oauth_token} = oauth_access(["read"])
516 User.follow(user, author, :follow_accept)
517
518 activity =
519 insert(:note_activity,
520 note:
521 insert(:note,
522 user: author,
523 data: %{"to" => ["TEST-FFF"]}
524 )
525 )
526
527 Streamer.get_topic_and_add_socket("public", user, oauth_token)
528 Streamer.stream("public", activity)
529 assert_receive {:render_with_user, _, _, ^activity}
530 assert Streamer.filtered_by_user?(user, activity)
531 end
532
533 test "it sends message if recipients invalid and thread containment is disabled" do
534 Pleroma.Config.put([:instance, :skip_thread_containment], true)
535 author = insert(:user)
536 %{user: user, token: oauth_token} = oauth_access(["read"])
537 User.follow(user, author, :follow_accept)
538
539 activity =
540 insert(:note_activity,
541 note:
542 insert(:note,
543 user: author,
544 data: %{"to" => ["TEST-FFF"]}
545 )
546 )
547
548 Streamer.get_topic_and_add_socket("public", user, oauth_token)
549 Streamer.stream("public", activity)
550
551 assert_receive {:render_with_user, _, _, ^activity}
552 refute Streamer.filtered_by_user?(user, activity)
553 end
554
555 test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
556 Pleroma.Config.put([:instance, :skip_thread_containment], false)
557 author = insert(:user)
558 user = insert(:user, skip_thread_containment: true)
559 %{token: oauth_token} = oauth_access(["read"], user: user)
560 User.follow(user, author, :follow_accept)
561
562 activity =
563 insert(:note_activity,
564 note:
565 insert(:note,
566 user: author,
567 data: %{"to" => ["TEST-FFF"]}
568 )
569 )
570
571 Streamer.get_topic_and_add_socket("public", user, oauth_token)
572 Streamer.stream("public", activity)
573
574 assert_receive {:render_with_user, _, _, ^activity}
575 refute Streamer.filtered_by_user?(user, activity)
576 end
577 end
578
579 describe "blocks" do
580 setup do: oauth_access(["read"])
581
582 test "it filters messages involving blocked users", %{user: user, token: oauth_token} do
583 blocked_user = insert(:user)
584 {:ok, _user_relationship} = User.block(user, blocked_user)
585
586 Streamer.get_topic_and_add_socket("public", user, oauth_token)
587 {:ok, activity} = CommonAPI.post(blocked_user, %{status: "Test"})
588 assert_receive {:render_with_user, _, _, ^activity}
589 assert Streamer.filtered_by_user?(user, activity)
590 end
591
592 test "it filters messages transitively involving blocked users", %{
593 user: blocker,
594 token: blocker_token
595 } do
596 blockee = insert(:user)
597 friend = insert(:user)
598
599 Streamer.get_topic_and_add_socket("public", blocker, blocker_token)
600
601 {:ok, _user_relationship} = User.block(blocker, blockee)
602
603 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
604
605 assert_receive {:render_with_user, _, _, ^activity_one}
606 assert Streamer.filtered_by_user?(blocker, activity_one)
607
608 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
609
610 assert_receive {:render_with_user, _, _, ^activity_two}
611 assert Streamer.filtered_by_user?(blocker, activity_two)
612
613 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
614
615 assert_receive {:render_with_user, _, _, ^activity_three}
616 assert Streamer.filtered_by_user?(blocker, activity_three)
617 end
618 end
619
620 describe "lists" do
621 setup do: oauth_access(["read"])
622
623 test "it doesn't send unwanted DMs to list", %{user: user_a, token: user_a_token} do
624 user_b = insert(:user)
625 user_c = insert(:user)
626
627 {:ok, user_a, user_b} = User.follow(user_a, user_b)
628
629 {:ok, list} = List.create("Test", user_a)
630 {:ok, list} = List.follow(list, user_b)
631
632 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
633
634 {:ok, _activity} =
635 CommonAPI.post(user_b, %{
636 status: "@#{user_c.nickname} Test",
637 visibility: "direct"
638 })
639
640 refute_receive _
641 end
642
643 test "it doesn't send unwanted private posts to list", %{user: user_a, token: user_a_token} do
644 user_b = insert(:user)
645
646 {:ok, list} = List.create("Test", user_a)
647 {:ok, list} = List.follow(list, user_b)
648
649 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
650
651 {:ok, _activity} =
652 CommonAPI.post(user_b, %{
653 status: "Test",
654 visibility: "private"
655 })
656
657 refute_receive _
658 end
659
660 test "it sends wanted private posts to list", %{user: user_a, token: user_a_token} do
661 user_b = insert(:user)
662
663 {:ok, user_a, user_b} = User.follow(user_a, user_b)
664
665 {:ok, list} = List.create("Test", user_a)
666 {:ok, list} = List.follow(list, user_b)
667
668 Streamer.get_topic_and_add_socket("list", user_a, user_a_token, %{"list" => list.id})
669
670 {:ok, activity} =
671 CommonAPI.post(user_b, %{
672 status: "Test",
673 visibility: "private"
674 })
675
676 assert_receive {:render_with_user, _, _, ^activity}
677 refute Streamer.filtered_by_user?(user_a, activity)
678 end
679 end
680
681 describe "muted reblogs" do
682 setup do: oauth_access(["read"])
683
684 test "it filters muted reblogs", %{user: user1, token: user1_token} do
685 user2 = insert(:user)
686 user3 = insert(:user)
687 CommonAPI.follow(user1, user2)
688 CommonAPI.hide_reblogs(user1, user2)
689
690 {:ok, create_activity} = CommonAPI.post(user3, %{status: "I'm kawen"})
691
692 Streamer.get_topic_and_add_socket("user", user1, user1_token)
693 {:ok, announce_activity} = CommonAPI.repeat(create_activity.id, user2)
694 assert_receive {:render_with_user, _, _, ^announce_activity}
695 assert Streamer.filtered_by_user?(user1, announce_activity)
696 end
697
698 test "it filters reblog notification for reblog-muted actors", %{
699 user: user1,
700 token: user1_token
701 } do
702 user2 = insert(:user)
703 CommonAPI.follow(user1, user2)
704 CommonAPI.hide_reblogs(user1, user2)
705
706 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
707 Streamer.get_topic_and_add_socket("user", user1, user1_token)
708 {:ok, _announce_activity} = CommonAPI.repeat(create_activity.id, user2)
709
710 assert_receive {:render_with_user, _, "notification.json", notif}
711 assert Streamer.filtered_by_user?(user1, notif)
712 end
713
714 test "it send non-reblog notification for reblog-muted actors", %{
715 user: user1,
716 token: user1_token
717 } do
718 user2 = insert(:user)
719 CommonAPI.follow(user1, user2)
720 CommonAPI.hide_reblogs(user1, user2)
721
722 {:ok, create_activity} = CommonAPI.post(user1, %{status: "I'm kawen"})
723 Streamer.get_topic_and_add_socket("user", user1, user1_token)
724 {:ok, _favorite_activity} = CommonAPI.favorite(user2, create_activity.id)
725
726 assert_receive {:render_with_user, _, "notification.json", notif}
727 refute Streamer.filtered_by_user?(user1, notif)
728 end
729 end
730
731 describe "muted threads" do
732 test "it filters posts from muted threads" do
733 user = insert(:user)
734 %{user: user2, token: user2_token} = oauth_access(["read"])
735 Streamer.get_topic_and_add_socket("user", user2, user2_token)
736
737 {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)
738 {:ok, activity} = CommonAPI.post(user, %{status: "super hot take"})
739 {:ok, _} = CommonAPI.add_mute(user2, activity)
740
741 assert_receive {:render_with_user, _, _, ^activity}
742 assert Streamer.filtered_by_user?(user2, activity)
743 end
744 end
745
746 describe "direct streams" do
747 setup do: oauth_access(["read"])
748
749 test "it sends conversation update to the 'direct' stream", %{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: "hey @#{user.nickname}",
757 visibility: "direct"
758 })
759
760 assert_receive {:text, received_event}
761
762 assert %{"event" => "conversation", "payload" => received_payload} =
763 Jason.decode!(received_event)
764
765 assert %{"last_status" => last_status} = Jason.decode!(received_payload)
766 [participation] = Participation.for_user(user)
767 assert last_status["pleroma"]["direct_conversation_id"] == participation.id
768 end
769
770 test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted",
771 %{user: user, token: oauth_token} do
772 another_user = insert(:user)
773
774 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
775
776 {:ok, create_activity} =
777 CommonAPI.post(another_user, %{
778 status: "hi @#{user.nickname}",
779 visibility: "direct"
780 })
781
782 create_activity_id = create_activity.id
783 assert_receive {:render_with_user, _, _, ^create_activity}
784 assert_receive {:text, received_conversation1}
785 assert %{"event" => "conversation", "payload" => _} = Jason.decode!(received_conversation1)
786
787 {:ok, _} = CommonAPI.delete(create_activity_id, another_user)
788
789 assert_receive {:text, received_event}
790
791 assert %{"event" => "delete", "payload" => ^create_activity_id} =
792 Jason.decode!(received_event)
793
794 refute_receive _
795 end
796
797 test "it sends conversation update to the 'direct' stream when a message is deleted", %{
798 user: user,
799 token: oauth_token
800 } do
801 another_user = insert(:user)
802 Streamer.get_topic_and_add_socket("direct", user, oauth_token)
803
804 {:ok, create_activity} =
805 CommonAPI.post(another_user, %{
806 status: "hi @#{user.nickname}",
807 visibility: "direct"
808 })
809
810 {:ok, create_activity2} =
811 CommonAPI.post(another_user, %{
812 status: "hi @#{user.nickname} 2",
813 in_reply_to_status_id: create_activity.id,
814 visibility: "direct"
815 })
816
817 assert_receive {:render_with_user, _, _, ^create_activity}
818 assert_receive {:render_with_user, _, _, ^create_activity2}
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 end