1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
194 test "works for guppe actors" do
195 user_id = "https://gup.pe/u/bernie2020"
198 %{method: :get, url: ^user_id} ->
201 body: File.read!("test/fixtures/guppe-actor.json"),
202 headers: [{"content-type", "application/activity+json"}]
206 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
208 assert user.name == "Bernie2020 group"
209 assert user.actor_type == "Group"
212 test "works for bridgy actors" do
213 user_id = "https://fed.brid.gy/jk.nipponalba.scot"
216 %{method: :get, url: ^user_id} ->
219 body: File.read!("test/fixtures/bridgy/actor.json"),
220 headers: [{"content-type", "application/activity+json"}]
224 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
226 assert user.actor_type == "Person"
228 assert user.avatar == %{
230 "url" => [%{"href" => "https://jk.nipponalba.scot/images/profile.jpg"}]
233 assert user.banner == %{
235 "url" => [%{"href" => "https://jk.nipponalba.scot/images/profile.jpg"}]
239 test "fetches user featured collection" do
240 ap_id = "https://example.com/users/lain"
242 featured_url = "https://example.com/users/lain/collections/featured"
245 "test/fixtures/users_mock/user.json"
247 |> String.replace("{{nickname}}", "lain")
249 |> Map.put("featured", featured_url)
252 object_id = Ecto.UUID.generate()
255 "test/fixtures/mastodon/collections/featured.json"
257 |> String.replace("{{domain}}", "example.com")
258 |> String.replace("{{nickname}}", "lain")
259 |> String.replace("{{object_id}}", object_id)
261 object_url = "https://example.com/objects/#{object_id}"
264 "test/fixtures/statuses/note.json"
266 |> String.replace("{{object_id}}", object_id)
267 |> String.replace("{{nickname}}", "lain")
277 headers: [{"content-type", "application/activity+json"}]
287 headers: [{"content-type", "application/activity+json"}]
291 Tesla.Mock.mock_global(fn
299 headers: [{"content-type", "application/activity+json"}]
303 {:ok, user} = ActivityPub.make_user_from_ap_id(ap_id)
306 assert user.featured_address == featured_url
307 assert Map.has_key?(user.pinned_objects, object_url)
309 in_db = Pleroma.User.get_by_ap_id(ap_id)
310 assert in_db.featured_address == featured_url
311 assert Map.has_key?(user.pinned_objects, object_url)
313 assert %{data: %{"id" => ^object_url}} = Object.get_by_ap_id(object_url)
317 test "it fetches the appropriate tag-restricted posts" do
320 {:ok, status_one} = CommonAPI.post(user, %{status: ". #TEST"})
321 {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
322 {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #Reject"})
324 {:ok, status_four} = CommonAPI.post(user, %{status: ". #Any1 #any2"})
325 {:ok, status_five} = CommonAPI.post(user, %{status: ". #Any2 #any1"})
327 for hashtag_timeline_strategy <- [:enabled, :disabled] do
328 clear_config([:features, :improved_hashtag_timeline], hashtag_timeline_strategy)
330 fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"})
332 fetch_two = ActivityPub.fetch_activities([], %{type: "Create", tag: ["TEST", "essais"]})
335 ActivityPub.fetch_activities([], %{
337 tag: ["test", "Essais"],
338 tag_reject: ["reject"]
342 ActivityPub.fetch_activities([], %{
345 tag_all: ["test", "REJECT"]
348 # Testing that deduplication (if needed) is done on DB (not Ecto) level; :limit is important
350 ActivityPub.fetch_activities([], %{
352 tag: ["ANY1", "any2"],
357 ActivityPub.fetch_activities([], %{
359 tag: ["any1", "Any2"],
364 # Regression test: passing empty lists as filter options shouldn't affect the results
365 assert fetch_five == fetch_six
367 [fetch_one, fetch_two, fetch_three, fetch_four, fetch_five] =
368 Enum.map([fetch_one, fetch_two, fetch_three, fetch_four, fetch_five], fn statuses ->
369 Enum.map(statuses, fn s -> Repo.preload(s, object: :hashtags) end)
372 assert fetch_one == [status_one, status_three]
373 assert fetch_two == [status_one, status_two, status_three]
374 assert fetch_three == [status_one, status_two]
375 assert fetch_four == [status_three]
376 assert fetch_five == [status_four, status_five]
380 describe "insertion" do
381 test "drops activities beyond a certain limit" do
382 limit = Config.get([:instance, :remote_limit])
385 :crypto.strong_rand_bytes(limit + 1)
387 |> binary_part(0, limit + 1)
392 "content" => random_text
396 assert {:error, :remote_limit} = ActivityPub.insert(data)
399 test "doesn't drop activities with content being null" do
403 "actor" => user.ap_id,
406 "actor" => user.ap_id,
413 assert {:ok, _} = ActivityPub.insert(data)
416 test "returns the activity if one with the same id is already in" do
417 activity = insert(:note_activity)
418 {:ok, new_activity} = ActivityPub.insert(activity.data)
420 assert activity.id == new_activity.id
423 test "inserts a given map into the activity database, giving it an id if it has none." do
427 "actor" => user.ap_id,
430 "actor" => user.ap_id,
437 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
438 assert activity.data["ok"] == data["ok"]
439 assert is_binary(activity.data["id"])
445 "actor" => user.ap_id,
447 "context" => "blabla",
449 "actor" => user.ap_id,
456 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
457 assert activity.data["ok"] == data["ok"]
458 assert activity.data["id"] == given_id
459 assert activity.data["context"] == "blabla"
460 assert activity.data["context_id"]
463 test "adds a context when none is there" do
467 "actor" => user.ap_id,
470 "actor" => user.ap_id,
477 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
478 object = Pleroma.Object.normalize(activity, fetch: false)
480 assert is_binary(activity.data["context"])
481 assert is_binary(object.data["context"])
482 assert activity.data["context_id"]
483 assert object.data["context_id"]
486 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
490 "actor" => user.ap_id,
493 "actor" => user.ap_id,
500 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
501 assert object = Object.normalize(activity, fetch: false)
502 assert is_binary(object.data["id"])
506 describe "listen activities" do
507 test "does not increase user note count" do
511 ActivityPub.listen(%{
512 to: ["https://www.w3.org/ns/activitystreams#Public"],
516 "actor" => user.ap_id,
517 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
519 "title" => "lain radio episode 1",
525 assert activity.actor == user.ap_id
527 user = User.get_cached_by_id(user.id)
528 assert user.note_count == 0
531 test "can be fetched into a timeline" do
532 _listen_activity_1 = insert(:listen)
533 _listen_activity_2 = insert(:listen)
534 _listen_activity_3 = insert(:listen)
536 timeline = ActivityPub.fetch_activities([], %{type: ["Listen"]})
538 assert length(timeline) == 3
542 describe "create activities" do
544 [user: insert(:user)]
547 test "it reverts create", %{user: user} do
548 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
549 assert {:error, :reverted} =
550 ActivityPub.create(%{
551 to: ["user1", "user2"],
555 "to" => ["user1", "user2"],
557 "content" => "testing"
562 assert Repo.aggregate(Activity, :count, :id) == 0
563 assert Repo.aggregate(Object, :count, :id) == 0
566 test "creates activity if expiration is not configured and expires_at is not passed", %{
569 clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
572 ActivityPub.create(%{
573 to: ["user1", "user2"],
577 "to" => ["user1", "user2"],
579 "content" => "testing"
584 test "rejects activity if expires_at present but expiration is not configured", %{user: user} do
585 clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
587 assert {:error, :expired_activities_disabled} =
588 ActivityPub.create(%{
589 to: ["user1", "user2"],
593 "to" => ["user1", "user2"],
595 "content" => "testing"
598 "expires_at" => DateTime.utc_now()
602 assert Repo.aggregate(Activity, :count, :id) == 0
603 assert Repo.aggregate(Object, :count, :id) == 0
606 test "removes doubled 'to' recipients", %{user: user} do
608 ActivityPub.create(%{
609 to: ["user1", "user1", "user2"],
613 "to" => ["user1", "user1", "user2"],
615 "content" => "testing"
619 assert activity.data["to"] == ["user1", "user2"]
620 assert activity.actor == user.ap_id
621 assert activity.recipients == ["user1", "user2", user.ap_id]
624 test "increases user note count only for public activities", %{user: user} do
626 CommonAPI.post(User.get_cached_by_id(user.id), %{
632 CommonAPI.post(User.get_cached_by_id(user.id), %{
634 visibility: "unlisted"
638 CommonAPI.post(User.get_cached_by_id(user.id), %{
640 visibility: "private"
644 CommonAPI.post(User.get_cached_by_id(user.id), %{
649 user = User.get_cached_by_id(user.id)
650 assert user.note_count == 2
653 test "increases replies count", %{user: user} do
654 user2 = insert(:user)
656 {:ok, activity} = CommonAPI.post(user, %{status: "1", visibility: "public"})
657 ap_id = activity.data["id"]
658 reply_data = %{status: "1", in_reply_to_status_id: activity.id}
661 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "public"))
662 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
663 assert object.data["repliesCount"] == 1
666 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "unlisted"))
667 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
668 assert object.data["repliesCount"] == 2
671 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "private"))
672 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
673 assert object.data["repliesCount"] == 2
676 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "direct"))
677 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
678 assert object.data["repliesCount"] == 2
682 describe "fetch activities for recipients" do
683 test "retrieve the activities for certain recipients" do
684 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
685 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
686 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
688 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
689 assert length(activities) == 2
690 assert activities == [activity_one, activity_two]
694 describe "fetch activities in context" do
695 test "retrieves activities that have a given context" do
696 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
697 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
698 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
699 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
700 activity_five = insert(:note_activity)
703 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
705 activities = ActivityPub.fetch_activities_for_context("2hu", %{blocking_user: user})
706 assert activities == [activity_two, activity]
709 test "doesn't return activities with filtered words" do
711 user_two = insert(:user)
712 insert(:filter, user: user, phrase: "test", hide: true)
714 {:ok, %{id: id1, data: %{"context" => context}}} = CommonAPI.post(user, %{status: "1"})
716 {:ok, %{id: id2}} = CommonAPI.post(user_two, %{status: "2", in_reply_to_status_id: id1})
718 {:ok, %{id: id3} = user_activity} =
719 CommonAPI.post(user, %{status: "3 test?", in_reply_to_status_id: id2})
721 {:ok, %{id: id4} = filtered_activity} =
722 CommonAPI.post(user_two, %{status: "4 test!", in_reply_to_status_id: id3})
724 {:ok, _} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
728 |> ActivityPub.fetch_activities_for_context(%{user: user})
731 assert length(activities) == 4
732 assert user_activity.id in activities
733 refute filtered_activity.id in activities
737 test "doesn't return blocked activities" do
738 activity_one = insert(:note_activity)
739 activity_two = insert(:note_activity)
740 activity_three = insert(:note_activity)
742 booster = insert(:user)
743 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
745 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
747 assert Enum.member?(activities, activity_two)
748 assert Enum.member?(activities, activity_three)
749 refute Enum.member?(activities, activity_one)
751 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
753 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
755 assert Enum.member?(activities, activity_two)
756 assert Enum.member?(activities, activity_three)
757 assert Enum.member?(activities, activity_one)
759 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
760 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
761 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
762 activity_three = Activity.get_by_id(activity_three.id)
764 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
766 assert Enum.member?(activities, activity_two)
767 refute Enum.member?(activities, activity_three)
768 refute Enum.member?(activities, boost_activity)
769 assert Enum.member?(activities, activity_one)
771 activities = ActivityPub.fetch_activities([], %{blocking_user: nil, skip_preload: true})
773 assert Enum.member?(activities, activity_two)
774 assert Enum.member?(activities, activity_three)
775 assert Enum.member?(activities, boost_activity)
776 assert Enum.member?(activities, activity_one)
779 test "always see your own posts even when they address people you block" do
781 blockee = insert(:user)
783 {:ok, _} = User.block(user, blockee)
784 {:ok, activity} = CommonAPI.post(user, %{status: "hey! @#{blockee.nickname}"})
786 activities = ActivityPub.fetch_activities([], %{blocking_user: user})
788 assert Enum.member?(activities, activity)
791 test "doesn't return transitive interactions concerning blocked users" do
792 blocker = insert(:user)
793 blockee = insert(:user)
794 friend = insert(:user)
796 {:ok, _user_relationship} = User.block(blocker, blockee)
798 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
800 {:ok, activity_two} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
802 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
804 {:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
806 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker})
808 assert Enum.member?(activities, activity_one)
809 refute Enum.member?(activities, activity_two)
810 refute Enum.member?(activities, activity_three)
811 refute Enum.member?(activities, activity_four)
814 test "doesn't return announce activities with blocked users in 'to'" do
815 blocker = insert(:user)
816 blockee = insert(:user)
817 friend = insert(:user)
819 {:ok, _user_relationship} = User.block(blocker, blockee)
821 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
823 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
825 {:ok, activity_three} = CommonAPI.repeat(activity_two.id, friend)
828 ActivityPub.fetch_activities([], %{blocking_user: blocker})
829 |> Enum.map(fn act -> act.id end)
831 assert Enum.member?(activities, activity_one.id)
832 refute Enum.member?(activities, activity_two.id)
833 refute Enum.member?(activities, activity_three.id)
836 test "doesn't return announce activities with blocked users in 'cc'" do
837 blocker = insert(:user)
838 blockee = insert(:user)
839 friend = insert(:user)
841 {:ok, _user_relationship} = User.block(blocker, blockee)
843 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
845 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
847 assert object = Pleroma.Object.normalize(activity_two, fetch: false)
850 "actor" => friend.ap_id,
851 "object" => object.data["id"],
852 "context" => object.data["context"],
853 "type" => "Announce",
854 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
855 "cc" => [blockee.ap_id]
858 assert {:ok, activity_three} = ActivityPub.insert(data)
861 ActivityPub.fetch_activities([], %{blocking_user: blocker})
862 |> Enum.map(fn act -> act.id end)
864 assert Enum.member?(activities, activity_one.id)
865 refute Enum.member?(activities, activity_two.id)
866 refute Enum.member?(activities, activity_three.id)
869 test "doesn't return activities from blocked domains" do
870 domain = "dogwhistle.zone"
871 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
872 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
873 activity = insert(:note_activity, %{note: note})
875 {:ok, user} = User.block_domain(user, domain)
877 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
879 refute activity in activities
881 followed_user = insert(:user)
882 CommonAPI.follow(user, followed_user)
883 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user)
885 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
887 refute repeat_activity in activities
890 test "see your own posts even when they adress actors from blocked domains" do
893 domain = "dogwhistle.zone"
894 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
896 {:ok, user} = User.block_domain(user, domain)
898 {:ok, activity} = CommonAPI.post(user, %{status: "hey! @#{domain_user.nickname}"})
900 activities = ActivityPub.fetch_activities([], %{blocking_user: user})
902 assert Enum.member?(activities, activity)
905 test "does return activities from followed users on blocked domains" do
906 domain = "meanies.social"
907 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
908 blocker = insert(:user)
910 {:ok, blocker, domain_user} = User.follow(blocker, domain_user)
911 {:ok, blocker} = User.block_domain(blocker, domain)
913 assert User.following?(blocker, domain_user)
914 assert User.blocks_domain?(blocker, domain_user)
915 refute User.blocks?(blocker, domain_user)
917 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
918 activity = insert(:note_activity, %{note: note})
920 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
922 assert activity in activities
924 # And check that if the guy we DO follow boosts someone else from their domain,
925 # that should be hidden
926 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
927 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
928 bad_activity = insert(:note_activity, %{note: bad_note})
929 {:ok, repeat_activity} = CommonAPI.repeat(bad_activity.id, domain_user)
931 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
933 refute repeat_activity in activities
936 test "returns your own posts regardless of mute" do
938 muted = insert(:user)
940 {:ok, muted_post} = CommonAPI.post(muted, %{status: "Im stupid"})
943 CommonAPI.post(user, %{status: "I'm muting you", in_reply_to_status_id: muted_post.id})
945 {:ok, _} = User.mute(user, muted)
947 [activity] = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
949 assert activity.id == reply.id
952 test "doesn't return muted activities" do
953 activity_one = insert(:note_activity)
954 activity_two = insert(:note_activity)
955 activity_three = insert(:note_activity)
957 booster = insert(:user)
959 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
960 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
962 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
964 assert Enum.member?(activities, activity_two)
965 assert Enum.member?(activities, activity_three)
966 refute Enum.member?(activities, activity_one)
968 # Calling with 'with_muted' will deliver muted activities, too.
970 ActivityPub.fetch_activities([], %{
976 assert Enum.member?(activities, activity_two)
977 assert Enum.member?(activities, activity_three)
978 assert Enum.member?(activities, activity_one)
980 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
982 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
984 assert Enum.member?(activities, activity_two)
985 assert Enum.member?(activities, activity_three)
986 assert Enum.member?(activities, activity_one)
988 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
989 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
990 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
991 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
992 activity_three = Activity.get_by_id(activity_three.id)
994 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
996 assert Enum.member?(activities, activity_two)
997 refute Enum.member?(activities, activity_three)
998 refute Enum.member?(activities, boost_activity)
999 assert Enum.member?(activities, activity_one)
1001 activities = ActivityPub.fetch_activities([], %{muting_user: nil, skip_preload: true})
1003 assert Enum.member?(activities, activity_two)
1004 assert Enum.member?(activities, activity_three)
1005 assert Enum.member?(activities, boost_activity)
1006 assert Enum.member?(activities, activity_one)
1009 test "doesn't return thread muted activities" do
1010 user = insert(:user)
1011 _activity_one = insert(:note_activity)
1012 note_two = insert(:note, data: %{"context" => "suya.."})
1013 activity_two = insert(:note_activity, note: note_two)
1015 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
1017 assert [_activity_one] = ActivityPub.fetch_activities([], %{muting_user: user})
1020 test "returns thread muted activities when with_muted is set" do
1021 user = insert(:user)
1022 _activity_one = insert(:note_activity)
1023 note_two = insert(:note, data: %{"context" => "suya.."})
1024 activity_two = insert(:note_activity, note: note_two)
1026 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
1028 assert [_activity_two, _activity_one] =
1029 ActivityPub.fetch_activities([], %{muting_user: user, with_muted: true})
1032 test "does include announces on request" do
1033 activity_three = insert(:note_activity)
1034 user = insert(:user)
1035 booster = insert(:user)
1037 {:ok, user, booster} = User.follow(user, booster)
1039 {:ok, announce} = CommonAPI.repeat(activity_three.id, booster)
1041 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
1043 assert announce_activity.id == announce.id
1046 test "excludes reblogs on request" do
1047 user = insert(:user)
1048 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
1049 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
1051 [activity] = ActivityPub.fetch_user_activities(user, nil, %{exclude_reblogs: true})
1053 assert activity == expected_activity
1056 describe "irreversible filters" do
1058 user = insert(:user)
1059 user_two = insert(:user)
1061 insert(:filter, user: user_two, phrase: "cofe", hide: true)
1062 insert(:filter, user: user_two, phrase: "ok boomer", hide: true)
1063 insert(:filter, user: user_two, phrase: "test", hide: false)
1066 type: ["Create", "Announce"],
1070 {:ok, %{user: user, user_two: user_two, params: params}}
1073 test "it returns statuses if they don't contain exact filter words", %{
1077 {:ok, _} = CommonAPI.post(user, %{status: "hey"})
1078 {:ok, _} = CommonAPI.post(user, %{status: "got cofefe?"})
1079 {:ok, _} = CommonAPI.post(user, %{status: "I am not a boomer"})
1080 {:ok, _} = CommonAPI.post(user, %{status: "ok boomers"})
1081 {:ok, _} = CommonAPI.post(user, %{status: "ccofee is not a word"})
1082 {:ok, _} = CommonAPI.post(user, %{status: "this is a test"})
1084 activities = ActivityPub.fetch_activities([], params)
1086 assert Enum.count(activities) == 6
1089 test "it does not filter user's own statuses", %{user_two: user_two, params: params} do
1090 {:ok, _} = CommonAPI.post(user_two, %{status: "Give me some cofe!"})
1091 {:ok, _} = CommonAPI.post(user_two, %{status: "ok boomer"})
1093 activities = ActivityPub.fetch_activities([], params)
1095 assert Enum.count(activities) == 2
1098 test "it excludes statuses with filter words", %{user: user, params: params} do
1099 {:ok, _} = CommonAPI.post(user, %{status: "Give me some cofe!"})
1100 {:ok, _} = CommonAPI.post(user, %{status: "ok boomer"})
1101 {:ok, _} = CommonAPI.post(user, %{status: "is it a cOfE?"})
1102 {:ok, _} = CommonAPI.post(user, %{status: "cofe is all I need"})
1103 {:ok, _} = CommonAPI.post(user, %{status: "— ok BOOMER\n"})
1105 activities = ActivityPub.fetch_activities([], params)
1107 assert Enum.empty?(activities)
1110 test "it returns all statuses if user does not have any filters" do
1111 another_user = insert(:user)
1112 {:ok, _} = CommonAPI.post(another_user, %{status: "got cofe?"})
1113 {:ok, _} = CommonAPI.post(another_user, %{status: "test!"})
1116 ActivityPub.fetch_activities([], %{
1117 type: ["Create", "Announce"],
1121 assert Enum.count(activities) == 2
1125 describe "public fetch activities" do
1126 test "doesn't retrieve unlisted activities" do
1127 user = insert(:user)
1129 {:ok, _unlisted_activity} = CommonAPI.post(user, %{status: "yeah", visibility: "unlisted"})
1131 {:ok, listed_activity} = CommonAPI.post(user, %{status: "yeah"})
1133 [activity] = ActivityPub.fetch_public_activities()
1135 assert activity == listed_activity
1138 test "retrieves public activities" do
1139 _activities = ActivityPub.fetch_public_activities()
1141 %{public: public} = ActivityBuilder.public_and_non_public()
1143 activities = ActivityPub.fetch_public_activities()
1144 assert length(activities) == 1
1145 assert Enum.at(activities, 0) == public
1148 test "retrieves a maximum of 20 activities" do
1149 ActivityBuilder.insert_list(10)
1150 expected_activities = ActivityBuilder.insert_list(20)
1152 activities = ActivityPub.fetch_public_activities()
1154 assert collect_ids(activities) == collect_ids(expected_activities)
1155 assert length(activities) == 20
1158 test "retrieves ids starting from a since_id" do
1159 activities = ActivityBuilder.insert_list(30)
1160 expected_activities = ActivityBuilder.insert_list(10)
1161 since_id = List.last(activities).id
1163 activities = ActivityPub.fetch_public_activities(%{since_id: since_id})
1165 assert collect_ids(activities) == collect_ids(expected_activities)
1166 assert length(activities) == 10
1169 test "retrieves ids up to max_id" do
1170 ActivityBuilder.insert_list(10)
1171 expected_activities = ActivityBuilder.insert_list(20)
1175 |> ActivityBuilder.insert_list()
1178 activities = ActivityPub.fetch_public_activities(%{max_id: max_id})
1180 assert length(activities) == 20
1181 assert collect_ids(activities) == collect_ids(expected_activities)
1184 test "paginates via offset/limit" do
1185 _first_part_activities = ActivityBuilder.insert_list(10)
1186 second_part_activities = ActivityBuilder.insert_list(10)
1188 later_activities = ActivityBuilder.insert_list(10)
1190 activities = ActivityPub.fetch_public_activities(%{page: "2", page_size: "20"}, :offset)
1192 assert length(activities) == 20
1194 assert collect_ids(activities) ==
1195 collect_ids(second_part_activities) ++ collect_ids(later_activities)
1198 test "doesn't return reblogs for users for whom reblogs have been muted" do
1199 activity = insert(:note_activity)
1200 user = insert(:user)
1201 booster = insert(:user)
1202 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
1204 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
1206 activities = ActivityPub.fetch_activities([], %{muting_user: user})
1208 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
1211 test "returns reblogs for users for whom reblogs have not been muted" do
1212 activity = insert(:note_activity)
1213 user = insert(:user)
1214 booster = insert(:user)
1215 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
1216 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
1218 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
1220 activities = ActivityPub.fetch_activities([], %{muting_user: user})
1222 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
1226 describe "uploading files" do
1228 test_file = %Plug.Upload{
1229 content_type: "image/jpeg",
1230 path: Path.absname("test/fixtures/image.jpg"),
1231 filename: "an_image.jpg"
1234 %{test_file: test_file}
1237 test "sets a description if given", %{test_file: file} do
1238 {:ok, %Object{} = object} = ActivityPub.upload(file, description: "a cool file")
1239 assert object.data["name"] == "a cool file"
1242 test "it sets the default description depending on the configuration", %{test_file: file} do
1243 clear_config([Pleroma.Upload, :default_description])
1245 clear_config([Pleroma.Upload, :default_description], nil)
1246 {:ok, %Object{} = object} = ActivityPub.upload(file)
1247 assert object.data["name"] == ""
1249 clear_config([Pleroma.Upload, :default_description], :filename)
1250 {:ok, %Object{} = object} = ActivityPub.upload(file)
1251 assert object.data["name"] == "an_image.jpg"
1253 clear_config([Pleroma.Upload, :default_description], "unnamed attachment")
1254 {:ok, %Object{} = object} = ActivityPub.upload(file)
1255 assert object.data["name"] == "unnamed attachment"
1258 test "copies the file to the configured folder", %{test_file: file} do
1259 clear_config([Pleroma.Upload, :default_description], :filename)
1260 {:ok, %Object{} = object} = ActivityPub.upload(file)
1261 assert object.data["name"] == "an_image.jpg"
1264 test "works with base64 encoded images" do
1269 {:ok, %Object{}} = ActivityPub.upload(file)
1273 describe "fetch the latest Follow" do
1274 test "fetches the latest Follow activity" do
1275 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1276 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1277 followed = Repo.get_by(User, ap_id: activity.data["object"])
1279 assert activity == Utils.fetch_latest_follow(follower, followed)
1283 describe "unfollowing" do
1284 test "it reverts unfollow activity" do
1285 follower = insert(:user)
1286 followed = insert(:user)
1288 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1290 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1291 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
1294 activity = Activity.get_by_id(follow_activity.id)
1295 assert activity.data["type"] == "Follow"
1296 assert activity.data["actor"] == follower.ap_id
1298 assert activity.data["object"] == followed.ap_id
1301 test "creates an undo activity for the last follow" do
1302 follower = insert(:user)
1303 followed = insert(:user)
1305 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1306 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1308 assert activity.data["type"] == "Undo"
1309 assert activity.data["actor"] == follower.ap_id
1311 embedded_object = activity.data["object"]
1312 assert is_map(embedded_object)
1313 assert embedded_object["type"] == "Follow"
1314 assert embedded_object["object"] == followed.ap_id
1315 assert embedded_object["id"] == follow_activity.data["id"]
1318 test "creates an undo activity for a pending follow request" do
1319 follower = insert(:user)
1320 followed = insert(:user, %{is_locked: true})
1322 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1323 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1325 assert activity.data["type"] == "Undo"
1326 assert activity.data["actor"] == follower.ap_id
1328 embedded_object = activity.data["object"]
1329 assert is_map(embedded_object)
1330 assert embedded_object["type"] == "Follow"
1331 assert embedded_object["object"] == followed.ap_id
1332 assert embedded_object["id"] == follow_activity.data["id"]
1336 describe "timeline post-processing" do
1337 test "it filters broken threads" do
1338 user1 = insert(:user)
1339 user2 = insert(:user)
1340 user3 = insert(:user)
1342 {:ok, user1, user3} = User.follow(user1, user3)
1343 assert User.following?(user1, user3)
1345 {:ok, user2, user3} = User.follow(user2, user3)
1346 assert User.following?(user2, user3)
1348 {:ok, user3, user2} = User.follow(user3, user2)
1349 assert User.following?(user3, user2)
1351 {:ok, public_activity} = CommonAPI.post(user3, %{status: "hi 1"})
1353 {:ok, private_activity_1} = CommonAPI.post(user3, %{status: "hi 2", visibility: "private"})
1355 {:ok, private_activity_2} =
1356 CommonAPI.post(user2, %{
1358 visibility: "private",
1359 in_reply_to_status_id: private_activity_1.id
1362 {:ok, private_activity_3} =
1363 CommonAPI.post(user3, %{
1365 visibility: "private",
1366 in_reply_to_status_id: private_activity_2.id
1370 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1371 |> Enum.map(fn a -> a.id end)
1373 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1375 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1377 assert length(activities) == 3
1380 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{user: user1})
1381 |> Enum.map(fn a -> a.id end)
1383 assert [public_activity.id, private_activity_1.id] == activities
1384 assert length(activities) == 2
1388 describe "flag/1" do
1390 reporter = insert(:user)
1391 target_account = insert(:user)
1393 {:ok, activity} = CommonAPI.post(target_account, %{status: content})
1394 context = Utils.generate_context_id()
1396 reporter_ap_id = reporter.ap_id
1397 target_ap_id = target_account.ap_id
1398 activity_ap_id = activity.data["id"]
1400 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1406 target_account: target_account,
1407 reported_activity: activity,
1409 activity_ap_id: activity_ap_id,
1410 activity_with_object: activity_with_object,
1411 reporter_ap_id: reporter_ap_id,
1412 target_ap_id: target_ap_id
1416 test "it can create a Flag activity",
1420 target_account: target_account,
1421 reported_activity: reported_activity,
1423 activity_ap_id: activity_ap_id,
1424 activity_with_object: activity_with_object,
1425 reporter_ap_id: reporter_ap_id,
1426 target_ap_id: target_ap_id
1428 assert {:ok, activity} =
1432 account: target_account,
1433 statuses: [reported_activity],
1439 "id" => activity_ap_id,
1440 "content" => content,
1441 "published" => activity_with_object.object.data["published"],
1443 AccountView.render("show.json", %{user: target_account, skip_visibility_check: true})
1447 actor: ^reporter_ap_id,
1450 "content" => ^content,
1451 "context" => ^context,
1452 "object" => [^target_ap_id, ^note_obj]
1457 test_with_mock "strips status data from Flag, before federating it",
1461 target_account: target_account,
1462 reported_activity: reported_activity,
1472 account: target_account,
1473 statuses: [reported_activity],
1478 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1480 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1483 test_with_mock "reverts on error",
1487 target_account: target_account,
1488 reported_activity: reported_activity,
1493 maybe_federate: fn _ -> {:error, :reverted} end do
1494 assert {:error, :reverted} =
1498 account: target_account,
1499 statuses: [reported_activity],
1503 assert Repo.aggregate(Activity, :count, :id) == 1
1504 assert Repo.aggregate(Object, :count, :id) == 2
1505 assert Repo.aggregate(Notification, :count, :id) == 0
1509 test "fetch_activities/2 returns activities addressed to a list " do
1510 user = insert(:user)
1511 member = insert(:user)
1512 {:ok, list} = Pleroma.List.create("foo", user)
1513 {:ok, list} = Pleroma.List.follow(list, member)
1515 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1517 activity = Repo.preload(activity, :bookmark)
1518 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1520 assert ActivityPub.fetch_activities([], %{user: user}) == [activity]
1524 File.read!("test/fixtures/avatar_data_uri")
1527 describe "fetch_activities_bounded" do
1528 test "fetches private posts for followed users" do
1529 user = insert(:user)
1532 CommonAPI.post(user, %{
1533 status: "thought I looked cute might delete later :3",
1534 visibility: "private"
1537 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1538 assert result.id == activity.id
1541 test "fetches only public posts for other users" do
1542 user = insert(:user)
1543 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe", visibility: "public"})
1545 {:ok, _private_activity} =
1546 CommonAPI.post(user, %{
1547 status: "why is tenshi eating a corndog so cute?",
1548 visibility: "private"
1551 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1552 assert result.id == activity.id
1556 describe "fetch_follow_information_for_user" do
1557 test "syncronizes following/followers counters" do
1561 follower_address: "http://localhost:4001/users/fuser2/followers",
1562 following_address: "http://localhost:4001/users/fuser2/following"
1565 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1566 assert info.follower_count == 527
1567 assert info.following_count == 267
1570 test "detects hidden followers" do
1573 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1574 %Tesla.Env{status: 403, body: ""}
1577 apply(HttpRequestMock, :request, [env])
1584 follower_address: "http://localhost:4001/users/masto_closed/followers",
1585 following_address: "http://localhost:4001/users/masto_closed/following"
1588 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1589 assert follow_info.hide_followers == true
1590 assert follow_info.hide_follows == false
1593 test "detects hidden follows" do
1596 "http://localhost:4001/users/masto_closed/following?page=1" ->
1597 %Tesla.Env{status: 403, body: ""}
1600 apply(HttpRequestMock, :request, [env])
1607 follower_address: "http://localhost:4001/users/masto_closed/followers",
1608 following_address: "http://localhost:4001/users/masto_closed/following"
1611 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1612 assert follow_info.hide_followers == false
1613 assert follow_info.hide_follows == true
1616 test "detects hidden follows/followers for friendica" do
1620 follower_address: "http://localhost:8080/followers/fuser3",
1621 following_address: "http://localhost:8080/following/fuser3"
1624 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1625 assert follow_info.hide_followers == true
1626 assert follow_info.follower_count == 296
1627 assert follow_info.following_count == 32
1628 assert follow_info.hide_follows == true
1631 test "doesn't crash when follower and following counters are hidden" do
1634 "http://localhost:4001/users/masto_hidden_counters/following" ->
1637 "@context" => "https://www.w3.org/ns/activitystreams",
1638 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1640 headers: HttpRequestMock.activitypub_object_headers()
1643 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1644 %Tesla.Env{status: 403, body: ""}
1646 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1649 "@context" => "https://www.w3.org/ns/activitystreams",
1650 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1652 headers: HttpRequestMock.activitypub_object_headers()
1655 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1656 %Tesla.Env{status: 403, body: ""}
1663 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1664 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1667 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1669 assert follow_info.hide_followers == true
1670 assert follow_info.follower_count == 0
1671 assert follow_info.hide_follows == true
1672 assert follow_info.following_count == 0
1676 describe "fetch_favourites/3" do
1677 test "returns a favourite activities sorted by adds to favorite" do
1678 user = insert(:user)
1679 other_user = insert(:user)
1680 user1 = insert(:user)
1681 user2 = insert(:user)
1682 {:ok, a1} = CommonAPI.post(user1, %{status: "bla"})
1683 {:ok, _a2} = CommonAPI.post(user2, %{status: "traps are happy"})
1684 {:ok, a3} = CommonAPI.post(user2, %{status: "Trees Are "})
1685 {:ok, a4} = CommonAPI.post(user2, %{status: "Agent Smith "})
1686 {:ok, a5} = CommonAPI.post(user1, %{status: "Red or Blue "})
1688 {:ok, _} = CommonAPI.favorite(user, a4.id)
1689 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1690 {:ok, _} = CommonAPI.favorite(user, a3.id)
1691 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1692 {:ok, _} = CommonAPI.favorite(user, a5.id)
1693 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1694 {:ok, _} = CommonAPI.favorite(user, a1.id)
1695 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1696 result = ActivityPub.fetch_favourites(user)
1698 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1700 result = ActivityPub.fetch_favourites(user, %{limit: 2})
1701 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1705 describe "Move activity" do
1707 %{ap_id: old_ap_id} = old_user = insert(:user)
1708 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1709 follower = insert(:user)
1710 follower_move_opted_out = insert(:user, allow_following_move: false)
1712 User.follow(follower, old_user)
1713 User.follow(follower_move_opted_out, old_user)
1715 assert User.following?(follower, old_user)
1716 assert User.following?(follower_move_opted_out, old_user)
1718 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1723 "actor" => ^old_ap_id,
1724 "object" => ^old_ap_id,
1725 "target" => ^new_ap_id,
1732 "op" => "move_following",
1733 "origin_id" => old_user.id,
1734 "target_id" => new_user.id
1737 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1739 Pleroma.Workers.BackgroundWorker.perform(%Oban.Job{args: params})
1741 refute User.following?(follower, old_user)
1742 assert User.following?(follower, new_user)
1744 assert User.following?(follower_move_opted_out, old_user)
1745 refute User.following?(follower_move_opted_out, new_user)
1747 activity = %Activity{activity | object: nil}
1749 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1751 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1754 test "old user must be in the new user's `also_known_as` list" do
1755 old_user = insert(:user)
1756 new_user = insert(:user)
1758 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1759 ActivityPub.move(old_user, new_user)
1763 test "doesn't retrieve replies activities with exclude_replies" do
1764 user = insert(:user)
1766 {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
1768 {:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id})
1770 [result] = ActivityPub.fetch_public_activities(%{exclude_replies: true})
1772 assert result.id == activity.id
1774 assert length(ActivityPub.fetch_public_activities()) == 2
1777 describe "replies filtering with public messages" do
1778 setup :public_messages
1780 test "public timeline", %{users: %{u1: user}} do
1783 |> Map.put(:type, ["Create", "Announce"])
1784 |> Map.put(:local_only, false)
1785 |> Map.put(:blocking_user, user)
1786 |> Map.put(:muting_user, user)
1787 |> Map.put(:reply_filtering_user, user)
1788 |> ActivityPub.fetch_public_activities()
1789 |> Enum.map(& &1.id)
1791 assert length(activities_ids) == 16
1794 test "public timeline with reply_visibility `following`", %{
1800 activities: activities
1804 |> Map.put(:type, ["Create", "Announce"])
1805 |> Map.put(:local_only, false)
1806 |> Map.put(:blocking_user, user)
1807 |> Map.put(:muting_user, user)
1808 |> Map.put(:reply_visibility, "following")
1809 |> Map.put(:reply_filtering_user, user)
1810 |> ActivityPub.fetch_public_activities()
1811 |> Enum.map(& &1.id)
1813 assert length(activities_ids) == 14
1816 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1818 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1821 test "public timeline with reply_visibility `self`", %{
1827 activities: activities
1831 |> Map.put(:type, ["Create", "Announce"])
1832 |> Map.put(:local_only, false)
1833 |> Map.put(:blocking_user, user)
1834 |> Map.put(:muting_user, user)
1835 |> Map.put(:reply_visibility, "self")
1836 |> Map.put(:reply_filtering_user, user)
1837 |> ActivityPub.fetch_public_activities()
1838 |> Enum.map(& &1.id)
1840 assert length(activities_ids) == 10
1841 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1842 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1845 test "home timeline", %{
1847 activities: activities,
1855 |> Map.put(:type, ["Create", "Announce"])
1856 |> Map.put(:blocking_user, user)
1857 |> Map.put(:muting_user, user)
1858 |> Map.put(:user, user)
1859 |> Map.put(:reply_filtering_user, user)
1862 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1863 |> Enum.map(& &1.id)
1865 assert length(activities_ids) == 13
1880 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1883 test "home timeline with reply_visibility `following`", %{
1885 activities: activities,
1893 |> Map.put(:type, ["Create", "Announce"])
1894 |> Map.put(:blocking_user, user)
1895 |> Map.put(:muting_user, user)
1896 |> Map.put(:user, user)
1897 |> Map.put(:reply_visibility, "following")
1898 |> Map.put(:reply_filtering_user, user)
1901 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1902 |> Enum.map(& &1.id)
1904 assert length(activities_ids) == 11
1919 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1922 test "home timeline with reply_visibility `self`", %{
1924 activities: activities,
1932 |> Map.put(:type, ["Create", "Announce"])
1933 |> Map.put(:blocking_user, user)
1934 |> Map.put(:muting_user, user)
1935 |> Map.put(:user, user)
1936 |> Map.put(:reply_visibility, "self")
1937 |> Map.put(:reply_filtering_user, user)
1940 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1941 |> Enum.map(& &1.id)
1943 assert length(activities_ids) == 9
1956 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1959 test "filtering out announces where the user is the actor of the announced message" do
1960 user = insert(:user)
1961 other_user = insert(:user)
1962 third_user = insert(:user)
1963 User.follow(user, other_user)
1965 {:ok, post} = CommonAPI.post(user, %{status: "yo"})
1966 {:ok, other_post} = CommonAPI.post(third_user, %{status: "yo"})
1967 {:ok, _announce} = CommonAPI.repeat(post.id, other_user)
1968 {:ok, _announce} = CommonAPI.repeat(post.id, third_user)
1969 {:ok, announce} = CommonAPI.repeat(other_post.id, other_user)
1976 [user.ap_id | User.following(user)]
1977 |> ActivityPub.fetch_activities(params)
1979 assert length(results) == 3
1983 announce_filtering_user: user
1987 [user.ap_id | User.following(user)]
1988 |> ActivityPub.fetch_activities(params)
1990 assert result.id == announce.id
1994 describe "replies filtering with private messages" do
1995 setup :private_messages
1997 test "public timeline", %{users: %{u1: user}} do
2000 |> Map.put(:type, ["Create", "Announce"])
2001 |> Map.put(:local_only, false)
2002 |> Map.put(:blocking_user, user)
2003 |> Map.put(:muting_user, user)
2004 |> Map.put(:user, user)
2005 |> ActivityPub.fetch_public_activities()
2006 |> Enum.map(& &1.id)
2008 assert activities_ids == []
2011 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
2014 |> Map.put(:type, ["Create", "Announce"])
2015 |> Map.put(:local_only, false)
2016 |> Map.put(:blocking_user, user)
2017 |> Map.put(:muting_user, user)
2018 |> Map.put(:reply_visibility, "following")
2019 |> Map.put(:reply_filtering_user, user)
2020 |> Map.put(:user, user)
2021 |> ActivityPub.fetch_public_activities()
2022 |> Enum.map(& &1.id)
2024 assert activities_ids == []
2027 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
2030 |> Map.put(:type, ["Create", "Announce"])
2031 |> Map.put(:local_only, false)
2032 |> Map.put(:blocking_user, user)
2033 |> Map.put(:muting_user, user)
2034 |> Map.put(:reply_visibility, "self")
2035 |> Map.put(:reply_filtering_user, user)
2036 |> Map.put(:user, user)
2037 |> ActivityPub.fetch_public_activities()
2038 |> Enum.map(& &1.id)
2040 assert activities_ids == []
2044 |> Map.put(:reply_visibility, "self")
2045 |> Map.put(:reply_filtering_user, nil)
2046 |> ActivityPub.fetch_public_activities()
2048 assert activities_ids == []
2051 test "home timeline", %{users: %{u1: user}} do
2054 |> Map.put(:type, ["Create", "Announce"])
2055 |> Map.put(:blocking_user, user)
2056 |> Map.put(:muting_user, user)
2057 |> Map.put(:user, user)
2060 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2061 |> Enum.map(& &1.id)
2063 assert length(activities_ids) == 12
2066 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
2069 |> Map.put(:type, ["Create", "Announce"])
2070 |> Map.put(:blocking_user, user)
2071 |> Map.put(:muting_user, user)
2072 |> Map.put(:user, user)
2073 |> Map.put(:reply_visibility, "following")
2074 |> Map.put(:reply_filtering_user, user)
2077 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2078 |> Enum.map(& &1.id)
2080 assert length(activities_ids) == 12
2083 test "home timeline with default reply_visibility `self`", %{
2085 activities: activities,
2093 |> Map.put(:type, ["Create", "Announce"])
2094 |> Map.put(:blocking_user, user)
2095 |> Map.put(:muting_user, user)
2096 |> Map.put(:user, user)
2097 |> Map.put(:reply_visibility, "self")
2098 |> Map.put(:reply_filtering_user, user)
2101 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2102 |> Enum.map(& &1.id)
2104 assert length(activities_ids) == 10
2107 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
2109 assert Enum.all?(visible_ids, &(&1 in activities_ids))
2113 defp public_messages(_) do
2114 [u1, u2, u3, u4] = insert_list(4, :user)
2115 {:ok, u1, u2} = User.follow(u1, u2)
2116 {:ok, u2, u1} = User.follow(u2, u1)
2117 {:ok, u1, u4} = User.follow(u1, u4)
2118 {:ok, u4, u1} = User.follow(u4, u1)
2120 {:ok, u2, u3} = User.follow(u2, u3)
2121 {:ok, u3, u2} = User.follow(u3, u2)
2123 {:ok, a1} = CommonAPI.post(u1, %{status: "Status"})
2126 CommonAPI.post(u2, %{
2127 status: "@#{u1.nickname} reply from u2 to u1",
2128 in_reply_to_status_id: a1.id
2132 CommonAPI.post(u3, %{
2133 status: "@#{u1.nickname} reply from u3 to u1",
2134 in_reply_to_status_id: a1.id
2138 CommonAPI.post(u4, %{
2139 status: "@#{u1.nickname} reply from u4 to u1",
2140 in_reply_to_status_id: a1.id
2143 {:ok, a2} = CommonAPI.post(u2, %{status: "Status"})
2146 CommonAPI.post(u1, %{
2147 status: "@#{u2.nickname} reply from u1 to u2",
2148 in_reply_to_status_id: a2.id
2152 CommonAPI.post(u3, %{
2153 status: "@#{u2.nickname} reply from u3 to u2",
2154 in_reply_to_status_id: a2.id
2158 CommonAPI.post(u4, %{
2159 status: "@#{u2.nickname} reply from u4 to u2",
2160 in_reply_to_status_id: a2.id
2163 {:ok, a3} = CommonAPI.post(u3, %{status: "Status"})
2166 CommonAPI.post(u1, %{
2167 status: "@#{u3.nickname} reply from u1 to u3",
2168 in_reply_to_status_id: a3.id
2172 CommonAPI.post(u2, %{
2173 status: "@#{u3.nickname} reply from u2 to u3",
2174 in_reply_to_status_id: a3.id
2178 CommonAPI.post(u4, %{
2179 status: "@#{u3.nickname} reply from u4 to u3",
2180 in_reply_to_status_id: a3.id
2183 {:ok, a4} = CommonAPI.post(u4, %{status: "Status"})
2186 CommonAPI.post(u1, %{
2187 status: "@#{u4.nickname} reply from u1 to u4",
2188 in_reply_to_status_id: a4.id
2192 CommonAPI.post(u2, %{
2193 status: "@#{u4.nickname} reply from u2 to u4",
2194 in_reply_to_status_id: a4.id
2198 CommonAPI.post(u3, %{
2199 status: "@#{u4.nickname} reply from u3 to u4",
2200 in_reply_to_status_id: a4.id
2204 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2205 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2206 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2207 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
2208 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
2209 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
2212 defp private_messages(_) do
2213 [u1, u2, u3, u4] = insert_list(4, :user)
2214 {:ok, u1, u2} = User.follow(u1, u2)
2215 {:ok, u2, u1} = User.follow(u2, u1)
2216 {:ok, u1, u3} = User.follow(u1, u3)
2217 {:ok, u3, u1} = User.follow(u3, u1)
2218 {:ok, u1, u4} = User.follow(u1, u4)
2219 {:ok, u4, u1} = User.follow(u4, u1)
2221 {:ok, u2, u3} = User.follow(u2, u3)
2222 {:ok, u3, u2} = User.follow(u3, u2)
2224 {:ok, a1} = CommonAPI.post(u1, %{status: "Status", visibility: "private"})
2227 CommonAPI.post(u2, %{
2228 status: "@#{u1.nickname} reply from u2 to u1",
2229 in_reply_to_status_id: a1.id,
2230 visibility: "private"
2234 CommonAPI.post(u3, %{
2235 status: "@#{u1.nickname} reply from u3 to u1",
2236 in_reply_to_status_id: a1.id,
2237 visibility: "private"
2241 CommonAPI.post(u4, %{
2242 status: "@#{u1.nickname} reply from u4 to u1",
2243 in_reply_to_status_id: a1.id,
2244 visibility: "private"
2247 {:ok, a2} = CommonAPI.post(u2, %{status: "Status", visibility: "private"})
2250 CommonAPI.post(u1, %{
2251 status: "@#{u2.nickname} reply from u1 to u2",
2252 in_reply_to_status_id: a2.id,
2253 visibility: "private"
2257 CommonAPI.post(u3, %{
2258 status: "@#{u2.nickname} reply from u3 to u2",
2259 in_reply_to_status_id: a2.id,
2260 visibility: "private"
2263 {:ok, a3} = CommonAPI.post(u3, %{status: "Status", visibility: "private"})
2266 CommonAPI.post(u1, %{
2267 status: "@#{u3.nickname} reply from u1 to u3",
2268 in_reply_to_status_id: a3.id,
2269 visibility: "private"
2273 CommonAPI.post(u2, %{
2274 status: "@#{u3.nickname} reply from u2 to u3",
2275 in_reply_to_status_id: a3.id,
2276 visibility: "private"
2279 {:ok, a4} = CommonAPI.post(u4, %{status: "Status", visibility: "private"})
2282 CommonAPI.post(u1, %{
2283 status: "@#{u4.nickname} reply from u1 to u4",
2284 in_reply_to_status_id: a4.id,
2285 visibility: "private"
2289 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2290 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2291 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2292 u2: %{r1: r2_1.id, r2: r2_2.id},
2293 u3: %{r1: r3_1.id, r2: r3_2.id},
2297 describe "maybe_update_follow_information/1" do
2299 clear_config([:instance, :external_user_synchronization], true)
2303 ap_id: "https://gensokyo.2hu/users/raymoo",
2304 following_address: "https://gensokyo.2hu/users/following",
2305 follower_address: "https://gensokyo.2hu/users/followers",
2312 test "logs an error when it can't fetch the info", %{user: user} do
2313 assert capture_log(fn ->
2314 ActivityPub.maybe_update_follow_information(user)
2315 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2318 test "just returns the input if the user type is Application", %{
2323 |> Map.put(:type, "Application")
2325 refute capture_log(fn ->
2326 assert ^user = ActivityPub.maybe_update_follow_information(user)
2327 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2330 test "it just returns the input if the user has no following/follower addresses", %{
2335 |> Map.put(:following_address, nil)
2336 |> Map.put(:follower_address, nil)
2338 refute capture_log(fn ->
2339 assert ^user = ActivityPub.maybe_update_follow_information(user)
2340 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2344 describe "global activity expiration" do
2345 test "creates an activity expiration for local Create activities" do
2346 clear_config([:mrf, :policies], Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy)
2348 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
2349 {:ok, follow} = ActivityBuilder.insert(%{"type" => "Follow", "context" => "3hu"})
2352 worker: Pleroma.Workers.PurgeExpiredActivity,
2353 args: %{activity_id: activity.id},
2355 activity.inserted_at
2356 |> DateTime.from_naive!("Etc/UTC")
2357 |> Timex.shift(days: 365)
2361 worker: Pleroma.Workers.PurgeExpiredActivity,
2362 args: %{activity_id: follow.id}
2367 describe "handling of clashing nicknames" do
2368 test "renames an existing user with a clashing nickname and a different ap id" do
2373 nickname: "admin@mastodon.example.org",
2374 ap_id: "http://mastodon.example.org/users/harinezumigari"
2378 nickname: orig_user.nickname,
2379 ap_id: orig_user.ap_id <> "part_2"
2381 |> ActivityPub.maybe_handle_clashing_nickname()
2383 user = User.get_by_id(orig_user.id)
2385 assert user.nickname == "#{orig_user.id}.admin@mastodon.example.org"
2388 test "does nothing with a clashing nickname and the same ap id" do
2393 nickname: "admin@mastodon.example.org",
2394 ap_id: "http://mastodon.example.org/users/harinezumigari"
2398 nickname: orig_user.nickname,
2399 ap_id: orig_user.ap_id
2401 |> ActivityPub.maybe_handle_clashing_nickname()
2403 user = User.get_by_id(orig_user.id)
2405 assert user.nickname == orig_user.nickname
2409 describe "reply filtering" do
2410 test "`following` still contains announcements by friends" do
2411 user = insert(:user)
2412 followed = insert(:user)
2413 not_followed = insert(:user)
2415 User.follow(user, followed)
2417 {:ok, followed_post} = CommonAPI.post(followed, %{status: "Hello"})
2419 {:ok, not_followed_to_followed} =
2420 CommonAPI.post(not_followed, %{
2421 status: "Also hello",
2422 in_reply_to_status_id: followed_post.id
2425 {:ok, retoot} = CommonAPI.repeat(not_followed_to_followed.id, followed)
2429 |> Map.put(:type, ["Create", "Announce"])
2430 |> Map.put(:blocking_user, user)
2431 |> Map.put(:muting_user, user)
2432 |> Map.put(:reply_filtering_user, user)
2433 |> Map.put(:reply_visibility, "following")
2434 |> Map.put(:announce_filtering_user, user)
2435 |> Map.put(:user, user)
2438 [user.ap_id | User.following(user)]
2439 |> ActivityPub.fetch_activities(params)
2441 followed_post_id = followed_post.id
2442 retoot_id = retoot.id
2444 assert [%{id: ^followed_post_id}, %{id: ^retoot_id}] = activities
2446 assert length(activities) == 2
2449 # This test is skipped because, while this is the desired behavior,
2450 # there seems to be no good way to achieve it with the method that
2451 # we currently use for detecting to who a reply is directed.
2452 # This is a TODO and should be fixed by a later rewrite of the code
2455 test "`following` still contains self-replies by friends" do
2456 user = insert(:user)
2457 followed = insert(:user)
2458 not_followed = insert(:user)
2460 User.follow(user, followed)
2462 {:ok, followed_post} = CommonAPI.post(followed, %{status: "Hello"})
2463 {:ok, not_followed_post} = CommonAPI.post(not_followed, %{status: "Also hello"})
2465 {:ok, _followed_to_not_followed} =
2466 CommonAPI.post(followed, %{status: "sup", in_reply_to_status_id: not_followed_post.id})
2468 {:ok, _followed_self_reply} =
2469 CommonAPI.post(followed, %{status: "Also cofe", in_reply_to_status_id: followed_post.id})
2473 |> Map.put(:type, ["Create", "Announce"])
2474 |> Map.put(:blocking_user, user)
2475 |> Map.put(:muting_user, user)
2476 |> Map.put(:reply_filtering_user, user)
2477 |> Map.put(:reply_visibility, "following")
2478 |> Map.put(:announce_filtering_user, user)
2479 |> Map.put(:user, user)
2482 [user.ap_id | User.following(user)]
2483 |> ActivityPub.fetch_activities(params)
2485 assert length(activities) == 2
2489 test "allow fetching of accounts with an empty string name field" do
2491 %{method: :get, url: "https://princess.cat/users/mewmew"} ->
2492 file = File.read!("test/fixtures/mewmew_no_name.json")
2493 %Tesla.Env{status: 200, body: file, headers: HttpRequestMock.activitypub_object_headers()}
2496 {:ok, user} = ActivityPub.make_user_from_ap_id("https://princess.cat/users/mewmew")
2497 assert user.name == " "