1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Builders.ActivityBuilder
12 alias Pleroma.Notification
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.AdminAPI.AccountView
18 alias Pleroma.Web.CommonAPI
20 import ExUnit.CaptureLog
22 import Pleroma.Factory
26 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
30 setup do: clear_config([:instance, :federating])
32 describe "streaming out participations" do
33 test "it streams them out" do
35 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
37 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
40 conversation.participations
41 |> Repo.preload(:user)
43 with_mock Pleroma.Web.Streamer,
44 stream: fn _, _ -> nil end do
45 ActivityPub.stream_out_participations(conversation.participations)
47 assert called(Pleroma.Web.Streamer.stream("participation", participations))
51 test "streams them out on activity creation" do
52 user_one = insert(:user)
53 user_two = insert(:user)
55 with_mock Pleroma.Web.Streamer,
56 stream: fn _, _ -> nil end do
58 CommonAPI.post(user_one, %{
59 status: "@#{user_two.nickname}",
64 activity.data["context"]
65 |> Pleroma.Conversation.get_for_ap_id()
66 |> Repo.preload(participations: :user)
68 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
73 describe "fetching restricted by visibility" do
74 test "it restricts by the appropriate visibility" do
77 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
79 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
81 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
83 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
86 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
88 assert activities == [direct_activity]
91 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
93 assert activities == [unlisted_activity]
96 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
98 assert activities == [private_activity]
101 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
103 assert activities == [public_activity]
106 ActivityPub.fetch_activities([], %{
107 :visibility => ~w[private public],
108 "actor_id" => user.ap_id
111 assert activities == [public_activity, private_activity]
115 describe "fetching excluded by visibility" do
116 test "it excludes by the appropriate visibility" do
119 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
121 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
123 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
125 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
128 ActivityPub.fetch_activities([], %{
129 "exclude_visibilities" => "direct",
130 "actor_id" => user.ap_id
133 assert public_activity in activities
134 assert unlisted_activity in activities
135 assert private_activity in activities
136 refute direct_activity in activities
139 ActivityPub.fetch_activities([], %{
140 "exclude_visibilities" => "unlisted",
141 "actor_id" => user.ap_id
144 assert public_activity in activities
145 refute unlisted_activity in activities
146 assert private_activity in activities
147 assert direct_activity in activities
150 ActivityPub.fetch_activities([], %{
151 "exclude_visibilities" => "private",
152 "actor_id" => user.ap_id
155 assert public_activity in activities
156 assert unlisted_activity in activities
157 refute private_activity in activities
158 assert direct_activity in activities
161 ActivityPub.fetch_activities([], %{
162 "exclude_visibilities" => "public",
163 "actor_id" => user.ap_id
166 refute public_activity in activities
167 assert unlisted_activity in activities
168 assert private_activity in activities
169 assert direct_activity in activities
173 describe "building a user from his ap id" do
174 test "it returns a user" do
175 user_id = "http://mastodon.example.org/users/admin"
176 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
177 assert user.ap_id == user_id
178 assert user.nickname == "admin@mastodon.example.org"
179 assert user.ap_enabled
180 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
183 test "it returns a user that is invisible" do
184 user_id = "http://mastodon.example.org/users/relay"
185 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
186 assert User.invisible?(user)
189 test "it fetches the appropriate tag-restricted posts" do
192 {:ok, status_one} = CommonAPI.post(user, %{status: ". #test"})
193 {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
194 {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #reject"})
196 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
199 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
202 ActivityPub.fetch_activities([], %{
204 "tag" => ["test", "essais"],
205 "tag_reject" => ["reject"]
209 ActivityPub.fetch_activities([], %{
212 "tag_all" => ["test", "reject"]
215 assert fetch_one == [status_one, status_three]
216 assert fetch_two == [status_one, status_two, status_three]
217 assert fetch_three == [status_one, status_two]
218 assert fetch_four == [status_three]
222 describe "insertion" do
223 test "drops activities beyond a certain limit" do
224 limit = Config.get([:instance, :remote_limit])
227 :crypto.strong_rand_bytes(limit + 1)
229 |> binary_part(0, limit + 1)
234 "content" => random_text
238 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
241 test "doesn't drop activities with content being null" do
245 "actor" => user.ap_id,
248 "actor" => user.ap_id,
255 assert {:ok, _} = ActivityPub.insert(data)
258 test "returns the activity if one with the same id is already in" do
259 activity = insert(:note_activity)
260 {:ok, new_activity} = ActivityPub.insert(activity.data)
262 assert activity.id == new_activity.id
265 test "inserts a given map into the activity database, giving it an id if it has none." do
269 "actor" => user.ap_id,
272 "actor" => user.ap_id,
279 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
280 assert activity.data["ok"] == data["ok"]
281 assert is_binary(activity.data["id"])
287 "actor" => user.ap_id,
289 "context" => "blabla",
291 "actor" => user.ap_id,
298 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
299 assert activity.data["ok"] == data["ok"]
300 assert activity.data["id"] == given_id
301 assert activity.data["context"] == "blabla"
302 assert activity.data["context_id"]
305 test "adds a context when none is there" do
309 "actor" => user.ap_id,
312 "actor" => user.ap_id,
319 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
320 object = Pleroma.Object.normalize(activity)
322 assert is_binary(activity.data["context"])
323 assert is_binary(object.data["context"])
324 assert activity.data["context_id"]
325 assert object.data["context_id"]
328 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
332 "actor" => user.ap_id,
335 "actor" => user.ap_id,
342 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
343 assert object = Object.normalize(activity)
344 assert is_binary(object.data["id"])
348 describe "listen activities" do
349 test "does not increase user note count" do
353 ActivityPub.listen(%{
354 to: ["https://www.w3.org/ns/activitystreams#Public"],
358 "actor" => user.ap_id,
359 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
361 "title" => "lain radio episode 1",
367 assert activity.actor == user.ap_id
369 user = User.get_cached_by_id(user.id)
370 assert user.note_count == 0
373 test "can be fetched into a timeline" do
374 _listen_activity_1 = insert(:listen)
375 _listen_activity_2 = insert(:listen)
376 _listen_activity_3 = insert(:listen)
378 timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]})
380 assert length(timeline) == 3
384 describe "create activities" do
385 test "it reverts create" do
388 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
389 assert {:error, :reverted} =
390 ActivityPub.create(%{
391 to: ["user1", "user2"],
395 "to" => ["user1", "user2"],
397 "content" => "testing"
402 assert Repo.aggregate(Activity, :count, :id) == 0
403 assert Repo.aggregate(Object, :count, :id) == 0
406 test "removes doubled 'to' recipients" do
410 ActivityPub.create(%{
411 to: ["user1", "user1", "user2"],
415 "to" => ["user1", "user1", "user2"],
417 "content" => "testing"
421 assert activity.data["to"] == ["user1", "user2"]
422 assert activity.actor == user.ap_id
423 assert activity.recipients == ["user1", "user2", user.ap_id]
426 test "increases user note count only for public activities" do
430 CommonAPI.post(User.get_cached_by_id(user.id), %{
436 CommonAPI.post(User.get_cached_by_id(user.id), %{
438 visibility: "unlisted"
442 CommonAPI.post(User.get_cached_by_id(user.id), %{
444 visibility: "private"
448 CommonAPI.post(User.get_cached_by_id(user.id), %{
453 user = User.get_cached_by_id(user.id)
454 assert user.note_count == 2
457 test "increases replies count" do
459 user2 = insert(:user)
461 {:ok, activity} = CommonAPI.post(user, %{status: "1", visibility: "public"})
462 ap_id = activity.data["id"]
463 reply_data = %{status: "1", in_reply_to_status_id: activity.id}
466 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "public"))
467 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
468 assert object.data["repliesCount"] == 1
471 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "unlisted"))
472 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
473 assert object.data["repliesCount"] == 2
476 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "private"))
477 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
478 assert object.data["repliesCount"] == 2
481 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "direct"))
482 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
483 assert object.data["repliesCount"] == 2
487 describe "fetch activities for recipients" do
488 test "retrieve the activities for certain recipients" do
489 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
490 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
491 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
493 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
494 assert length(activities) == 2
495 assert activities == [activity_one, activity_two]
499 describe "fetch activities in context" do
500 test "retrieves activities that have a given context" do
501 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
502 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
503 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
504 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
505 activity_five = insert(:note_activity)
508 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
510 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
511 assert activities == [activity_two, activity]
515 test "doesn't return blocked activities" do
516 activity_one = insert(:note_activity)
517 activity_two = insert(:note_activity)
518 activity_three = insert(:note_activity)
520 booster = insert(:user)
521 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
524 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
526 assert Enum.member?(activities, activity_two)
527 assert Enum.member?(activities, activity_three)
528 refute Enum.member?(activities, activity_one)
530 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
533 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
535 assert Enum.member?(activities, activity_two)
536 assert Enum.member?(activities, activity_three)
537 assert Enum.member?(activities, activity_one)
539 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
540 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
541 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
542 activity_three = Activity.get_by_id(activity_three.id)
545 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
547 assert Enum.member?(activities, activity_two)
548 refute Enum.member?(activities, activity_three)
549 refute Enum.member?(activities, boost_activity)
550 assert Enum.member?(activities, activity_one)
553 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
555 assert Enum.member?(activities, activity_two)
556 assert Enum.member?(activities, activity_three)
557 assert Enum.member?(activities, boost_activity)
558 assert Enum.member?(activities, activity_one)
561 test "doesn't return transitive interactions concerning blocked users" do
562 blocker = insert(:user)
563 blockee = insert(:user)
564 friend = insert(:user)
566 {:ok, _user_relationship} = User.block(blocker, blockee)
568 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
570 {:ok, activity_two} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
572 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
574 {:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
576 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
578 assert Enum.member?(activities, activity_one)
579 refute Enum.member?(activities, activity_two)
580 refute Enum.member?(activities, activity_three)
581 refute Enum.member?(activities, activity_four)
584 test "doesn't return announce activities concerning blocked users" do
585 blocker = insert(:user)
586 blockee = insert(:user)
587 friend = insert(:user)
589 {:ok, _user_relationship} = User.block(blocker, blockee)
591 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
593 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
595 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
598 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
599 |> Enum.map(fn act -> act.id end)
601 assert Enum.member?(activities, activity_one.id)
602 refute Enum.member?(activities, activity_two.id)
603 refute Enum.member?(activities, activity_three.id)
606 test "doesn't return activities from blocked domains" do
607 domain = "dogwhistle.zone"
608 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
609 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
610 activity = insert(:note_activity, %{note: note})
612 {:ok, user} = User.block_domain(user, domain)
615 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
617 refute activity in activities
619 followed_user = insert(:user)
620 ActivityPub.follow(user, followed_user)
621 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
624 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
626 refute repeat_activity in activities
629 test "does return activities from followed users on blocked domains" do
630 domain = "meanies.social"
631 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
632 blocker = insert(:user)
634 {:ok, blocker} = User.follow(blocker, domain_user)
635 {:ok, blocker} = User.block_domain(blocker, domain)
637 assert User.following?(blocker, domain_user)
638 assert User.blocks_domain?(blocker, domain_user)
639 refute User.blocks?(blocker, domain_user)
641 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
642 activity = insert(:note_activity, %{note: note})
645 ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
647 assert activity in activities
649 # And check that if the guy we DO follow boosts someone else from their domain,
650 # that should be hidden
651 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
652 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
653 bad_activity = insert(:note_activity, %{note: bad_note})
654 {:ok, repeat_activity, _} = CommonAPI.repeat(bad_activity.id, domain_user)
657 ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
659 refute repeat_activity in activities
662 test "doesn't return muted activities" do
663 activity_one = insert(:note_activity)
664 activity_two = insert(:note_activity)
665 activity_three = insert(:note_activity)
667 booster = insert(:user)
669 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
670 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
673 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
675 assert Enum.member?(activities, activity_two)
676 assert Enum.member?(activities, activity_three)
677 refute Enum.member?(activities, activity_one)
679 # Calling with 'with_muted' will deliver muted activities, too.
681 ActivityPub.fetch_activities([], %{
682 "muting_user" => user,
683 "with_muted" => true,
684 "skip_preload" => true
687 assert Enum.member?(activities, activity_two)
688 assert Enum.member?(activities, activity_three)
689 assert Enum.member?(activities, activity_one)
691 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
694 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
696 assert Enum.member?(activities, activity_two)
697 assert Enum.member?(activities, activity_three)
698 assert Enum.member?(activities, activity_one)
700 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
701 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
702 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
703 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
704 activity_three = Activity.get_by_id(activity_three.id)
707 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
709 assert Enum.member?(activities, activity_two)
710 refute Enum.member?(activities, activity_three)
711 refute Enum.member?(activities, boost_activity)
712 assert Enum.member?(activities, activity_one)
714 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
716 assert Enum.member?(activities, activity_two)
717 assert Enum.member?(activities, activity_three)
718 assert Enum.member?(activities, boost_activity)
719 assert Enum.member?(activities, activity_one)
722 test "doesn't return thread muted activities" do
724 _activity_one = insert(:note_activity)
725 note_two = insert(:note, data: %{"context" => "suya.."})
726 activity_two = insert(:note_activity, note: note_two)
728 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
730 assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user})
733 test "returns thread muted activities when with_muted is set" do
735 _activity_one = insert(:note_activity)
736 note_two = insert(:note, data: %{"context" => "suya.."})
737 activity_two = insert(:note_activity, note: note_two)
739 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
741 assert [_activity_two, _activity_one] =
742 ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true})
745 test "does include announces on request" do
746 activity_three = insert(:note_activity)
748 booster = insert(:user)
750 {:ok, user} = User.follow(user, booster)
752 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
754 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
756 assert announce_activity.id == announce.id
759 test "excludes reblogs on request" do
761 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
762 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
764 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
766 assert activity == expected_activity
769 describe "public fetch activities" do
770 test "doesn't retrieve unlisted activities" do
773 {:ok, _unlisted_activity} = CommonAPI.post(user, %{status: "yeah", visibility: "unlisted"})
775 {:ok, listed_activity} = CommonAPI.post(user, %{status: "yeah"})
777 [activity] = ActivityPub.fetch_public_activities()
779 assert activity == listed_activity
782 test "retrieves public activities" do
783 _activities = ActivityPub.fetch_public_activities()
785 %{public: public} = ActivityBuilder.public_and_non_public()
787 activities = ActivityPub.fetch_public_activities()
788 assert length(activities) == 1
789 assert Enum.at(activities, 0) == public
792 test "retrieves a maximum of 20 activities" do
793 ActivityBuilder.insert_list(10)
794 expected_activities = ActivityBuilder.insert_list(20)
796 activities = ActivityPub.fetch_public_activities()
798 assert collect_ids(activities) == collect_ids(expected_activities)
799 assert length(activities) == 20
802 test "retrieves ids starting from a since_id" do
803 activities = ActivityBuilder.insert_list(30)
804 expected_activities = ActivityBuilder.insert_list(10)
805 since_id = List.last(activities).id
807 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
809 assert collect_ids(activities) == collect_ids(expected_activities)
810 assert length(activities) == 10
813 test "retrieves ids up to max_id" do
814 ActivityBuilder.insert_list(10)
815 expected_activities = ActivityBuilder.insert_list(20)
819 |> ActivityBuilder.insert_list()
822 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
824 assert length(activities) == 20
825 assert collect_ids(activities) == collect_ids(expected_activities)
828 test "paginates via offset/limit" do
829 _first_part_activities = ActivityBuilder.insert_list(10)
830 second_part_activities = ActivityBuilder.insert_list(10)
832 later_activities = ActivityBuilder.insert_list(10)
835 ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
837 assert length(activities) == 20
839 assert collect_ids(activities) ==
840 collect_ids(second_part_activities) ++ collect_ids(later_activities)
843 test "doesn't return reblogs for users for whom reblogs have been muted" do
844 activity = insert(:note_activity)
846 booster = insert(:user)
847 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
849 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
851 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
853 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
856 test "returns reblogs for users for whom reblogs have not been muted" do
857 activity = insert(:note_activity)
859 booster = insert(:user)
860 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
861 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
863 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
865 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
867 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
871 describe "announcing an object" do
872 test "adds an announce activity to the db" do
873 note_activity = insert(:note_activity)
874 object = Object.normalize(note_activity)
877 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
878 assert object.data["announcement_count"] == 1
879 assert object.data["announcements"] == [user.ap_id]
881 assert announce_activity.data["to"] == [
882 User.ap_followers(user),
883 note_activity.data["actor"]
886 assert announce_activity.data["object"] == object.data["id"]
887 assert announce_activity.data["actor"] == user.ap_id
888 assert announce_activity.data["context"] == object.data["context"]
891 test "reverts annouce from object on error" do
892 note_activity = insert(:note_activity)
893 object = Object.normalize(note_activity)
896 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
897 assert {:error, :reverted} = ActivityPub.announce(user, object)
900 reloaded_object = Object.get_by_ap_id(object.data["id"])
901 assert reloaded_object == object
902 refute reloaded_object.data["announcement_count"]
903 refute reloaded_object.data["announcements"]
907 describe "announcing a private object" do
908 test "adds an announce activity to the db if the audience is not widened" do
910 {:ok, note_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
911 object = Object.normalize(note_activity)
913 {:ok, announce_activity, object} = ActivityPub.announce(user, object, nil, true, false)
915 assert announce_activity.data["to"] == [User.ap_followers(user)]
917 assert announce_activity.data["object"] == object.data["id"]
918 assert announce_activity.data["actor"] == user.ap_id
919 assert announce_activity.data["context"] == object.data["context"]
922 test "does not add an announce activity to the db if the audience is widened" do
924 {:ok, note_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
925 object = Object.normalize(note_activity)
927 assert {:error, _} = ActivityPub.announce(user, object, nil, true, true)
930 test "does not add an announce activity to the db if the announcer is not the author" do
932 announcer = insert(:user)
933 {:ok, note_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
934 object = Object.normalize(note_activity)
936 assert {:error, _} = ActivityPub.announce(announcer, object, nil, true, false)
940 describe "uploading files" do
941 test "copies the file to the configured folder" do
943 content_type: "image/jpg",
944 path: Path.absname("test/fixtures/image.jpg"),
945 filename: "an_image.jpg"
948 {:ok, %Object{} = object} = ActivityPub.upload(file)
949 assert object.data["name"] == "an_image.jpg"
952 test "works with base64 encoded images" do
957 {:ok, %Object{}} = ActivityPub.upload(file)
961 describe "fetch the latest Follow" do
962 test "fetches the latest Follow activity" do
963 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
964 follower = Repo.get_by(User, ap_id: activity.data["actor"])
965 followed = Repo.get_by(User, ap_id: activity.data["object"])
967 assert activity == Utils.fetch_latest_follow(follower, followed)
971 describe "following / unfollowing" do
972 test "it reverts follow activity" do
973 follower = insert(:user)
974 followed = insert(:user)
976 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
977 assert {:error, :reverted} = ActivityPub.follow(follower, followed)
980 assert Repo.aggregate(Activity, :count, :id) == 0
981 assert Repo.aggregate(Object, :count, :id) == 0
984 test "it reverts unfollow activity" do
985 follower = insert(:user)
986 followed = insert(:user)
988 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
990 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
991 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
994 activity = Activity.get_by_id(follow_activity.id)
995 assert activity.data["type"] == "Follow"
996 assert activity.data["actor"] == follower.ap_id
998 assert activity.data["object"] == followed.ap_id
1001 test "creates a follow activity" do
1002 follower = insert(:user)
1003 followed = insert(:user)
1005 {:ok, activity} = ActivityPub.follow(follower, followed)
1006 assert activity.data["type"] == "Follow"
1007 assert activity.data["actor"] == follower.ap_id
1008 assert activity.data["object"] == followed.ap_id
1011 test "creates an undo activity for the last follow" do
1012 follower = insert(:user)
1013 followed = insert(:user)
1015 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1016 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1018 assert activity.data["type"] == "Undo"
1019 assert activity.data["actor"] == follower.ap_id
1021 embedded_object = activity.data["object"]
1022 assert is_map(embedded_object)
1023 assert embedded_object["type"] == "Follow"
1024 assert embedded_object["object"] == followed.ap_id
1025 assert embedded_object["id"] == follow_activity.data["id"]
1028 test "creates an undo activity for a pending follow request" do
1029 follower = insert(:user)
1030 followed = insert(:user, %{locked: true})
1032 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1033 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1035 assert activity.data["type"] == "Undo"
1036 assert activity.data["actor"] == follower.ap_id
1038 embedded_object = activity.data["object"]
1039 assert is_map(embedded_object)
1040 assert embedded_object["type"] == "Follow"
1041 assert embedded_object["object"] == followed.ap_id
1042 assert embedded_object["id"] == follow_activity.data["id"]
1046 describe "blocking" do
1047 test "reverts block activity on error" do
1048 [blocker, blocked] = insert_list(2, :user)
1050 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1051 assert {:error, :reverted} = ActivityPub.block(blocker, blocked)
1054 assert Repo.aggregate(Activity, :count, :id) == 0
1055 assert Repo.aggregate(Object, :count, :id) == 0
1058 test "creates a block activity" do
1059 clear_config([:instance, :federating], true)
1060 blocker = insert(:user)
1061 blocked = insert(:user)
1063 with_mock Pleroma.Web.Federator,
1064 publish: fn _ -> nil end do
1065 {:ok, activity} = ActivityPub.block(blocker, blocked)
1067 assert activity.data["type"] == "Block"
1068 assert activity.data["actor"] == blocker.ap_id
1069 assert activity.data["object"] == blocked.ap_id
1071 assert called(Pleroma.Web.Federator.publish(activity))
1075 test "works with outgoing blocks disabled, but doesn't federate" do
1076 clear_config([:instance, :federating], true)
1077 clear_config([:activitypub, :outgoing_blocks], false)
1078 blocker = insert(:user)
1079 blocked = insert(:user)
1081 with_mock Pleroma.Web.Federator,
1082 publish: fn _ -> nil end do
1083 {:ok, activity} = ActivityPub.block(blocker, blocked)
1085 assert activity.data["type"] == "Block"
1086 assert activity.data["actor"] == blocker.ap_id
1087 assert activity.data["object"] == blocked.ap_id
1089 refute called(Pleroma.Web.Federator.publish(:_))
1094 describe "timeline post-processing" do
1095 test "it filters broken threads" do
1096 user1 = insert(:user)
1097 user2 = insert(:user)
1098 user3 = insert(:user)
1100 {:ok, user1} = User.follow(user1, user3)
1101 assert User.following?(user1, user3)
1103 {:ok, user2} = User.follow(user2, user3)
1104 assert User.following?(user2, user3)
1106 {:ok, user3} = User.follow(user3, user2)
1107 assert User.following?(user3, user2)
1109 {:ok, public_activity} = CommonAPI.post(user3, %{status: "hi 1"})
1111 {:ok, private_activity_1} = CommonAPI.post(user3, %{status: "hi 2", visibility: "private"})
1113 {:ok, private_activity_2} =
1114 CommonAPI.post(user2, %{
1116 visibility: "private",
1117 in_reply_to_status_id: private_activity_1.id
1120 {:ok, private_activity_3} =
1121 CommonAPI.post(user3, %{
1123 visibility: "private",
1124 in_reply_to_status_id: private_activity_2.id
1128 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1129 |> Enum.map(fn a -> a.id end)
1131 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1133 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1135 assert length(activities) == 3
1138 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{"user" => user1})
1139 |> Enum.map(fn a -> a.id end)
1141 assert [public_activity.id, private_activity_1.id] == activities
1142 assert length(activities) == 2
1146 describe "update" do
1147 setup do: clear_config([:instance, :max_pinned_statuses])
1149 test "it creates an update activity with the new user data" do
1150 user = insert(:user)
1151 {:ok, user} = User.ensure_keys_present(user)
1152 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1155 ActivityPub.update(%{
1156 actor: user_data["id"],
1157 to: [user.follower_address],
1162 assert update.data["actor"] == user.ap_id
1163 assert update.data["to"] == [user.follower_address]
1164 assert embedded_object = update.data["object"]
1165 assert embedded_object["id"] == user_data["id"]
1166 assert embedded_object["type"] == user_data["type"]
1170 test "returned pinned statuses" do
1171 Config.put([:instance, :max_pinned_statuses], 3)
1172 user = insert(:user)
1174 {:ok, activity_one} = CommonAPI.post(user, %{status: "HI!!!"})
1175 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
1176 {:ok, activity_three} = CommonAPI.post(user, %{status: "HI!!!"})
1178 CommonAPI.pin(activity_one.id, user)
1179 user = refresh_record(user)
1181 CommonAPI.pin(activity_two.id, user)
1182 user = refresh_record(user)
1184 CommonAPI.pin(activity_three.id, user)
1185 user = refresh_record(user)
1187 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1189 assert 3 = length(activities)
1192 describe "flag/1" do
1194 reporter = insert(:user)
1195 target_account = insert(:user)
1197 {:ok, activity} = CommonAPI.post(target_account, %{status: content})
1198 context = Utils.generate_context_id()
1200 reporter_ap_id = reporter.ap_id
1201 target_ap_id = target_account.ap_id
1202 activity_ap_id = activity.data["id"]
1204 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1210 target_account: target_account,
1211 reported_activity: activity,
1213 activity_ap_id: activity_ap_id,
1214 activity_with_object: activity_with_object,
1215 reporter_ap_id: reporter_ap_id,
1216 target_ap_id: target_ap_id
1220 test "it can create a Flag activity",
1224 target_account: target_account,
1225 reported_activity: reported_activity,
1227 activity_ap_id: activity_ap_id,
1228 activity_with_object: activity_with_object,
1229 reporter_ap_id: reporter_ap_id,
1230 target_ap_id: target_ap_id
1232 assert {:ok, activity} =
1236 account: target_account,
1237 statuses: [reported_activity],
1243 "id" => activity_ap_id,
1244 "content" => content,
1245 "published" => activity_with_object.object.data["published"],
1246 "actor" => AccountView.render("show.json", %{user: target_account})
1250 actor: ^reporter_ap_id,
1253 "content" => ^content,
1254 "context" => ^context,
1255 "object" => [^target_ap_id, ^note_obj]
1260 test_with_mock "strips status data from Flag, before federating it",
1264 target_account: target_account,
1265 reported_activity: reported_activity,
1275 account: target_account,
1276 statuses: [reported_activity],
1281 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1283 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1287 test "fetch_activities/2 returns activities addressed to a list " do
1288 user = insert(:user)
1289 member = insert(:user)
1290 {:ok, list} = Pleroma.List.create("foo", user)
1291 {:ok, list} = Pleroma.List.follow(list, member)
1293 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1295 activity = Repo.preload(activity, :bookmark)
1296 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1298 assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity]
1302 File.read!("test/fixtures/avatar_data_uri")
1305 describe "fetch_activities_bounded" do
1306 test "fetches private posts for followed users" do
1307 user = insert(:user)
1310 CommonAPI.post(user, %{
1311 status: "thought I looked cute might delete later :3",
1312 visibility: "private"
1315 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1316 assert result.id == activity.id
1319 test "fetches only public posts for other users" do
1320 user = insert(:user)
1321 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe", visibility: "public"})
1323 {:ok, _private_activity} =
1324 CommonAPI.post(user, %{
1325 status: "why is tenshi eating a corndog so cute?",
1326 visibility: "private"
1329 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1330 assert result.id == activity.id
1334 describe "fetch_follow_information_for_user" do
1335 test "syncronizes following/followers counters" do
1339 follower_address: "http://localhost:4001/users/fuser2/followers",
1340 following_address: "http://localhost:4001/users/fuser2/following"
1343 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1344 assert info.follower_count == 527
1345 assert info.following_count == 267
1348 test "detects hidden followers" do
1351 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1352 %Tesla.Env{status: 403, body: ""}
1355 apply(HttpRequestMock, :request, [env])
1362 follower_address: "http://localhost:4001/users/masto_closed/followers",
1363 following_address: "http://localhost:4001/users/masto_closed/following"
1366 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1367 assert follow_info.hide_followers == true
1368 assert follow_info.hide_follows == false
1371 test "detects hidden follows" do
1374 "http://localhost:4001/users/masto_closed/following?page=1" ->
1375 %Tesla.Env{status: 403, body: ""}
1378 apply(HttpRequestMock, :request, [env])
1385 follower_address: "http://localhost:4001/users/masto_closed/followers",
1386 following_address: "http://localhost:4001/users/masto_closed/following"
1389 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1390 assert follow_info.hide_followers == false
1391 assert follow_info.hide_follows == true
1394 test "detects hidden follows/followers for friendica" do
1398 follower_address: "http://localhost:8080/followers/fuser3",
1399 following_address: "http://localhost:8080/following/fuser3"
1402 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1403 assert follow_info.hide_followers == true
1404 assert follow_info.follower_count == 296
1405 assert follow_info.following_count == 32
1406 assert follow_info.hide_follows == true
1409 test "doesn't crash when follower and following counters are hidden" do
1412 "http://localhost:4001/users/masto_hidden_counters/following" ->
1414 "@context" => "https://www.w3.org/ns/activitystreams",
1415 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1418 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1419 %Tesla.Env{status: 403, body: ""}
1421 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1423 "@context" => "https://www.w3.org/ns/activitystreams",
1424 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1427 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1428 %Tesla.Env{status: 403, body: ""}
1435 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1436 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1439 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1441 assert follow_info.hide_followers == true
1442 assert follow_info.follower_count == 0
1443 assert follow_info.hide_follows == true
1444 assert follow_info.following_count == 0
1448 describe "fetch_favourites/3" do
1449 test "returns a favourite activities sorted by adds to favorite" do
1450 user = insert(:user)
1451 other_user = insert(:user)
1452 user1 = insert(:user)
1453 user2 = insert(:user)
1454 {:ok, a1} = CommonAPI.post(user1, %{status: "bla"})
1455 {:ok, _a2} = CommonAPI.post(user2, %{status: "traps are happy"})
1456 {:ok, a3} = CommonAPI.post(user2, %{status: "Trees Are "})
1457 {:ok, a4} = CommonAPI.post(user2, %{status: "Agent Smith "})
1458 {:ok, a5} = CommonAPI.post(user1, %{status: "Red or Blue "})
1460 {:ok, _} = CommonAPI.favorite(user, a4.id)
1461 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1462 {:ok, _} = CommonAPI.favorite(user, a3.id)
1463 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1464 {:ok, _} = CommonAPI.favorite(user, a5.id)
1465 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1466 {:ok, _} = CommonAPI.favorite(user, a1.id)
1467 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1468 result = ActivityPub.fetch_favourites(user)
1470 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1472 result = ActivityPub.fetch_favourites(user, %{"limit" => 2})
1473 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1477 describe "Move activity" do
1479 %{ap_id: old_ap_id} = old_user = insert(:user)
1480 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1481 follower = insert(:user)
1482 follower_move_opted_out = insert(:user, allow_following_move: false)
1484 User.follow(follower, old_user)
1485 User.follow(follower_move_opted_out, old_user)
1487 assert User.following?(follower, old_user)
1488 assert User.following?(follower_move_opted_out, old_user)
1490 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1495 "actor" => ^old_ap_id,
1496 "object" => ^old_ap_id,
1497 "target" => ^new_ap_id,
1504 "op" => "move_following",
1505 "origin_id" => old_user.id,
1506 "target_id" => new_user.id
1509 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1511 Pleroma.Workers.BackgroundWorker.perform(params, nil)
1513 refute User.following?(follower, old_user)
1514 assert User.following?(follower, new_user)
1516 assert User.following?(follower_move_opted_out, old_user)
1517 refute User.following?(follower_move_opted_out, new_user)
1519 activity = %Activity{activity | object: nil}
1521 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1523 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1526 test "old user must be in the new user's `also_known_as` list" do
1527 old_user = insert(:user)
1528 new_user = insert(:user)
1530 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1531 ActivityPub.move(old_user, new_user)
1535 test "doesn't retrieve replies activities with exclude_replies" do
1536 user = insert(:user)
1538 {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
1540 {:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id})
1542 [result] = ActivityPub.fetch_public_activities(%{"exclude_replies" => "true"})
1544 assert result.id == activity.id
1546 assert length(ActivityPub.fetch_public_activities()) == 2
1549 describe "replies filtering with public messages" do
1550 setup :public_messages
1552 test "public timeline", %{users: %{u1: user}} do
1555 |> Map.put("type", ["Create", "Announce"])
1556 |> Map.put("local_only", false)
1557 |> Map.put("blocking_user", user)
1558 |> Map.put("muting_user", user)
1559 |> Map.put("reply_filtering_user", user)
1560 |> ActivityPub.fetch_public_activities()
1561 |> Enum.map(& &1.id)
1563 assert length(activities_ids) == 16
1566 test "public timeline with reply_visibility `following`", %{
1572 activities: activities
1576 |> Map.put("type", ["Create", "Announce"])
1577 |> Map.put("local_only", false)
1578 |> Map.put("blocking_user", user)
1579 |> Map.put("muting_user", user)
1580 |> Map.put("reply_visibility", "following")
1581 |> Map.put("reply_filtering_user", user)
1582 |> ActivityPub.fetch_public_activities()
1583 |> Enum.map(& &1.id)
1585 assert length(activities_ids) == 14
1588 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1590 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1593 test "public timeline with reply_visibility `self`", %{
1599 activities: activities
1603 |> Map.put("type", ["Create", "Announce"])
1604 |> Map.put("local_only", false)
1605 |> Map.put("blocking_user", user)
1606 |> Map.put("muting_user", user)
1607 |> Map.put("reply_visibility", "self")
1608 |> Map.put("reply_filtering_user", user)
1609 |> ActivityPub.fetch_public_activities()
1610 |> Enum.map(& &1.id)
1612 assert length(activities_ids) == 10
1613 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1614 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1617 test "home timeline", %{
1619 activities: activities,
1627 |> Map.put("type", ["Create", "Announce"])
1628 |> Map.put("blocking_user", user)
1629 |> Map.put("muting_user", user)
1630 |> Map.put("user", user)
1631 |> Map.put("reply_filtering_user", user)
1634 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1635 |> Enum.map(& &1.id)
1637 assert length(activities_ids) == 13
1652 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1655 test "home timeline with reply_visibility `following`", %{
1657 activities: activities,
1665 |> Map.put("type", ["Create", "Announce"])
1666 |> Map.put("blocking_user", user)
1667 |> Map.put("muting_user", user)
1668 |> Map.put("user", user)
1669 |> Map.put("reply_visibility", "following")
1670 |> Map.put("reply_filtering_user", user)
1673 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1674 |> Enum.map(& &1.id)
1676 assert length(activities_ids) == 11
1691 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1694 test "home timeline with reply_visibility `self`", %{
1696 activities: activities,
1704 |> Map.put("type", ["Create", "Announce"])
1705 |> Map.put("blocking_user", user)
1706 |> Map.put("muting_user", user)
1707 |> Map.put("user", user)
1708 |> Map.put("reply_visibility", "self")
1709 |> Map.put("reply_filtering_user", user)
1712 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1713 |> Enum.map(& &1.id)
1715 assert length(activities_ids) == 9
1728 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1732 describe "replies filtering with private messages" do
1733 setup :private_messages
1735 test "public timeline", %{users: %{u1: user}} do
1738 |> Map.put("type", ["Create", "Announce"])
1739 |> Map.put("local_only", false)
1740 |> Map.put("blocking_user", user)
1741 |> Map.put("muting_user", user)
1742 |> Map.put("user", user)
1743 |> ActivityPub.fetch_public_activities()
1744 |> Enum.map(& &1.id)
1746 assert activities_ids == []
1749 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1752 |> Map.put("type", ["Create", "Announce"])
1753 |> Map.put("local_only", false)
1754 |> Map.put("blocking_user", user)
1755 |> Map.put("muting_user", user)
1756 |> Map.put("reply_visibility", "following")
1757 |> Map.put("reply_filtering_user", user)
1758 |> Map.put("user", user)
1759 |> ActivityPub.fetch_public_activities()
1760 |> Enum.map(& &1.id)
1762 assert activities_ids == []
1765 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
1768 |> Map.put("type", ["Create", "Announce"])
1769 |> Map.put("local_only", false)
1770 |> Map.put("blocking_user", user)
1771 |> Map.put("muting_user", user)
1772 |> Map.put("reply_visibility", "self")
1773 |> Map.put("reply_filtering_user", user)
1774 |> Map.put("user", user)
1775 |> ActivityPub.fetch_public_activities()
1776 |> Enum.map(& &1.id)
1778 assert activities_ids == []
1781 test "home timeline", %{users: %{u1: user}} do
1784 |> Map.put("type", ["Create", "Announce"])
1785 |> Map.put("blocking_user", user)
1786 |> Map.put("muting_user", user)
1787 |> Map.put("user", user)
1790 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1791 |> Enum.map(& &1.id)
1793 assert length(activities_ids) == 12
1796 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1799 |> Map.put("type", ["Create", "Announce"])
1800 |> Map.put("blocking_user", user)
1801 |> Map.put("muting_user", user)
1802 |> Map.put("user", user)
1803 |> Map.put("reply_visibility", "following")
1804 |> Map.put("reply_filtering_user", user)
1807 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1808 |> Enum.map(& &1.id)
1810 assert length(activities_ids) == 12
1813 test "home timeline with default reply_visibility `self`", %{
1815 activities: activities,
1823 |> Map.put("type", ["Create", "Announce"])
1824 |> Map.put("blocking_user", user)
1825 |> Map.put("muting_user", user)
1826 |> Map.put("user", user)
1827 |> Map.put("reply_visibility", "self")
1828 |> Map.put("reply_filtering_user", user)
1831 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1832 |> Enum.map(& &1.id)
1834 assert length(activities_ids) == 10
1837 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
1839 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1843 defp public_messages(_) do
1844 [u1, u2, u3, u4] = insert_list(4, :user)
1845 {:ok, u1} = User.follow(u1, u2)
1846 {:ok, u2} = User.follow(u2, u1)
1847 {:ok, u1} = User.follow(u1, u4)
1848 {:ok, u4} = User.follow(u4, u1)
1850 {:ok, u2} = User.follow(u2, u3)
1851 {:ok, u3} = User.follow(u3, u2)
1853 {:ok, a1} = CommonAPI.post(u1, %{status: "Status"})
1856 CommonAPI.post(u2, %{
1857 status: "@#{u1.nickname} reply from u2 to u1",
1858 in_reply_to_status_id: a1.id
1862 CommonAPI.post(u3, %{
1863 status: "@#{u1.nickname} reply from u3 to u1",
1864 in_reply_to_status_id: a1.id
1868 CommonAPI.post(u4, %{
1869 status: "@#{u1.nickname} reply from u4 to u1",
1870 in_reply_to_status_id: a1.id
1873 {:ok, a2} = CommonAPI.post(u2, %{status: "Status"})
1876 CommonAPI.post(u1, %{
1877 status: "@#{u2.nickname} reply from u1 to u2",
1878 in_reply_to_status_id: a2.id
1882 CommonAPI.post(u3, %{
1883 status: "@#{u2.nickname} reply from u3 to u2",
1884 in_reply_to_status_id: a2.id
1888 CommonAPI.post(u4, %{
1889 status: "@#{u2.nickname} reply from u4 to u2",
1890 in_reply_to_status_id: a2.id
1893 {:ok, a3} = CommonAPI.post(u3, %{status: "Status"})
1896 CommonAPI.post(u1, %{
1897 status: "@#{u3.nickname} reply from u1 to u3",
1898 in_reply_to_status_id: a3.id
1902 CommonAPI.post(u2, %{
1903 status: "@#{u3.nickname} reply from u2 to u3",
1904 in_reply_to_status_id: a3.id
1908 CommonAPI.post(u4, %{
1909 status: "@#{u3.nickname} reply from u4 to u3",
1910 in_reply_to_status_id: a3.id
1913 {:ok, a4} = CommonAPI.post(u4, %{status: "Status"})
1916 CommonAPI.post(u1, %{
1917 status: "@#{u4.nickname} reply from u1 to u4",
1918 in_reply_to_status_id: a4.id
1922 CommonAPI.post(u2, %{
1923 status: "@#{u4.nickname} reply from u2 to u4",
1924 in_reply_to_status_id: a4.id
1928 CommonAPI.post(u3, %{
1929 status: "@#{u4.nickname} reply from u3 to u4",
1930 in_reply_to_status_id: a4.id
1934 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
1935 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
1936 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
1937 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
1938 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
1939 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
1942 defp private_messages(_) do
1943 [u1, u2, u3, u4] = insert_list(4, :user)
1944 {:ok, u1} = User.follow(u1, u2)
1945 {:ok, u2} = User.follow(u2, u1)
1946 {:ok, u1} = User.follow(u1, u3)
1947 {:ok, u3} = User.follow(u3, u1)
1948 {:ok, u1} = User.follow(u1, u4)
1949 {:ok, u4} = User.follow(u4, u1)
1951 {:ok, u2} = User.follow(u2, u3)
1952 {:ok, u3} = User.follow(u3, u2)
1954 {:ok, a1} = CommonAPI.post(u1, %{status: "Status", visibility: "private"})
1957 CommonAPI.post(u2, %{
1958 status: "@#{u1.nickname} reply from u2 to u1",
1959 in_reply_to_status_id: a1.id,
1960 visibility: "private"
1964 CommonAPI.post(u3, %{
1965 status: "@#{u1.nickname} reply from u3 to u1",
1966 in_reply_to_status_id: a1.id,
1967 visibility: "private"
1971 CommonAPI.post(u4, %{
1972 status: "@#{u1.nickname} reply from u4 to u1",
1973 in_reply_to_status_id: a1.id,
1974 visibility: "private"
1977 {:ok, a2} = CommonAPI.post(u2, %{status: "Status", visibility: "private"})
1980 CommonAPI.post(u1, %{
1981 status: "@#{u2.nickname} reply from u1 to u2",
1982 in_reply_to_status_id: a2.id,
1983 visibility: "private"
1987 CommonAPI.post(u3, %{
1988 status: "@#{u2.nickname} reply from u3 to u2",
1989 in_reply_to_status_id: a2.id,
1990 visibility: "private"
1993 {:ok, a3} = CommonAPI.post(u3, %{status: "Status", visibility: "private"})
1996 CommonAPI.post(u1, %{
1997 status: "@#{u3.nickname} reply from u1 to u3",
1998 in_reply_to_status_id: a3.id,
1999 visibility: "private"
2003 CommonAPI.post(u2, %{
2004 status: "@#{u3.nickname} reply from u2 to u3",
2005 in_reply_to_status_id: a3.id,
2006 visibility: "private"
2009 {:ok, a4} = CommonAPI.post(u4, %{status: "Status", visibility: "private"})
2012 CommonAPI.post(u1, %{
2013 status: "@#{u4.nickname} reply from u1 to u4",
2014 in_reply_to_status_id: a4.id,
2015 visibility: "private"
2019 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2020 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2021 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2022 u2: %{r1: r2_1.id, r2: r2_2.id},
2023 u3: %{r1: r3_1.id, r2: r3_2.id},
2027 describe "maybe_update_follow_information/1" do
2029 clear_config([:instance, :external_user_synchronization], true)
2033 ap_id: "https://gensokyo.2hu/users/raymoo",
2034 following_address: "https://gensokyo.2hu/users/following",
2035 follower_address: "https://gensokyo.2hu/users/followers",
2042 test "logs an error when it can't fetch the info", %{user: user} do
2043 assert capture_log(fn ->
2044 ActivityPub.maybe_update_follow_information(user)
2045 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2048 test "just returns the input if the user type is Application", %{
2053 |> Map.put(:type, "Application")
2055 refute capture_log(fn ->
2056 assert ^user = ActivityPub.maybe_update_follow_information(user)
2057 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2060 test "it just returns the input if the user has no following/follower addresses", %{
2065 |> Map.put(:following_address, nil)
2066 |> Map.put(:follower_address, nil)
2068 refute capture_log(fn ->
2069 assert ^user = ActivityPub.maybe_update_follow_information(user)
2070 end) =~ "Follower/Following counter update for #{user.ap_id} failed"