more tests and update for docs and changelog
[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 only_remote_ids =
135 conn
136 |> get("/api/v1/timelines/home?only_remote=true")
137 |> json_response_and_validate_schema(200)
138 |> Enum.map(& &1["id"])
139
140 refute local_activity.id in only_remote_ids
141 assert remote_activity.id in only_remote_ids
142 refute with_media.id in only_remote_ids
143
144 assert conn
145 |> get("/api/v1/timelines/home?only_remote=true&only_media=true")
146 |> json_response_and_validate_schema(200) == []
147
148 assert conn
149 |> get("/api/v1/timelines/home?only_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?only_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?only_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 "filering", %{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 only_remote_ids =
703 conn
704 |> get("/api/v1/timelines/list/#{list.id}?only_remote=true")
705 |> json_response_and_validate_schema(200)
706 |> Enum.map(& &1["id"])
707
708 refute local_activity.id in only_remote_ids
709 refute with_media.id in only_remote_ids
710 assert remote_activity.id in only_remote_ids
711
712 assert conn
713 |> get("/api/v1/timelines/list/#{list.id}?only_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(
728 "/api/v1/timelines/list/#{list.id}?only_media=true&local=true&only_remote=true"
729 )
730 |> json_response_and_validate_schema(200) == []
731 end
732 end
733
734 describe "hashtag" do
735 setup do: oauth_access(["n/a"])
736
737 @tag capture_log: true
738 test "hashtag timeline", %{conn: conn} do
739 following = insert(:user)
740
741 {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
742 with_media = create_with_media_activity(following)
743
744 remote = insert(:user, local: false)
745 remote_activity = create_remote_activity(remote)
746
747 all_ids =
748 conn
749 |> get("/api/v1/timelines/tag/2hu")
750 |> json_response_and_validate_schema(:ok)
751 |> Enum.map(& &1["id"])
752
753 assert activity.id in all_ids
754 assert with_media.id in all_ids
755 assert remote_activity.id in all_ids
756
757 # works for different capitalization too
758 all_ids =
759 conn
760 |> get("/api/v1/timelines/tag/2HU")
761 |> json_response_and_validate_schema(:ok)
762 |> Enum.map(& &1["id"])
763
764 assert activity.id in all_ids
765 assert with_media.id in all_ids
766 assert remote_activity.id in all_ids
767
768 local_ids =
769 conn
770 |> get("/api/v1/timelines/tag/2hu?local=true")
771 |> json_response_and_validate_schema(:ok)
772 |> Enum.map(& &1["id"])
773
774 assert activity.id in local_ids
775 assert with_media.id in local_ids
776 refute remote_activity.id in local_ids
777
778 remote_ids =
779 conn
780 |> get("/api/v1/timelines/tag/2hu?only_remote=true")
781 |> json_response_and_validate_schema(:ok)
782 |> Enum.map(& &1["id"])
783
784 refute activity.id in remote_ids
785 refute with_media.id in remote_ids
786 assert remote_activity.id in remote_ids
787
788 media_ids =
789 conn
790 |> get("/api/v1/timelines/tag/2hu?only_media=true")
791 |> json_response_and_validate_schema(:ok)
792 |> Enum.map(& &1["id"])
793
794 refute activity.id in media_ids
795 assert with_media.id in media_ids
796 refute remote_activity.id in media_ids
797
798 media_local_ids =
799 conn
800 |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true")
801 |> json_response_and_validate_schema(:ok)
802 |> Enum.map(& &1["id"])
803
804 refute activity.id in media_local_ids
805 assert with_media.id in media_local_ids
806 refute remote_activity.id in media_local_ids
807
808 ids =
809 conn
810 |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true&only_remote=true")
811 |> json_response_and_validate_schema(:ok)
812 |> Enum.map(& &1["id"])
813
814 refute activity.id in ids
815 refute with_media.id in ids
816 refute remote_activity.id in ids
817
818 assert conn
819 |> get("/api/v1/timelines/tag/2hu?only_media=true&only_remote=true")
820 |> json_response_and_validate_schema(:ok) == []
821 end
822
823 test "multi-hashtag timeline", %{conn: conn} do
824 user = insert(:user)
825
826 {:ok, activity_test} = CommonAPI.post(user, %{status: "#test"})
827 {:ok, activity_test1} = CommonAPI.post(user, %{status: "#test #test1"})
828 {:ok, activity_none} = CommonAPI.post(user, %{status: "#test #none"})
829
830 any_test = get(conn, "/api/v1/timelines/tag/test?any[]=test1")
831
832 [status_none, status_test1, status_test] = json_response_and_validate_schema(any_test, :ok)
833
834 assert to_string(activity_test.id) == status_test["id"]
835 assert to_string(activity_test1.id) == status_test1["id"]
836 assert to_string(activity_none.id) == status_none["id"]
837
838 restricted_test = get(conn, "/api/v1/timelines/tag/test?all[]=test1&none[]=none")
839
840 assert [status_test1] == json_response_and_validate_schema(restricted_test, :ok)
841
842 all_test = get(conn, "/api/v1/timelines/tag/test?all[]=none")
843
844 assert [status_none] == json_response_and_validate_schema(all_test, :ok)
845 end
846
847 test "muted emotions", %{conn: conn} do
848 user = insert(:user)
849 token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
850
851 conn =
852 conn
853 |> assign(:user, user)
854 |> assign(:token, token)
855
856 other_user = insert(:user)
857 {:ok, activity} = CommonAPI.post(user, %{status: "test #2hu"})
858
859 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
860 User.mute(user, other_user)
861
862 result =
863 conn
864 |> get("/api/v1/timelines/tag/2hu")
865 |> json_response_and_validate_schema(200)
866
867 assert [
868 %{
869 "pleroma" => %{
870 "emoji_reactions" => []
871 }
872 }
873 ] = result
874
875 result =
876 conn
877 |> get("/api/v1/timelines/tag/2hu?with_muted=true")
878 |> json_response_and_validate_schema(200)
879
880 assert [
881 %{
882 "pleroma" => %{
883 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
884 }
885 }
886 ] = result
887 end
888 end
889
890 describe "hashtag timeline handling of :restrict_unauthenticated setting" do
891 setup do
892 user = insert(:user)
893 {:ok, activity1} = CommonAPI.post(user, %{status: "test #tag1"})
894 {:ok, _activity2} = CommonAPI.post(user, %{status: "test #tag1"})
895
896 activity1
897 |> Ecto.Changeset.change(%{local: false})
898 |> Pleroma.Repo.update()
899
900 base_uri = "/api/v1/timelines/tag/tag1"
901 error_response = %{"error" => "authorization required for timeline view"}
902
903 %{base_uri: base_uri, error_response: error_response}
904 end
905
906 defp ensure_authenticated_access(base_uri) do
907 %{conn: auth_conn} = oauth_access(["read:statuses"])
908
909 res_conn = get(auth_conn, "#{base_uri}?local=true")
910 assert length(json_response(res_conn, 200)) == 1
911
912 res_conn = get(auth_conn, "#{base_uri}?local=false")
913 assert length(json_response(res_conn, 200)) == 2
914 end
915
916 test "with default settings on private instances, returns 403 for unauthenticated users", %{
917 conn: conn,
918 base_uri: base_uri,
919 error_response: error_response
920 } do
921 clear_config([:instance, :public], false)
922 clear_config([:restrict_unauthenticated, :timelines])
923
924 for local <- [true, false] do
925 res_conn = get(conn, "#{base_uri}?local=#{local}")
926
927 assert json_response(res_conn, :unauthorized) == error_response
928 end
929
930 ensure_authenticated_access(base_uri)
931 end
932
933 test "with `%{local: true, federated: true}`, returns 403 for unauthenticated users", %{
934 conn: conn,
935 base_uri: base_uri,
936 error_response: error_response
937 } do
938 clear_config([:restrict_unauthenticated, :timelines, :local], true)
939 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
940
941 for local <- [true, false] do
942 res_conn = get(conn, "#{base_uri}?local=#{local}")
943
944 assert json_response(res_conn, :unauthorized) == error_response
945 end
946
947 ensure_authenticated_access(base_uri)
948 end
949
950 test "with `%{local: false, federated: true}`, forbids unauthenticated access to federated timeline",
951 %{conn: conn, base_uri: base_uri, error_response: error_response} do
952 clear_config([:restrict_unauthenticated, :timelines, :local], false)
953 clear_config([:restrict_unauthenticated, :timelines, :federated], true)
954
955 res_conn = get(conn, "#{base_uri}?local=true")
956 assert length(json_response(res_conn, 200)) == 1
957
958 res_conn = get(conn, "#{base_uri}?local=false")
959 assert json_response(res_conn, :unauthorized) == error_response
960
961 ensure_authenticated_access(base_uri)
962 end
963
964 test "with `%{local: true, federated: false}`, forbids unauthenticated access to public timeline" <>
965 "(but not to local public activities which are delivered as part of federated timeline)",
966 %{conn: conn, base_uri: base_uri, error_response: error_response} do
967 clear_config([:restrict_unauthenticated, :timelines, :local], true)
968 clear_config([:restrict_unauthenticated, :timelines, :federated], false)
969
970 res_conn = get(conn, "#{base_uri}?local=true")
971 assert json_response(res_conn, :unauthorized) == error_response
972
973 # Note: local activities get delivered as part of federated timeline
974 res_conn = get(conn, "#{base_uri}?local=false")
975 assert length(json_response(res_conn, 200)) == 2
976
977 ensure_authenticated_access(base_uri)
978 end
979 end
980
981 defp create_remote_activity(user) do
982 obj =
983 insert(:note, %{
984 data: %{
985 "to" => [
986 "https://www.w3.org/ns/activitystreams#Public",
987 User.ap_followers(user)
988 ]
989 },
990 user: user
991 })
992
993 insert(:note_activity, %{
994 note: obj,
995 recipients: [
996 "https://www.w3.org/ns/activitystreams#Public",
997 User.ap_followers(user)
998 ],
999 user: user,
1000 local: false
1001 })
1002 end
1003
1004 defp create_with_media_activity(user) do
1005 obj = insert(:attachment_note, user: user)
1006
1007 insert(:note_activity, %{
1008 note: obj,
1009 recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(user)],
1010 user: user
1011 })
1012 end
1013 end