Merge branch 'develop' into 'fix/2412-filters'
[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 replies if follow is posting with users from blocked domain" do
277 %{conn: conn, user: blocker} = oauth_access(["read:statuses"])
278 friend = insert(:user)
279 blockee = insert(:user, ap_id: "https://example.com/users/blocked")
280 {:ok, blocker, friend} = User.follow(blocker, friend)
281 {:ok, blocker} = User.block_domain(blocker, "example.com")
282
283 conn = assign(conn, :user, blocker)
284
285 {:ok, %{id: activity_id} = activity} = CommonAPI.post(friend, %{status: "hey!"})
286
287 {:ok, reply_from_blockee} =
288 CommonAPI.post(blockee, %{status: "heya", in_reply_to_status_id: activity})
289
290 {:ok, _reply_from_friend} =
291 CommonAPI.post(friend, %{status: "status", in_reply_to_status_id: reply_from_blockee})
292
293 res_conn = get(conn, "/api/v1/timelines/public")
294
295 activities = json_response_and_validate_schema(res_conn, 200)
296 [%{"id" => ^activity_id}] = activities
297 end
298
299 test "can be filtered by instance", %{conn: conn} do
300 user = insert(:user, ap_id: "https://lain.com/users/lain")
301 insert(:note_activity, local: false)
302 insert(:note_activity, local: false)
303
304 {:ok, _} = CommonAPI.post(user, %{status: "test"})
305
306 conn = get(conn, "/api/v1/timelines/public?instance=lain.com")
307
308 assert length(json_response_and_validate_schema(conn, :ok)) == 1
309 end
310
311 test "muted emotions", %{conn: conn} do
312 user = insert(:user)
313 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
314
315 conn =
316 conn
317 |> assign(:user, user)
318 |> assign(:token, token)
319
320 other_user = insert(:user)
321 {:ok, activity} = CommonAPI.post(user, %{status: "."})
322
323 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
324 User.mute(user, other_user)
325
326 result =
327 conn
328 |> get("/api/v1/timelines/public")
329 |> json_response_and_validate_schema(200)
330
331 assert [
332 %{
333 "pleroma" => %{
334 "emoji_reactions" => []
335 }
336 }
337 ] = result
338
339 result =
340 conn
341 |> get("/api/v1/timelines/public?with_muted=true")
342 |> json_response_and_validate_schema(200)
343
344 assert [
345 %{
346 "pleroma" => %{
347 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
348 }
349 }
350 ] = result
351 end
352 end
353
354 defp local_and_remote_activities do
355 insert(:note_activity)
356 insert(:note_activity, local: false)
357 :ok
358 end
359
360 describe "public with restrict unauthenticated timeline for local and federated timelines" do
361 setup do: local_and_remote_activities()
362
363 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
364
365 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
366
367 test "if user is unauthenticated", %{conn: conn} do
368 res_conn = get(conn, "/api/v1/timelines/public?local=true")
369
370 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
371 "error" => "authorization required for timeline view"
372 }
373
374 res_conn = get(conn, "/api/v1/timelines/public?local=false")
375
376 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
377 "error" => "authorization required for timeline view"
378 }
379 end
380
381 test "if user is authenticated" do
382 %{conn: conn} = oauth_access(["read:statuses"])
383
384 res_conn = get(conn, "/api/v1/timelines/public?local=true")
385 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
386
387 res_conn = get(conn, "/api/v1/timelines/public?local=false")
388 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
389 end
390 end
391
392 describe "public with restrict unauthenticated timeline for local" do
393 setup do: local_and_remote_activities()
394
395 setup do: clear_config([:restrict_unauthenticated, :timelines, :local], true)
396
397 test "if user is unauthenticated", %{conn: conn} do
398 res_conn = get(conn, "/api/v1/timelines/public?local=true")
399
400 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
401 "error" => "authorization required for timeline view"
402 }
403
404 res_conn = get(conn, "/api/v1/timelines/public?local=false")
405 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
406 end
407
408 test "if user is authenticated", %{conn: _conn} do
409 %{conn: conn} = oauth_access(["read:statuses"])
410
411 res_conn = get(conn, "/api/v1/timelines/public?local=true")
412 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
413
414 res_conn = get(conn, "/api/v1/timelines/public?local=false")
415 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
416 end
417 end
418
419 describe "public with restrict unauthenticated timeline for remote" do
420 setup do: local_and_remote_activities()
421
422 setup do: clear_config([:restrict_unauthenticated, :timelines, :federated], true)
423
424 test "if user is unauthenticated", %{conn: conn} do
425 res_conn = get(conn, "/api/v1/timelines/public?local=true")
426 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
427
428 res_conn = get(conn, "/api/v1/timelines/public?local=false")
429
430 assert json_response_and_validate_schema(res_conn, :unauthorized) == %{
431 "error" => "authorization required for timeline view"
432 }
433 end
434
435 test "if user is authenticated", %{conn: _conn} do
436 %{conn: conn} = oauth_access(["read:statuses"])
437
438 res_conn = get(conn, "/api/v1/timelines/public?local=true")
439 assert length(json_response_and_validate_schema(res_conn, 200)) == 1
440
441 res_conn = get(conn, "/api/v1/timelines/public?local=false")
442 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
443 end
444 end
445
446 describe "direct" do
447 test "direct timeline", %{conn: conn} do
448 user_one = insert(:user)
449 user_two = insert(:user)
450
451 {:ok, user_two, user_one} = User.follow(user_two, user_one)
452
453 {:ok, direct} =
454 CommonAPI.post(user_one, %{
455 status: "Hi @#{user_two.nickname}!",
456 visibility: "direct"
457 })
458
459 {:ok, _follower_only} =
460 CommonAPI.post(user_one, %{
461 status: "Hi @#{user_two.nickname}!",
462 visibility: "private"
463 })
464
465 conn_user_two =
466 conn
467 |> assign(:user, user_two)
468 |> assign(:token, insert(:oauth_token, user: user_two, scopes: ["read:statuses"]))
469
470 # Only direct should be visible here
471 res_conn = get(conn_user_two, "api/v1/timelines/direct")
472
473 assert [status] = json_response_and_validate_schema(res_conn, :ok)
474
475 assert %{"visibility" => "direct"} = status
476 assert status["url"] != direct.data["id"]
477
478 # User should be able to see their own direct message
479 res_conn =
480 build_conn()
481 |> assign(:user, user_one)
482 |> assign(:token, insert(:oauth_token, user: user_one, scopes: ["read:statuses"]))
483 |> get("api/v1/timelines/direct")
484
485 [status] = json_response_and_validate_schema(res_conn, :ok)
486
487 assert %{"visibility" => "direct"} = status
488
489 # Both should be visible here
490 res_conn = get(conn_user_two, "api/v1/timelines/home")
491
492 [_s1, _s2] = json_response_and_validate_schema(res_conn, :ok)
493
494 # Test pagination
495 Enum.each(1..20, fn _ ->
496 {:ok, _} =
497 CommonAPI.post(user_one, %{
498 status: "Hi @#{user_two.nickname}!",
499 visibility: "direct"
500 })
501 end)
502
503 res_conn = get(conn_user_two, "api/v1/timelines/direct")
504
505 statuses = json_response_and_validate_schema(res_conn, :ok)
506 assert length(statuses) == 20
507
508 max_id = List.last(statuses)["id"]
509
510 res_conn = get(conn_user_two, "api/v1/timelines/direct?max_id=#{max_id}")
511
512 assert [status] = json_response_and_validate_schema(res_conn, :ok)
513
514 assert status["url"] != direct.data["id"]
515 end
516
517 test "doesn't include DMs from blocked users" do
518 %{user: blocker, conn: conn} = oauth_access(["read:statuses"])
519 blocked = insert(:user)
520 other_user = insert(:user)
521 {:ok, _user_relationship} = User.block(blocker, blocked)
522
523 {:ok, _blocked_direct} =
524 CommonAPI.post(blocked, %{
525 status: "Hi @#{blocker.nickname}!",
526 visibility: "direct"
527 })
528
529 {:ok, direct} =
530 CommonAPI.post(other_user, %{
531 status: "Hi @#{blocker.nickname}!",
532 visibility: "direct"
533 })
534
535 res_conn = get(conn, "api/v1/timelines/direct")
536
537 [status] = json_response_and_validate_schema(res_conn, :ok)
538 assert status["id"] == direct.id
539 end
540 end
541
542 describe "list" do
543 setup do: oauth_access(["read:lists"])
544
545 test "does not contain retoots", %{user: user, conn: conn} do
546 other_user = insert(:user)
547 {:ok, activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
548 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is stupid."})
549 {:ok, _} = CommonAPI.repeat(activity_one.id, other_user)
550
551 {:ok, list} = Pleroma.List.create("name", user)
552 {:ok, list} = Pleroma.List.follow(list, other_user)
553
554 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
555
556 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
557
558 assert id == to_string(activity_two.id)
559 end
560
561 test "works with pagination", %{user: user, conn: conn} do
562 other_user = insert(:user)
563 {:ok, list} = Pleroma.List.create("name", user)
564 {:ok, list} = Pleroma.List.follow(list, other_user)
565
566 Enum.each(1..30, fn i ->
567 CommonAPI.post(other_user, %{status: "post number #{i}"})
568 end)
569
570 res =
571 get(conn, "/api/v1/timelines/list/#{list.id}?limit=1")
572 |> json_response_and_validate_schema(:ok)
573
574 assert length(res) == 1
575
576 [first] = res
577
578 res =
579 get(conn, "/api/v1/timelines/list/#{list.id}?max_id=#{first["id"]}&limit=30")
580 |> json_response_and_validate_schema(:ok)
581
582 assert length(res) == 29
583 end
584
585 test "list timeline", %{user: user, conn: conn} do
586 other_user = insert(:user)
587 {:ok, _activity_one} = CommonAPI.post(user, %{status: "Marisa is cute."})
588 {:ok, activity_two} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
589 {:ok, list} = Pleroma.List.create("name", user)
590 {:ok, list} = Pleroma.List.follow(list, other_user)
591
592 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
593
594 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
595
596 assert id == to_string(activity_two.id)
597 end
598
599 test "list timeline does not leak non-public statuses for unfollowed users", %{
600 user: user,
601 conn: conn
602 } do
603 other_user = insert(:user)
604 {:ok, activity_one} = CommonAPI.post(other_user, %{status: "Marisa is cute."})
605
606 {:ok, _activity_two} =
607 CommonAPI.post(other_user, %{
608 status: "Marisa is cute.",
609 visibility: "private"
610 })
611
612 {:ok, list} = Pleroma.List.create("name", user)
613 {:ok, list} = Pleroma.List.follow(list, other_user)
614
615 conn = get(conn, "/api/v1/timelines/list/#{list.id}")
616
617 assert [%{"id" => id}] = json_response_and_validate_schema(conn, :ok)
618
619 assert id == to_string(activity_one.id)
620 end
621
622 test "muted emotions", %{user: user, conn: conn} do
623 user2 = insert(:user)
624 user3 = insert(:user)
625 {:ok, activity} = CommonAPI.post(user2, %{status: "."})
626
627 {:ok, _} = CommonAPI.react_with_emoji(activity.id, user3, "🎅")
628 User.mute(user, user3)
629
630 {:ok, list} = Pleroma.List.create("name", user)
631 {:ok, list} = Pleroma.List.follow(list, user2)
632
633 result =
634 conn
635 |> get("/api/v1/timelines/list/#{list.id}")
636 |> json_response_and_validate_schema(200)
637
638 assert [
639 %{
640 "pleroma" => %{
641 "emoji_reactions" => []
642 }
643 }
644 ] = result
645
646 result =
647 conn
648 |> get("/api/v1/timelines/list/#{list.id}?with_muted=true")
649 |> json_response_and_validate_schema(200)
650
651 assert [
652 %{
653 "pleroma" => %{
654 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
655 }
656 }
657 ] = result
658 end
659
660 test "filtering", %{user: user, conn: conn} do
661 {:ok, list} = Pleroma.List.create("name", user)
662
663 local_user = insert(:user)
664 {:ok, local_activity} = CommonAPI.post(local_user, %{status: "Marisa is stupid."})
665 with_media = create_with_media_activity(local_user)
666 {:ok, list} = Pleroma.List.follow(list, local_user)
667
668 remote_user = insert(:user, local: false)
669 remote_activity = create_remote_activity(remote_user)
670 {:ok, list} = Pleroma.List.follow(list, remote_user)
671
672 all_ids =
673 conn
674 |> get("/api/v1/timelines/list/#{list.id}")
675 |> json_response_and_validate_schema(200)
676 |> Enum.map(& &1["id"])
677
678 assert local_activity.id in all_ids
679 assert with_media.id in all_ids
680 assert remote_activity.id in all_ids
681
682 only_local_ids =
683 conn
684 |> get("/api/v1/timelines/list/#{list.id}?local=true")
685 |> json_response_and_validate_schema(200)
686 |> Enum.map(& &1["id"])
687
688 assert local_activity.id in only_local_ids
689 assert with_media.id in only_local_ids
690 refute remote_activity.id in only_local_ids
691
692 only_local_media_ids =
693 conn
694 |> get("/api/v1/timelines/list/#{list.id}?local=true&only_media=true")
695 |> json_response_and_validate_schema(200)
696 |> Enum.map(& &1["id"])
697
698 refute local_activity.id in only_local_media_ids
699 assert with_media.id in only_local_media_ids
700 refute remote_activity.id in only_local_media_ids
701
702 remote_ids =
703 conn
704 |> get("/api/v1/timelines/list/#{list.id}?remote=true")
705 |> json_response_and_validate_schema(200)
706 |> Enum.map(& &1["id"])
707
708 refute local_activity.id in remote_ids
709 refute with_media.id in remote_ids
710 assert remote_activity.id in remote_ids
711
712 assert conn
713 |> get("/api/v1/timelines/list/#{list.id}?remote=true&only_media=true")
714 |> json_response_and_validate_schema(200) == []
715
716 only_media_ids =
717 conn
718 |> get("/api/v1/timelines/list/#{list.id}?only_media=true")
719 |> json_response_and_validate_schema(200)
720 |> Enum.map(& &1["id"])
721
722 refute local_activity.id in only_media_ids
723 assert with_media.id in only_media_ids
724 refute remote_activity.id in only_media_ids
725
726 assert conn
727 |> get("/api/v1/timelines/list/#{list.id}?only_media=true&local=true&remote=true")
728 |> json_response_and_validate_schema(200) == []
729 end
730 end
731
732 describe "hashtag" do
733 setup do: oauth_access(["n/a"])
734
735 @tag capture_log: true
736 test "hashtag timeline", %{conn: conn} do
737 following = insert(:user)
738
739 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
740 with_media = create_with_media_activity(following)
741
742 remote = insert(:user, local: false)
743 remote_activity = create_remote_activity(remote)
744
745 all_ids =
746 conn
747 |> get("/api/v1/timelines/tag/2hu")
748 |> json_response_and_validate_schema(:ok)
749 |> Enum.map(& &1["id"])
750
751 assert activity.id in all_ids
752 assert with_media.id in all_ids
753 assert remote_activity.id in all_ids
754
755 # works for different capitalization too
756 all_ids =
757 conn
758 |> get("/api/v1/timelines/tag/2HU")
759 |> json_response_and_validate_schema(:ok)
760 |> Enum.map(& &1["id"])
761
762 assert activity.id in all_ids
763 assert with_media.id in all_ids
764 assert remote_activity.id in all_ids
765
766 local_ids =
767 conn
768 |> get("/api/v1/timelines/tag/2hu?local=true")
769 |> json_response_and_validate_schema(:ok)
770 |> Enum.map(& &1["id"])
771
772 assert activity.id in local_ids
773 assert with_media.id in local_ids
774 refute remote_activity.id in local_ids
775
776 remote_ids =
777 conn
778 |> get("/api/v1/timelines/tag/2hu?remote=true")
779 |> json_response_and_validate_schema(:ok)
780 |> Enum.map(& &1["id"])
781
782 refute activity.id in remote_ids
783 refute with_media.id in remote_ids
784 assert remote_activity.id in remote_ids
785
786 media_ids =
787 conn
788 |> get("/api/v1/timelines/tag/2hu?only_media=true")
789 |> json_response_and_validate_schema(:ok)
790 |> Enum.map(& &1["id"])
791
792 refute activity.id in media_ids
793 assert with_media.id in media_ids
794 refute remote_activity.id in media_ids
795
796 media_local_ids =
797 conn
798 |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true")
799 |> json_response_and_validate_schema(:ok)
800 |> Enum.map(& &1["id"])
801
802 refute activity.id in media_local_ids
803 assert with_media.id in media_local_ids
804 refute remote_activity.id in media_local_ids
805
806 ids =
807 conn
808 |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true&remote=true")
809 |> json_response_and_validate_schema(:ok)
810 |> Enum.map(& &1["id"])
811
812 refute activity.id in ids
813 refute with_media.id in ids
814 refute remote_activity.id in ids
815
816 assert conn
817 |> get("/api/v1/timelines/tag/2hu?only_media=true&remote=true")
818 |> json_response_and_validate_schema(:ok) == []
819 end
820
821 test "multi-hashtag timeline", %{conn: conn} do
822 user = insert(:user)
823
824 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
825 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
826 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
827
828 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
829
830 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
831
832 assert to_string(activity_test.id) == status_test["id"]
833 assert to_string(activity_test1.id) == status_test1["id"]
834 assert to_string(activity_none.id) == status_none["id"]
835
836 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
837
838 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
839
840 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
841
842 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
843 end
844
845 test "muted emotions", %{conn: conn} do
846 user = insert(:user)
847 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
848
849 conn =
850 conn
851 |> assign(:user, user)
852 |> assign(:token, token)
853
854 other_user = insert(:user)
855 {:ok, activity} = CommonAPI.post(user, %{status: "test #2hu"})
856
857 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
858 User.mute(user, other_user)
859
860 result =
861 conn
862 |> get("/api/v1/timelines/tag/2hu")
863 |> json_response_and_validate_schema(200)
864
865 assert [
866 %{
867 "pleroma" => %{
868 "emoji_reactions" => []
869 }
870 }
871 ] = result
872
873 result =
874 conn
875 |> get("/api/v1/timelines/tag/2hu?with_muted=true")
876 |> json_response_and_validate_schema(200)
877
878 assert [
879 %{
880 "pleroma" => %{
881 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
882 }
883 }
884 ] = result
885 end
886 end
887
888 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
889 setup do
890 user = insert(:user)
891 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
892 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
893
894 activity1
895 |> Ecto.Changeset.change(%{local: false})
896 |> Pleroma.Repo.update()
897
898 base_uri = "/api/v1/timelines/tag/tag1"
899 error_response = %{"error" => "authorization required for timeline view"}
900
901 %{base_uri: base_uri, error_response: error_response}
902 end
903
904 defp ensure_authenticated_access(base_uri) do
905 %{conn: auth_conn} = oauth_access(["read:statuses"])
906
907 res_conn = get(auth_conn, "#{base_uri}?local=true")
908 assert length(json_response(res_conn, 200)) == 1
909
910 res_conn = get(auth_conn, "#{base_uri}?local=false")
911 assert length(json_response(res_conn, 200)) == 2
912 end
913
914 test "with default settings on private instances, returns 403 for unauthenticated users", %{
915 conn: conn,
916 base_uri: base_uri,
917 error_response: error_response
918 } do
919 clear_config([:instance, :public], false)
920 clear_config([:restrict_unauthenticated, :timelines])
921
922 for local <- [true, false] do
923 res_conn = get(conn, "#{base_uri}?local=#{local}")
924
925 assert json_response(res_conn, :unauthorized) == error_response
926 end
927
928 ensure_authenticated_access(base_uri)
929 end
930
931 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
932 conn: conn,
933 base_uri: base_uri,
934 error_response: error_response
935 } do
936 clear_config([:restrict_unauthenticated, :timelines, :local], true)
937 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
938
939 for local <- [true, false] do
940 res_conn = get(conn, "#{base_uri}?local=#{local}")
941
942 assert json_response(res_conn, :unauthorized) == error_response
943 end
944
945 ensure_authenticated_access(base_uri)
946 end
947
948 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
949 %{conn: conn, base_uri: base_uri, error_response: error_response} do
950 clear_config([:restrict_unauthenticated, :timelines, :local], false)
951 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
952
953 res_conn = get(conn, "#{base_uri}?local=true")
954 assert length(json_response(res_conn, 200)) == 1
955
956 res_conn = get(conn, "#{base_uri}?local=false")
957 assert json_response(res_conn, :unauthorized) == error_response
958
959 ensure_authenticated_access(base_uri)
960 end
961
962 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
963 "(but not to local public activities which are delivered as part of federated timeline)",
964 %{conn: conn, base_uri: base_uri, error_response: error_response} do
965 clear_config([:restrict_unauthenticated, :timelines, :local], true)
966 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
967
968 res_conn = get(conn, "#{base_uri}?local=true")
969 assert json_response(res_conn, :unauthorized) == error_response
970
971 # Note: local activities get delivered as part of federated timeline
972 res_conn = get(conn, "#{base_uri}?local=false")
973 assert length(json_response(res_conn, 200)) == 2
974
975 ensure_authenticated_access(base_uri)
976 end
977 end
978
979 defp create_remote_activity(user) do
980 obj =
981 insert(:note, %{
982 data: %{
983 "to" => [
984 "https://www.w3.org/ns/activitystreams#Public",
985 User.ap_followers(user)
986 ]
987 },
988 user: user
989 })
990
991 insert(:note_activity, %{
992 note: obj,
993 recipients: [
994 "https://www.w3.org/ns/activitystreams#Public",
995 User.ap_followers(user)
996 ],
997 user: user,
998 local: false
999 })
1000 end
1001
1002 defp create_with_media_activity(user) do
1003 obj = insert(:attachment_note, user: user)
1004
1005 insert(:note_activity, %{
1006 note: obj,
1007 recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(user)],
1008 user: user
1009 })
1010 end
1011 end