f609db7a43de53157aa02627ca74d45ca312c673
[akkoma] / test / pleroma / web / mastodon_api / controllers / timeline_controller_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.MastodonAPI.TimelineControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9 import Tesla.Mock
10
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13
14 setup do
15 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
16 :ok
17 end
18
19 describe "home" do
20 setup do: oauth_access(["read:statuses"])
21
22 test "does NOT embed account/pleroma/relationship in statuses", %{
23 user: user,
24 conn: conn
25 } do
26 other_user = insert(:user)
27
28 {:ok, _} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
29
30 response =
31 conn
32 |> assign(:user, user)
33 |> get("/api/v1/timelines/home")
34 |> json_response_and_validate_schema(200)
35
36 assert Enum.all?(response, fn n ->
37 get_in(n, ["account", "pleroma", "relationship"]) == %{}
38 end)
39 end
40
41 test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do
42 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
43 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
44
45 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
46
47 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
48
49 conn = get(conn, "/api/v1/timelines/home?exclude_visibilities[]=direct")
50
51 assert status_ids = json_response_and_validate_schema(conn, :ok) |> Enum.map(& &1["id"])
52 assert public_activity.id in status_ids
53 assert unlisted_activity.id in status_ids
54 assert private_activity.id in status_ids
55 refute direct_activity.id in status_ids
56 end
57
58 test "muted emotions", %{user: user, conn: conn} do
59 other_user = insert(:user)
60 {:ok, activity} = CommonAPI.post(user, %{status: "."})
61
62 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
63 User.mute(user, other_user)
64
65 result =
66 conn
67 |> assign(:user, user)
68 |> get("/api/v1/timelines/home")
69 |> json_response_and_validate_schema(200)
70
71 assert [
72 %{
73 "pleroma" => %{
74 "emoji_reactions" => []
75 }
76 }
77 ] = result
78
79 result =
80 conn
81 |> assign(:user, user)
82 |> get("/api/v1/timelines/home?with_muted=true")
83 |> json_response_and_validate_schema(200)
84
85 assert [
86 %{
87 "pleroma" => %{
88 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
89 }
90 }
91 ] = result
92 end
93
94 test "filtering", %{conn: conn, user: user} do
95 local_user = insert(:user)
96 {:ok, user, local_user} = User.follow(user, local_user)
97 {:ok, local_activity} = CommonAPI.post(local_user, %{status: "Status"})
98 with_media = create_with_media_activity(local_user)
99
100 remote_user = insert(:user, local: false)
101 {:ok, _user, remote_user} = User.follow(user, remote_user)
102 remote_activity = create_remote_activity(remote_user)
103
104 without_filter_ids =
105 conn
106 |> get("/api/v1/timelines/home")
107 |> json_response_and_validate_schema(200)
108 |> Enum.map(& &1["id"])
109
110 assert local_activity.id in without_filter_ids
111 assert remote_activity.id in without_filter_ids
112 assert with_media.id in without_filter_ids
113
114 only_local_ids =
115 conn
116 |> get("/api/v1/timelines/home?local=true")
117 |> json_response_and_validate_schema(200)
118 |> Enum.map(& &1["id"])
119
120 assert local_activity.id in only_local_ids
121 refute remote_activity.id in only_local_ids
122 assert with_media.id in only_local_ids
123
124 only_local_media_ids =
125 conn
126 |> get("/api/v1/timelines/home?local=true&only_media=true")
127 |> json_response_and_validate_schema(200)
128 |> Enum.map(& &1["id"])
129
130 refute local_activity.id in only_local_media_ids
131 refute remote_activity.id in only_local_media_ids
132 assert with_media.id in only_local_media_ids
133
134 remote_ids =
135 conn
136 |> get("/api/v1/timelines/home?remote=true")
137 |> json_response_and_validate_schema(200)
138 |> Enum.map(& &1["id"])
139
140 refute local_activity.id in remote_ids
141 assert remote_activity.id in remote_ids
142 refute with_media.id in remote_ids
143
144 assert conn
145 |> get("/api/v1/timelines/home?remote=true&only_media=true")
146 |> json_response_and_validate_schema(200) == []
147
148 assert conn
149 |> get("/api/v1/timelines/home?remote=true&local=true")
150 |> json_response_and_validate_schema(200) == []
151 end
152 end
153
154 describe "public" do
155 @tag capture_log: true
156 test "the public timeline", %{conn: conn} do
157 user = insert(:user)
158
159 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
160 with_media = create_with_media_activity(user)
161
162 remote = insert(:note_activity, local: false)
163
164 assert conn
165 |> get("/api/v1/timelines/public?local=False")
166 |> json_response_and_validate_schema(:ok)
167 |> length == 3
168
169 local_ids =
170 conn
171 |> get("/api/v1/timelines/public?local=True")
172 |> json_response_and_validate_schema(:ok)
173 |> Enum.map(& &1["id"])
174
175 assert activity.id in local_ids
176 assert with_media.id in local_ids
177 refute remote.id in local_ids
178
179 local_ids =
180 conn
181 |> get("/api/v1/timelines/public?local=True")
182 |> json_response_and_validate_schema(:ok)
183 |> Enum.map(& &1["id"])
184
185 assert activity.id in local_ids
186 assert with_media.id in local_ids
187 refute remote.id in local_ids
188
189 local_ids =
190 conn
191 |> get("/api/v1/timelines/public?local=True&only_media=true")
192 |> json_response_and_validate_schema(:ok)
193 |> Enum.map(& &1["id"])
194
195 refute activity.id in local_ids
196 assert with_media.id in local_ids
197 refute remote.id in local_ids
198
199 local_ids =
200 conn
201 |> get("/api/v1/timelines/public?local=1")
202 |> json_response_and_validate_schema(:ok)
203 |> Enum.map(& &1["id"])
204
205 assert activity.id in local_ids
206 assert with_media.id in local_ids
207 refute remote.id in local_ids
208
209 remote_id = remote.id
210
211 assert [%{"id" => ^remote_id}] =
212 conn
213 |> get("/api/v1/timelines/public?remote=true")
214 |> json_response_and_validate_schema(:ok)
215
216 with_media_id = with_media.id
217
218 assert [%{"id" => ^with_media_id}] =
219 conn
220 |> get("/api/v1/timelines/public?only_media=true")
221 |> json_response_and_validate_schema(:ok)
222
223 assert conn
224 |> get("/api/v1/timelines/public?remote=true&only_media=true")
225 |> json_response_and_validate_schema(:ok) == []
226
227 # does not contain repeats
228 {:ok, _} = CommonAPI.repeat(activity.id, user)
229
230 assert [_, _] =
231 conn
232 |> get("/api/v1/timelines/public?local=true")
233 |> json_response_and_validate_schema(:ok)
234 end
235
236 test "the public timeline includes only public statuses for an authenticated user" do
237 %{user: user, conn: conn} = oauth_access(["read:statuses"])
238
239 {:ok, _activity} = CommonAPI.post(user, %{status: "test"})
240 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "private"})
241 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "unlisted"})
242 {:ok, _activity} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
243
244 res_conn = get(conn, "/api/v1/timelines/public")
245 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
246 end
247
248 test "doesn't return replies if follower is posting with blocked user" do
249 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
250 [blockee, friend] = insert_list(2, :user)
251 {:ok, blocker, friend} = User.follow(blocker, friend)
252 {:ok, _} = User.block(blocker, blockee)
253
254 conn = assign(conn, :user, blocker)
255
256 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
257
258 {:ok, reply_from_blockee} =
259 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
260
261 {:ok, _reply_from_friend} =
262 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
263
264 # Still shows replies from yourself
265 {:ok, %{id: reply_from_me}} =
266 CommonAPI.post(blocker, %{status: "status", in_reply_to_status_id: reply_from_blockee})
267
268 response =
269 get(conn, "/api/v1/timelines/public")
270 |> json_response_and_validate_schema(200)
271
272 assert length(response) == 2
273 [%{"id" => ^reply_from_me}, %{"id" => ^activity_id}] = response
274 end
275
276 test "doesn't return posts from users who blocked you when :blockers_visible is disabled" do
277 clear_config([:activitypub, :blockers_visible], false)
278
279 %{conn: conn, user: blockee} = oauth_access(["read:statuses"])
280 blocker = insert(:user)
281 {:ok, _} = User.block(blocker, blockee)
282
283 conn = assign(conn, :user, blockee)
284
285 {:ok, _} = CommonAPI.post(blocker, %{status: "hey!"})
286
287 response =
288 get(conn, "/api/v1/timelines/public")
289 |> json_response_and_validate_schema(200)
290
291 assert length(response) == 0
292 end
293
294 test "doesn't return replies if follow is posting with users from blocked domain" do
295 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
296 friend = insert(:user)
297 blockee = insert(:user, ap_id: "https://example.com/users/blocked")
298 {:ok, blocker, friend} = User.follow(blocker, friend)
299 {:ok, blocker} = User.block_domain(blocker, "example.com")
300
301 conn = assign(conn, :user, blocker)
302
303 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
304
305 {:ok, reply_from_blockee} =
306 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
307
308 {:ok, _reply_from_friend} =
309 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
310
311 res_conn = get(conn, "/api/v1/timelines/public")
312
313 activities = json_response_and_validate_schema(res_conn, 200)
314 [%{"id" => ^activity_id}] = activities
315 end
316
317 test "can be filtered by instance", %{conn: conn} do
318 user = insert(:user, ap_id: "https://lain.com/users/lain")
319 insert(:note_activity, local: false)
320 insert(:note_activity, local: false)
321
322 {:ok, _} = CommonAPI.post(user, %{status: "test"})
323
324 conn = get(conn, "/api/v1/timelines/public?instance=lain.com")
325
326 assert length(json_response_and_validate_schema(conn, :ok)) == 1
327 end
328
329 test "muted emotions", %{conn: conn} do
330 user = insert(:user)
331 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
332
333 conn =
334 conn
335 |> assign(:user, user)
336 |> assign(:token, token)
337
338 other_user = insert(:user)
339 {:ok, activity} = CommonAPI.post(user, %{status: "."})
340
341 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
342 User.mute(user, other_user)
343
344 result =
345 conn
346 |> get("/api/v1/timelines/public")
347 |> json_response_and_validate_schema(200)
348
349 assert [
350 %{
351 "pleroma" => %{
352 "emoji_reactions" => []
353 }
354 }
355 ] = result
356
357 result =
358 conn
359 |> get("/api/v1/timelines/public?with_muted=true")
360 |> json_response_and_validate_schema(200)
361
362 assert [
363 %{
364 "pleroma" => %{
365 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
366 }
367 }
368 ] = result
369 end
370
371 test "should return local-only posts for authenticated users" do
372 user = insert(:user)
373 %{user: _reader, conn: conn} = oauth_access(["read:statuses"])
374
375 {:ok, %{id: id}} = CommonAPI.post(user, %{status: "#2hu #2HU", visibility: "local"})
376
377 result =
378 conn
379 |> get("/api/v1/timelines/public")
380 |> json_response_and_validate_schema(200)
381
382 assert [%{"id" => ^id}] = result
383 end
384
385 test "should not return local-only posts for users without read:statuses" do
386 user = insert(:user)
387 %{user: _reader, conn: conn} = oauth_access([])
388
389 {:ok, _activity} = CommonAPI.post(user, %{status: "#2hu #2HU", visibility: "local"})
390
391 result =
392 conn
393 |> get("/api/v1/timelines/public")
394 |> json_response_and_validate_schema(200)
395
396 assert [] = result
397 end
398
399 test "should not return local-only posts for anonymous users" do
400 user = insert(:user)
401
402 {:ok, _activity} = CommonAPI.post(user, %{status: "#2hu #2HU", visibility: "local"})
403
404 result =
405 build_conn()
406 |> get("/api/v1/timelines/public")
407 |> json_response_and_validate_schema(200)
408
409 assert [] = result
410 end
411 end
412
413 defp local_and_remote_activities do
414 insert(:note_activity)
415 insert(:note_activity, local: false)
416 :ok
417 end
418
419 describe "public with restrict unauthenticated timeline for local and federated timelines" do
420 setup do: local_and_remote_activities()
421
422 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
423
424 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
425
426 test "if user is unauthenticated", %{conn: conn} do
427 res_conn = get(conn, "/api/v1/timelines/public?local=true")
428
429 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
430 "error" => "authorization required for timeline view"
431 }
432
433 res_conn = get(conn, "/api/v1/timelines/public?local=false")
434
435 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
436 "error" => "authorization required for timeline view"
437 }
438 end
439
440 test "if user is authenticated" do
441 %{conn: conn} = oauth_access(["read:statuses"])
442
443 res_conn = get(conn, "/api/v1/timelines/public?local=true")
444 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
445
446 res_conn = get(conn, "/api/v1/timelines/public?local=false")
447 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
448 end
449 end
450
451 describe "public with restrict unauthenticated timeline for local" do
452 setup do: local_and_remote_activities()
453
454 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
455
456 test "if user is unauthenticated", %{conn: conn} do
457 res_conn = get(conn, "/api/v1/timelines/public?local=true")
458
459 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
460 "error" => "authorization required for timeline view"
461 }
462
463 res_conn = get(conn, "/api/v1/timelines/public?local=false")
464 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
465 end
466
467 test "if user is authenticated", %{conn: _conn} do
468 %{conn: conn} = oauth_access(["read:statuses"])
469
470 res_conn = get(conn, "/api/v1/timelines/public?local=true")
471 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
472
473 res_conn = get(conn, "/api/v1/timelines/public?local=false")
474 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
475 end
476 end
477
478 describe "public with restrict unauthenticated timeline for remote" do
479 setup do: local_and_remote_activities()
480
481 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
482
483 test "if user is unauthenticated", %{conn: conn} do
484 res_conn = get(conn, "/api/v1/timelines/public?local=true")
485 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
486
487 res_conn = get(conn, "/api/v1/timelines/public?local=false")
488
489 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
490 "error" => "authorization required for timeline view"
491 }
492 end
493
494 test "if user is authenticated", %{conn: _conn} do
495 %{conn: conn} = oauth_access(["read:statuses"])
496
497 res_conn = get(conn, "/api/v1/timelines/public?local=true")
498 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
499
500 res_conn = get(conn, "/api/v1/timelines/public?local=false")
501 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
502 end
503 end
504
505 describe "direct" do
506 test "direct timeline", %{conn: conn} do
507 user_one = insert(:user)
508 user_two = insert(:user)
509
510 {:ok, user_two, user_one} = User.follow(user_two, user_one)
511
512 {:ok, direct} =
513 CommonAPI.post(user_one, %{
514 status: "Hi @#{user_two.nickname}!",
515 visibility: "direct"
516 })
517
518 {:ok, _follower_only} =
519 CommonAPI.post(user_one, %{
520 status: "Hi @#{user_two.nickname}!",
521 visibility: "private"
522 })
523
524 conn_user_two =
525 conn
526 |> assign(:user, user_two)
527 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
528
529 # Only direct should be visible here
530 res_conn = get(conn_user_two, "api/v1/timelines/direct")
531
532 assert [status] = json_response_and_validate_schema(res_conn, :ok)
533
534 assert %{"visibility" => "direct"} = status
535 assert status["url"] != direct.data["id"]
536
537 # User should be able to see their own direct message
538 res_conn =
539 build_conn()
540 |> assign(:user, user_one)
541 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
542 |> get("api/v1/timelines/direct")
543
544 [status] = json_response_and_validate_schema(res_conn, :ok)
545
546 assert %{"visibility" => "direct"} = status
547
548 # Both should be visible here
549 res_conn = get(conn_user_two, "api/v1/timelines/home")
550
551 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
552
553 # Test pagination
554 Enum.each(1..20, fn _ ->
555 {:ok, _} =
556 CommonAPI.post(user_one, %{
557 status: "Hi @#{user_two.nickname}!",
558 visibility: "direct"
559 })
560 end)
561
562 res_conn = get(conn_user_two, "api/v1/timelines/direct")
563
564 statuses = json_response_and_validate_schema(res_conn, :ok)
565 assert length(statuses) == 20
566
567 max_id = List.last(statuses)["id"]
568
569 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
570
571 assert [status] = json_response_and_validate_schema(res_conn, :ok)
572
573 assert status["url"] != direct.data["id"]
574 end
575
576 test "doesn't include DMs from blocked users" do
577 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
578 blocked = insert(:user)
579 other_user = insert(:user)
580 {:ok, _user_relationship} = User.block(blocker, blocked)
581
582 {:ok, _blocked_direct} =
583 CommonAPI.post(blocked, %{
584 status: "Hi @#{blocker.nickname}!",
585 visibility: "direct"
586 })
587
588 {:ok, direct} =
589 CommonAPI.post(other_user, %{
590 status: "Hi @#{blocker.nickname}!",
591 visibility: "direct"
592 })
593
594 res_conn = get(conn, "api/v1/timelines/direct")
595
596 [status] = json_response_and_validate_schema(res_conn, :ok)
597 assert status["id"] == direct.id
598 end
599 end
600
601 describe "list" do
602 setup do: oauth_access(["read:lists"])
603
604 test "does not contain retoots", %{user: user, conn: conn} do
605 other_user = insert(:user)
606 {:ok, activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
607 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is stupid."})
608 {:ok, _} = CommonAPI.repeat(activity_one.id, other_user)
609
610 {:ok, list} = Pleroma.List.create("name", user)
611 {:ok, list} = Pleroma.List.follow(list, other_user)
612
613 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
614
615 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
616
617 assert id == to_string(activity_two.id)
618 end
619
620 test "works with pagination", %{user: user, conn: conn} do
621 other_user = insert(:user)
622 {:ok, list} = Pleroma.List.create("name", user)
623 {:ok, list} = Pleroma.List.follow(list, other_user)
624
625 Enum.each(1..30, fn i ->
626 CommonAPI.post(other_user, %{status: "post number #{i}"})
627 end)
628
629 res =
630 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
631 |> json_response_and_validate_schema(:ok)
632
633 assert length(res) == 1
634
635 [first] = res
636
637 res =
638 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
639 |> json_response_and_validate_schema(:ok)
640
641 assert length(res) == 29
642 end
643
644 test "list timeline", %{user: user, conn: conn} do
645 other_user = insert(:user)
646 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
647 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
648 {:ok, list} = Pleroma.List.create("name", user)
649 {:ok, list} = Pleroma.List.follow(list, other_user)
650
651 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
652
653 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
654
655 assert id == to_string(activity_two.id)
656 end
657
658 test "list timeline does not leak non-public statuses for unfollowed users", %{
659 user: user,
660 conn: conn
661 } do
662 other_user = insert(:user)
663 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
664
665 {:ok, _activity_two} =
666 CommonAPI.post(other_user, %{
667 status: "Marisa is cute.",
668 visibility: "private"
669 })
670
671 {:ok, list} = Pleroma.List.create("name", user)
672 {:ok, list} = Pleroma.List.follow(list, other_user)
673
674 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
675
676 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
677
678 assert id == to_string(activity_one.id)
679 end
680
681 test "muted emotions", %{user: user, conn: conn} do
682 user2 = insert(:user)
683 user3 = insert(:user)
684 {:ok, activity} = CommonAPI.post(user2, %{status: "."})
685
686 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
687 User.mute(user, user3)
688
689 {:ok, list} = Pleroma.List.create("name", user)
690 {:ok, list} = Pleroma.List.follow(list, user2)
691
692 result =
693 conn
694 |> get("/api/v1/timelines/list/#{list.id}")
695 |> json_response_and_validate_schema(200)
696
697 assert [
698 %{
699 "pleroma" => %{
700 "emoji_reactions" => []
701 }
702 }
703 ] = result
704
705 result =
706 conn
707 |> get("/api/v1/timelines/list/#{list.id}?with_muted=true")
708 |> json_response_and_validate_schema(200)
709
710 assert [
711 %{
712 "pleroma" => %{
713 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
714 }
715 }
716 ] = result
717 end
718
719 test "filtering", %{user: user, conn: conn} do
720 {:ok, list} = Pleroma.List.create("name", user)
721
722 local_user = insert(:user)
723 {:ok, local_activity} = CommonAPI.post(local_user, %{status: "Marisa is stupid."})
724 with_media = create_with_media_activity(local_user)
725 {:ok, list} = Pleroma.List.follow(list, local_user)
726
727 remote_user = insert(:user, local: false)
728 remote_activity = create_remote_activity(remote_user)
729 {:ok, list} = Pleroma.List.follow(list, remote_user)
730
731 all_ids =
732 conn
733 |> get("/api/v1/timelines/list/#{list.id}")
734 |> json_response_and_validate_schema(200)
735 |> Enum.map(& &1["id"])
736
737 assert local_activity.id in all_ids
738 assert with_media.id in all_ids
739 assert remote_activity.id in all_ids
740
741 only_local_ids =
742 conn
743 |> get("/api/v1/timelines/list/#{list.id}?local=true")
744 |> json_response_and_validate_schema(200)
745 |> Enum.map(& &1["id"])
746
747 assert local_activity.id in only_local_ids
748 assert with_media.id in only_local_ids
749 refute remote_activity.id in only_local_ids
750
751 only_local_media_ids =
752 conn
753 |> get("/api/v1/timelines/list/#{list.id}?local=true&only_media=true")
754 |> json_response_and_validate_schema(200)
755 |> Enum.map(& &1["id"])
756
757 refute local_activity.id in only_local_media_ids
758 assert with_media.id in only_local_media_ids
759 refute remote_activity.id in only_local_media_ids
760
761 remote_ids =
762 conn
763 |> get("/api/v1/timelines/list/#{list.id}?remote=true")
764 |> json_response_and_validate_schema(200)
765 |> Enum.map(& &1["id"])
766
767 refute local_activity.id in remote_ids
768 refute with_media.id in remote_ids
769 assert remote_activity.id in remote_ids
770
771 assert conn
772 |> get("/api/v1/timelines/list/#{list.id}?remote=true&only_media=true")
773 |> json_response_and_validate_schema(200) == []
774
775 only_media_ids =
776 conn
777 |> get("/api/v1/timelines/list/#{list.id}?only_media=true")
778 |> json_response_and_validate_schema(200)
779 |> Enum.map(& &1["id"])
780
781 refute local_activity.id in only_media_ids
782 assert with_media.id in only_media_ids
783 refute remote_activity.id in only_media_ids
784
785 assert conn
786 |> get("/api/v1/timelines/list/#{list.id}?only_media=true&local=true&remote=true")
787 |> json_response_and_validate_schema(200) == []
788 end
789 end
790
791 describe "hashtag" do
792 setup do: oauth_access(["n/a"])
793
794 @tag capture_log: true
795 test "hashtag timeline", %{conn: conn} do
796 following = insert(:user)
797
798 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
799 with_media = create_with_media_activity(following)
800
801 remote = insert(:user, local: false)
802 remote_activity = create_remote_activity(remote)
803
804 all_ids =
805 conn
806 |> get("/api/v1/timelines/tag/2hu")
807 |> json_response_and_validate_schema(:ok)
808 |> Enum.map(& &1["id"])
809
810 assert activity.id in all_ids
811 assert with_media.id in all_ids
812 assert remote_activity.id in all_ids
813
814 # works for different capitalization too
815 all_ids =
816 conn
817 |> get("/api/v1/timelines/tag/2HU")
818 |> json_response_and_validate_schema(:ok)
819 |> Enum.map(& &1["id"])
820
821 assert activity.id in all_ids
822 assert with_media.id in all_ids
823 assert remote_activity.id in all_ids
824
825 local_ids =
826 conn
827 |> get("/api/v1/timelines/tag/2hu?local=true")
828 |> json_response_and_validate_schema(:ok)
829 |> Enum.map(& &1["id"])
830
831 assert activity.id in local_ids
832 assert with_media.id in local_ids
833 refute remote_activity.id in local_ids
834
835 remote_ids =
836 conn
837 |> get("/api/v1/timelines/tag/2hu?remote=true")
838 |> json_response_and_validate_schema(:ok)
839 |> Enum.map(& &1["id"])
840
841 refute activity.id in remote_ids
842 refute with_media.id in remote_ids
843 assert remote_activity.id in remote_ids
844
845 media_ids =
846 conn
847 |> get("/api/v1/timelines/tag/2hu?only_media=true")
848 |> json_response_and_validate_schema(:ok)
849 |> Enum.map(& &1["id"])
850
851 refute activity.id in media_ids
852 assert with_media.id in media_ids
853 refute remote_activity.id in media_ids
854
855 media_local_ids =
856 conn
857 |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true")
858 |> json_response_and_validate_schema(:ok)
859 |> Enum.map(& &1["id"])
860
861 refute activity.id in media_local_ids
862 assert with_media.id in media_local_ids
863 refute remote_activity.id in media_local_ids
864
865 ids =
866 conn
867 |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true&remote=true")
868 |> json_response_and_validate_schema(:ok)
869 |> Enum.map(& &1["id"])
870
871 refute activity.id in ids
872 refute with_media.id in ids
873 refute remote_activity.id in ids
874
875 assert conn
876 |> get("/api/v1/timelines/tag/2hu?only_media=true&remote=true")
877 |> json_response_and_validate_schema(:ok) == []
878 end
879
880 test "multi-hashtag timeline", %{conn: conn} do
881 user = insert(:user)
882
883 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
884 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
885 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
886
887 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
888
889 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
890
891 assert to_string(activity_test.id) == status_test["id"]
892 assert to_string(activity_test1.id) == status_test1["id"]
893 assert to_string(activity_none.id) == status_none["id"]
894
895 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
896
897 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
898
899 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
900
901 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
902 end
903
904 test "muted emotions", %{conn: conn} do
905 user = insert(:user)
906 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
907
908 conn =
909 conn
910 |> assign(:user, user)
911 |> assign(:token, token)
912
913 other_user = insert(:user)
914 {:ok, activity} = CommonAPI.post(user, %{status: "test #2hu"})
915
916 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
917 User.mute(user, other_user)
918
919 result =
920 conn
921 |> get("/api/v1/timelines/tag/2hu")
922 |> json_response_and_validate_schema(200)
923
924 assert [
925 %{
926 "pleroma" => %{
927 "emoji_reactions" => []
928 }
929 }
930 ] = result
931
932 result =
933 conn
934 |> get("/api/v1/timelines/tag/2hu?with_muted=true")
935 |> json_response_and_validate_schema(200)
936
937 assert [
938 %{
939 "pleroma" => %{
940 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
941 }
942 }
943 ] = result
944 end
945 end
946
947 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
948 setup do
949 user = insert(:user)
950 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
951 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
952
953 activity1
954 |> Ecto.Changeset.change(%{local: false})
955 |> Pleroma.Repo.update()
956
957 base_uri = "/api/v1/timelines/tag/tag1"
958 error_response = %{"error" => "authorization required for timeline view"}
959
960 %{base_uri: base_uri, error_response: error_response}
961 end
962
963 defp ensure_authenticated_access(base_uri) do
964 %{conn: auth_conn} = oauth_access(["read:statuses"])
965
966 res_conn = get(auth_conn, "#{base_uri}?local=true")
967 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
968
969 res_conn = get(auth_conn, "#{base_uri}?local=false")
970 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
971 end
972
973 test "with default settings on private instances, returns 403 for unauthenticated users", %{
974 conn: conn,
975 base_uri: base_uri,
976 error_response: error_response
977 } do
978 clear_config([:instance, :public], false)
979 clear_config([:restrict_unauthenticated, :timelines])
980
981 for local <- [true, false] do
982 res_conn = get(conn, "#{base_uri}?local=#{local}")
983
984 assert json_response_and_validate_schema(res_conn, :unauthorized) == error_response
985 end
986
987 ensure_authenticated_access(base_uri)
988 end
989
990 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
991 conn: conn,
992 base_uri: base_uri,
993 error_response: error_response
994 } do
995 clear_config([:restrict_unauthenticated, :timelines, :local], true)
996 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
997
998 for local <- [true, false] do
999 res_conn = get(conn, "#{base_uri}?local=#{local}")
1000
1001 assert json_response_and_validate_schema(res_conn, :unauthorized) == error_response
1002 end
1003
1004 ensure_authenticated_access(base_uri)
1005 end
1006
1007 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
1008 %{conn: conn, base_uri: base_uri, error_response: error_response} do
1009 clear_config([:restrict_unauthenticated, :timelines, :local], false)
1010 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
1011
1012 res_conn = get(conn, "#{base_uri}?local=true")
1013 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
1014
1015 res_conn = get(conn, "#{base_uri}?local=false")
1016 assert json_response_and_validate_schema(res_conn, :unauthorized) == error_response
1017
1018 ensure_authenticated_access(base_uri)
1019 end
1020
1021 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline",
1022 %{conn: conn, base_uri: base_uri, error_response: error_response} do
1023 # (but not to local public activities which are delivered as part of federated timeline)
1024 clear_config([:restrict_unauthenticated, :timelines, :local], true)
1025 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
1026
1027 res_conn = get(conn, "#{base_uri}?local=true")
1028 assert json_response_and_validate_schema(res_conn, :unauthorized) == error_response
1029
1030 # Note: local activities get delivered as part of federated timeline
1031 res_conn = get(conn, "#{base_uri}?local=false")
1032 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
1033
1034 ensure_authenticated_access(base_uri)
1035 end
1036 end
1037
1038 describe "bubble" do
1039 setup do: oauth_access(["read:statuses"])
1040
1041 test "filtering", %{conn: conn, user: user} do
1042 clear_config([:instance, :local_bubble], [])
1043 # our endpoint host has a port in it so let's set the AP ID
1044 local_user = insert(:user, %{ap_id: "https://localhost/users/user"})
1045 remote_user = insert(:user, %{ap_id: "https://example.com/users/remote_user"})
1046 {:ok, user, local_user} = User.follow(user, local_user)
1047 {:ok, _user, remote_user} = User.follow(user, remote_user)
1048
1049 {:ok, local_activity} = CommonAPI.post(local_user, %{status: "Status"})
1050 remote_activity = create_remote_activity(remote_user)
1051
1052 # If nothing, only include ours
1053 clear_config([:instance, :local_bubble], [])
1054
1055 one_instance =
1056 conn
1057 |> get("/api/v1/timelines/bubble")
1058 |> json_response_and_validate_schema(200)
1059 |> Enum.map(& &1["id"])
1060
1061 assert local_activity.id in one_instance
1062
1063 # If we have others, also include theirs
1064 clear_config([:instance, :local_bubble], ["example.com"])
1065
1066 two_instances =
1067 conn
1068 |> get("/api/v1/timelines/bubble")
1069 |> json_response_and_validate_schema(200)
1070 |> Enum.map(& &1["id"])
1071
1072 assert local_activity.id in two_instances
1073 assert remote_activity.id in two_instances
1074 end
1075 end
1076
1077 defp create_remote_activity(user) do
1078 obj =
1079 insert(:note, %{
1080 data: %{
1081 "to" => [
1082 "https://www.w3.org/ns/activitystreams#Public",
1083 User.ap_followers(user)
1084 ]
1085 },
1086 user: user
1087 })
1088
1089 insert(:note_activity, %{
1090 note: obj,
1091 recipients: [
1092 "https://www.w3.org/ns/activitystreams#Public",
1093 User.ap_followers(user)
1094 ],
1095 user: user,
1096 local: false
1097 })
1098 end
1099
1100 defp create_with_media_activity(user) do
1101 obj = insert(:attachment_note, user: user)
1102
1103 insert(:note_activity, %{
1104 note: obj,
1105 recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(user)],
1106 user: user
1107 })
1108 end
1109 end