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
11 alias Pleroma.Web.ActivityPub.Builder
13 alias Pleroma.Notification
16 alias Pleroma.Web.ActivityPub.ActivityPub
17 alias Pleroma.Web.ActivityPub.Utils
18 alias Pleroma.Web.AdminAPI.AccountView
19 alias Pleroma.Web.CommonAPI
21 import ExUnit.CaptureLog
23 import Pleroma.Factory
27 clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
28 clear_config([Pleroma.Uploaders.Local, :uploads], "uploads")
29 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
33 setup do: clear_config([:instance, :federating])
35 describe "streaming out participations" do
36 test "it streams them out" do
38 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
40 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
43 conversation.participations
44 |> Repo.preload(:user)
46 with_mock Pleroma.Web.Streamer,
47 stream: fn _, _ -> nil end do
48 ActivityPub.stream_out_participations(conversation.participations)
50 assert called(Pleroma.Web.Streamer.stream("participation", participations))
54 test "streams them out on activity creation" do
55 user_one = insert(:user)
56 user_two = insert(:user)
58 with_mock Pleroma.Web.Streamer,
59 stream: fn _, _ -> nil end do
61 CommonAPI.post(user_one, %{
62 status: "@#{user_two.nickname}",
67 activity.data["context"]
68 |> Pleroma.Conversation.get_for_ap_id()
69 |> Repo.preload(participations: :user)
71 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
76 describe "fetching restricted by visibility" do
77 test "it restricts by the appropriate visibility" do
80 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
82 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
84 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
86 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
88 activities = ActivityPub.fetch_activities([], %{visibility: "direct", actor_id: user.ap_id})
90 assert activities == [direct_activity]
93 ActivityPub.fetch_activities([], %{visibility: "unlisted", actor_id: user.ap_id})
95 assert activities == [unlisted_activity]
98 ActivityPub.fetch_activities([], %{visibility: "private", actor_id: user.ap_id})
100 assert activities == [private_activity]
102 activities = ActivityPub.fetch_activities([], %{visibility: "public", actor_id: user.ap_id})
104 assert activities == [public_activity]
107 ActivityPub.fetch_activities([], %{
108 visibility: ~w[private public],
112 assert activities == [public_activity, private_activity]
116 describe "fetching excluded by visibility" do
117 test "it excludes by the appropriate visibility" do
120 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
122 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
124 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
126 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
129 ActivityPub.fetch_activities([], %{
130 exclude_visibilities: "direct",
134 assert public_activity in activities
135 assert unlisted_activity in activities
136 assert private_activity in activities
137 refute direct_activity in activities
140 ActivityPub.fetch_activities([], %{
141 exclude_visibilities: "unlisted",
145 assert public_activity in activities
146 refute unlisted_activity in activities
147 assert private_activity in activities
148 assert direct_activity in activities
151 ActivityPub.fetch_activities([], %{
152 exclude_visibilities: "private",
156 assert public_activity in activities
157 assert unlisted_activity in activities
158 refute private_activity in activities
159 assert direct_activity in activities
162 ActivityPub.fetch_activities([], %{
163 exclude_visibilities: "public",
167 refute public_activity in activities
168 assert unlisted_activity in activities
169 assert private_activity in activities
170 assert direct_activity in activities
174 describe "building a user from his ap id" do
175 test "it returns a user" do
176 user_id = "http://mastodon.example.org/users/admin"
177 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
178 assert user.ap_id == user_id
179 assert user.nickname == "admin@mastodon.example.org"
180 assert user.ap_enabled
181 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
184 test "it returns a user that is invisible" do
185 user_id = "http://mastodon.example.org/users/relay"
186 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
187 assert User.invisible?(user)
190 test "works for guppe actors" do
191 user_id = "https://gup.pe/u/bernie2020"
194 %{method: :get, url: ^user_id} ->
197 body: File.read!("test/fixtures/guppe-actor.json"),
198 headers: [{"content-type", "application/activity+json"}]
202 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
204 assert user.name == "Bernie2020 group"
205 assert user.actor_type == "Group"
208 test "works for bridgy actors" do
209 user_id = "https://fed.brid.gy/jk.nipponalba.scot"
212 %{method: :get, url: ^user_id} ->
215 body: File.read!("test/fixtures/bridgy/actor.json"),
216 headers: [{"content-type", "application/activity+json"}]
220 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
222 assert user.actor_type == "Person"
224 assert user.avatar == %{
226 "url" => [%{"href" => "https://jk.nipponalba.scot/images/profile.jpg"}]
229 assert user.banner == %{
231 "url" => [%{"href" => "https://jk.nipponalba.scot/images/profile.jpg"}]
235 test "fetches user featured collection" do
236 ap_id = "https://example.com/users/lain"
238 featured_url = "https://example.com/users/lain/collections/featured"
241 "test/fixtures/users_mock/user.json"
243 |> String.replace("{{nickname}}", "lain")
245 |> Map.put("featured", featured_url)
248 object_id = Ecto.UUID.generate()
251 "test/fixtures/mastodon/collections/featured.json"
253 |> String.replace("{{domain}}", "example.com")
254 |> String.replace("{{nickname}}", "lain")
255 |> String.replace("{{object_id}}", object_id)
257 object_url = "https://example.com/objects/#{object_id}"
260 "test/fixtures/statuses/note.json"
262 |> String.replace("{{object_id}}", object_id)
263 |> String.replace("{{nickname}}", "lain")
273 headers: [{"content-type", "application/activity+json"}]
283 headers: [{"content-type", "application/activity+json"}]
287 Tesla.Mock.mock_global(fn
295 headers: [{"content-type", "application/activity+json"}]
299 {:ok, user} = ActivityPub.make_user_from_ap_id(ap_id)
302 assert user.featured_address == featured_url
303 assert Map.has_key?(user.pinned_objects, object_url)
305 in_db = Pleroma.User.get_by_ap_id(ap_id)
306 assert in_db.featured_address == featured_url
307 assert Map.has_key?(user.pinned_objects, object_url)
309 assert %{data: %{"id" => ^object_url}} = Object.get_by_ap_id(object_url)
313 test "fetches user featured collection using the first property" do
314 featured_url = "https://friendica.example.com/raha/collections/featured"
315 first_url = "https://friendica.example.com/featured/raha?page=1"
318 "test/fixtures/friendica/friendica_featured_collection.json"
322 "test/fixtures/friendica/friendica_featured_collection_first.json"
333 headers: [{"content-type", "application/activity+json"}]
343 headers: [{"content-type", "application/activity+json"}]
347 {:ok, data} = ActivityPub.fetch_and_prepare_featured_from_ap_id(featured_url)
348 assert Map.has_key?(data, "http://inserted")
351 test "fetches user featured when it has string IDs" do
352 featured_url = "https://example.com/alisaie/collections/featured"
353 dead_url = "https://example.com/users/alisaie/statuses/108311386746229284"
356 "test/fixtures/mastodon/featured_collection.json"
367 headers: [{"content-type", "application/activity+json"}]
377 headers: [{"content-type", "application/activity+json"}]
381 {:ok, %{}} = ActivityPub.fetch_and_prepare_featured_from_ap_id(featured_url)
384 test "it fetches the appropriate tag-restricted posts" do
387 {:ok, status_one} = CommonAPI.post(user, %{status: ". #TEST"})
388 {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
389 {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #Reject"})
391 {:ok, status_four} = CommonAPI.post(user, %{status: ". #Any1 #any2"})
392 {:ok, status_five} = CommonAPI.post(user, %{status: ". #Any2 #any1"})
394 for hashtag_timeline_strategy <- [:enabled, :disabled] do
395 clear_config([:features, :improved_hashtag_timeline], hashtag_timeline_strategy)
397 fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"})
399 fetch_two = ActivityPub.fetch_activities([], %{type: "Create", tag: ["TEST", "essais"]})
402 ActivityPub.fetch_activities([], %{
404 tag: ["test", "Essais"],
405 tag_reject: ["reject"]
409 ActivityPub.fetch_activities([], %{
412 tag_all: ["test", "REJECT"]
415 # Testing that deduplication (if needed) is done on DB (not Ecto) level; :limit is important
417 ActivityPub.fetch_activities([], %{
419 tag: ["ANY1", "any2"],
424 ActivityPub.fetch_activities([], %{
426 tag: ["any1", "Any2"],
431 # Regression test: passing empty lists as filter options shouldn't affect the results
432 assert fetch_five == fetch_six
434 [fetch_one, fetch_two, fetch_three, fetch_four, fetch_five] =
435 Enum.map([fetch_one, fetch_two, fetch_three, fetch_four, fetch_five], fn statuses ->
436 Enum.map(statuses, fn s -> Repo.preload(s, object: :hashtags) end)
439 assert fetch_one == [status_one, status_three]
440 assert fetch_two == [status_one, status_two, status_three]
441 assert fetch_three == [status_one, status_two]
442 assert fetch_four == [status_three]
443 assert fetch_five == [status_four, status_five]
447 describe "insertion" do
448 test "drops activities beyond a certain limit" do
449 limit = Config.get([:instance, :remote_limit])
452 :crypto.strong_rand_bytes(limit + 1)
454 |> binary_part(0, limit + 1)
459 "content" => random_text
463 assert {:error, :remote_limit} = ActivityPub.insert(data)
466 test "doesn't drop activities with content being null" do
470 "actor" => user.ap_id,
473 "actor" => user.ap_id,
480 assert {:ok, _} = ActivityPub.insert(data)
483 test "returns the activity if one with the same id is already in" do
484 activity = insert(:note_activity)
485 {:ok, new_activity} = ActivityPub.insert(activity.data)
487 assert activity.id == new_activity.id
490 test "inserts a given map into the activity database, giving it an id if it has none." do
494 "actor" => user.ap_id,
497 "actor" => user.ap_id,
504 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
505 assert activity.data["ok"] == data["ok"]
506 assert is_binary(activity.data["id"])
512 "actor" => user.ap_id,
514 "context" => "blabla",
516 "actor" => user.ap_id,
523 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
524 assert activity.data["ok"] == data["ok"]
525 assert activity.data["id"] == given_id
526 assert activity.data["context"] == "blabla"
529 test "adds a context when none is there" do
533 "actor" => user.ap_id,
536 "actor" => user.ap_id,
543 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
544 object = Pleroma.Object.normalize(activity, fetch: false)
546 assert is_binary(activity.data["context"])
547 assert is_binary(object.data["context"])
550 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
554 "actor" => user.ap_id,
557 "actor" => user.ap_id,
564 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
565 assert object = Object.normalize(activity, fetch: false)
566 assert is_binary(object.data["id"])
570 describe "create activities" do
572 [user: insert(:user)]
575 test "it reverts create", %{user: user} do
576 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
577 assert {:error, :reverted} =
578 ActivityPub.create(%{
579 to: ["user1", "user2"],
583 "to" => ["user1", "user2"],
585 "content" => "testing"
590 assert Repo.aggregate(Activity, :count, :id) == 0
591 assert Repo.aggregate(Object, :count, :id) == 0
594 test "creates activity if expiration is not configured and expires_at is not passed", %{
597 clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
600 ActivityPub.create(%{
601 to: ["user1", "user2"],
605 "to" => ["user1", "user2"],
607 "content" => "testing"
612 test "rejects activity if expires_at present but expiration is not configured", %{user: user} do
613 clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
615 assert {:error, :expired_activities_disabled} =
616 ActivityPub.create(%{
617 to: ["user1", "user2"],
621 "to" => ["user1", "user2"],
623 "content" => "testing"
626 "expires_at" => DateTime.utc_now()
630 assert Repo.aggregate(Activity, :count, :id) == 0
631 assert Repo.aggregate(Object, :count, :id) == 0
634 test "removes doubled 'to' recipients", %{user: user} do
636 ActivityPub.create(%{
637 to: ["user1", "user1", "user2"],
641 "to" => ["user1", "user1", "user2"],
643 "content" => "testing"
647 assert activity.data["to"] == ["user1", "user2"]
648 assert activity.actor == user.ap_id
649 assert activity.recipients == ["user1", "user2", user.ap_id]
652 test "increases user note count only for public activities", %{user: user} do
654 CommonAPI.post(User.get_cached_by_id(user.id), %{
660 CommonAPI.post(User.get_cached_by_id(user.id), %{
662 visibility: "unlisted"
666 CommonAPI.post(User.get_cached_by_id(user.id), %{
668 visibility: "private"
672 CommonAPI.post(User.get_cached_by_id(user.id), %{
677 user = User.get_cached_by_id(user.id)
678 assert user.note_count == 2
681 test "increases replies count", %{user: user} do
682 user2 = insert(:user)
684 {:ok, activity} = CommonAPI.post(user, %{status: "1", visibility: "public"})
685 ap_id = activity.data["id"]
686 reply_data = %{status: "1", in_reply_to_status_id: activity.id}
689 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "public"))
690 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
691 assert object.data["repliesCount"] == 1
694 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "unlisted"))
695 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
696 assert object.data["repliesCount"] == 2
699 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "private"))
700 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
701 assert object.data["repliesCount"] == 2
704 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "direct"))
705 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
706 assert object.data["repliesCount"] == 2
710 describe "fetch activities for recipients" do
711 test "retrieve the activities for certain recipients" do
712 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
713 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
714 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
716 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
717 assert length(activities) == 2
718 assert activities == [activity_one, activity_two]
722 describe "fetch activities for followed hashtags" do
723 test "it should return public activities that reference a given hashtag" do
724 hashtag = insert(:hashtag, name: "tenshi")
726 other_user = insert(:user)
728 {:ok, normally_visible} =
729 CommonAPI.post(other_user, %{status: "hello :)", visibility: "public"})
731 {:ok, public} = CommonAPI.post(user, %{status: "maji #tenshi", visibility: "public"})
732 {:ok, _unrelated} = CommonAPI.post(user, %{status: "dai #tensh", visibility: "public"})
733 {:ok, unlisted} = CommonAPI.post(user, %{status: "maji #tenshi", visibility: "unlisted"})
734 {:ok, _private} = CommonAPI.post(user, %{status: "maji #tenshi", visibility: "private"})
737 ActivityPub.fetch_activities([other_user.follower_address], %{
738 followed_hashtags: [hashtag.id]
741 assert length(activities) == 3
742 normal_id = normally_visible.id
743 public_id = public.id
744 unlisted_id = unlisted.id
745 assert [%{id: ^normal_id}, %{id: ^public_id}, %{id: ^unlisted_id}] = activities
749 describe "fetch activities in context" do
750 test "retrieves activities that have a given context" do
751 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
752 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
753 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
754 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
755 activity_five = insert(:note_activity)
758 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
760 activities = ActivityPub.fetch_activities_for_context("2hu", %{blocking_user: user})
761 assert activities == [activity_two, activity]
764 test "doesn't return activities with filtered words" do
766 user_two = insert(:user)
767 insert(:filter, user: user, phrase: "test", hide: true)
769 {:ok, %{id: id1, data: %{"context" => context}}} = CommonAPI.post(user, %{status: "1"})
771 {:ok, %{id: id2}} = CommonAPI.post(user_two, %{status: "2", in_reply_to_status_id: id1})
773 {:ok, %{id: id3} = user_activity} =
774 CommonAPI.post(user, %{status: "3 test?", in_reply_to_status_id: id2})
776 {:ok, %{id: id4} = filtered_activity} =
777 CommonAPI.post(user_two, %{status: "4 test!", in_reply_to_status_id: id3})
779 {:ok, _} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
783 |> ActivityPub.fetch_activities_for_context(%{user: user})
786 assert length(activities) == 4
787 assert user_activity.id in activities
788 refute filtered_activity.id in activities
792 test "doesn't return blocked activities" do
793 activity_one = insert(:note_activity)
794 activity_two = insert(:note_activity)
795 activity_three = insert(:note_activity)
797 booster = insert(:user)
798 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
800 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
802 assert Enum.member?(activities, activity_two)
803 assert Enum.member?(activities, activity_three)
804 refute Enum.member?(activities, activity_one)
806 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
808 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
810 assert Enum.member?(activities, activity_two)
811 assert Enum.member?(activities, activity_three)
812 assert Enum.member?(activities, activity_one)
814 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
815 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
816 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
817 activity_three = Activity.get_by_id(activity_three.id)
819 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
821 assert Enum.member?(activities, activity_two)
822 refute Enum.member?(activities, activity_three)
823 refute Enum.member?(activities, boost_activity)
824 assert Enum.member?(activities, activity_one)
826 activities = ActivityPub.fetch_activities([], %{blocking_user: nil, skip_preload: true})
828 assert Enum.member?(activities, activity_two)
829 assert Enum.member?(activities, activity_three)
830 assert Enum.member?(activities, boost_activity)
831 assert Enum.member?(activities, activity_one)
834 test "doesn't return activities from deactivated users" do
835 _user = insert(:user)
836 deactivated = insert(:user)
837 active = insert(:user)
838 {:ok, activity_one} = CommonAPI.post(deactivated, %{status: "hey!"})
839 {:ok, activity_two} = CommonAPI.post(active, %{status: "yay!"})
840 {:ok, _updated_user} = User.set_activation(deactivated, false)
842 activities = ActivityPub.fetch_activities([], %{})
844 refute Enum.member?(activities, activity_one)
845 assert Enum.member?(activities, activity_two)
848 test "always see your own posts even when they address people you block" do
850 blockee = insert(:user)
852 {:ok, _} = User.block(user, blockee)
853 {:ok, activity} = CommonAPI.post(user, %{status: "hey! @#{blockee.nickname}"})
855 activities = ActivityPub.fetch_activities([], %{blocking_user: user})
857 assert Enum.member?(activities, activity)
860 test "doesn't return transitive interactions concerning blocked users" do
861 blocker = insert(:user)
862 blockee = insert(:user)
863 friend = insert(:user)
865 {:ok, _user_relationship} = User.block(blocker, blockee)
867 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
869 {:ok, activity_two} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
871 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
873 {:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
875 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker})
877 assert Enum.member?(activities, activity_one)
878 refute Enum.member?(activities, activity_two)
879 refute Enum.member?(activities, activity_three)
880 refute Enum.member?(activities, activity_four)
883 test "doesn't return announce activities with blocked users in 'to'" do
884 blocker = insert(:user)
885 blockee = insert(:user)
886 friend = insert(:user)
888 {:ok, _user_relationship} = User.block(blocker, blockee)
890 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
892 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
894 {:ok, activity_three} = CommonAPI.repeat(activity_two.id, friend)
897 ActivityPub.fetch_activities([], %{blocking_user: blocker})
898 |> Enum.map(fn act -> act.id end)
900 assert Enum.member?(activities, activity_one.id)
901 refute Enum.member?(activities, activity_two.id)
902 refute Enum.member?(activities, activity_three.id)
905 test "doesn't return announce activities with blocked users in 'cc'" do
906 blocker = insert(:user)
907 blockee = insert(:user)
908 friend = insert(:user)
910 {:ok, _user_relationship} = User.block(blocker, blockee)
912 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
914 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
916 assert object = Pleroma.Object.normalize(activity_two, fetch: false)
919 "actor" => friend.ap_id,
920 "object" => object.data["id"],
921 "context" => object.data["context"],
922 "type" => "Announce",
923 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
924 "cc" => [blockee.ap_id]
927 assert {:ok, activity_three} = ActivityPub.insert(data)
930 ActivityPub.fetch_activities([], %{blocking_user: blocker})
931 |> Enum.map(fn act -> act.id end)
933 assert Enum.member?(activities, activity_one.id)
934 refute Enum.member?(activities, activity_two.id)
935 refute Enum.member?(activities, activity_three.id)
938 test "doesn't return activities from blocked domains" do
939 domain = "dogwhistle.zone"
940 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
941 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
942 activity = insert(:note_activity, %{note: note})
944 {:ok, user} = User.block_domain(user, domain)
946 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
948 refute activity in activities
950 followed_user = insert(:user)
951 CommonAPI.follow(user, followed_user)
952 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user)
954 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
956 refute repeat_activity in activities
959 test "see your own posts even when they adress actors from blocked domains" do
962 domain = "dogwhistle.zone"
963 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
965 {:ok, user} = User.block_domain(user, domain)
967 {:ok, activity} = CommonAPI.post(user, %{status: "hey! @#{domain_user.nickname}"})
969 activities = ActivityPub.fetch_activities([], %{blocking_user: user})
971 assert Enum.member?(activities, activity)
974 test "does return activities from followed users on blocked domains" do
975 domain = "meanies.social"
976 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
977 blocker = insert(:user)
979 {:ok, blocker, domain_user} = User.follow(blocker, domain_user)
980 {:ok, blocker} = User.block_domain(blocker, domain)
982 assert User.following?(blocker, domain_user)
983 assert User.blocks_domain?(blocker, domain_user)
984 refute User.blocks?(blocker, domain_user)
986 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
987 activity = insert(:note_activity, %{note: note})
989 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
991 assert activity in activities
993 # And check that if the guy we DO follow boosts someone else from their domain,
994 # that should be hidden
995 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
996 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
997 bad_activity = insert(:note_activity, %{note: bad_note})
998 {:ok, repeat_activity} = CommonAPI.repeat(bad_activity.id, domain_user)
1000 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
1002 refute repeat_activity in activities
1005 test "returns your own posts regardless of mute" do
1006 user = insert(:user)
1007 muted = insert(:user)
1009 {:ok, muted_post} = CommonAPI.post(muted, %{status: "Im stupid"})
1012 CommonAPI.post(user, %{status: "I'm muting you", in_reply_to_status_id: muted_post.id})
1014 {:ok, _} = User.mute(user, muted)
1016 [activity] = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
1018 assert activity.id == reply.id
1021 test "doesn't return muted activities" do
1022 activity_one = insert(:note_activity)
1023 activity_two = insert(:note_activity)
1024 activity_three = insert(:note_activity)
1025 user = insert(:user)
1026 booster = insert(:user)
1028 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
1029 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
1031 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
1033 assert Enum.member?(activities, activity_two)
1034 assert Enum.member?(activities, activity_three)
1035 refute Enum.member?(activities, activity_one)
1037 # Calling with 'with_muted' will deliver muted activities, too.
1039 ActivityPub.fetch_activities([], %{
1045 assert Enum.member?(activities, activity_two)
1046 assert Enum.member?(activities, activity_three)
1047 assert Enum.member?(activities, activity_one)
1049 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
1051 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
1053 assert Enum.member?(activities, activity_two)
1054 assert Enum.member?(activities, activity_three)
1055 assert Enum.member?(activities, activity_one)
1057 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
1058 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
1059 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
1060 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
1061 activity_three = Activity.get_by_id(activity_three.id)
1063 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
1065 assert Enum.member?(activities, activity_two)
1066 refute Enum.member?(activities, activity_three)
1067 refute Enum.member?(activities, boost_activity)
1068 assert Enum.member?(activities, activity_one)
1070 activities = ActivityPub.fetch_activities([], %{muting_user: nil, skip_preload: true})
1072 assert Enum.member?(activities, activity_two)
1073 assert Enum.member?(activities, activity_three)
1074 assert Enum.member?(activities, boost_activity)
1075 assert Enum.member?(activities, activity_one)
1078 test "doesn't return thread muted activities" do
1079 user = insert(:user)
1080 _activity_one = insert(:note_activity)
1081 note_two = insert(:note, data: %{"context" => "suya.."})
1082 activity_two = insert(:note_activity, note: note_two)
1084 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
1086 assert [_activity_one] = ActivityPub.fetch_activities([], %{muting_user: user})
1089 test "returns thread muted activities when with_muted is set" do
1090 user = insert(:user)
1091 _activity_one = insert(:note_activity)
1092 note_two = insert(:note, data: %{"context" => "suya.."})
1093 activity_two = insert(:note_activity, note: note_two)
1095 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
1097 assert [_activity_two, _activity_one] =
1098 ActivityPub.fetch_activities([], %{muting_user: user, with_muted: true})
1101 test "does include announces on request" do
1102 activity_three = insert(:note_activity)
1103 user = insert(:user)
1104 booster = insert(:user)
1106 {:ok, user, booster} = User.follow(user, booster)
1108 {:ok, announce} = CommonAPI.repeat(activity_three.id, booster)
1110 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
1112 assert announce_activity.id == announce.id
1115 test "excludes reblogs on request" do
1116 user = insert(:user)
1117 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
1118 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
1120 [activity] = ActivityPub.fetch_user_activities(user, nil, %{exclude_reblogs: true})
1122 assert activity == expected_activity
1125 describe "irreversible filters" do
1127 user = insert(:user)
1128 user_two = insert(:user)
1130 insert(:filter, user: user_two, phrase: "cofe", hide: true)
1131 insert(:filter, user: user_two, phrase: "ok boomer", hide: true)
1132 insert(:filter, user: user_two, phrase: "test", hide: false)
1135 type: ["Create", "Announce"],
1139 {:ok, %{user: user, user_two: user_two, params: params}}
1142 test "it returns statuses if they don't contain exact filter words", %{
1146 {:ok, _} = CommonAPI.post(user, %{status: "hey"})
1147 {:ok, _} = CommonAPI.post(user, %{status: "got cofefe?"})
1148 {:ok, _} = CommonAPI.post(user, %{status: "I am not a boomer"})
1149 {:ok, _} = CommonAPI.post(user, %{status: "ok boomers"})
1150 {:ok, _} = CommonAPI.post(user, %{status: "ccofee is not a word"})
1151 {:ok, _} = CommonAPI.post(user, %{status: "this is a test"})
1153 activities = ActivityPub.fetch_activities([], params)
1155 assert Enum.count(activities) == 6
1158 test "it does not filter user's own statuses", %{user_two: user_two, params: params} do
1159 {:ok, _} = CommonAPI.post(user_two, %{status: "Give me some cofe!"})
1160 {:ok, _} = CommonAPI.post(user_two, %{status: "ok boomer"})
1162 activities = ActivityPub.fetch_activities([], params)
1164 assert Enum.count(activities) == 2
1167 test "it excludes statuses with filter words", %{user: user, params: params} do
1168 {:ok, _} = CommonAPI.post(user, %{status: "Give me some cofe!"})
1169 {:ok, _} = CommonAPI.post(user, %{status: "ok boomer"})
1170 {:ok, _} = CommonAPI.post(user, %{status: "is it a cOfE?"})
1171 {:ok, _} = CommonAPI.post(user, %{status: "cofe is all I need"})
1172 {:ok, _} = CommonAPI.post(user, %{status: "— ok BOOMER\n"})
1174 activities = ActivityPub.fetch_activities([], params)
1176 assert Enum.empty?(activities)
1179 test "it returns all statuses if user does not have any filters" do
1180 another_user = insert(:user)
1181 {:ok, _} = CommonAPI.post(another_user, %{status: "got cofe?"})
1182 {:ok, _} = CommonAPI.post(another_user, %{status: "test!"})
1185 ActivityPub.fetch_activities([], %{
1186 type: ["Create", "Announce"],
1190 assert Enum.count(activities) == 2
1194 describe "public fetch activities" do
1195 test "doesn't retrieve unlisted activities" do
1196 user = insert(:user)
1198 {:ok, _unlisted_activity} = CommonAPI.post(user, %{status: "yeah", visibility: "unlisted"})
1200 {:ok, listed_activity} = CommonAPI.post(user, %{status: "yeah"})
1202 [activity] = ActivityPub.fetch_public_activities()
1204 assert activity == listed_activity
1207 test "retrieves public activities" do
1208 _activities = ActivityPub.fetch_public_activities()
1210 %{public: public} = ActivityBuilder.public_and_non_public()
1212 activities = ActivityPub.fetch_public_activities()
1213 assert length(activities) == 1
1214 assert Enum.at(activities, 0) == public
1217 test "retrieves a maximum of 20 activities" do
1218 ActivityBuilder.insert_list(10)
1219 expected_activities = ActivityBuilder.insert_list(20)
1221 activities = ActivityPub.fetch_public_activities()
1223 assert collect_ids(activities) == collect_ids(expected_activities)
1224 assert length(activities) == 20
1227 test "retrieves ids starting from a since_id" do
1228 activities = ActivityBuilder.insert_list(30)
1229 expected_activities = ActivityBuilder.insert_list(10)
1230 since_id = List.last(activities).id
1232 activities = ActivityPub.fetch_public_activities(%{since_id: since_id})
1234 assert collect_ids(activities) == collect_ids(expected_activities)
1235 assert length(activities) == 10
1238 test "retrieves ids up to max_id" do
1239 ActivityBuilder.insert_list(10)
1240 expected_activities = ActivityBuilder.insert_list(20)
1244 |> ActivityBuilder.insert_list()
1247 activities = ActivityPub.fetch_public_activities(%{max_id: max_id})
1249 assert length(activities) == 20
1250 assert collect_ids(activities) == collect_ids(expected_activities)
1253 test "paginates via offset/limit" do
1254 _first_part_activities = ActivityBuilder.insert_list(10)
1255 second_part_activities = ActivityBuilder.insert_list(10)
1257 later_activities = ActivityBuilder.insert_list(10)
1259 activities = ActivityPub.fetch_public_activities(%{page: "2", page_size: "20"}, :offset)
1261 assert length(activities) == 20
1263 assert collect_ids(activities) ==
1264 collect_ids(second_part_activities) ++ collect_ids(later_activities)
1267 test "doesn't return reblogs for users for whom reblogs have been muted" do
1268 activity = insert(:note_activity)
1269 user = insert(:user)
1270 booster = insert(:user)
1271 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
1273 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
1275 activities = ActivityPub.fetch_activities([], %{muting_user: user})
1277 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
1280 test "returns reblogs for users for whom reblogs have not been muted" do
1281 activity = insert(:note_activity)
1282 user = insert(:user)
1283 booster = insert(:user)
1284 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
1285 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
1287 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
1289 activities = ActivityPub.fetch_activities([], %{muting_user: user})
1291 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
1295 describe "uploading files" do
1297 test_file = %Plug.Upload{
1298 content_type: "image/jpeg",
1299 path: Path.absname("test/fixtures/image.jpg"),
1300 filename: "an_image.jpg"
1303 %{test_file: test_file}
1306 test "sets a description if given", %{test_file: file} do
1307 {:ok, %Object{} = object} = ActivityPub.upload(file, description: "a cool file")
1308 assert object.data["name"] == "a cool file"
1311 test "it sets the default description depending on the configuration", %{test_file: file} do
1312 clear_config([Pleroma.Upload, :default_description])
1314 clear_config([Pleroma.Upload, :default_description], nil)
1315 {:ok, %Object{} = object} = ActivityPub.upload(file)
1316 assert object.data["name"] == ""
1318 clear_config([Pleroma.Upload, :default_description], :filename)
1319 {:ok, %Object{} = object} = ActivityPub.upload(file)
1320 assert object.data["name"] == "an_image.jpg"
1322 clear_config([Pleroma.Upload, :default_description], "unnamed attachment")
1323 {:ok, %Object{} = object} = ActivityPub.upload(file)
1324 assert object.data["name"] == "unnamed attachment"
1327 test "copies the file to the configured folder", %{test_file: file} do
1328 clear_config([Pleroma.Upload, :default_description], :filename)
1329 {:ok, %Object{} = object} = ActivityPub.upload(file)
1330 assert object.data["name"] == "an_image.jpg"
1333 test "works with base64 encoded images" do
1338 {:ok, %Object{}} = ActivityPub.upload(file)
1342 describe "fetch the latest Follow" do
1343 test "fetches the latest Follow activity" do
1344 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1345 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1346 followed = Repo.get_by(User, ap_id: activity.data["object"])
1348 assert activity == Utils.fetch_latest_follow(follower, followed)
1352 describe "unfollowing" do
1353 test "it reverts unfollow activity" do
1354 follower = insert(:user)
1355 followed = insert(:user)
1357 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1359 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1360 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
1363 activity = Activity.get_by_id(follow_activity.id)
1364 assert activity.data["type"] == "Follow"
1365 assert activity.data["actor"] == follower.ap_id
1367 assert activity.data["object"] == followed.ap_id
1370 test "creates an undo activity for the last follow" do
1371 follower = insert(:user)
1372 followed = insert(:user)
1374 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1375 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1377 assert activity.data["type"] == "Undo"
1378 assert activity.data["actor"] == follower.ap_id
1380 embedded_object = activity.data["object"]
1381 assert is_map(embedded_object)
1382 assert embedded_object["type"] == "Follow"
1383 assert embedded_object["object"] == followed.ap_id
1384 assert embedded_object["id"] == follow_activity.data["id"]
1387 test "creates an undo activity for a pending follow request" do
1388 follower = insert(:user)
1389 followed = insert(:user, %{is_locked: true})
1391 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1392 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1394 assert activity.data["type"] == "Undo"
1395 assert activity.data["actor"] == follower.ap_id
1397 embedded_object = activity.data["object"]
1398 assert is_map(embedded_object)
1399 assert embedded_object["type"] == "Follow"
1400 assert embedded_object["object"] == followed.ap_id
1401 assert embedded_object["id"] == follow_activity.data["id"]
1404 test "it removes the follow activity if it was local" do
1405 follower = insert(:user, local: true)
1406 followed = insert(:user)
1408 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1409 {:ok, activity} = ActivityPub.unfollow(follower, followed, nil, true)
1411 assert activity.data["type"] == "Undo"
1412 assert activity.data["actor"] == follower.ap_id
1414 follow_activity = Activity.get_by_id(follow_activity.id)
1415 assert is_nil(follow_activity)
1416 assert is_nil(Utils.fetch_latest_follow(follower, followed))
1418 # We need to keep our own undo
1419 undo_activity = Activity.get_by_ap_id(activity.data["id"])
1420 refute is_nil(undo_activity)
1423 test "it removes the follow activity if it was remote" do
1424 follower = insert(:user, local: false)
1425 followed = insert(:user)
1427 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1428 {:ok, activity} = ActivityPub.unfollow(follower, followed, nil, false)
1430 assert activity.data["type"] == "Undo"
1431 assert activity.data["actor"] == follower.ap_id
1433 follow_activity = Activity.get_by_id(follow_activity.id)
1434 assert is_nil(follow_activity)
1435 assert is_nil(Utils.fetch_latest_follow(follower, followed))
1437 undo_activity = Activity.get_by_ap_id(activity.data["id"])
1438 assert is_nil(undo_activity)
1442 describe "timeline post-processing" do
1443 test "it filters broken threads" do
1444 user1 = insert(:user)
1445 user2 = insert(:user)
1446 user3 = insert(:user)
1448 {:ok, user1, user3} = User.follow(user1, user3)
1449 assert User.following?(user1, user3)
1451 {:ok, user2, user3} = User.follow(user2, user3)
1452 assert User.following?(user2, user3)
1454 {:ok, user3, user2} = User.follow(user3, user2)
1455 assert User.following?(user3, user2)
1457 {:ok, public_activity} = CommonAPI.post(user3, %{status: "hi 1"})
1459 {:ok, private_activity_1} = CommonAPI.post(user3, %{status: "hi 2", visibility: "private"})
1461 {:ok, private_activity_2} =
1462 CommonAPI.post(user2, %{
1464 visibility: "private",
1465 in_reply_to_status_id: private_activity_1.id
1468 {:ok, private_activity_3} =
1469 CommonAPI.post(user3, %{
1471 visibility: "private",
1472 in_reply_to_status_id: private_activity_2.id
1476 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1477 |> Enum.map(fn a -> a.id end)
1479 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1481 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1483 assert length(activities) == 3
1486 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{user: user1})
1487 |> Enum.map(fn a -> a.id end)
1489 assert [public_activity.id, private_activity_1.id] == activities
1490 assert length(activities) == 2
1494 describe "flag/1" do
1496 reporter = insert(:user)
1497 target_account = insert(:user)
1499 {:ok, activity} = CommonAPI.post(target_account, %{status: content})
1500 context = Utils.generate_context_id()
1502 reporter_ap_id = reporter.ap_id
1503 target_ap_id = target_account.ap_id
1504 activity_ap_id = activity.data["id"]
1506 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1512 target_account: target_account,
1513 reported_activity: activity,
1515 activity_ap_id: activity_ap_id,
1516 activity_with_object: activity_with_object,
1517 reporter_ap_id: reporter_ap_id,
1518 target_ap_id: target_ap_id
1522 test "it can create a Flag activity",
1526 target_account: target_account,
1527 reported_activity: reported_activity,
1529 activity_ap_id: activity_ap_id,
1530 activity_with_object: activity_with_object,
1531 reporter_ap_id: reporter_ap_id,
1532 target_ap_id: target_ap_id
1534 assert {:ok, activity} =
1538 account: target_account,
1539 statuses: [reported_activity],
1545 "id" => activity_ap_id,
1546 "content" => content,
1547 "published" => activity_with_object.object.data["published"],
1549 AccountView.render("show.json", %{user: target_account, skip_visibility_check: true})
1553 actor: ^reporter_ap_id,
1556 "content" => ^content,
1557 "context" => ^context,
1558 "object" => [^target_ap_id, ^note_obj]
1563 test_with_mock "strips status data from Flag, before federating it",
1567 target_account: target_account,
1568 reported_activity: reported_activity,
1578 account: target_account,
1579 statuses: [reported_activity],
1584 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1586 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1589 test_with_mock "reverts on error",
1593 target_account: target_account,
1594 reported_activity: reported_activity,
1599 maybe_federate: fn _ -> {:error, :reverted} end do
1600 assert {:error, :reverted} =
1604 account: target_account,
1605 statuses: [reported_activity],
1609 assert Repo.aggregate(Activity, :count, :id) == 1
1610 assert Repo.aggregate(Object, :count, :id) == 1
1611 assert Repo.aggregate(Notification, :count, :id) == 0
1615 test "fetch_activities/2 returns activities addressed to a list " do
1616 user = insert(:user)
1617 member = insert(:user)
1618 {:ok, list} = Pleroma.List.create("foo", user)
1619 {:ok, list} = Pleroma.List.follow(list, member)
1621 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1623 activity = Repo.preload(activity, :bookmark)
1624 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1626 assert ActivityPub.fetch_activities([], %{user: user}) == [activity]
1630 File.read!("test/fixtures/avatar_data_uri")
1633 describe "fetch_activities_bounded" do
1634 test "fetches private posts for followed users" do
1635 user = insert(:user)
1638 CommonAPI.post(user, %{
1639 status: "thought I looked cute might delete later :3",
1640 visibility: "private"
1643 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1644 assert result.id == activity.id
1647 test "fetches only public posts for other users" do
1648 user = insert(:user)
1649 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe", visibility: "public"})
1651 {:ok, _private_activity} =
1652 CommonAPI.post(user, %{
1653 status: "why is tenshi eating a corndog so cute?",
1654 visibility: "private"
1657 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1658 assert result.id == activity.id
1662 describe "fetch_follow_information_for_user" do
1663 test "synchronizes following/followers counters" do
1667 follower_address: "http://localhost:4001/users/fuser2/followers",
1668 following_address: "http://localhost:4001/users/fuser2/following"
1671 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1672 assert info.follower_count == 527
1673 assert info.following_count == 267
1676 test "detects hidden followers" do
1679 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1680 %Tesla.Env{status: 403, body: ""}
1683 apply(HttpRequestMock, :request, [env])
1690 follower_address: "http://localhost:4001/users/masto_closed/followers",
1691 following_address: "http://localhost:4001/users/masto_closed/following"
1694 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1695 assert follow_info.hide_followers == true
1696 assert follow_info.hide_follows == false
1699 test "detects hidden follows" do
1702 "http://localhost:4001/users/masto_closed/following?page=1" ->
1703 %Tesla.Env{status: 403, body: ""}
1706 apply(HttpRequestMock, :request, [env])
1713 follower_address: "http://localhost:4001/users/masto_closed/followers",
1714 following_address: "http://localhost:4001/users/masto_closed/following"
1717 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1718 assert follow_info.hide_followers == false
1719 assert follow_info.hide_follows == true
1722 test "detects hidden follows/followers for friendica" do
1726 follower_address: "http://localhost:8080/followers/fuser3",
1727 following_address: "http://localhost:8080/following/fuser3"
1730 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1731 assert follow_info.hide_followers == true
1732 assert follow_info.follower_count == 296
1733 assert follow_info.following_count == 32
1734 assert follow_info.hide_follows == true
1737 test "doesn't crash when follower and following counters are hidden" do
1740 "http://localhost:4001/users/masto_hidden_counters/following" ->
1743 "@context" => "https://www.w3.org/ns/activitystreams",
1744 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1746 headers: HttpRequestMock.activitypub_object_headers()
1749 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1750 %Tesla.Env{status: 403, body: ""}
1752 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1755 "@context" => "https://www.w3.org/ns/activitystreams",
1756 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1758 headers: HttpRequestMock.activitypub_object_headers()
1761 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1762 %Tesla.Env{status: 403, body: ""}
1769 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1770 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1773 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1775 assert follow_info.hide_followers == true
1776 assert follow_info.follower_count == 0
1777 assert follow_info.hide_follows == true
1778 assert follow_info.following_count == 0
1782 describe "fetch_favourites/3" do
1783 test "returns a favourite activities sorted by adds to favorite" do
1784 user = insert(:user)
1785 other_user = insert(:user)
1786 user1 = insert(:user)
1787 user2 = insert(:user)
1788 {:ok, a1} = CommonAPI.post(user1, %{status: "bla"})
1789 {:ok, _a2} = CommonAPI.post(user2, %{status: "traps are happy"})
1790 {:ok, a3} = CommonAPI.post(user2, %{status: "Trees Are "})
1791 {:ok, a4} = CommonAPI.post(user2, %{status: "Agent Smith "})
1792 {:ok, a5} = CommonAPI.post(user1, %{status: "Red or Blue "})
1794 {:ok, _} = CommonAPI.favorite(user, a4.id)
1795 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1796 {:ok, _} = CommonAPI.favorite(user, a3.id)
1797 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1798 {:ok, _} = CommonAPI.favorite(user, a5.id)
1799 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1800 {:ok, _} = CommonAPI.favorite(user, a1.id)
1801 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1802 result = ActivityPub.fetch_favourites(user)
1804 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1806 result = ActivityPub.fetch_favourites(user, %{limit: 2})
1807 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1811 describe "Move activity" do
1813 %{ap_id: old_ap_id} = old_user = insert(:user)
1814 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1815 follower = insert(:user)
1816 follower_move_opted_out = insert(:user, allow_following_move: false)
1818 User.follow(follower, old_user)
1819 User.follow(follower_move_opted_out, old_user)
1821 assert User.following?(follower, old_user)
1822 assert User.following?(follower_move_opted_out, old_user)
1824 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1829 "actor" => ^old_ap_id,
1830 "object" => ^old_ap_id,
1831 "target" => ^new_ap_id,
1835 recipients: recipients
1838 assert old_user.follower_address in recipients
1841 "op" => "move_following",
1842 "origin_id" => old_user.id,
1843 "target_id" => new_user.id
1846 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1848 Pleroma.Workers.BackgroundWorker.perform(%Oban.Job{args: params})
1850 refute User.following?(follower, old_user)
1851 assert User.following?(follower, new_user)
1853 assert User.following?(follower_move_opted_out, old_user)
1854 refute User.following?(follower_move_opted_out, new_user)
1856 activity = %Activity{activity | object: nil}
1858 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1860 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1863 test "old user must be in the new user's `also_known_as` list" do
1864 old_user = insert(:user)
1865 new_user = insert(:user)
1867 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1868 ActivityPub.move(old_user, new_user)
1871 test "do not move remote user following relationships" do
1872 %{ap_id: old_ap_id} = old_user = insert(:user)
1873 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1874 follower_remote = insert(:user, local: false)
1876 User.follow(follower_remote, old_user)
1878 assert User.following?(follower_remote, old_user)
1880 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1885 "actor" => ^old_ap_id,
1886 "object" => ^old_ap_id,
1887 "target" => ^new_ap_id,
1894 "op" => "move_following",
1895 "origin_id" => old_user.id,
1896 "target_id" => new_user.id
1899 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1901 Pleroma.Workers.BackgroundWorker.perform(%Oban.Job{args: params})
1903 assert User.following?(follower_remote, old_user)
1904 refute User.following?(follower_remote, new_user)
1908 test "doesn't retrieve replies activities with exclude_replies" do
1909 user = insert(:user)
1911 {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
1913 {:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id})
1915 [result] = ActivityPub.fetch_public_activities(%{exclude_replies: true})
1917 assert result.id == activity.id
1919 assert length(ActivityPub.fetch_public_activities()) == 2
1922 describe "replies filtering with public messages" do
1923 setup :public_messages
1925 test "public timeline", %{users: %{u1: user}} do
1928 |> Map.put(:type, ["Create", "Announce"])
1929 |> Map.put(:local_only, false)
1930 |> Map.put(:blocking_user, user)
1931 |> Map.put(:muting_user, user)
1932 |> Map.put(:reply_filtering_user, user)
1933 |> ActivityPub.fetch_public_activities()
1934 |> Enum.map(& &1.id)
1936 assert length(activities_ids) == 16
1939 test "public timeline with reply_visibility `following`", %{
1945 activities: activities
1949 |> Map.put(:type, ["Create", "Announce"])
1950 |> Map.put(:local_only, false)
1951 |> Map.put(:blocking_user, user)
1952 |> Map.put(:muting_user, user)
1953 |> Map.put(:reply_visibility, "following")
1954 |> Map.put(:reply_filtering_user, user)
1955 |> ActivityPub.fetch_public_activities()
1956 |> Enum.map(& &1.id)
1958 assert length(activities_ids) == 14
1961 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1963 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1966 test "public timeline with reply_visibility `self`", %{
1972 activities: activities
1976 |> Map.put(:type, ["Create", "Announce"])
1977 |> Map.put(:local_only, false)
1978 |> Map.put(:blocking_user, user)
1979 |> Map.put(:muting_user, user)
1980 |> Map.put(:reply_visibility, "self")
1981 |> Map.put(:reply_filtering_user, user)
1982 |> ActivityPub.fetch_public_activities()
1983 |> Enum.map(& &1.id)
1985 assert length(activities_ids) == 10
1986 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1987 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1990 test "home timeline", %{
1992 activities: activities,
2000 |> Map.put(:type, ["Create", "Announce"])
2001 |> Map.put(:blocking_user, user)
2002 |> Map.put(:muting_user, user)
2003 |> Map.put(:user, user)
2004 |> Map.put(:reply_filtering_user, user)
2007 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2008 |> Enum.map(& &1.id)
2010 assert length(activities_ids) == 13
2025 assert Enum.all?(visible_ids, &(&1 in activities_ids))
2028 test "home timeline with reply_visibility `following`", %{
2030 activities: activities,
2038 |> Map.put(:type, ["Create", "Announce"])
2039 |> Map.put(:blocking_user, user)
2040 |> Map.put(:muting_user, user)
2041 |> Map.put(:user, user)
2042 |> Map.put(:reply_visibility, "following")
2043 |> Map.put(:reply_filtering_user, user)
2046 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2047 |> Enum.map(& &1.id)
2049 assert length(activities_ids) == 11
2064 assert Enum.all?(visible_ids, &(&1 in activities_ids))
2067 test "home timeline with reply_visibility `self`", %{
2069 activities: activities,
2077 |> Map.put(:type, ["Create", "Announce"])
2078 |> Map.put(:blocking_user, user)
2079 |> Map.put(:muting_user, user)
2080 |> Map.put(:user, user)
2081 |> Map.put(:reply_visibility, "self")
2082 |> Map.put(:reply_filtering_user, user)
2085 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2086 |> Enum.map(& &1.id)
2088 assert length(activities_ids) == 9
2101 assert Enum.all?(visible_ids, &(&1 in activities_ids))
2104 test "filtering out announces where the user is the actor of the announced message" do
2105 user = insert(:user)
2106 other_user = insert(:user)
2107 third_user = insert(:user)
2108 User.follow(user, other_user)
2110 {:ok, post} = CommonAPI.post(user, %{status: "yo"})
2111 {:ok, other_post} = CommonAPI.post(third_user, %{status: "yo"})
2112 {:ok, _announce} = CommonAPI.repeat(post.id, other_user)
2113 {:ok, _announce} = CommonAPI.repeat(post.id, third_user)
2114 {:ok, announce} = CommonAPI.repeat(other_post.id, other_user)
2121 [user.ap_id | User.following(user)]
2122 |> ActivityPub.fetch_activities(params)
2124 assert length(results) == 3
2128 announce_filtering_user: user
2132 [user.ap_id | User.following(user)]
2133 |> ActivityPub.fetch_activities(params)
2135 assert result.id == announce.id
2139 describe "replies filtering with private messages" do
2140 setup :private_messages
2142 test "public timeline", %{users: %{u1: user}} do
2145 |> Map.put(:type, ["Create", "Announce"])
2146 |> Map.put(:local_only, false)
2147 |> Map.put(:blocking_user, user)
2148 |> Map.put(:muting_user, user)
2149 |> Map.put(:user, user)
2150 |> ActivityPub.fetch_public_activities()
2151 |> Enum.map(& &1.id)
2153 assert activities_ids == []
2156 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
2159 |> Map.put(:type, ["Create", "Announce"])
2160 |> Map.put(:local_only, false)
2161 |> Map.put(:blocking_user, user)
2162 |> Map.put(:muting_user, user)
2163 |> Map.put(:reply_visibility, "following")
2164 |> Map.put(:reply_filtering_user, user)
2165 |> Map.put(:user, user)
2166 |> ActivityPub.fetch_public_activities()
2167 |> Enum.map(& &1.id)
2169 assert activities_ids == []
2172 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
2175 |> Map.put(:type, ["Create", "Announce"])
2176 |> Map.put(:local_only, false)
2177 |> Map.put(:blocking_user, user)
2178 |> Map.put(:muting_user, user)
2179 |> Map.put(:reply_visibility, "self")
2180 |> Map.put(:reply_filtering_user, user)
2181 |> Map.put(:user, user)
2182 |> ActivityPub.fetch_public_activities()
2183 |> Enum.map(& &1.id)
2185 assert activities_ids == []
2189 |> Map.put(:reply_visibility, "self")
2190 |> Map.put(:reply_filtering_user, nil)
2191 |> ActivityPub.fetch_public_activities()
2193 assert activities_ids == []
2196 test "home timeline", %{users: %{u1: user}} do
2199 |> Map.put(:type, ["Create", "Announce"])
2200 |> Map.put(:blocking_user, user)
2201 |> Map.put(:muting_user, user)
2202 |> Map.put(:user, user)
2205 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2206 |> Enum.map(& &1.id)
2208 assert length(activities_ids) == 12
2211 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
2214 |> Map.put(:type, ["Create", "Announce"])
2215 |> Map.put(:blocking_user, user)
2216 |> Map.put(:muting_user, user)
2217 |> Map.put(:user, user)
2218 |> Map.put(:reply_visibility, "following")
2219 |> Map.put(:reply_filtering_user, user)
2222 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2223 |> Enum.map(& &1.id)
2225 assert length(activities_ids) == 12
2228 test "home timeline with default reply_visibility `self`", %{
2230 activities: activities,
2238 |> Map.put(:type, ["Create", "Announce"])
2239 |> Map.put(:blocking_user, user)
2240 |> Map.put(:muting_user, user)
2241 |> Map.put(:user, user)
2242 |> Map.put(:reply_visibility, "self")
2243 |> Map.put(:reply_filtering_user, user)
2246 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2247 |> Enum.map(& &1.id)
2249 assert length(activities_ids) == 10
2252 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
2254 assert Enum.all?(visible_ids, &(&1 in activities_ids))
2258 defp public_messages(_) do
2259 [u1, u2, u3, u4] = insert_list(4, :user)
2260 {:ok, u1, u2} = User.follow(u1, u2)
2261 {:ok, u2, u1} = User.follow(u2, u1)
2262 {:ok, u1, u4} = User.follow(u1, u4)
2263 {:ok, u4, u1} = User.follow(u4, u1)
2265 {:ok, u2, u3} = User.follow(u2, u3)
2266 {:ok, u3, u2} = User.follow(u3, u2)
2268 {:ok, a1} = CommonAPI.post(u1, %{status: "Status"})
2271 CommonAPI.post(u2, %{
2272 status: "@#{u1.nickname} reply from u2 to u1",
2273 in_reply_to_status_id: a1.id
2277 CommonAPI.post(u3, %{
2278 status: "@#{u1.nickname} reply from u3 to u1",
2279 in_reply_to_status_id: a1.id
2283 CommonAPI.post(u4, %{
2284 status: "@#{u1.nickname} reply from u4 to u1",
2285 in_reply_to_status_id: a1.id
2288 {:ok, a2} = CommonAPI.post(u2, %{status: "Status"})
2291 CommonAPI.post(u1, %{
2292 status: "@#{u2.nickname} reply from u1 to u2",
2293 in_reply_to_status_id: a2.id
2297 CommonAPI.post(u3, %{
2298 status: "@#{u2.nickname} reply from u3 to u2",
2299 in_reply_to_status_id: a2.id
2303 CommonAPI.post(u4, %{
2304 status: "@#{u2.nickname} reply from u4 to u2",
2305 in_reply_to_status_id: a2.id
2308 {:ok, a3} = CommonAPI.post(u3, %{status: "Status"})
2311 CommonAPI.post(u1, %{
2312 status: "@#{u3.nickname} reply from u1 to u3",
2313 in_reply_to_status_id: a3.id
2317 CommonAPI.post(u2, %{
2318 status: "@#{u3.nickname} reply from u2 to u3",
2319 in_reply_to_status_id: a3.id
2323 CommonAPI.post(u4, %{
2324 status: "@#{u3.nickname} reply from u4 to u3",
2325 in_reply_to_status_id: a3.id
2328 {:ok, a4} = CommonAPI.post(u4, %{status: "Status"})
2331 CommonAPI.post(u1, %{
2332 status: "@#{u4.nickname} reply from u1 to u4",
2333 in_reply_to_status_id: a4.id
2337 CommonAPI.post(u2, %{
2338 status: "@#{u4.nickname} reply from u2 to u4",
2339 in_reply_to_status_id: a4.id
2343 CommonAPI.post(u3, %{
2344 status: "@#{u4.nickname} reply from u3 to u4",
2345 in_reply_to_status_id: a4.id
2349 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2350 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2351 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2352 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
2353 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
2354 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
2357 defp private_messages(_) do
2358 [u1, u2, u3, u4] = insert_list(4, :user)
2359 {:ok, u1, u2} = User.follow(u1, u2)
2360 {:ok, u2, u1} = User.follow(u2, u1)
2361 {:ok, u1, u3} = User.follow(u1, u3)
2362 {:ok, u3, u1} = User.follow(u3, u1)
2363 {:ok, u1, u4} = User.follow(u1, u4)
2364 {:ok, u4, u1} = User.follow(u4, u1)
2366 {:ok, u2, u3} = User.follow(u2, u3)
2367 {:ok, u3, u2} = User.follow(u3, u2)
2369 {:ok, a1} = CommonAPI.post(u1, %{status: "Status", visibility: "private"})
2372 CommonAPI.post(u2, %{
2373 status: "@#{u1.nickname} reply from u2 to u1",
2374 in_reply_to_status_id: a1.id,
2375 visibility: "private"
2379 CommonAPI.post(u3, %{
2380 status: "@#{u1.nickname} reply from u3 to u1",
2381 in_reply_to_status_id: a1.id,
2382 visibility: "private"
2386 CommonAPI.post(u4, %{
2387 status: "@#{u1.nickname} reply from u4 to u1",
2388 in_reply_to_status_id: a1.id,
2389 visibility: "private"
2392 {:ok, a2} = CommonAPI.post(u2, %{status: "Status", visibility: "private"})
2395 CommonAPI.post(u1, %{
2396 status: "@#{u2.nickname} reply from u1 to u2",
2397 in_reply_to_status_id: a2.id,
2398 visibility: "private"
2402 CommonAPI.post(u3, %{
2403 status: "@#{u2.nickname} reply from u3 to u2",
2404 in_reply_to_status_id: a2.id,
2405 visibility: "private"
2408 {:ok, a3} = CommonAPI.post(u3, %{status: "Status", visibility: "private"})
2411 CommonAPI.post(u1, %{
2412 status: "@#{u3.nickname} reply from u1 to u3",
2413 in_reply_to_status_id: a3.id,
2414 visibility: "private"
2418 CommonAPI.post(u2, %{
2419 status: "@#{u3.nickname} reply from u2 to u3",
2420 in_reply_to_status_id: a3.id,
2421 visibility: "private"
2424 {:ok, a4} = CommonAPI.post(u4, %{status: "Status", visibility: "private"})
2427 CommonAPI.post(u1, %{
2428 status: "@#{u4.nickname} reply from u1 to u4",
2429 in_reply_to_status_id: a4.id,
2430 visibility: "private"
2434 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2435 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2436 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2437 u2: %{r1: r2_1.id, r2: r2_2.id},
2438 u3: %{r1: r3_1.id, r2: r3_2.id},
2442 describe "maybe_update_follow_information/1" do
2444 clear_config([:instance, :external_user_synchronization], true)
2448 ap_id: "https://gensokyo.2hu/users/raymoo",
2449 following_address: "https://gensokyo.2hu/users/following",
2450 follower_address: "https://gensokyo.2hu/users/followers",
2457 test "logs an error when it can't fetch the info", %{user: user} do
2458 assert capture_log(fn ->
2459 ActivityPub.maybe_update_follow_information(user)
2460 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2463 test "just returns the input if the user type is Application", %{
2468 |> Map.put(:type, "Application")
2470 refute capture_log(fn ->
2471 assert ^user = ActivityPub.maybe_update_follow_information(user)
2472 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2475 test "it just returns the input if the user has no following/follower addresses", %{
2480 |> Map.put(:following_address, nil)
2481 |> Map.put(:follower_address, nil)
2483 refute capture_log(fn ->
2484 assert ^user = ActivityPub.maybe_update_follow_information(user)
2485 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2489 describe "global activity expiration" do
2490 test "creates an activity expiration for local Create activities" do
2491 clear_config([:mrf, :policies], Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy)
2493 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
2494 {:ok, follow} = ActivityBuilder.insert(%{"type" => "Follow", "context" => "3hu"})
2497 worker: Pleroma.Workers.PurgeExpiredActivity,
2498 args: %{activity_id: activity.id},
2500 activity.inserted_at
2501 |> DateTime.from_naive!("Etc/UTC")
2502 |> Timex.shift(days: 365)
2506 worker: Pleroma.Workers.PurgeExpiredActivity,
2507 args: %{activity_id: follow.id}
2512 describe "handling of clashing nicknames" do
2513 test "renames an existing user with a clashing nickname and a different ap id" do
2518 nickname: "admin@mastodon.example.org",
2519 ap_id: "http://mastodon.example.org/users/harinezumigari"
2523 nickname: orig_user.nickname,
2524 ap_id: orig_user.ap_id <> "part_2"
2526 |> ActivityPub.maybe_handle_clashing_nickname()
2528 user = User.get_by_id(orig_user.id)
2530 assert user.nickname == "#{orig_user.id}.admin@mastodon.example.org"
2533 test "does nothing with a clashing nickname and the same ap id" do
2538 nickname: "admin@mastodon.example.org",
2539 ap_id: "http://mastodon.example.org/users/harinezumigari"
2543 nickname: orig_user.nickname,
2544 ap_id: orig_user.ap_id
2546 |> ActivityPub.maybe_handle_clashing_nickname()
2548 user = User.get_by_id(orig_user.id)
2550 assert user.nickname == orig_user.nickname
2554 describe "reply filtering" do
2555 test "`following` still contains announcements by friends" do
2556 user = insert(:user)
2557 followed = insert(:user)
2558 not_followed = insert(:user)
2560 User.follow(user, followed)
2562 {:ok, followed_post} = CommonAPI.post(followed, %{status: "Hello"})
2564 {:ok, not_followed_to_followed} =
2565 CommonAPI.post(not_followed, %{
2566 status: "Also hello",
2567 in_reply_to_status_id: followed_post.id
2570 {:ok, retoot} = CommonAPI.repeat(not_followed_to_followed.id, followed)
2574 |> Map.put(:type, ["Create", "Announce"])
2575 |> Map.put(:blocking_user, user)
2576 |> Map.put(:muting_user, user)
2577 |> Map.put(:reply_filtering_user, user)
2578 |> Map.put(:reply_visibility, "following")
2579 |> Map.put(:announce_filtering_user, user)
2580 |> Map.put(:user, user)
2583 [user.ap_id | User.following(user)]
2584 |> ActivityPub.fetch_activities(params)
2586 followed_post_id = followed_post.id
2587 retoot_id = retoot.id
2589 assert [%{id: ^followed_post_id}, %{id: ^retoot_id}] = activities
2591 assert length(activities) == 2
2594 # This test is skipped because, while this is the desired behavior,
2595 # there seems to be no good way to achieve it with the method that
2596 # we currently use for detecting to who a reply is directed.
2597 # This is a TODO and should be fixed by a later rewrite of the code
2600 test "`following` still contains self-replies by friends" do
2601 user = insert(:user)
2602 followed = insert(:user)
2603 not_followed = insert(:user)
2605 User.follow(user, followed)
2607 {:ok, followed_post} = CommonAPI.post(followed, %{status: "Hello"})
2608 {:ok, not_followed_post} = CommonAPI.post(not_followed, %{status: "Also hello"})
2610 {:ok, _followed_to_not_followed} =
2611 CommonAPI.post(followed, %{status: "sup", in_reply_to_status_id: not_followed_post.id})
2613 {:ok, _followed_self_reply} =
2614 CommonAPI.post(followed, %{status: "Also cofe", in_reply_to_status_id: followed_post.id})
2618 |> Map.put(:type, ["Create", "Announce"])
2619 |> Map.put(:blocking_user, user)
2620 |> Map.put(:muting_user, user)
2621 |> Map.put(:reply_filtering_user, user)
2622 |> Map.put(:reply_visibility, "following")
2623 |> Map.put(:announce_filtering_user, user)
2624 |> Map.put(:user, user)
2627 [user.ap_id | User.following(user)]
2628 |> ActivityPub.fetch_activities(params)
2630 assert length(activities) == 2
2634 test "allow fetching of accounts with an empty string name field" do
2636 %{method: :get, url: "https://princess.cat/users/mewmew"} ->
2637 file = File.read!("test/fixtures/mewmew_no_name.json")
2638 %Tesla.Env{status: 200, body: file, headers: HttpRequestMock.activitypub_object_headers()}
2641 {:ok, user} = ActivityPub.make_user_from_ap_id("https://princess.cat/users/mewmew")
2642 assert user.name == " "
2645 describe "persist/1" do
2646 test "should not persist remote delete activities" do
2647 poster = insert(:user, local: false)
2648 {:ok, post} = CommonAPI.post(poster, %{status: "hhhhhh"})
2650 {:ok, delete_data, meta} = Builder.delete(poster, post)
2651 local_opts = Keyword.put(meta, :local, false)
2652 {:ok, act, _meta} = ActivityPub.persist(delete_data, local_opts)
2653 refute act.inserted_at
2656 test "should not persist remote undo activities" do
2657 poster = insert(:user, local: false)
2658 liker = insert(:user, local: false)
2659 {:ok, post} = CommonAPI.post(poster, %{status: "hhhhhh"})
2660 {:ok, like} = CommonAPI.favorite(liker, post.id)
2662 {:ok, undo_data, meta} = Builder.undo(liker, like)
2663 local_opts = Keyword.put(meta, :local, false)
2664 {:ok, act, _meta} = ActivityPub.persist(undo_data, local_opts)
2665 refute act.inserted_at