1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Notification
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Utils
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.CommonAPI
19 import Pleroma.Factory
24 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 clear_config([:instance, :federating])
30 describe "streaming out participations" do
31 test "it streams them out" do
33 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
35 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
38 conversation.participations
39 |> Repo.preload(:user)
41 with_mock Pleroma.Web.Streamer,
42 stream: fn _, _ -> nil end do
43 ActivityPub.stream_out_participations(conversation.participations)
45 assert called(Pleroma.Web.Streamer.stream("participation", participations))
49 test "streams them out on activity creation" do
50 user_one = insert(:user)
51 user_two = insert(:user)
53 with_mock Pleroma.Web.Streamer,
54 stream: fn _, _ -> nil end do
56 CommonAPI.post(user_one, %{
57 "status" => "@#{user_two.nickname}",
58 "visibility" => "direct"
62 activity.data["context"]
63 |> Pleroma.Conversation.get_for_ap_id()
64 |> Repo.preload(participations: :user)
66 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
71 describe "fetching restricted by visibility" do
72 test "it restricts by the appropriate visibility" do
75 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
77 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
79 {:ok, unlisted_activity} =
80 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
82 {:ok, private_activity} =
83 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} =
124 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
126 {:ok, private_activity} =
127 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
130 ActivityPub.fetch_activities([], %{
131 "exclude_visibilities" => "direct",
132 "actor_id" => user.ap_id
135 assert public_activity in activities
136 assert unlisted_activity in activities
137 assert private_activity in activities
138 refute direct_activity in activities
141 ActivityPub.fetch_activities([], %{
142 "exclude_visibilities" => "unlisted",
143 "actor_id" => user.ap_id
146 assert public_activity in activities
147 refute unlisted_activity in activities
148 assert private_activity in activities
149 assert direct_activity in activities
152 ActivityPub.fetch_activities([], %{
153 "exclude_visibilities" => "private",
154 "actor_id" => user.ap_id
157 assert public_activity in activities
158 assert unlisted_activity in activities
159 refute private_activity in activities
160 assert direct_activity in activities
163 ActivityPub.fetch_activities([], %{
164 "exclude_visibilities" => "public",
165 "actor_id" => user.ap_id
168 refute public_activity in activities
169 assert unlisted_activity in activities
170 assert private_activity in activities
171 assert direct_activity in activities
175 describe "building a user from his ap id" do
176 test "it returns a user" do
177 user_id = "http://mastodon.example.org/users/admin"
178 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
179 assert user.ap_id == user_id
180 assert user.nickname == "admin@mastodon.example.org"
181 assert user.source_data
182 assert user.ap_enabled
183 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
186 test "it returns a user that is invisible" do
187 user_id = "http://mastodon.example.org/users/relay"
188 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
189 assert User.invisible?(user)
192 test "it fetches the appropriate tag-restricted posts" do
195 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
196 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
197 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
199 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
202 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
205 ActivityPub.fetch_activities([], %{
207 "tag" => ["test", "essais"],
208 "tag_reject" => ["reject"]
212 ActivityPub.fetch_activities([], %{
215 "tag_all" => ["test", "reject"]
218 assert fetch_one == [status_one, status_three]
219 assert fetch_two == [status_one, status_two, status_three]
220 assert fetch_three == [status_one, status_two]
221 assert fetch_four == [status_three]
225 describe "insertion" do
226 test "drops activities beyond a certain limit" do
227 limit = Pleroma.Config.get([:instance, :remote_limit])
230 :crypto.strong_rand_bytes(limit + 1)
232 |> binary_part(0, limit + 1)
237 "content" => random_text
241 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
244 test "doesn't drop activities with content being null" do
248 "actor" => user.ap_id,
251 "actor" => user.ap_id,
258 assert {:ok, _} = ActivityPub.insert(data)
261 test "returns the activity if one with the same id is already in" do
262 activity = insert(:note_activity)
263 {:ok, new_activity} = ActivityPub.insert(activity.data)
265 assert activity.id == new_activity.id
268 test "inserts a given map into the activity database, giving it an id if it has none." do
272 "actor" => user.ap_id,
275 "actor" => user.ap_id,
282 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
283 assert activity.data["ok"] == data["ok"]
284 assert is_binary(activity.data["id"])
290 "actor" => user.ap_id,
292 "context" => "blabla",
294 "actor" => user.ap_id,
301 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
302 assert activity.data["ok"] == data["ok"]
303 assert activity.data["id"] == given_id
304 assert activity.data["context"] == "blabla"
305 assert activity.data["context_id"]
308 test "adds a context when none is there" do
312 "actor" => user.ap_id,
315 "actor" => user.ap_id,
322 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
323 object = Pleroma.Object.normalize(activity)
325 assert is_binary(activity.data["context"])
326 assert is_binary(object.data["context"])
327 assert activity.data["context_id"]
328 assert object.data["context_id"]
331 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
335 "actor" => user.ap_id,
338 "actor" => user.ap_id,
345 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
346 assert object = Object.normalize(activity)
347 assert is_binary(object.data["id"])
351 describe "listen activities" do
352 test "does not increase user note count" do
356 ActivityPub.listen(%{
357 to: ["https://www.w3.org/ns/activitystreams#Public"],
361 "actor" => user.ap_id,
362 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
364 "title" => "lain radio episode 1",
370 assert activity.actor == user.ap_id
372 user = User.get_cached_by_id(user.id)
373 assert user.note_count == 0
376 test "can be fetched into a timeline" do
377 _listen_activity_1 = insert(:listen)
378 _listen_activity_2 = insert(:listen)
379 _listen_activity_3 = insert(:listen)
381 timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]})
383 assert length(timeline) == 3
387 describe "create activities" do
388 test "removes doubled 'to' recipients" do
392 ActivityPub.create(%{
393 to: ["user1", "user1", "user2"],
397 "to" => ["user1", "user1", "user2"],
399 "content" => "testing"
403 assert activity.data["to"] == ["user1", "user2"]
404 assert activity.actor == user.ap_id
405 assert activity.recipients == ["user1", "user2", user.ap_id]
408 test "increases user note count only for public activities" do
412 CommonAPI.post(User.get_cached_by_id(user.id), %{
414 "visibility" => "public"
418 CommonAPI.post(User.get_cached_by_id(user.id), %{
420 "visibility" => "unlisted"
424 CommonAPI.post(User.get_cached_by_id(user.id), %{
426 "visibility" => "private"
430 CommonAPI.post(User.get_cached_by_id(user.id), %{
432 "visibility" => "direct"
435 user = User.get_cached_by_id(user.id)
436 assert user.note_count == 2
439 test "increases replies count" do
441 user2 = insert(:user)
443 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
444 ap_id = activity.data["id"]
445 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
448 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
449 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
450 assert object.data["repliesCount"] == 1
453 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
454 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
455 assert object.data["repliesCount"] == 2
458 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
459 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
460 assert object.data["repliesCount"] == 2
463 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
464 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
465 assert object.data["repliesCount"] == 2
469 describe "fetch activities for recipients" do
470 test "retrieve the activities for certain recipients" do
471 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
472 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
473 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
475 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
476 assert length(activities) == 2
477 assert activities == [activity_one, activity_two]
481 describe "fetch activities in context" do
482 test "retrieves activities that have a given context" do
483 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
484 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
485 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
486 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
487 activity_five = insert(:note_activity)
490 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
492 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
493 assert activities == [activity_two, activity]
497 test "doesn't return blocked activities" do
498 activity_one = insert(:note_activity)
499 activity_two = insert(:note_activity)
500 activity_three = insert(:note_activity)
502 booster = insert(:user)
503 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
506 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
508 assert Enum.member?(activities, activity_two)
509 assert Enum.member?(activities, activity_three)
510 refute Enum.member?(activities, activity_one)
512 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
515 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
517 assert Enum.member?(activities, activity_two)
518 assert Enum.member?(activities, activity_three)
519 assert Enum.member?(activities, activity_one)
521 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
522 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
523 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
524 activity_three = Activity.get_by_id(activity_three.id)
527 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
529 assert Enum.member?(activities, activity_two)
530 refute Enum.member?(activities, activity_three)
531 refute Enum.member?(activities, boost_activity)
532 assert Enum.member?(activities, activity_one)
535 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
537 assert Enum.member?(activities, activity_two)
538 assert Enum.member?(activities, activity_three)
539 assert Enum.member?(activities, boost_activity)
540 assert Enum.member?(activities, activity_one)
543 test "doesn't return transitive interactions concerning blocked users" do
544 blocker = insert(:user)
545 blockee = insert(:user)
546 friend = insert(:user)
548 {:ok, _user_relationship} = User.block(blocker, blockee)
550 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
552 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
554 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
556 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
558 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
560 assert Enum.member?(activities, activity_one)
561 refute Enum.member?(activities, activity_two)
562 refute Enum.member?(activities, activity_three)
563 refute Enum.member?(activities, activity_four)
566 test "doesn't return announce activities concerning blocked users" do
567 blocker = insert(:user)
568 blockee = insert(:user)
569 friend = insert(:user)
571 {:ok, _user_relationship} = User.block(blocker, blockee)
573 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
575 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
577 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
580 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
581 |> Enum.map(fn act -> act.id end)
583 assert Enum.member?(activities, activity_one.id)
584 refute Enum.member?(activities, activity_two.id)
585 refute Enum.member?(activities, activity_three.id)
588 test "doesn't return activities from blocked domains" do
589 domain = "dogwhistle.zone"
590 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
591 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
592 activity = insert(:note_activity, %{note: note})
594 {:ok, user} = User.block_domain(user, domain)
597 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
599 refute activity in activities
601 followed_user = insert(:user)
602 ActivityPub.follow(user, followed_user)
603 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
606 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
608 refute repeat_activity in activities
611 test "does return activities from followed users on blocked domains" do
612 domain = "meanies.social"
613 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
614 blocker = insert(:user)
616 {:ok, blocker} = User.follow(blocker, domain_user)
617 {:ok, blocker} = User.block_domain(blocker, domain)
619 assert User.following?(blocker, domain_user)
620 assert User.blocks_domain?(blocker, domain_user)
621 refute User.blocks?(blocker, domain_user)
623 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
624 activity = insert(:note_activity, %{note: note})
627 ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
629 assert activity in activities
631 # And check that if the guy we DO follow boosts someone else from their domain,
632 # that should be hidden
633 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
634 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
635 bad_activity = insert(:note_activity, %{note: bad_note})
636 {:ok, repeat_activity, _} = CommonAPI.repeat(bad_activity.id, domain_user)
639 ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
641 refute repeat_activity in activities
644 test "doesn't return muted activities" do
645 activity_one = insert(:note_activity)
646 activity_two = insert(:note_activity)
647 activity_three = insert(:note_activity)
649 booster = insert(:user)
651 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
652 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
655 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
657 assert Enum.member?(activities, activity_two)
658 assert Enum.member?(activities, activity_three)
659 refute Enum.member?(activities, activity_one)
661 # Calling with 'with_muted' will deliver muted activities, too.
663 ActivityPub.fetch_activities([], %{
664 "muting_user" => user,
665 "with_muted" => true,
666 "skip_preload" => true
669 assert Enum.member?(activities, activity_two)
670 assert Enum.member?(activities, activity_three)
671 assert Enum.member?(activities, activity_one)
673 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
676 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
678 assert Enum.member?(activities, activity_two)
679 assert Enum.member?(activities, activity_three)
680 assert Enum.member?(activities, activity_one)
682 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
683 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
684 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
685 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
686 activity_three = Activity.get_by_id(activity_three.id)
689 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
691 assert Enum.member?(activities, activity_two)
692 refute Enum.member?(activities, activity_three)
693 refute Enum.member?(activities, boost_activity)
694 assert Enum.member?(activities, activity_one)
696 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
698 assert Enum.member?(activities, activity_two)
699 assert Enum.member?(activities, activity_three)
700 assert Enum.member?(activities, boost_activity)
701 assert Enum.member?(activities, activity_one)
704 test "doesn't return thread muted activities" do
706 _activity_one = insert(:note_activity)
707 note_two = insert(:note, data: %{"context" => "suya.."})
708 activity_two = insert(:note_activity, note: note_two)
710 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
712 assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user})
715 test "returns thread muted activities when with_muted is set" do
717 _activity_one = insert(:note_activity)
718 note_two = insert(:note, data: %{"context" => "suya.."})
719 activity_two = insert(:note_activity, note: note_two)
721 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
723 assert [_activity_two, _activity_one] =
724 ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true})
727 test "does include announces on request" do
728 activity_three = insert(:note_activity)
730 booster = insert(:user)
732 {:ok, user} = User.follow(user, booster)
734 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
736 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
738 assert announce_activity.id == announce.id
741 test "excludes reblogs on request" do
743 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
744 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
746 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
748 assert activity == expected_activity
751 describe "public fetch activities" do
752 test "doesn't retrieve unlisted activities" do
755 {:ok, _unlisted_activity} =
756 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
758 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
760 [activity] = ActivityPub.fetch_public_activities()
762 assert activity == listed_activity
765 test "retrieves public activities" do
766 _activities = ActivityPub.fetch_public_activities()
768 %{public: public} = ActivityBuilder.public_and_non_public()
770 activities = ActivityPub.fetch_public_activities()
771 assert length(activities) == 1
772 assert Enum.at(activities, 0) == public
775 test "retrieves a maximum of 20 activities" do
776 ActivityBuilder.insert_list(10)
777 expected_activities = ActivityBuilder.insert_list(20)
779 activities = ActivityPub.fetch_public_activities()
781 assert collect_ids(activities) == collect_ids(expected_activities)
782 assert length(activities) == 20
785 test "retrieves ids starting from a since_id" do
786 activities = ActivityBuilder.insert_list(30)
787 expected_activities = ActivityBuilder.insert_list(10)
788 since_id = List.last(activities).id
790 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
792 assert collect_ids(activities) == collect_ids(expected_activities)
793 assert length(activities) == 10
796 test "retrieves ids up to max_id" do
797 ActivityBuilder.insert_list(10)
798 expected_activities = ActivityBuilder.insert_list(20)
802 |> ActivityBuilder.insert_list()
805 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
807 assert length(activities) == 20
808 assert collect_ids(activities) == collect_ids(expected_activities)
811 test "paginates via offset/limit" do
812 _first_part_activities = ActivityBuilder.insert_list(10)
813 second_part_activities = ActivityBuilder.insert_list(10)
815 later_activities = ActivityBuilder.insert_list(10)
818 ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
820 assert length(activities) == 20
822 assert collect_ids(activities) ==
823 collect_ids(second_part_activities) ++ collect_ids(later_activities)
826 test "doesn't return reblogs for users for whom reblogs have been muted" do
827 activity = insert(:note_activity)
829 booster = insert(:user)
830 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
832 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
834 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
836 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
839 test "returns reblogs for users for whom reblogs have not been muted" do
840 activity = insert(:note_activity)
842 booster = insert(:user)
843 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
844 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
846 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
848 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
850 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
854 describe "react to an object" do
855 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
856 Pleroma.Config.put([:instance, :federating], true)
858 reactor = insert(:user)
859 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
860 assert object = Object.normalize(activity)
862 {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
864 assert called(Pleroma.Web.Federator.publish(reaction_activity))
867 test "adds an emoji reaction activity to the db" do
869 reactor = insert(:user)
870 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
871 assert object = Object.normalize(activity)
873 {:ok, reaction_activity, object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
875 assert reaction_activity
877 assert reaction_activity.data["actor"] == reactor.ap_id
878 assert reaction_activity.data["type"] == "EmojiReaction"
879 assert reaction_activity.data["content"] == "🔥"
880 assert reaction_activity.data["object"] == object.data["id"]
881 assert reaction_activity.data["to"] == [User.ap_followers(reactor), activity.data["actor"]]
882 assert reaction_activity.data["context"] == object.data["context"]
883 assert object.data["reaction_count"] == 1
884 assert object.data["reactions"]["🔥"] == [reactor.ap_id]
888 describe "unreacting to an object" do
889 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
890 Pleroma.Config.put([:instance, :federating], true)
892 reactor = insert(:user)
893 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
894 assert object = Object.normalize(activity)
896 {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
898 assert called(Pleroma.Web.Federator.publish(reaction_activity))
900 {:ok, unreaction_activity, _object} =
901 ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"])
903 assert called(Pleroma.Web.Federator.publish(unreaction_activity))
906 test "adds an undo activity to the db" do
908 reactor = insert(:user)
909 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
910 assert object = Object.normalize(activity)
912 {:ok, reaction_activity, _object} = ActivityPub.react_with_emoji(reactor, object, "🔥")
914 {:ok, unreaction_activity, _object} =
915 ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"])
917 assert unreaction_activity.actor == reactor.ap_id
918 assert unreaction_activity.data["object"] == reaction_activity.data["id"]
920 object = Object.get_by_ap_id(object.data["id"])
921 assert object.data["reaction_count"] == 0
922 assert object.data["reactions"] == %{}
926 describe "like an object" do
927 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
928 Pleroma.Config.put([:instance, :federating], true)
929 note_activity = insert(:note_activity)
930 assert object_activity = Object.normalize(note_activity)
934 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
935 assert called(Pleroma.Web.Federator.publish(like_activity))
938 test "returns exist activity if object already liked" do
939 note_activity = insert(:note_activity)
940 assert object_activity = Object.normalize(note_activity)
944 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
946 {:ok, like_activity_exist, _object} = ActivityPub.like(user, object_activity)
947 assert like_activity == like_activity_exist
950 test "adds a like activity to the db" do
951 note_activity = insert(:note_activity)
952 assert object = Object.normalize(note_activity)
955 user_two = insert(:user)
957 {:ok, like_activity, object} = ActivityPub.like(user, object)
959 assert like_activity.data["actor"] == user.ap_id
960 assert like_activity.data["type"] == "Like"
961 assert like_activity.data["object"] == object.data["id"]
962 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
963 assert like_activity.data["context"] == object.data["context"]
964 assert object.data["like_count"] == 1
965 assert object.data["likes"] == [user.ap_id]
967 # Just return the original activity if the user already liked it.
968 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
970 assert like_activity == same_like_activity
971 assert object.data["likes"] == [user.ap_id]
972 assert object.data["like_count"] == 1
974 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
975 assert object.data["like_count"] == 2
979 describe "unliking" do
980 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
981 Pleroma.Config.put([:instance, :federating], true)
983 note_activity = insert(:note_activity)
984 object = Object.normalize(note_activity)
987 {:ok, object} = ActivityPub.unlike(user, object)
988 refute called(Pleroma.Web.Federator.publish())
990 {:ok, _like_activity, object} = ActivityPub.like(user, object)
991 assert object.data["like_count"] == 1
993 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
994 assert object.data["like_count"] == 0
996 assert called(Pleroma.Web.Federator.publish(unlike_activity))
999 test "unliking a previously liked object" do
1000 note_activity = insert(:note_activity)
1001 object = Object.normalize(note_activity)
1002 user = insert(:user)
1004 # Unliking something that hasn't been liked does nothing
1005 {:ok, object} = ActivityPub.unlike(user, object)
1006 assert object.data["like_count"] == 0
1008 {:ok, like_activity, object} = ActivityPub.like(user, object)
1009 assert object.data["like_count"] == 1
1011 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
1012 assert object.data["like_count"] == 0
1014 assert Activity.get_by_id(like_activity.id) == nil
1015 assert note_activity.actor in unlike_activity.recipients
1019 describe "announcing an object" do
1020 test "adds an announce activity to the db" do
1021 note_activity = insert(:note_activity)
1022 object = Object.normalize(note_activity)
1023 user = insert(:user)
1025 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
1026 assert object.data["announcement_count"] == 1
1027 assert object.data["announcements"] == [user.ap_id]
1029 assert announce_activity.data["to"] == [
1030 User.ap_followers(user),
1031 note_activity.data["actor"]
1034 assert announce_activity.data["object"] == object.data["id"]
1035 assert announce_activity.data["actor"] == user.ap_id
1036 assert announce_activity.data["context"] == object.data["context"]
1040 describe "announcing a private object" do
1041 test "adds an announce activity to the db if the audience is not widened" do
1042 user = insert(:user)
1043 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1044 object = Object.normalize(note_activity)
1046 {:ok, announce_activity, object} = ActivityPub.announce(user, object, nil, true, false)
1048 assert announce_activity.data["to"] == [User.ap_followers(user)]
1050 assert announce_activity.data["object"] == object.data["id"]
1051 assert announce_activity.data["actor"] == user.ap_id
1052 assert announce_activity.data["context"] == object.data["context"]
1055 test "does not add an announce activity to the db if the audience is widened" do
1056 user = insert(:user)
1057 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1058 object = Object.normalize(note_activity)
1060 assert {:error, _} = ActivityPub.announce(user, object, nil, true, true)
1063 test "does not add an announce activity to the db if the announcer is not the author" do
1064 user = insert(:user)
1065 announcer = insert(:user)
1066 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1067 object = Object.normalize(note_activity)
1069 assert {:error, _} = ActivityPub.announce(announcer, object, nil, true, false)
1073 describe "unannouncing an object" do
1074 test "unannouncing a previously announced object" do
1075 note_activity = insert(:note_activity)
1076 object = Object.normalize(note_activity)
1077 user = insert(:user)
1079 # Unannouncing an object that is not announced does nothing
1080 # {:ok, object} = ActivityPub.unannounce(user, object)
1081 # assert object.data["announcement_count"] == 0
1083 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
1084 assert object.data["announcement_count"] == 1
1086 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
1087 assert object.data["announcement_count"] == 0
1089 assert unannounce_activity.data["to"] == [
1090 User.ap_followers(user),
1091 object.data["actor"]
1094 assert unannounce_activity.data["type"] == "Undo"
1095 assert unannounce_activity.data["object"] == announce_activity.data
1096 assert unannounce_activity.data["actor"] == user.ap_id
1097 assert unannounce_activity.data["context"] == announce_activity.data["context"]
1099 assert Activity.get_by_id(announce_activity.id) == nil
1103 describe "uploading files" do
1104 test "copies the file to the configured folder" do
1105 file = %Plug.Upload{
1106 content_type: "image/jpg",
1107 path: Path.absname("test/fixtures/image.jpg"),
1108 filename: "an_image.jpg"
1111 {:ok, %Object{} = object} = ActivityPub.upload(file)
1112 assert object.data["name"] == "an_image.jpg"
1115 test "works with base64 encoded images" do
1120 {:ok, %Object{}} = ActivityPub.upload(file)
1124 describe "fetch the latest Follow" do
1125 test "fetches the latest Follow activity" do
1126 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1127 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1128 followed = Repo.get_by(User, ap_id: activity.data["object"])
1130 assert activity == Utils.fetch_latest_follow(follower, followed)
1134 describe "following / unfollowing" do
1135 test "creates a follow activity" do
1136 follower = insert(:user)
1137 followed = insert(:user)
1139 {:ok, activity} = ActivityPub.follow(follower, followed)
1140 assert activity.data["type"] == "Follow"
1141 assert activity.data["actor"] == follower.ap_id
1142 assert activity.data["object"] == followed.ap_id
1145 test "creates an undo activity for the last follow" do
1146 follower = insert(:user)
1147 followed = insert(:user)
1149 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1150 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1152 assert activity.data["type"] == "Undo"
1153 assert activity.data["actor"] == follower.ap_id
1155 embedded_object = activity.data["object"]
1156 assert is_map(embedded_object)
1157 assert embedded_object["type"] == "Follow"
1158 assert embedded_object["object"] == followed.ap_id
1159 assert embedded_object["id"] == follow_activity.data["id"]
1163 describe "blocking / unblocking" do
1164 test "creates a block activity" do
1165 blocker = insert(:user)
1166 blocked = insert(:user)
1168 {:ok, activity} = ActivityPub.block(blocker, blocked)
1170 assert activity.data["type"] == "Block"
1171 assert activity.data["actor"] == blocker.ap_id
1172 assert activity.data["object"] == blocked.ap_id
1175 test "creates an undo activity for the last block" do
1176 blocker = insert(:user)
1177 blocked = insert(:user)
1179 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
1180 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
1182 assert activity.data["type"] == "Undo"
1183 assert activity.data["actor"] == blocker.ap_id
1185 embedded_object = activity.data["object"]
1186 assert is_map(embedded_object)
1187 assert embedded_object["type"] == "Block"
1188 assert embedded_object["object"] == blocked.ap_id
1189 assert embedded_object["id"] == block_activity.data["id"]
1193 describe "deletion" do
1194 test "it creates a delete activity and deletes the original object" do
1195 note = insert(:note_activity)
1196 object = Object.normalize(note)
1197 {:ok, delete} = ActivityPub.delete(object)
1199 assert delete.data["type"] == "Delete"
1200 assert delete.data["actor"] == note.data["actor"]
1201 assert delete.data["object"] == object.data["id"]
1203 assert Activity.get_by_id(delete.id) != nil
1205 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
1208 test "decrements user note count only for public activities" do
1209 user = insert(:user, note_count: 10)
1212 CommonAPI.post(User.get_cached_by_id(user.id), %{
1214 "visibility" => "public"
1218 CommonAPI.post(User.get_cached_by_id(user.id), %{
1220 "visibility" => "unlisted"
1224 CommonAPI.post(User.get_cached_by_id(user.id), %{
1226 "visibility" => "private"
1230 CommonAPI.post(User.get_cached_by_id(user.id), %{
1232 "visibility" => "direct"
1235 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
1236 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
1237 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
1238 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
1240 user = User.get_cached_by_id(user.id)
1241 assert user.note_count == 10
1244 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
1245 user = insert(:user)
1246 note = insert(:note_activity)
1247 object = Object.normalize(note)
1253 "actor" => object.data["actor"],
1254 "id" => object.data["id"],
1255 "to" => [user.ap_id],
1259 |> Object.update_and_set_cache()
1261 {:ok, delete} = ActivityPub.delete(object)
1263 assert user.ap_id in delete.data["to"]
1266 test "decreases reply count" do
1267 user = insert(:user)
1268 user2 = insert(:user)
1270 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
1271 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
1272 ap_id = activity.data["id"]
1274 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
1275 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
1276 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
1277 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
1279 _ = CommonAPI.delete(direct_reply.id, user2)
1280 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1281 assert object.data["repliesCount"] == 2
1283 _ = CommonAPI.delete(private_reply.id, user2)
1284 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1285 assert object.data["repliesCount"] == 2
1287 _ = CommonAPI.delete(public_reply.id, user2)
1288 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1289 assert object.data["repliesCount"] == 1
1291 _ = CommonAPI.delete(unlisted_reply.id, user2)
1292 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1293 assert object.data["repliesCount"] == 0
1296 test "it passes delete activity through MRF before deleting the object" do
1297 rewrite_policy = Pleroma.Config.get([:instance, :rewrite_policy])
1298 Pleroma.Config.put([:instance, :rewrite_policy], Pleroma.Web.ActivityPub.MRF.DropPolicy)
1300 on_exit(fn -> Pleroma.Config.put([:instance, :rewrite_policy], rewrite_policy) end)
1302 note = insert(:note_activity)
1303 object = Object.normalize(note)
1305 {:error, {:reject, _}} = ActivityPub.delete(object)
1307 assert Activity.get_by_id(note.id)
1308 assert Repo.get(Object, object.id).data["type"] == object.data["type"]
1312 describe "timeline post-processing" do
1313 test "it filters broken threads" do
1314 user1 = insert(:user)
1315 user2 = insert(:user)
1316 user3 = insert(:user)
1318 {:ok, user1} = User.follow(user1, user3)
1319 assert User.following?(user1, user3)
1321 {:ok, user2} = User.follow(user2, user3)
1322 assert User.following?(user2, user3)
1324 {:ok, user3} = User.follow(user3, user2)
1325 assert User.following?(user3, user2)
1327 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
1329 {:ok, private_activity_1} =
1330 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
1332 {:ok, private_activity_2} =
1333 CommonAPI.post(user2, %{
1335 "visibility" => "private",
1336 "in_reply_to_status_id" => private_activity_1.id
1339 {:ok, private_activity_3} =
1340 CommonAPI.post(user3, %{
1342 "visibility" => "private",
1343 "in_reply_to_status_id" => private_activity_2.id
1347 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1348 |> Enum.map(fn a -> a.id end)
1350 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1352 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1354 assert length(activities) == 3
1357 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{"user" => user1})
1358 |> Enum.map(fn a -> a.id end)
1360 assert [public_activity.id, private_activity_1.id] == activities
1361 assert length(activities) == 2
1365 describe "update" do
1366 test "it creates an update activity with the new user data" do
1367 user = insert(:user)
1368 {:ok, user} = User.ensure_keys_present(user)
1369 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1372 ActivityPub.update(%{
1373 actor: user_data["id"],
1374 to: [user.follower_address],
1379 assert update.data["actor"] == user.ap_id
1380 assert update.data["to"] == [user.follower_address]
1381 assert embedded_object = update.data["object"]
1382 assert embedded_object["id"] == user_data["id"]
1383 assert embedded_object["type"] == user_data["type"]
1387 test "returned pinned statuses" do
1388 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
1389 user = insert(:user)
1391 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1392 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1393 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1395 CommonAPI.pin(activity_one.id, user)
1396 user = refresh_record(user)
1398 CommonAPI.pin(activity_two.id, user)
1399 user = refresh_record(user)
1401 CommonAPI.pin(activity_three.id, user)
1402 user = refresh_record(user)
1404 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1406 assert 3 = length(activities)
1409 describe "flag/1" do
1411 reporter = insert(:user)
1412 target_account = insert(:user)
1414 {:ok, activity} = CommonAPI.post(target_account, %{"status" => content})
1415 context = Utils.generate_context_id()
1417 reporter_ap_id = reporter.ap_id
1418 target_ap_id = target_account.ap_id
1419 activity_ap_id = activity.data["id"]
1421 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1427 target_account: target_account,
1428 reported_activity: activity,
1430 activity_ap_id: activity_ap_id,
1431 activity_with_object: activity_with_object,
1432 reporter_ap_id: reporter_ap_id,
1433 target_ap_id: target_ap_id
1437 test "it can create a Flag activity",
1441 target_account: target_account,
1442 reported_activity: reported_activity,
1444 activity_ap_id: activity_ap_id,
1445 activity_with_object: activity_with_object,
1446 reporter_ap_id: reporter_ap_id,
1447 target_ap_id: target_ap_id
1449 assert {:ok, activity} =
1453 account: target_account,
1454 statuses: [reported_activity],
1460 "id" => activity_ap_id,
1461 "content" => content,
1462 "published" => activity_with_object.object.data["published"],
1463 "actor" => AccountView.render("show.json", %{user: target_account})
1467 actor: ^reporter_ap_id,
1470 "content" => ^content,
1471 "context" => ^context,
1472 "object" => [^target_ap_id, ^note_obj]
1477 test_with_mock "strips status data from Flag, before federating it",
1481 target_account: target_account,
1482 reported_activity: reported_activity,
1492 account: target_account,
1493 statuses: [reported_activity],
1498 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1500 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1504 test "fetch_activities/2 returns activities addressed to a list " do
1505 user = insert(:user)
1506 member = insert(:user)
1507 {:ok, list} = Pleroma.List.create("foo", user)
1508 {:ok, list} = Pleroma.List.follow(list, member)
1511 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1513 activity = Repo.preload(activity, :bookmark)
1514 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1516 assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity]
1520 File.read!("test/fixtures/avatar_data_uri")
1523 describe "fetch_activities_bounded" do
1524 test "fetches private posts for followed users" do
1525 user = insert(:user)
1528 CommonAPI.post(user, %{
1529 "status" => "thought I looked cute might delete later :3",
1530 "visibility" => "private"
1533 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1534 assert result.id == activity.id
1537 test "fetches only public posts for other users" do
1538 user = insert(:user)
1539 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe", "visibility" => "public"})
1541 {:ok, _private_activity} =
1542 CommonAPI.post(user, %{
1543 "status" => "why is tenshi eating a corndog so cute?",
1544 "visibility" => "private"
1547 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1548 assert result.id == activity.id
1552 describe "fetch_follow_information_for_user" do
1553 test "syncronizes following/followers counters" do
1557 follower_address: "http://localhost:4001/users/fuser2/followers",
1558 following_address: "http://localhost:4001/users/fuser2/following"
1561 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1562 assert info.follower_count == 527
1563 assert info.following_count == 267
1566 test "detects hidden followers" do
1569 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1570 %Tesla.Env{status: 403, body: ""}
1573 apply(HttpRequestMock, :request, [env])
1580 follower_address: "http://localhost:4001/users/masto_closed/followers",
1581 following_address: "http://localhost:4001/users/masto_closed/following"
1584 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1585 assert follow_info.hide_followers == true
1586 assert follow_info.hide_follows == false
1589 test "detects hidden follows" do
1592 "http://localhost:4001/users/masto_closed/following?page=1" ->
1593 %Tesla.Env{status: 403, body: ""}
1596 apply(HttpRequestMock, :request, [env])
1603 follower_address: "http://localhost:4001/users/masto_closed/followers",
1604 following_address: "http://localhost:4001/users/masto_closed/following"
1607 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1608 assert follow_info.hide_followers == false
1609 assert follow_info.hide_follows == true
1612 test "detects hidden follows/followers for friendica" do
1616 follower_address: "http://localhost:8080/followers/fuser3",
1617 following_address: "http://localhost:8080/following/fuser3"
1620 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1621 assert follow_info.hide_followers == true
1622 assert follow_info.follower_count == 296
1623 assert follow_info.following_count == 32
1624 assert follow_info.hide_follows == true
1627 test "doesn't crash when follower and following counters are hidden" do
1630 "http://localhost:4001/users/masto_hidden_counters/following" ->
1632 "@context" => "https://www.w3.org/ns/activitystreams",
1633 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1636 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1637 %Tesla.Env{status: 403, body: ""}
1639 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1641 "@context" => "https://www.w3.org/ns/activitystreams",
1642 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1645 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1646 %Tesla.Env{status: 403, body: ""}
1653 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1654 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1657 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1659 assert follow_info.hide_followers == true
1660 assert follow_info.follower_count == 0
1661 assert follow_info.hide_follows == true
1662 assert follow_info.following_count == 0
1666 describe "fetch_favourites/3" do
1667 test "returns a favourite activities sorted by adds to favorite" do
1668 user = insert(:user)
1669 other_user = insert(:user)
1670 user1 = insert(:user)
1671 user2 = insert(:user)
1672 {:ok, a1} = CommonAPI.post(user1, %{"status" => "bla"})
1673 {:ok, _a2} = CommonAPI.post(user2, %{"status" => "traps are happy"})
1674 {:ok, a3} = CommonAPI.post(user2, %{"status" => "Trees Are "})
1675 {:ok, a4} = CommonAPI.post(user2, %{"status" => "Agent Smith "})
1676 {:ok, a5} = CommonAPI.post(user1, %{"status" => "Red or Blue "})
1678 {:ok, _, _} = CommonAPI.favorite(a4.id, user)
1679 {:ok, _, _} = CommonAPI.favorite(a3.id, other_user)
1680 {:ok, _, _} = CommonAPI.favorite(a3.id, user)
1681 {:ok, _, _} = CommonAPI.favorite(a5.id, other_user)
1682 {:ok, _, _} = CommonAPI.favorite(a5.id, user)
1683 {:ok, _, _} = CommonAPI.favorite(a4.id, other_user)
1684 {:ok, _, _} = CommonAPI.favorite(a1.id, user)
1685 {:ok, _, _} = CommonAPI.favorite(a1.id, other_user)
1686 result = ActivityPub.fetch_favourites(user)
1688 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1690 result = ActivityPub.fetch_favourites(user, %{"limit" => 2})
1691 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1695 describe "Move activity" do
1697 %{ap_id: old_ap_id} = old_user = insert(:user)
1698 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1699 follower = insert(:user)
1700 follower_move_opted_out = insert(:user, allow_following_move: false)
1702 User.follow(follower, old_user)
1703 User.follow(follower_move_opted_out, old_user)
1705 assert User.following?(follower, old_user)
1706 assert User.following?(follower_move_opted_out, old_user)
1708 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1713 "actor" => ^old_ap_id,
1714 "object" => ^old_ap_id,
1715 "target" => ^new_ap_id,
1722 "op" => "move_following",
1723 "origin_id" => old_user.id,
1724 "target_id" => new_user.id
1727 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1729 Pleroma.Workers.BackgroundWorker.perform(params, nil)
1731 refute User.following?(follower, old_user)
1732 assert User.following?(follower, new_user)
1734 assert User.following?(follower_move_opted_out, old_user)
1735 refute User.following?(follower_move_opted_out, new_user)
1737 activity = %Activity{activity | object: nil}
1739 assert [%Notification{activity: ^activity}] =
1740 Notification.for_user(follower, %{with_move: true})
1742 assert [%Notification{activity: ^activity}] =
1743 Notification.for_user(follower_move_opted_out, %{with_move: true})
1746 test "old user must be in the new user's `also_known_as` list" do
1747 old_user = insert(:user)
1748 new_user = insert(:user)
1750 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1751 ActivityPub.move(old_user, new_user)