1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Builders.ActivityBuilder
12 alias Pleroma.Notification
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.AdminAPI.AccountView
18 alias Pleroma.Web.CommonAPI
20 import ExUnit.CaptureLog
22 import Pleroma.Factory
26 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
30 setup do: clear_config([:instance, :federating])
32 describe "streaming out participations" do
33 test "it streams them out" do
35 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
37 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
40 conversation.participations
41 |> Repo.preload(:user)
43 with_mock Pleroma.Web.Streamer,
44 stream: fn _, _ -> nil end do
45 ActivityPub.stream_out_participations(conversation.participations)
47 assert called(Pleroma.Web.Streamer.stream("participation", participations))
51 test "streams them out on activity creation" do
52 user_one = insert(:user)
53 user_two = insert(:user)
55 with_mock Pleroma.Web.Streamer,
56 stream: fn _, _ -> nil end do
58 CommonAPI.post(user_one, %{
59 status: "@#{user_two.nickname}",
64 activity.data["context"]
65 |> Pleroma.Conversation.get_for_ap_id()
66 |> Repo.preload(participations: :user)
68 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
73 describe "fetching restricted by visibility" do
74 test "it restricts by the appropriate visibility" do
77 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
79 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
81 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
83 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
85 activities = ActivityPub.fetch_activities([], %{visibility: "direct", actor_id: user.ap_id})
87 assert activities == [direct_activity]
90 ActivityPub.fetch_activities([], %{visibility: "unlisted", actor_id: user.ap_id})
92 assert activities == [unlisted_activity]
95 ActivityPub.fetch_activities([], %{visibility: "private", actor_id: user.ap_id})
97 assert activities == [private_activity]
99 activities = ActivityPub.fetch_activities([], %{visibility: "public", actor_id: user.ap_id})
101 assert activities == [public_activity]
104 ActivityPub.fetch_activities([], %{
105 visibility: ~w[private public],
109 assert activities == [public_activity, private_activity]
113 describe "fetching excluded by visibility" do
114 test "it excludes by the appropriate visibility" do
117 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
119 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
121 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
123 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
126 ActivityPub.fetch_activities([], %{
127 exclude_visibilities: "direct",
131 assert public_activity in activities
132 assert unlisted_activity in activities
133 assert private_activity in activities
134 refute direct_activity in activities
137 ActivityPub.fetch_activities([], %{
138 exclude_visibilities: "unlisted",
142 assert public_activity in activities
143 refute unlisted_activity in activities
144 assert private_activity in activities
145 assert direct_activity in activities
148 ActivityPub.fetch_activities([], %{
149 exclude_visibilities: "private",
153 assert public_activity in activities
154 assert unlisted_activity in activities
155 refute private_activity in activities
156 assert direct_activity in activities
159 ActivityPub.fetch_activities([], %{
160 exclude_visibilities: "public",
164 refute public_activity in activities
165 assert unlisted_activity in activities
166 assert private_activity in activities
167 assert direct_activity in activities
171 describe "building a user from his ap id" do
172 test "it returns a user" do
173 user_id = "http://mastodon.example.org/users/admin"
174 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
175 assert user.ap_id == user_id
176 assert user.nickname == "admin@mastodon.example.org"
177 assert user.ap_enabled
178 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
181 test "it returns a user that is invisible" do
182 user_id = "http://mastodon.example.org/users/relay"
183 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
184 assert User.invisible?(user)
187 test "it returns a user that accepts chat messages" do
188 user_id = "http://mastodon.example.org/users/admin"
189 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
191 assert user.accepts_chat_messages
195 test "it fetches the appropriate tag-restricted posts" do
198 {:ok, status_one} = CommonAPI.post(user, %{status: ". #test"})
199 {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
200 {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #reject"})
202 for new_timeline_enabled <- [true, false] do
203 clear_config([:instance, :improved_hashtag_timeline], new_timeline_enabled)
205 fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"})
207 fetch_two = ActivityPub.fetch_activities([], %{type: "Create", tag: ["test", "essais"]})
210 ActivityPub.fetch_activities([], %{
212 tag: ["test", "essais"],
213 tag_reject: ["reject"]
217 ActivityPub.fetch_activities([], %{
220 tag_all: ["test", "reject"]
223 [fetch_one, fetch_two, fetch_three, fetch_four] =
224 Enum.map([fetch_one, fetch_two, fetch_three, fetch_four], fn statuses ->
225 Enum.map(statuses, fn s -> Repo.preload(s, object: :hashtags) end)
228 assert fetch_one == [status_one, status_three]
229 assert fetch_two == [status_one, status_two, status_three]
230 assert fetch_three == [status_one, status_two]
231 assert fetch_four == [status_three]
235 describe "insertion" do
236 test "drops activities beyond a certain limit" do
237 limit = Config.get([:instance, :remote_limit])
240 :crypto.strong_rand_bytes(limit + 1)
242 |> binary_part(0, limit + 1)
247 "content" => random_text
251 assert {:error, :remote_limit} = ActivityPub.insert(data)
254 test "doesn't drop activities with content being null" do
258 "actor" => user.ap_id,
261 "actor" => user.ap_id,
268 assert {:ok, _} = ActivityPub.insert(data)
271 test "returns the activity if one with the same id is already in" do
272 activity = insert(:note_activity)
273 {:ok, new_activity} = ActivityPub.insert(activity.data)
275 assert activity.id == new_activity.id
278 test "inserts a given map into the activity database, giving it an id if it has none." do
282 "actor" => user.ap_id,
285 "actor" => user.ap_id,
292 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
293 assert activity.data["ok"] == data["ok"]
294 assert is_binary(activity.data["id"])
300 "actor" => user.ap_id,
302 "context" => "blabla",
304 "actor" => user.ap_id,
311 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
312 assert activity.data["ok"] == data["ok"]
313 assert activity.data["id"] == given_id
314 assert activity.data["context"] == "blabla"
315 assert activity.data["context_id"]
318 test "adds a context when none is there" do
322 "actor" => user.ap_id,
325 "actor" => user.ap_id,
332 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
333 object = Pleroma.Object.normalize(activity)
335 assert is_binary(activity.data["context"])
336 assert is_binary(object.data["context"])
337 assert activity.data["context_id"]
338 assert object.data["context_id"]
341 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
345 "actor" => user.ap_id,
348 "actor" => user.ap_id,
355 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
356 assert object = Object.normalize(activity)
357 assert is_binary(object.data["id"])
361 describe "listen activities" do
362 test "does not increase user note count" do
366 ActivityPub.listen(%{
367 to: ["https://www.w3.org/ns/activitystreams#Public"],
371 "actor" => user.ap_id,
372 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
374 "title" => "lain radio episode 1",
380 assert activity.actor == user.ap_id
382 user = User.get_cached_by_id(user.id)
383 assert user.note_count == 0
386 test "can be fetched into a timeline" do
387 _listen_activity_1 = insert(:listen)
388 _listen_activity_2 = insert(:listen)
389 _listen_activity_3 = insert(:listen)
391 timeline = ActivityPub.fetch_activities([], %{type: ["Listen"]})
393 assert length(timeline) == 3
397 describe "create activities" do
399 [user: insert(:user)]
402 test "it reverts create", %{user: user} do
403 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
404 assert {:error, :reverted} =
405 ActivityPub.create(%{
406 to: ["user1", "user2"],
410 "to" => ["user1", "user2"],
412 "content" => "testing"
417 assert Repo.aggregate(Activity, :count, :id) == 0
418 assert Repo.aggregate(Object, :count, :id) == 0
421 test "creates activity if expiration is not configured and expires_at is not passed", %{
424 clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
427 ActivityPub.create(%{
428 to: ["user1", "user2"],
432 "to" => ["user1", "user2"],
434 "content" => "testing"
439 test "rejects activity if expires_at present but expiration is not configured", %{user: user} do
440 clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
442 assert {:error, :expired_activities_disabled} =
443 ActivityPub.create(%{
444 to: ["user1", "user2"],
448 "to" => ["user1", "user2"],
450 "content" => "testing"
453 "expires_at" => DateTime.utc_now()
457 assert Repo.aggregate(Activity, :count, :id) == 0
458 assert Repo.aggregate(Object, :count, :id) == 0
461 test "removes doubled 'to' recipients", %{user: user} do
463 ActivityPub.create(%{
464 to: ["user1", "user1", "user2"],
468 "to" => ["user1", "user1", "user2"],
470 "content" => "testing"
474 assert activity.data["to"] == ["user1", "user2"]
475 assert activity.actor == user.ap_id
476 assert activity.recipients == ["user1", "user2", user.ap_id]
479 test "increases user note count only for public activities", %{user: user} do
481 CommonAPI.post(User.get_cached_by_id(user.id), %{
487 CommonAPI.post(User.get_cached_by_id(user.id), %{
489 visibility: "unlisted"
493 CommonAPI.post(User.get_cached_by_id(user.id), %{
495 visibility: "private"
499 CommonAPI.post(User.get_cached_by_id(user.id), %{
504 user = User.get_cached_by_id(user.id)
505 assert user.note_count == 2
508 test "increases replies count", %{user: user} do
509 user2 = insert(:user)
511 {:ok, activity} = CommonAPI.post(user, %{status: "1", visibility: "public"})
512 ap_id = activity.data["id"]
513 reply_data = %{status: "1", in_reply_to_status_id: activity.id}
516 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "public"))
517 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
518 assert object.data["repliesCount"] == 1
521 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "unlisted"))
522 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
523 assert object.data["repliesCount"] == 2
526 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "private"))
527 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
528 assert object.data["repliesCount"] == 2
531 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "direct"))
532 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
533 assert object.data["repliesCount"] == 2
537 describe "fetch activities for recipients" do
538 test "retrieve the activities for certain recipients" do
539 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
540 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
541 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
543 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
544 assert length(activities) == 2
545 assert activities == [activity_one, activity_two]
549 describe "fetch activities in context" do
550 test "retrieves activities that have a given context" do
551 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
552 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
553 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
554 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
555 activity_five = insert(:note_activity)
558 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
560 activities = ActivityPub.fetch_activities_for_context("2hu", %{blocking_user: user})
561 assert activities == [activity_two, activity]
564 test "doesn't return activities with filtered words" do
566 user_two = insert(:user)
567 insert(:filter, user: user, phrase: "test", hide: true)
569 {:ok, %{id: id1, data: %{"context" => context}}} = CommonAPI.post(user, %{status: "1"})
571 {:ok, %{id: id2}} = CommonAPI.post(user_two, %{status: "2", in_reply_to_status_id: id1})
573 {:ok, %{id: id3} = user_activity} =
574 CommonAPI.post(user, %{status: "3 test?", in_reply_to_status_id: id2})
576 {:ok, %{id: id4} = filtered_activity} =
577 CommonAPI.post(user_two, %{status: "4 test!", in_reply_to_status_id: id3})
579 {:ok, _} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
583 |> ActivityPub.fetch_activities_for_context(%{user: user})
586 assert length(activities) == 4
587 assert user_activity.id in activities
588 refute filtered_activity.id in activities
592 test "doesn't return blocked activities" do
593 activity_one = insert(:note_activity)
594 activity_two = insert(:note_activity)
595 activity_three = insert(:note_activity)
597 booster = insert(:user)
598 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
600 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
602 assert Enum.member?(activities, activity_two)
603 assert Enum.member?(activities, activity_three)
604 refute Enum.member?(activities, activity_one)
606 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
608 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
610 assert Enum.member?(activities, activity_two)
611 assert Enum.member?(activities, activity_three)
612 assert Enum.member?(activities, activity_one)
614 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
615 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
616 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
617 activity_three = Activity.get_by_id(activity_three.id)
619 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
621 assert Enum.member?(activities, activity_two)
622 refute Enum.member?(activities, activity_three)
623 refute Enum.member?(activities, boost_activity)
624 assert Enum.member?(activities, activity_one)
626 activities = ActivityPub.fetch_activities([], %{blocking_user: nil, skip_preload: true})
628 assert Enum.member?(activities, activity_two)
629 assert Enum.member?(activities, activity_three)
630 assert Enum.member?(activities, boost_activity)
631 assert Enum.member?(activities, activity_one)
634 test "doesn't return transitive interactions concerning blocked users" do
635 blocker = insert(:user)
636 blockee = insert(:user)
637 friend = insert(:user)
639 {:ok, _user_relationship} = User.block(blocker, blockee)
641 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
643 {:ok, activity_two} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
645 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
647 {:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
649 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker})
651 assert Enum.member?(activities, activity_one)
652 refute Enum.member?(activities, activity_two)
653 refute Enum.member?(activities, activity_three)
654 refute Enum.member?(activities, activity_four)
657 test "doesn't return announce activities with blocked users in 'to'" do
658 blocker = insert(:user)
659 blockee = insert(:user)
660 friend = insert(:user)
662 {:ok, _user_relationship} = User.block(blocker, blockee)
664 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
666 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
668 {:ok, activity_three} = CommonAPI.repeat(activity_two.id, friend)
671 ActivityPub.fetch_activities([], %{blocking_user: blocker})
672 |> Enum.map(fn act -> act.id end)
674 assert Enum.member?(activities, activity_one.id)
675 refute Enum.member?(activities, activity_two.id)
676 refute Enum.member?(activities, activity_three.id)
679 test "doesn't return announce activities with blocked users in 'cc'" do
680 blocker = insert(:user)
681 blockee = insert(:user)
682 friend = insert(:user)
684 {:ok, _user_relationship} = User.block(blocker, blockee)
686 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
688 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
690 assert object = Pleroma.Object.normalize(activity_two)
693 "actor" => friend.ap_id,
694 "object" => object.data["id"],
695 "context" => object.data["context"],
696 "type" => "Announce",
697 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
698 "cc" => [blockee.ap_id]
701 assert {:ok, activity_three} = ActivityPub.insert(data)
704 ActivityPub.fetch_activities([], %{blocking_user: blocker})
705 |> Enum.map(fn act -> act.id end)
707 assert Enum.member?(activities, activity_one.id)
708 refute Enum.member?(activities, activity_two.id)
709 refute Enum.member?(activities, activity_three.id)
712 test "doesn't return activities from blocked domains" do
713 domain = "dogwhistle.zone"
714 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
715 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
716 activity = insert(:note_activity, %{note: note})
718 {:ok, user} = User.block_domain(user, domain)
720 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
722 refute activity in activities
724 followed_user = insert(:user)
725 CommonAPI.follow(user, followed_user)
726 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user)
728 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
730 refute repeat_activity in activities
733 test "does return activities from followed users on blocked domains" do
734 domain = "meanies.social"
735 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
736 blocker = insert(:user)
738 {:ok, blocker, domain_user} = User.follow(blocker, domain_user)
739 {:ok, blocker} = User.block_domain(blocker, domain)
741 assert User.following?(blocker, domain_user)
742 assert User.blocks_domain?(blocker, domain_user)
743 refute User.blocks?(blocker, domain_user)
745 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
746 activity = insert(:note_activity, %{note: note})
748 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
750 assert activity in activities
752 # And check that if the guy we DO follow boosts someone else from their domain,
753 # that should be hidden
754 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
755 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
756 bad_activity = insert(:note_activity, %{note: bad_note})
757 {:ok, repeat_activity} = CommonAPI.repeat(bad_activity.id, domain_user)
759 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
761 refute repeat_activity in activities
764 test "returns your own posts regardless of mute" do
766 muted = insert(:user)
768 {:ok, muted_post} = CommonAPI.post(muted, %{status: "Im stupid"})
771 CommonAPI.post(user, %{status: "I'm muting you", in_reply_to_status_id: muted_post.id})
773 {:ok, _} = User.mute(user, muted)
775 [activity] = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
777 assert activity.id == reply.id
780 test "doesn't return muted activities" do
781 activity_one = insert(:note_activity)
782 activity_two = insert(:note_activity)
783 activity_three = insert(:note_activity)
785 booster = insert(:user)
787 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
788 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
790 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
792 assert Enum.member?(activities, activity_two)
793 assert Enum.member?(activities, activity_three)
794 refute Enum.member?(activities, activity_one)
796 # Calling with 'with_muted' will deliver muted activities, too.
798 ActivityPub.fetch_activities([], %{
804 assert Enum.member?(activities, activity_two)
805 assert Enum.member?(activities, activity_three)
806 assert Enum.member?(activities, activity_one)
808 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
810 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
812 assert Enum.member?(activities, activity_two)
813 assert Enum.member?(activities, activity_three)
814 assert Enum.member?(activities, activity_one)
816 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
817 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
818 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
819 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
820 activity_three = Activity.get_by_id(activity_three.id)
822 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
824 assert Enum.member?(activities, activity_two)
825 refute Enum.member?(activities, activity_three)
826 refute Enum.member?(activities, boost_activity)
827 assert Enum.member?(activities, activity_one)
829 activities = ActivityPub.fetch_activities([], %{muting_user: nil, skip_preload: true})
831 assert Enum.member?(activities, activity_two)
832 assert Enum.member?(activities, activity_three)
833 assert Enum.member?(activities, boost_activity)
834 assert Enum.member?(activities, activity_one)
837 test "doesn't return thread muted activities" do
839 _activity_one = insert(:note_activity)
840 note_two = insert(:note, data: %{"context" => "suya.."})
841 activity_two = insert(:note_activity, note: note_two)
843 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
845 assert [_activity_one] = ActivityPub.fetch_activities([], %{muting_user: user})
848 test "returns thread muted activities when with_muted is set" do
850 _activity_one = insert(:note_activity)
851 note_two = insert(:note, data: %{"context" => "suya.."})
852 activity_two = insert(:note_activity, note: note_two)
854 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
856 assert [_activity_two, _activity_one] =
857 ActivityPub.fetch_activities([], %{muting_user: user, with_muted: true})
860 test "does include announces on request" do
861 activity_three = insert(:note_activity)
863 booster = insert(:user)
865 {:ok, user, booster} = User.follow(user, booster)
867 {:ok, announce} = CommonAPI.repeat(activity_three.id, booster)
869 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
871 assert announce_activity.id == announce.id
874 test "excludes reblogs on request" do
876 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
877 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
879 [activity] = ActivityPub.fetch_user_activities(user, nil, %{exclude_reblogs: true})
881 assert activity == expected_activity
884 describe "irreversible filters" do
887 user_two = insert(:user)
889 insert(:filter, user: user_two, phrase: "cofe", hide: true)
890 insert(:filter, user: user_two, phrase: "ok boomer", hide: true)
891 insert(:filter, user: user_two, phrase: "test", hide: false)
894 type: ["Create", "Announce"],
898 {:ok, %{user: user, user_two: user_two, params: params}}
901 test "it returns statuses if they don't contain exact filter words", %{
905 {:ok, _} = CommonAPI.post(user, %{status: "hey"})
906 {:ok, _} = CommonAPI.post(user, %{status: "got cofefe?"})
907 {:ok, _} = CommonAPI.post(user, %{status: "I am not a boomer"})
908 {:ok, _} = CommonAPI.post(user, %{status: "ok boomers"})
909 {:ok, _} = CommonAPI.post(user, %{status: "ccofee is not a word"})
910 {:ok, _} = CommonAPI.post(user, %{status: "this is a test"})
912 activities = ActivityPub.fetch_activities([], params)
914 assert Enum.count(activities) == 6
917 test "it does not filter user's own statuses", %{user_two: user_two, params: params} do
918 {:ok, _} = CommonAPI.post(user_two, %{status: "Give me some cofe!"})
919 {:ok, _} = CommonAPI.post(user_two, %{status: "ok boomer"})
921 activities = ActivityPub.fetch_activities([], params)
923 assert Enum.count(activities) == 2
926 test "it excludes statuses with filter words", %{user: user, params: params} do
927 {:ok, _} = CommonAPI.post(user, %{status: "Give me some cofe!"})
928 {:ok, _} = CommonAPI.post(user, %{status: "ok boomer"})
929 {:ok, _} = CommonAPI.post(user, %{status: "is it a cOfE?"})
930 {:ok, _} = CommonAPI.post(user, %{status: "cofe is all I need"})
931 {:ok, _} = CommonAPI.post(user, %{status: "— ok BOOMER\n"})
933 activities = ActivityPub.fetch_activities([], params)
935 assert Enum.empty?(activities)
938 test "it returns all statuses if user does not have any filters" do
939 another_user = insert(:user)
940 {:ok, _} = CommonAPI.post(another_user, %{status: "got cofe?"})
941 {:ok, _} = CommonAPI.post(another_user, %{status: "test!"})
944 ActivityPub.fetch_activities([], %{
945 type: ["Create", "Announce"],
949 assert Enum.count(activities) == 2
953 describe "public fetch activities" do
954 test "doesn't retrieve unlisted activities" do
957 {:ok, _unlisted_activity} = CommonAPI.post(user, %{status: "yeah", visibility: "unlisted"})
959 {:ok, listed_activity} = CommonAPI.post(user, %{status: "yeah"})
961 [activity] = ActivityPub.fetch_public_activities()
963 assert activity == listed_activity
966 test "retrieves public activities" do
967 _activities = ActivityPub.fetch_public_activities()
969 %{public: public} = ActivityBuilder.public_and_non_public()
971 activities = ActivityPub.fetch_public_activities()
972 assert length(activities) == 1
973 assert Enum.at(activities, 0) == public
976 test "retrieves a maximum of 20 activities" do
977 ActivityBuilder.insert_list(10)
978 expected_activities = ActivityBuilder.insert_list(20)
980 activities = ActivityPub.fetch_public_activities()
982 assert collect_ids(activities) == collect_ids(expected_activities)
983 assert length(activities) == 20
986 test "retrieves ids starting from a since_id" do
987 activities = ActivityBuilder.insert_list(30)
988 expected_activities = ActivityBuilder.insert_list(10)
989 since_id = List.last(activities).id
991 activities = ActivityPub.fetch_public_activities(%{since_id: since_id})
993 assert collect_ids(activities) == collect_ids(expected_activities)
994 assert length(activities) == 10
997 test "retrieves ids up to max_id" do
998 ActivityBuilder.insert_list(10)
999 expected_activities = ActivityBuilder.insert_list(20)
1003 |> ActivityBuilder.insert_list()
1006 activities = ActivityPub.fetch_public_activities(%{max_id: max_id})
1008 assert length(activities) == 20
1009 assert collect_ids(activities) == collect_ids(expected_activities)
1012 test "paginates via offset/limit" do
1013 _first_part_activities = ActivityBuilder.insert_list(10)
1014 second_part_activities = ActivityBuilder.insert_list(10)
1016 later_activities = ActivityBuilder.insert_list(10)
1018 activities = ActivityPub.fetch_public_activities(%{page: "2", page_size: "20"}, :offset)
1020 assert length(activities) == 20
1022 assert collect_ids(activities) ==
1023 collect_ids(second_part_activities) ++ collect_ids(later_activities)
1026 test "doesn't return reblogs for users for whom reblogs have been muted" do
1027 activity = insert(:note_activity)
1028 user = insert(:user)
1029 booster = insert(:user)
1030 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
1032 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
1034 activities = ActivityPub.fetch_activities([], %{muting_user: user})
1036 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
1039 test "returns reblogs for users for whom reblogs have not been muted" do
1040 activity = insert(:note_activity)
1041 user = insert(:user)
1042 booster = insert(:user)
1043 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
1044 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
1046 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
1048 activities = ActivityPub.fetch_activities([], %{muting_user: user})
1050 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
1054 describe "uploading files" do
1056 test_file = %Plug.Upload{
1057 content_type: "image/jpeg",
1058 path: Path.absname("test/fixtures/image.jpg"),
1059 filename: "an_image.jpg"
1062 %{test_file: test_file}
1065 test "sets a description if given", %{test_file: file} do
1066 {:ok, %Object{} = object} = ActivityPub.upload(file, description: "a cool file")
1067 assert object.data["name"] == "a cool file"
1070 test "it sets the default description depending on the configuration", %{test_file: file} do
1071 clear_config([Pleroma.Upload, :default_description])
1073 Pleroma.Config.put([Pleroma.Upload, :default_description], nil)
1074 {:ok, %Object{} = object} = ActivityPub.upload(file)
1075 assert object.data["name"] == ""
1077 Pleroma.Config.put([Pleroma.Upload, :default_description], :filename)
1078 {:ok, %Object{} = object} = ActivityPub.upload(file)
1079 assert object.data["name"] == "an_image.jpg"
1081 Pleroma.Config.put([Pleroma.Upload, :default_description], "unnamed attachment")
1082 {:ok, %Object{} = object} = ActivityPub.upload(file)
1083 assert object.data["name"] == "unnamed attachment"
1086 test "copies the file to the configured folder", %{test_file: file} do
1087 clear_config([Pleroma.Upload, :default_description], :filename)
1088 {:ok, %Object{} = object} = ActivityPub.upload(file)
1089 assert object.data["name"] == "an_image.jpg"
1092 test "works with base64 encoded images" do
1097 {:ok, %Object{}} = ActivityPub.upload(file)
1101 describe "fetch the latest Follow" do
1102 test "fetches the latest Follow activity" do
1103 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1104 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1105 followed = Repo.get_by(User, ap_id: activity.data["object"])
1107 assert activity == Utils.fetch_latest_follow(follower, followed)
1111 describe "unfollowing" do
1112 test "it reverts unfollow activity" do
1113 follower = insert(:user)
1114 followed = insert(:user)
1116 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1118 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1119 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
1122 activity = Activity.get_by_id(follow_activity.id)
1123 assert activity.data["type"] == "Follow"
1124 assert activity.data["actor"] == follower.ap_id
1126 assert activity.data["object"] == followed.ap_id
1129 test "creates an undo activity for the last follow" do
1130 follower = insert(:user)
1131 followed = insert(:user)
1133 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1134 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1136 assert activity.data["type"] == "Undo"
1137 assert activity.data["actor"] == follower.ap_id
1139 embedded_object = activity.data["object"]
1140 assert is_map(embedded_object)
1141 assert embedded_object["type"] == "Follow"
1142 assert embedded_object["object"] == followed.ap_id
1143 assert embedded_object["id"] == follow_activity.data["id"]
1146 test "creates an undo activity for a pending follow request" do
1147 follower = insert(:user)
1148 followed = insert(:user, %{is_locked: true})
1150 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1151 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1153 assert activity.data["type"] == "Undo"
1154 assert activity.data["actor"] == follower.ap_id
1156 embedded_object = activity.data["object"]
1157 assert is_map(embedded_object)
1158 assert embedded_object["type"] == "Follow"
1159 assert embedded_object["object"] == followed.ap_id
1160 assert embedded_object["id"] == follow_activity.data["id"]
1164 describe "timeline post-processing" do
1165 test "it filters broken threads" do
1166 user1 = insert(:user)
1167 user2 = insert(:user)
1168 user3 = insert(:user)
1170 {:ok, user1, user3} = User.follow(user1, user3)
1171 assert User.following?(user1, user3)
1173 {:ok, user2, user3} = User.follow(user2, user3)
1174 assert User.following?(user2, user3)
1176 {:ok, user3, user2} = User.follow(user3, user2)
1177 assert User.following?(user3, user2)
1179 {:ok, public_activity} = CommonAPI.post(user3, %{status: "hi 1"})
1181 {:ok, private_activity_1} = CommonAPI.post(user3, %{status: "hi 2", visibility: "private"})
1183 {:ok, private_activity_2} =
1184 CommonAPI.post(user2, %{
1186 visibility: "private",
1187 in_reply_to_status_id: private_activity_1.id
1190 {:ok, private_activity_3} =
1191 CommonAPI.post(user3, %{
1193 visibility: "private",
1194 in_reply_to_status_id: private_activity_2.id
1198 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1199 |> Enum.map(fn a -> a.id end)
1201 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1203 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1205 assert length(activities) == 3
1208 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{user: user1})
1209 |> Enum.map(fn a -> a.id end)
1211 assert [public_activity.id, private_activity_1.id] == activities
1212 assert length(activities) == 2
1216 describe "flag/1" do
1218 reporter = insert(:user)
1219 target_account = insert(:user)
1221 {:ok, activity} = CommonAPI.post(target_account, %{status: content})
1222 context = Utils.generate_context_id()
1224 reporter_ap_id = reporter.ap_id
1225 target_ap_id = target_account.ap_id
1226 activity_ap_id = activity.data["id"]
1228 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1234 target_account: target_account,
1235 reported_activity: activity,
1237 activity_ap_id: activity_ap_id,
1238 activity_with_object: activity_with_object,
1239 reporter_ap_id: reporter_ap_id,
1240 target_ap_id: target_ap_id
1244 test "it can create a Flag activity",
1248 target_account: target_account,
1249 reported_activity: reported_activity,
1251 activity_ap_id: activity_ap_id,
1252 activity_with_object: activity_with_object,
1253 reporter_ap_id: reporter_ap_id,
1254 target_ap_id: target_ap_id
1256 assert {:ok, activity} =
1260 account: target_account,
1261 statuses: [reported_activity],
1267 "id" => activity_ap_id,
1268 "content" => content,
1269 "published" => activity_with_object.object.data["published"],
1271 AccountView.render("show.json", %{user: target_account, skip_visibility_check: true})
1275 actor: ^reporter_ap_id,
1278 "content" => ^content,
1279 "context" => ^context,
1280 "object" => [^target_ap_id, ^note_obj]
1285 test_with_mock "strips status data from Flag, before federating it",
1289 target_account: target_account,
1290 reported_activity: reported_activity,
1300 account: target_account,
1301 statuses: [reported_activity],
1306 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1308 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1311 test_with_mock "reverts on error",
1315 target_account: target_account,
1316 reported_activity: reported_activity,
1321 maybe_federate: fn _ -> {:error, :reverted} end do
1322 assert {:error, :reverted} =
1326 account: target_account,
1327 statuses: [reported_activity],
1331 assert Repo.aggregate(Activity, :count, :id) == 1
1332 assert Repo.aggregate(Object, :count, :id) == 2
1333 assert Repo.aggregate(Notification, :count, :id) == 0
1337 test "fetch_activities/2 returns activities addressed to a list " do
1338 user = insert(:user)
1339 member = insert(:user)
1340 {:ok, list} = Pleroma.List.create("foo", user)
1341 {:ok, list} = Pleroma.List.follow(list, member)
1343 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1345 activity = Repo.preload(activity, :bookmark)
1346 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1348 assert ActivityPub.fetch_activities([], %{user: user}) == [activity]
1352 File.read!("test/fixtures/avatar_data_uri")
1355 describe "fetch_activities_bounded" do
1356 test "fetches private posts for followed users" do
1357 user = insert(:user)
1360 CommonAPI.post(user, %{
1361 status: "thought I looked cute might delete later :3",
1362 visibility: "private"
1365 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1366 assert result.id == activity.id
1369 test "fetches only public posts for other users" do
1370 user = insert(:user)
1371 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe", visibility: "public"})
1373 {:ok, _private_activity} =
1374 CommonAPI.post(user, %{
1375 status: "why is tenshi eating a corndog so cute?",
1376 visibility: "private"
1379 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1380 assert result.id == activity.id
1384 describe "fetch_follow_information_for_user" do
1385 test "syncronizes following/followers counters" do
1389 follower_address: "http://localhost:4001/users/fuser2/followers",
1390 following_address: "http://localhost:4001/users/fuser2/following"
1393 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1394 assert info.follower_count == 527
1395 assert info.following_count == 267
1398 test "detects hidden followers" do
1401 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1402 %Tesla.Env{status: 403, body: ""}
1405 apply(HttpRequestMock, :request, [env])
1412 follower_address: "http://localhost:4001/users/masto_closed/followers",
1413 following_address: "http://localhost:4001/users/masto_closed/following"
1416 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1417 assert follow_info.hide_followers == true
1418 assert follow_info.hide_follows == false
1421 test "detects hidden follows" do
1424 "http://localhost:4001/users/masto_closed/following?page=1" ->
1425 %Tesla.Env{status: 403, body: ""}
1428 apply(HttpRequestMock, :request, [env])
1435 follower_address: "http://localhost:4001/users/masto_closed/followers",
1436 following_address: "http://localhost:4001/users/masto_closed/following"
1439 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1440 assert follow_info.hide_followers == false
1441 assert follow_info.hide_follows == true
1444 test "detects hidden follows/followers for friendica" do
1448 follower_address: "http://localhost:8080/followers/fuser3",
1449 following_address: "http://localhost:8080/following/fuser3"
1452 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1453 assert follow_info.hide_followers == true
1454 assert follow_info.follower_count == 296
1455 assert follow_info.following_count == 32
1456 assert follow_info.hide_follows == true
1459 test "doesn't crash when follower and following counters are hidden" do
1462 "http://localhost:4001/users/masto_hidden_counters/following" ->
1465 "@context" => "https://www.w3.org/ns/activitystreams",
1466 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1468 headers: HttpRequestMock.activitypub_object_headers()
1471 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1472 %Tesla.Env{status: 403, body: ""}
1474 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1477 "@context" => "https://www.w3.org/ns/activitystreams",
1478 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1480 headers: HttpRequestMock.activitypub_object_headers()
1483 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1484 %Tesla.Env{status: 403, body: ""}
1491 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1492 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1495 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1497 assert follow_info.hide_followers == true
1498 assert follow_info.follower_count == 0
1499 assert follow_info.hide_follows == true
1500 assert follow_info.following_count == 0
1504 describe "fetch_favourites/3" do
1505 test "returns a favourite activities sorted by adds to favorite" do
1506 user = insert(:user)
1507 other_user = insert(:user)
1508 user1 = insert(:user)
1509 user2 = insert(:user)
1510 {:ok, a1} = CommonAPI.post(user1, %{status: "bla"})
1511 {:ok, _a2} = CommonAPI.post(user2, %{status: "traps are happy"})
1512 {:ok, a3} = CommonAPI.post(user2, %{status: "Trees Are "})
1513 {:ok, a4} = CommonAPI.post(user2, %{status: "Agent Smith "})
1514 {:ok, a5} = CommonAPI.post(user1, %{status: "Red or Blue "})
1516 {:ok, _} = CommonAPI.favorite(user, a4.id)
1517 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1518 {:ok, _} = CommonAPI.favorite(user, a3.id)
1519 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1520 {:ok, _} = CommonAPI.favorite(user, a5.id)
1521 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1522 {:ok, _} = CommonAPI.favorite(user, a1.id)
1523 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1524 result = ActivityPub.fetch_favourites(user)
1526 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1528 result = ActivityPub.fetch_favourites(user, %{limit: 2})
1529 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1533 describe "Move activity" do
1535 %{ap_id: old_ap_id} = old_user = insert(:user)
1536 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1537 follower = insert(:user)
1538 follower_move_opted_out = insert(:user, allow_following_move: false)
1540 User.follow(follower, old_user)
1541 User.follow(follower_move_opted_out, old_user)
1543 assert User.following?(follower, old_user)
1544 assert User.following?(follower_move_opted_out, old_user)
1546 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1551 "actor" => ^old_ap_id,
1552 "object" => ^old_ap_id,
1553 "target" => ^new_ap_id,
1560 "op" => "move_following",
1561 "origin_id" => old_user.id,
1562 "target_id" => new_user.id
1565 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1567 Pleroma.Workers.BackgroundWorker.perform(%Oban.Job{args: params})
1569 refute User.following?(follower, old_user)
1570 assert User.following?(follower, new_user)
1572 assert User.following?(follower_move_opted_out, old_user)
1573 refute User.following?(follower_move_opted_out, new_user)
1575 activity = %Activity{activity | object: nil}
1577 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1579 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1582 test "old user must be in the new user's `also_known_as` list" do
1583 old_user = insert(:user)
1584 new_user = insert(:user)
1586 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1587 ActivityPub.move(old_user, new_user)
1591 test "doesn't retrieve replies activities with exclude_replies" do
1592 user = insert(:user)
1594 {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
1596 {:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id})
1598 [result] = ActivityPub.fetch_public_activities(%{exclude_replies: true})
1600 assert result.id == activity.id
1602 assert length(ActivityPub.fetch_public_activities()) == 2
1605 describe "replies filtering with public messages" do
1606 setup :public_messages
1608 test "public timeline", %{users: %{u1: user}} do
1611 |> Map.put(:type, ["Create", "Announce"])
1612 |> Map.put(:local_only, false)
1613 |> Map.put(:blocking_user, user)
1614 |> Map.put(:muting_user, user)
1615 |> Map.put(:reply_filtering_user, user)
1616 |> ActivityPub.fetch_public_activities()
1617 |> Enum.map(& &1.id)
1619 assert length(activities_ids) == 16
1622 test "public timeline with reply_visibility `following`", %{
1628 activities: activities
1632 |> Map.put(:type, ["Create", "Announce"])
1633 |> Map.put(:local_only, false)
1634 |> Map.put(:blocking_user, user)
1635 |> Map.put(:muting_user, user)
1636 |> Map.put(:reply_visibility, "following")
1637 |> Map.put(:reply_filtering_user, user)
1638 |> ActivityPub.fetch_public_activities()
1639 |> Enum.map(& &1.id)
1641 assert length(activities_ids) == 14
1644 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1646 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1649 test "public timeline with reply_visibility `self`", %{
1655 activities: activities
1659 |> Map.put(:type, ["Create", "Announce"])
1660 |> Map.put(:local_only, false)
1661 |> Map.put(:blocking_user, user)
1662 |> Map.put(:muting_user, user)
1663 |> Map.put(:reply_visibility, "self")
1664 |> Map.put(:reply_filtering_user, user)
1665 |> ActivityPub.fetch_public_activities()
1666 |> Enum.map(& &1.id)
1668 assert length(activities_ids) == 10
1669 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1670 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1673 test "home timeline", %{
1675 activities: activities,
1683 |> Map.put(:type, ["Create", "Announce"])
1684 |> Map.put(:blocking_user, user)
1685 |> Map.put(:muting_user, user)
1686 |> Map.put(:user, user)
1687 |> Map.put(:reply_filtering_user, user)
1690 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1691 |> Enum.map(& &1.id)
1693 assert length(activities_ids) == 13
1708 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1711 test "home timeline with reply_visibility `following`", %{
1713 activities: activities,
1721 |> Map.put(:type, ["Create", "Announce"])
1722 |> Map.put(:blocking_user, user)
1723 |> Map.put(:muting_user, user)
1724 |> Map.put(:user, user)
1725 |> Map.put(:reply_visibility, "following")
1726 |> Map.put(:reply_filtering_user, user)
1729 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1730 |> Enum.map(& &1.id)
1732 assert length(activities_ids) == 11
1747 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1750 test "home timeline with reply_visibility `self`", %{
1752 activities: activities,
1760 |> Map.put(:type, ["Create", "Announce"])
1761 |> Map.put(:blocking_user, user)
1762 |> Map.put(:muting_user, user)
1763 |> Map.put(:user, user)
1764 |> Map.put(:reply_visibility, "self")
1765 |> Map.put(:reply_filtering_user, user)
1768 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1769 |> Enum.map(& &1.id)
1771 assert length(activities_ids) == 9
1784 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1787 test "filtering out announces where the user is the actor of the announced message" do
1788 user = insert(:user)
1789 other_user = insert(:user)
1790 third_user = insert(:user)
1791 User.follow(user, other_user)
1793 {:ok, post} = CommonAPI.post(user, %{status: "yo"})
1794 {:ok, other_post} = CommonAPI.post(third_user, %{status: "yo"})
1795 {:ok, _announce} = CommonAPI.repeat(post.id, other_user)
1796 {:ok, _announce} = CommonAPI.repeat(post.id, third_user)
1797 {:ok, announce} = CommonAPI.repeat(other_post.id, other_user)
1804 [user.ap_id | User.following(user)]
1805 |> ActivityPub.fetch_activities(params)
1807 assert length(results) == 3
1811 announce_filtering_user: user
1815 [user.ap_id | User.following(user)]
1816 |> ActivityPub.fetch_activities(params)
1818 assert result.id == announce.id
1822 describe "replies filtering with private messages" do
1823 setup :private_messages
1825 test "public timeline", %{users: %{u1: user}} do
1828 |> Map.put(:type, ["Create", "Announce"])
1829 |> Map.put(:local_only, false)
1830 |> Map.put(:blocking_user, user)
1831 |> Map.put(:muting_user, user)
1832 |> Map.put(:user, user)
1833 |> ActivityPub.fetch_public_activities()
1834 |> Enum.map(& &1.id)
1836 assert activities_ids == []
1839 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1842 |> Map.put(:type, ["Create", "Announce"])
1843 |> Map.put(:local_only, false)
1844 |> Map.put(:blocking_user, user)
1845 |> Map.put(:muting_user, user)
1846 |> Map.put(:reply_visibility, "following")
1847 |> Map.put(:reply_filtering_user, user)
1848 |> Map.put(:user, user)
1849 |> ActivityPub.fetch_public_activities()
1850 |> Enum.map(& &1.id)
1852 assert activities_ids == []
1855 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
1858 |> Map.put(:type, ["Create", "Announce"])
1859 |> Map.put(:local_only, false)
1860 |> Map.put(:blocking_user, user)
1861 |> Map.put(:muting_user, user)
1862 |> Map.put(:reply_visibility, "self")
1863 |> Map.put(:reply_filtering_user, user)
1864 |> Map.put(:user, user)
1865 |> ActivityPub.fetch_public_activities()
1866 |> Enum.map(& &1.id)
1868 assert activities_ids == []
1872 |> Map.put(:reply_visibility, "self")
1873 |> Map.put(:reply_filtering_user, nil)
1874 |> ActivityPub.fetch_public_activities()
1876 assert activities_ids == []
1879 test "home timeline", %{users: %{u1: user}} do
1882 |> Map.put(:type, ["Create", "Announce"])
1883 |> Map.put(:blocking_user, user)
1884 |> Map.put(:muting_user, user)
1885 |> Map.put(:user, user)
1888 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1889 |> Enum.map(& &1.id)
1891 assert length(activities_ids) == 12
1894 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1897 |> Map.put(:type, ["Create", "Announce"])
1898 |> Map.put(:blocking_user, user)
1899 |> Map.put(:muting_user, user)
1900 |> Map.put(:user, user)
1901 |> Map.put(:reply_visibility, "following")
1902 |> Map.put(:reply_filtering_user, user)
1905 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1906 |> Enum.map(& &1.id)
1908 assert length(activities_ids) == 12
1911 test "home timeline with default reply_visibility `self`", %{
1913 activities: activities,
1921 |> Map.put(:type, ["Create", "Announce"])
1922 |> Map.put(:blocking_user, user)
1923 |> Map.put(:muting_user, user)
1924 |> Map.put(:user, user)
1925 |> Map.put(:reply_visibility, "self")
1926 |> Map.put(:reply_filtering_user, user)
1929 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1930 |> Enum.map(& &1.id)
1932 assert length(activities_ids) == 10
1935 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
1937 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1941 defp public_messages(_) do
1942 [u1, u2, u3, u4] = insert_list(4, :user)
1943 {:ok, u1, u2} = User.follow(u1, u2)
1944 {:ok, u2, u1} = User.follow(u2, u1)
1945 {:ok, u1, u4} = User.follow(u1, u4)
1946 {:ok, u4, u1} = User.follow(u4, u1)
1948 {:ok, u2, u3} = User.follow(u2, u3)
1949 {:ok, u3, u2} = User.follow(u3, u2)
1951 {:ok, a1} = CommonAPI.post(u1, %{status: "Status"})
1954 CommonAPI.post(u2, %{
1955 status: "@#{u1.nickname} reply from u2 to u1",
1956 in_reply_to_status_id: a1.id
1960 CommonAPI.post(u3, %{
1961 status: "@#{u1.nickname} reply from u3 to u1",
1962 in_reply_to_status_id: a1.id
1966 CommonAPI.post(u4, %{
1967 status: "@#{u1.nickname} reply from u4 to u1",
1968 in_reply_to_status_id: a1.id
1971 {:ok, a2} = CommonAPI.post(u2, %{status: "Status"})
1974 CommonAPI.post(u1, %{
1975 status: "@#{u2.nickname} reply from u1 to u2",
1976 in_reply_to_status_id: a2.id
1980 CommonAPI.post(u3, %{
1981 status: "@#{u2.nickname} reply from u3 to u2",
1982 in_reply_to_status_id: a2.id
1986 CommonAPI.post(u4, %{
1987 status: "@#{u2.nickname} reply from u4 to u2",
1988 in_reply_to_status_id: a2.id
1991 {:ok, a3} = CommonAPI.post(u3, %{status: "Status"})
1994 CommonAPI.post(u1, %{
1995 status: "@#{u3.nickname} reply from u1 to u3",
1996 in_reply_to_status_id: a3.id
2000 CommonAPI.post(u2, %{
2001 status: "@#{u3.nickname} reply from u2 to u3",
2002 in_reply_to_status_id: a3.id
2006 CommonAPI.post(u4, %{
2007 status: "@#{u3.nickname} reply from u4 to u3",
2008 in_reply_to_status_id: a3.id
2011 {:ok, a4} = CommonAPI.post(u4, %{status: "Status"})
2014 CommonAPI.post(u1, %{
2015 status: "@#{u4.nickname} reply from u1 to u4",
2016 in_reply_to_status_id: a4.id
2020 CommonAPI.post(u2, %{
2021 status: "@#{u4.nickname} reply from u2 to u4",
2022 in_reply_to_status_id: a4.id
2026 CommonAPI.post(u3, %{
2027 status: "@#{u4.nickname} reply from u3 to u4",
2028 in_reply_to_status_id: a4.id
2032 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2033 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2034 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2035 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
2036 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
2037 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
2040 defp private_messages(_) do
2041 [u1, u2, u3, u4] = insert_list(4, :user)
2042 {:ok, u1, u2} = User.follow(u1, u2)
2043 {:ok, u2, u1} = User.follow(u2, u1)
2044 {:ok, u1, u3} = User.follow(u1, u3)
2045 {:ok, u3, u1} = User.follow(u3, u1)
2046 {:ok, u1, u4} = User.follow(u1, u4)
2047 {:ok, u4, u1} = User.follow(u4, u1)
2049 {:ok, u2, u3} = User.follow(u2, u3)
2050 {:ok, u3, u2} = User.follow(u3, u2)
2052 {:ok, a1} = CommonAPI.post(u1, %{status: "Status", visibility: "private"})
2055 CommonAPI.post(u2, %{
2056 status: "@#{u1.nickname} reply from u2 to u1",
2057 in_reply_to_status_id: a1.id,
2058 visibility: "private"
2062 CommonAPI.post(u3, %{
2063 status: "@#{u1.nickname} reply from u3 to u1",
2064 in_reply_to_status_id: a1.id,
2065 visibility: "private"
2069 CommonAPI.post(u4, %{
2070 status: "@#{u1.nickname} reply from u4 to u1",
2071 in_reply_to_status_id: a1.id,
2072 visibility: "private"
2075 {:ok, a2} = CommonAPI.post(u2, %{status: "Status", visibility: "private"})
2078 CommonAPI.post(u1, %{
2079 status: "@#{u2.nickname} reply from u1 to u2",
2080 in_reply_to_status_id: a2.id,
2081 visibility: "private"
2085 CommonAPI.post(u3, %{
2086 status: "@#{u2.nickname} reply from u3 to u2",
2087 in_reply_to_status_id: a2.id,
2088 visibility: "private"
2091 {:ok, a3} = CommonAPI.post(u3, %{status: "Status", visibility: "private"})
2094 CommonAPI.post(u1, %{
2095 status: "@#{u3.nickname} reply from u1 to u3",
2096 in_reply_to_status_id: a3.id,
2097 visibility: "private"
2101 CommonAPI.post(u2, %{
2102 status: "@#{u3.nickname} reply from u2 to u3",
2103 in_reply_to_status_id: a3.id,
2104 visibility: "private"
2107 {:ok, a4} = CommonAPI.post(u4, %{status: "Status", visibility: "private"})
2110 CommonAPI.post(u1, %{
2111 status: "@#{u4.nickname} reply from u1 to u4",
2112 in_reply_to_status_id: a4.id,
2113 visibility: "private"
2117 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2118 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2119 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2120 u2: %{r1: r2_1.id, r2: r2_2.id},
2121 u3: %{r1: r3_1.id, r2: r3_2.id},
2125 describe "maybe_update_follow_information/1" do
2127 clear_config([:instance, :external_user_synchronization], true)
2131 ap_id: "https://gensokyo.2hu/users/raymoo",
2132 following_address: "https://gensokyo.2hu/users/following",
2133 follower_address: "https://gensokyo.2hu/users/followers",
2140 test "logs an error when it can't fetch the info", %{user: user} do
2141 assert capture_log(fn ->
2142 ActivityPub.maybe_update_follow_information(user)
2143 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2146 test "just returns the input if the user type is Application", %{
2151 |> Map.put(:type, "Application")
2153 refute capture_log(fn ->
2154 assert ^user = ActivityPub.maybe_update_follow_information(user)
2155 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2158 test "it just returns the input if the user has no following/follower addresses", %{
2163 |> Map.put(:following_address, nil)
2164 |> Map.put(:follower_address, nil)
2166 refute capture_log(fn ->
2167 assert ^user = ActivityPub.maybe_update_follow_information(user)
2168 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2172 describe "global activity expiration" do
2173 test "creates an activity expiration for local Create activities" do
2174 clear_config([:mrf, :policies], Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy)
2176 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
2177 {:ok, follow} = ActivityBuilder.insert(%{"type" => "Follow", "context" => "3hu"})
2180 worker: Pleroma.Workers.PurgeExpiredActivity,
2181 args: %{activity_id: activity.id},
2183 activity.inserted_at
2184 |> DateTime.from_naive!("Etc/UTC")
2185 |> Timex.shift(days: 365)
2189 worker: Pleroma.Workers.PurgeExpiredActivity,
2190 args: %{activity_id: follow.id}
2195 describe "handling of clashing nicknames" do
2196 test "renames an existing user with a clashing nickname and a different ap id" do
2201 nickname: "admin@mastodon.example.org",
2202 ap_id: "http://mastodon.example.org/users/harinezumigari"
2206 nickname: orig_user.nickname,
2207 ap_id: orig_user.ap_id <> "part_2"
2209 |> ActivityPub.maybe_handle_clashing_nickname()
2211 user = User.get_by_id(orig_user.id)
2213 assert user.nickname == "#{orig_user.id}.admin@mastodon.example.org"
2216 test "does nothing with a clashing nickname and the same ap id" do
2221 nickname: "admin@mastodon.example.org",
2222 ap_id: "http://mastodon.example.org/users/harinezumigari"
2226 nickname: orig_user.nickname,
2227 ap_id: orig_user.ap_id
2229 |> ActivityPub.maybe_handle_clashing_nickname()
2231 user = User.get_by_id(orig_user.id)
2233 assert user.nickname == orig_user.nickname
2237 describe "reply filtering" do
2238 test "`following` still contains announcements by friends" do
2239 user = insert(:user)
2240 followed = insert(:user)
2241 not_followed = insert(:user)
2243 User.follow(user, followed)
2245 {:ok, followed_post} = CommonAPI.post(followed, %{status: "Hello"})
2247 {:ok, not_followed_to_followed} =
2248 CommonAPI.post(not_followed, %{
2249 status: "Also hello",
2250 in_reply_to_status_id: followed_post.id
2253 {:ok, retoot} = CommonAPI.repeat(not_followed_to_followed.id, followed)
2257 |> Map.put(:type, ["Create", "Announce"])
2258 |> Map.put(:blocking_user, user)
2259 |> Map.put(:muting_user, user)
2260 |> Map.put(:reply_filtering_user, user)
2261 |> Map.put(:reply_visibility, "following")
2262 |> Map.put(:announce_filtering_user, user)
2263 |> Map.put(:user, user)
2266 [user.ap_id | User.following(user)]
2267 |> ActivityPub.fetch_activities(params)
2269 followed_post_id = followed_post.id
2270 retoot_id = retoot.id
2272 assert [%{id: ^followed_post_id}, %{id: ^retoot_id}] = activities
2274 assert length(activities) == 2
2277 # This test is skipped because, while this is the desired behavior,
2278 # there seems to be no good way to achieve it with the method that
2279 # we currently use for detecting to who a reply is directed.
2280 # This is a TODO and should be fixed by a later rewrite of the code
2283 test "`following` still contains self-replies by friends" do
2284 user = insert(:user)
2285 followed = insert(:user)
2286 not_followed = insert(:user)
2288 User.follow(user, followed)
2290 {:ok, followed_post} = CommonAPI.post(followed, %{status: "Hello"})
2291 {:ok, not_followed_post} = CommonAPI.post(not_followed, %{status: "Also hello"})
2293 {:ok, _followed_to_not_followed} =
2294 CommonAPI.post(followed, %{status: "sup", in_reply_to_status_id: not_followed_post.id})
2296 {:ok, _followed_self_reply} =
2297 CommonAPI.post(followed, %{status: "Also cofe", in_reply_to_status_id: followed_post.id})
2301 |> Map.put(:type, ["Create", "Announce"])
2302 |> Map.put(:blocking_user, user)
2303 |> Map.put(:muting_user, user)
2304 |> Map.put(:reply_filtering_user, user)
2305 |> Map.put(:reply_visibility, "following")
2306 |> Map.put(:announce_filtering_user, user)
2307 |> Map.put(:user, user)
2310 [user.ap_id | User.following(user)]
2311 |> ActivityPub.fetch_activities(params)
2313 assert length(activities) == 2
2317 test "allow fetching of accounts with an empty string name field" do
2319 %{method: :get, url: "https://princess.cat/users/mewmew"} ->
2320 file = File.read!("test/fixtures/mewmew_no_name.json")
2321 %Tesla.Env{status: 200, body: file, headers: HttpRequestMock.activitypub_object_headers()}
2324 {:ok, user} = ActivityPub.make_user_from_ap_id("https://princess.cat/users/mewmew")
2325 assert user.name == " "