1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Builders.ActivityBuilder
12 alias Pleroma.Notification
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.AdminAPI.AccountView
18 alias Pleroma.Web.CommonAPI
20 import ExUnit.CaptureLog
22 import Pleroma.Factory
26 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
30 setup do: clear_config([:instance, :federating])
32 describe "streaming out participations" do
33 test "it streams them out" do
35 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
37 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
40 conversation.participations
41 |> Repo.preload(:user)
43 with_mock Pleroma.Web.Streamer,
44 stream: fn _, _ -> nil end do
45 ActivityPub.stream_out_participations(conversation.participations)
47 assert called(Pleroma.Web.Streamer.stream("participation", participations))
51 test "streams them out on activity creation" do
52 user_one = insert(:user)
53 user_two = insert(:user)
55 with_mock Pleroma.Web.Streamer,
56 stream: fn _, _ -> nil end do
58 CommonAPI.post(user_one, %{
59 status: "@#{user_two.nickname}",
64 activity.data["context"]
65 |> Pleroma.Conversation.get_for_ap_id()
66 |> Repo.preload(participations: :user)
68 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
73 describe "fetching restricted by visibility" do
74 test "it restricts by the appropriate visibility" do
77 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
79 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
81 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
83 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
85 activities = ActivityPub.fetch_activities([], %{visibility: "direct", actor_id: user.ap_id})
87 assert activities == [direct_activity]
90 ActivityPub.fetch_activities([], %{visibility: "unlisted", actor_id: user.ap_id})
92 assert activities == [unlisted_activity]
95 ActivityPub.fetch_activities([], %{visibility: "private", actor_id: user.ap_id})
97 assert activities == [private_activity]
99 activities = ActivityPub.fetch_activities([], %{visibility: "public", actor_id: user.ap_id})
101 assert activities == [public_activity]
104 ActivityPub.fetch_activities([], %{
105 visibility: ~w[private public],
109 assert activities == [public_activity, private_activity]
113 describe "fetching excluded by visibility" do
114 test "it excludes by the appropriate visibility" do
117 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
119 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
121 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
123 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
126 ActivityPub.fetch_activities([], %{
127 exclude_visibilities: "direct",
131 assert public_activity in activities
132 assert unlisted_activity in activities
133 assert private_activity in activities
134 refute direct_activity in activities
137 ActivityPub.fetch_activities([], %{
138 exclude_visibilities: "unlisted",
142 assert public_activity in activities
143 refute unlisted_activity in activities
144 assert private_activity in activities
145 assert direct_activity in activities
148 ActivityPub.fetch_activities([], %{
149 exclude_visibilities: "private",
153 assert public_activity in activities
154 assert unlisted_activity in activities
155 refute private_activity in activities
156 assert direct_activity in activities
159 ActivityPub.fetch_activities([], %{
160 exclude_visibilities: "public",
164 refute public_activity in activities
165 assert unlisted_activity in activities
166 assert private_activity in activities
167 assert direct_activity in activities
171 describe "building a user from his ap id" do
172 test "it returns a user" do
173 user_id = "http://mastodon.example.org/users/admin"
174 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
175 assert user.ap_id == user_id
176 assert user.nickname == "admin@mastodon.example.org"
177 assert user.ap_enabled
178 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
181 test "it returns a user that is invisible" do
182 user_id = "http://mastodon.example.org/users/relay"
183 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
184 assert User.invisible?(user)
187 test "it returns a user that accepts chat messages" do
188 user_id = "http://mastodon.example.org/users/admin"
189 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
191 assert user.accepts_chat_messages
194 test "works for guppe actors" do
195 user_id = "https://gup.pe/u/bernie2020"
198 %{method: :get, url: ^user_id} ->
201 body: File.read!("test/fixtures/guppe-actor.json"),
202 headers: [{"content-type", "application/activity+json"}]
206 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
208 assert user.name == "Bernie2020 group"
209 assert user.actor_type == "Group"
213 test "it fetches the appropriate tag-restricted posts" do
216 {:ok, status_one} = CommonAPI.post(user, %{status: ". #TEST"})
217 {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
218 {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #Reject"})
220 {:ok, status_four} = CommonAPI.post(user, %{status: ". #Any1 #any2"})
221 {:ok, status_five} = CommonAPI.post(user, %{status: ". #Any2 #any1"})
223 for hashtag_timeline_strategy <- [true, false] do
224 clear_config([:database, :improved_hashtag_timeline], hashtag_timeline_strategy)
226 fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"})
228 fetch_two = ActivityPub.fetch_activities([], %{type: "Create", tag: ["TEST", "essais"]})
231 ActivityPub.fetch_activities([], %{
233 tag: ["test", "Essais"],
234 tag_reject: ["reject"]
238 ActivityPub.fetch_activities([], %{
241 tag_all: ["test", "REJECT"]
244 # Testing that deduplication (if needed) is done on DB (not Ecto) level; :limit is important
246 ActivityPub.fetch_activities([], %{
248 tag: ["ANY1", "any2"],
253 ActivityPub.fetch_activities([], %{
255 tag: ["any1", "Any2"],
260 # Regression test: passing empty lists as filter options shouldn't affect the results
261 assert fetch_five == fetch_six
263 [fetch_one, fetch_two, fetch_three, fetch_four, fetch_five] =
264 Enum.map([fetch_one, fetch_two, fetch_three, fetch_four, fetch_five], fn statuses ->
265 Enum.map(statuses, fn s -> Repo.preload(s, object: :hashtags) end)
268 assert fetch_one == [status_one, status_three]
269 assert fetch_two == [status_one, status_two, status_three]
270 assert fetch_three == [status_one, status_two]
271 assert fetch_four == [status_three]
272 assert fetch_five == [status_four, status_five]
276 describe "insertion" do
277 test "drops activities beyond a certain limit" do
278 limit = Config.get([:instance, :remote_limit])
281 :crypto.strong_rand_bytes(limit + 1)
283 |> binary_part(0, limit + 1)
288 "content" => random_text
292 assert {:error, :remote_limit} = ActivityPub.insert(data)
295 test "doesn't drop activities with content being null" do
299 "actor" => user.ap_id,
302 "actor" => user.ap_id,
309 assert {:ok, _} = ActivityPub.insert(data)
312 test "returns the activity if one with the same id is already in" do
313 activity = insert(:note_activity)
314 {:ok, new_activity} = ActivityPub.insert(activity.data)
316 assert activity.id == new_activity.id
319 test "inserts a given map into the activity database, giving it an id if it has none." do
323 "actor" => user.ap_id,
326 "actor" => user.ap_id,
333 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
334 assert activity.data["ok"] == data["ok"]
335 assert is_binary(activity.data["id"])
341 "actor" => user.ap_id,
343 "context" => "blabla",
345 "actor" => user.ap_id,
352 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
353 assert activity.data["ok"] == data["ok"]
354 assert activity.data["id"] == given_id
355 assert activity.data["context"] == "blabla"
356 assert activity.data["context_id"]
359 test "adds a context when none is there" do
363 "actor" => user.ap_id,
366 "actor" => user.ap_id,
373 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
374 object = Pleroma.Object.normalize(activity, fetch: false)
376 assert is_binary(activity.data["context"])
377 assert is_binary(object.data["context"])
378 assert activity.data["context_id"]
379 assert object.data["context_id"]
382 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
386 "actor" => user.ap_id,
389 "actor" => user.ap_id,
396 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
397 assert object = Object.normalize(activity, fetch: false)
398 assert is_binary(object.data["id"])
402 describe "listen activities" do
403 test "does not increase user note count" do
407 ActivityPub.listen(%{
408 to: ["https://www.w3.org/ns/activitystreams#Public"],
412 "actor" => user.ap_id,
413 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
415 "title" => "lain radio episode 1",
421 assert activity.actor == user.ap_id
423 user = User.get_cached_by_id(user.id)
424 assert user.note_count == 0
427 test "can be fetched into a timeline" do
428 _listen_activity_1 = insert(:listen)
429 _listen_activity_2 = insert(:listen)
430 _listen_activity_3 = insert(:listen)
432 timeline = ActivityPub.fetch_activities([], %{type: ["Listen"]})
434 assert length(timeline) == 3
438 describe "create activities" do
440 [user: insert(:user)]
443 test "it reverts create", %{user: user} do
444 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
445 assert {:error, :reverted} =
446 ActivityPub.create(%{
447 to: ["user1", "user2"],
451 "to" => ["user1", "user2"],
453 "content" => "testing"
458 assert Repo.aggregate(Activity, :count, :id) == 0
459 assert Repo.aggregate(Object, :count, :id) == 0
462 test "creates activity if expiration is not configured and expires_at is not passed", %{
465 clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
468 ActivityPub.create(%{
469 to: ["user1", "user2"],
473 "to" => ["user1", "user2"],
475 "content" => "testing"
480 test "rejects activity if expires_at present but expiration is not configured", %{user: user} do
481 clear_config([Pleroma.Workers.PurgeExpiredActivity, :enabled], false)
483 assert {:error, :expired_activities_disabled} =
484 ActivityPub.create(%{
485 to: ["user1", "user2"],
489 "to" => ["user1", "user2"],
491 "content" => "testing"
494 "expires_at" => DateTime.utc_now()
498 assert Repo.aggregate(Activity, :count, :id) == 0
499 assert Repo.aggregate(Object, :count, :id) == 0
502 test "removes doubled 'to' recipients", %{user: user} do
504 ActivityPub.create(%{
505 to: ["user1", "user1", "user2"],
509 "to" => ["user1", "user1", "user2"],
511 "content" => "testing"
515 assert activity.data["to"] == ["user1", "user2"]
516 assert activity.actor == user.ap_id
517 assert activity.recipients == ["user1", "user2", user.ap_id]
520 test "increases user note count only for public activities", %{user: user} do
522 CommonAPI.post(User.get_cached_by_id(user.id), %{
528 CommonAPI.post(User.get_cached_by_id(user.id), %{
530 visibility: "unlisted"
534 CommonAPI.post(User.get_cached_by_id(user.id), %{
536 visibility: "private"
540 CommonAPI.post(User.get_cached_by_id(user.id), %{
545 user = User.get_cached_by_id(user.id)
546 assert user.note_count == 2
549 test "increases replies count", %{user: user} do
550 user2 = insert(:user)
552 {:ok, activity} = CommonAPI.post(user, %{status: "1", visibility: "public"})
553 ap_id = activity.data["id"]
554 reply_data = %{status: "1", in_reply_to_status_id: activity.id}
557 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "public"))
558 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
559 assert object.data["repliesCount"] == 1
562 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "unlisted"))
563 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
564 assert object.data["repliesCount"] == 2
567 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "private"))
568 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
569 assert object.data["repliesCount"] == 2
572 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "direct"))
573 assert %{data: _data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
574 assert object.data["repliesCount"] == 2
578 describe "fetch activities for recipients" do
579 test "retrieve the activities for certain recipients" do
580 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
581 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
582 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
584 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
585 assert length(activities) == 2
586 assert activities == [activity_one, activity_two]
590 describe "fetch activities in context" do
591 test "retrieves activities that have a given context" do
592 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
593 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
594 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
595 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
596 activity_five = insert(:note_activity)
599 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
601 activities = ActivityPub.fetch_activities_for_context("2hu", %{blocking_user: user})
602 assert activities == [activity_two, activity]
605 test "doesn't return activities with filtered words" do
607 user_two = insert(:user)
608 insert(:filter, user: user, phrase: "test", hide: true)
610 {:ok, %{id: id1, data: %{"context" => context}}} = CommonAPI.post(user, %{status: "1"})
612 {:ok, %{id: id2}} = CommonAPI.post(user_two, %{status: "2", in_reply_to_status_id: id1})
614 {:ok, %{id: id3} = user_activity} =
615 CommonAPI.post(user, %{status: "3 test?", in_reply_to_status_id: id2})
617 {:ok, %{id: id4} = filtered_activity} =
618 CommonAPI.post(user_two, %{status: "4 test!", in_reply_to_status_id: id3})
620 {:ok, _} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
624 |> ActivityPub.fetch_activities_for_context(%{user: user})
627 assert length(activities) == 4
628 assert user_activity.id in activities
629 refute filtered_activity.id in activities
633 test "doesn't return blocked activities" do
634 activity_one = insert(:note_activity)
635 activity_two = insert(:note_activity)
636 activity_three = insert(:note_activity)
638 booster = insert(:user)
639 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
641 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
643 assert Enum.member?(activities, activity_two)
644 assert Enum.member?(activities, activity_three)
645 refute Enum.member?(activities, activity_one)
647 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
649 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
651 assert Enum.member?(activities, activity_two)
652 assert Enum.member?(activities, activity_three)
653 assert Enum.member?(activities, activity_one)
655 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
656 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
657 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
658 activity_three = Activity.get_by_id(activity_three.id)
660 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
662 assert Enum.member?(activities, activity_two)
663 refute Enum.member?(activities, activity_three)
664 refute Enum.member?(activities, boost_activity)
665 assert Enum.member?(activities, activity_one)
667 activities = ActivityPub.fetch_activities([], %{blocking_user: nil, skip_preload: true})
669 assert Enum.member?(activities, activity_two)
670 assert Enum.member?(activities, activity_three)
671 assert Enum.member?(activities, boost_activity)
672 assert Enum.member?(activities, activity_one)
675 test "doesn't return transitive interactions concerning blocked users" do
676 blocker = insert(:user)
677 blockee = insert(:user)
678 friend = insert(:user)
680 {:ok, _user_relationship} = User.block(blocker, blockee)
682 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
684 {:ok, activity_two} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
686 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
688 {:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
690 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker})
692 assert Enum.member?(activities, activity_one)
693 refute Enum.member?(activities, activity_two)
694 refute Enum.member?(activities, activity_three)
695 refute Enum.member?(activities, activity_four)
698 test "doesn't return announce activities with blocked users in 'to'" do
699 blocker = insert(:user)
700 blockee = insert(:user)
701 friend = insert(:user)
703 {:ok, _user_relationship} = User.block(blocker, blockee)
705 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
707 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
709 {:ok, activity_three} = CommonAPI.repeat(activity_two.id, friend)
712 ActivityPub.fetch_activities([], %{blocking_user: blocker})
713 |> Enum.map(fn act -> act.id end)
715 assert Enum.member?(activities, activity_one.id)
716 refute Enum.member?(activities, activity_two.id)
717 refute Enum.member?(activities, activity_three.id)
720 test "doesn't return announce activities with blocked users in 'cc'" do
721 blocker = insert(:user)
722 blockee = insert(:user)
723 friend = insert(:user)
725 {:ok, _user_relationship} = User.block(blocker, blockee)
727 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
729 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
731 assert object = Pleroma.Object.normalize(activity_two, fetch: false)
734 "actor" => friend.ap_id,
735 "object" => object.data["id"],
736 "context" => object.data["context"],
737 "type" => "Announce",
738 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
739 "cc" => [blockee.ap_id]
742 assert {:ok, activity_three} = ActivityPub.insert(data)
745 ActivityPub.fetch_activities([], %{blocking_user: blocker})
746 |> Enum.map(fn act -> act.id end)
748 assert Enum.member?(activities, activity_one.id)
749 refute Enum.member?(activities, activity_two.id)
750 refute Enum.member?(activities, activity_three.id)
753 test "doesn't return activities from blocked domains" do
754 domain = "dogwhistle.zone"
755 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
756 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
757 activity = insert(:note_activity, %{note: note})
759 {:ok, user} = User.block_domain(user, domain)
761 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
763 refute activity in activities
765 followed_user = insert(:user)
766 CommonAPI.follow(user, followed_user)
767 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user)
769 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
771 refute repeat_activity in activities
774 test "does return activities from followed users on blocked domains" do
775 domain = "meanies.social"
776 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
777 blocker = insert(:user)
779 {:ok, blocker, domain_user} = User.follow(blocker, domain_user)
780 {:ok, blocker} = User.block_domain(blocker, domain)
782 assert User.following?(blocker, domain_user)
783 assert User.blocks_domain?(blocker, domain_user)
784 refute User.blocks?(blocker, domain_user)
786 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
787 activity = insert(:note_activity, %{note: note})
789 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
791 assert activity in activities
793 # And check that if the guy we DO follow boosts someone else from their domain,
794 # that should be hidden
795 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
796 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
797 bad_activity = insert(:note_activity, %{note: bad_note})
798 {:ok, repeat_activity} = CommonAPI.repeat(bad_activity.id, domain_user)
800 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
802 refute repeat_activity in activities
805 test "returns your own posts regardless of mute" do
807 muted = insert(:user)
809 {:ok, muted_post} = CommonAPI.post(muted, %{status: "Im stupid"})
812 CommonAPI.post(user, %{status: "I'm muting you", in_reply_to_status_id: muted_post.id})
814 {:ok, _} = User.mute(user, muted)
816 [activity] = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
818 assert activity.id == reply.id
821 test "doesn't return muted activities" do
822 activity_one = insert(:note_activity)
823 activity_two = insert(:note_activity)
824 activity_three = insert(:note_activity)
826 booster = insert(:user)
828 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
829 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
831 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
833 assert Enum.member?(activities, activity_two)
834 assert Enum.member?(activities, activity_three)
835 refute Enum.member?(activities, activity_one)
837 # Calling with 'with_muted' will deliver muted activities, too.
839 ActivityPub.fetch_activities([], %{
845 assert Enum.member?(activities, activity_two)
846 assert Enum.member?(activities, activity_three)
847 assert Enum.member?(activities, activity_one)
849 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
851 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
853 assert Enum.member?(activities, activity_two)
854 assert Enum.member?(activities, activity_three)
855 assert Enum.member?(activities, activity_one)
857 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
858 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
859 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
860 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
861 activity_three = Activity.get_by_id(activity_three.id)
863 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
865 assert Enum.member?(activities, activity_two)
866 refute Enum.member?(activities, activity_three)
867 refute Enum.member?(activities, boost_activity)
868 assert Enum.member?(activities, activity_one)
870 activities = ActivityPub.fetch_activities([], %{muting_user: nil, skip_preload: true})
872 assert Enum.member?(activities, activity_two)
873 assert Enum.member?(activities, activity_three)
874 assert Enum.member?(activities, boost_activity)
875 assert Enum.member?(activities, activity_one)
878 test "doesn't return thread muted activities" do
880 _activity_one = insert(:note_activity)
881 note_two = insert(:note, data: %{"context" => "suya.."})
882 activity_two = insert(:note_activity, note: note_two)
884 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
886 assert [_activity_one] = ActivityPub.fetch_activities([], %{muting_user: user})
889 test "returns thread muted activities when with_muted is set" do
891 _activity_one = insert(:note_activity)
892 note_two = insert(:note, data: %{"context" => "suya.."})
893 activity_two = insert(:note_activity, note: note_two)
895 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
897 assert [_activity_two, _activity_one] =
898 ActivityPub.fetch_activities([], %{muting_user: user, with_muted: true})
901 test "does include announces on request" do
902 activity_three = insert(:note_activity)
904 booster = insert(:user)
906 {:ok, user, booster} = User.follow(user, booster)
908 {:ok, announce} = CommonAPI.repeat(activity_three.id, booster)
910 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
912 assert announce_activity.id == announce.id
915 test "excludes reblogs on request" do
917 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
918 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
920 [activity] = ActivityPub.fetch_user_activities(user, nil, %{exclude_reblogs: true})
922 assert activity == expected_activity
925 describe "irreversible filters" do
928 user_two = insert(:user)
930 insert(:filter, user: user_two, phrase: "cofe", hide: true)
931 insert(:filter, user: user_two, phrase: "ok boomer", hide: true)
932 insert(:filter, user: user_two, phrase: "test", hide: false)
935 type: ["Create", "Announce"],
939 {:ok, %{user: user, user_two: user_two, params: params}}
942 test "it returns statuses if they don't contain exact filter words", %{
946 {:ok, _} = CommonAPI.post(user, %{status: "hey"})
947 {:ok, _} = CommonAPI.post(user, %{status: "got cofefe?"})
948 {:ok, _} = CommonAPI.post(user, %{status: "I am not a boomer"})
949 {:ok, _} = CommonAPI.post(user, %{status: "ok boomers"})
950 {:ok, _} = CommonAPI.post(user, %{status: "ccofee is not a word"})
951 {:ok, _} = CommonAPI.post(user, %{status: "this is a test"})
953 activities = ActivityPub.fetch_activities([], params)
955 assert Enum.count(activities) == 6
958 test "it does not filter user's own statuses", %{user_two: user_two, params: params} do
959 {:ok, _} = CommonAPI.post(user_two, %{status: "Give me some cofe!"})
960 {:ok, _} = CommonAPI.post(user_two, %{status: "ok boomer"})
962 activities = ActivityPub.fetch_activities([], params)
964 assert Enum.count(activities) == 2
967 test "it excludes statuses with filter words", %{user: user, params: params} do
968 {:ok, _} = CommonAPI.post(user, %{status: "Give me some cofe!"})
969 {:ok, _} = CommonAPI.post(user, %{status: "ok boomer"})
970 {:ok, _} = CommonAPI.post(user, %{status: "is it a cOfE?"})
971 {:ok, _} = CommonAPI.post(user, %{status: "cofe is all I need"})
972 {:ok, _} = CommonAPI.post(user, %{status: "— ok BOOMER\n"})
974 activities = ActivityPub.fetch_activities([], params)
976 assert Enum.empty?(activities)
979 test "it returns all statuses if user does not have any filters" do
980 another_user = insert(:user)
981 {:ok, _} = CommonAPI.post(another_user, %{status: "got cofe?"})
982 {:ok, _} = CommonAPI.post(another_user, %{status: "test!"})
985 ActivityPub.fetch_activities([], %{
986 type: ["Create", "Announce"],
990 assert Enum.count(activities) == 2
994 describe "public fetch activities" do
995 test "doesn't retrieve unlisted activities" do
998 {:ok, _unlisted_activity} = CommonAPI.post(user, %{status: "yeah", visibility: "unlisted"})
1000 {:ok, listed_activity} = CommonAPI.post(user, %{status: "yeah"})
1002 [activity] = ActivityPub.fetch_public_activities()
1004 assert activity == listed_activity
1007 test "retrieves public activities" do
1008 _activities = ActivityPub.fetch_public_activities()
1010 %{public: public} = ActivityBuilder.public_and_non_public()
1012 activities = ActivityPub.fetch_public_activities()
1013 assert length(activities) == 1
1014 assert Enum.at(activities, 0) == public
1017 test "retrieves a maximum of 20 activities" do
1018 ActivityBuilder.insert_list(10)
1019 expected_activities = ActivityBuilder.insert_list(20)
1021 activities = ActivityPub.fetch_public_activities()
1023 assert collect_ids(activities) == collect_ids(expected_activities)
1024 assert length(activities) == 20
1027 test "retrieves ids starting from a since_id" do
1028 activities = ActivityBuilder.insert_list(30)
1029 expected_activities = ActivityBuilder.insert_list(10)
1030 since_id = List.last(activities).id
1032 activities = ActivityPub.fetch_public_activities(%{since_id: since_id})
1034 assert collect_ids(activities) == collect_ids(expected_activities)
1035 assert length(activities) == 10
1038 test "retrieves ids up to max_id" do
1039 ActivityBuilder.insert_list(10)
1040 expected_activities = ActivityBuilder.insert_list(20)
1044 |> ActivityBuilder.insert_list()
1047 activities = ActivityPub.fetch_public_activities(%{max_id: max_id})
1049 assert length(activities) == 20
1050 assert collect_ids(activities) == collect_ids(expected_activities)
1053 test "paginates via offset/limit" do
1054 _first_part_activities = ActivityBuilder.insert_list(10)
1055 second_part_activities = ActivityBuilder.insert_list(10)
1057 later_activities = ActivityBuilder.insert_list(10)
1059 activities = ActivityPub.fetch_public_activities(%{page: "2", page_size: "20"}, :offset)
1061 assert length(activities) == 20
1063 assert collect_ids(activities) ==
1064 collect_ids(second_part_activities) ++ collect_ids(later_activities)
1067 test "doesn't return reblogs for users for whom reblogs have been muted" do
1068 activity = insert(:note_activity)
1069 user = insert(:user)
1070 booster = insert(:user)
1071 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
1073 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
1075 activities = ActivityPub.fetch_activities([], %{muting_user: user})
1077 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
1080 test "returns reblogs for users for whom reblogs have not been muted" do
1081 activity = insert(:note_activity)
1082 user = insert(:user)
1083 booster = insert(:user)
1084 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
1085 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
1087 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
1089 activities = ActivityPub.fetch_activities([], %{muting_user: user})
1091 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
1095 describe "uploading files" do
1097 test_file = %Plug.Upload{
1098 content_type: "image/jpeg",
1099 path: Path.absname("test/fixtures/image.jpg"),
1100 filename: "an_image.jpg"
1103 %{test_file: test_file}
1106 test "sets a description if given", %{test_file: file} do
1107 {:ok, %Object{} = object} = ActivityPub.upload(file, description: "a cool file")
1108 assert object.data["name"] == "a cool file"
1111 test "it sets the default description depending on the configuration", %{test_file: file} do
1112 clear_config([Pleroma.Upload, :default_description])
1114 clear_config([Pleroma.Upload, :default_description], nil)
1115 {:ok, %Object{} = object} = ActivityPub.upload(file)
1116 assert object.data["name"] == ""
1118 clear_config([Pleroma.Upload, :default_description], :filename)
1119 {:ok, %Object{} = object} = ActivityPub.upload(file)
1120 assert object.data["name"] == "an_image.jpg"
1122 clear_config([Pleroma.Upload, :default_description], "unnamed attachment")
1123 {:ok, %Object{} = object} = ActivityPub.upload(file)
1124 assert object.data["name"] == "unnamed attachment"
1127 test "copies the file to the configured folder", %{test_file: file} do
1128 clear_config([Pleroma.Upload, :default_description], :filename)
1129 {:ok, %Object{} = object} = ActivityPub.upload(file)
1130 assert object.data["name"] == "an_image.jpg"
1133 test "works with base64 encoded images" do
1138 {:ok, %Object{}} = ActivityPub.upload(file)
1142 describe "fetch the latest Follow" do
1143 test "fetches the latest Follow activity" do
1144 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1145 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1146 followed = Repo.get_by(User, ap_id: activity.data["object"])
1148 assert activity == Utils.fetch_latest_follow(follower, followed)
1152 describe "unfollowing" do
1153 test "it reverts unfollow activity" do
1154 follower = insert(:user)
1155 followed = insert(:user)
1157 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1159 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1160 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
1163 activity = Activity.get_by_id(follow_activity.id)
1164 assert activity.data["type"] == "Follow"
1165 assert activity.data["actor"] == follower.ap_id
1167 assert activity.data["object"] == followed.ap_id
1170 test "creates an undo activity for the last follow" do
1171 follower = insert(:user)
1172 followed = insert(:user)
1174 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1175 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1177 assert activity.data["type"] == "Undo"
1178 assert activity.data["actor"] == follower.ap_id
1180 embedded_object = activity.data["object"]
1181 assert is_map(embedded_object)
1182 assert embedded_object["type"] == "Follow"
1183 assert embedded_object["object"] == followed.ap_id
1184 assert embedded_object["id"] == follow_activity.data["id"]
1187 test "creates an undo activity for a pending follow request" do
1188 follower = insert(:user)
1189 followed = insert(:user, %{is_locked: true})
1191 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1192 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1194 assert activity.data["type"] == "Undo"
1195 assert activity.data["actor"] == follower.ap_id
1197 embedded_object = activity.data["object"]
1198 assert is_map(embedded_object)
1199 assert embedded_object["type"] == "Follow"
1200 assert embedded_object["object"] == followed.ap_id
1201 assert embedded_object["id"] == follow_activity.data["id"]
1205 describe "timeline post-processing" do
1206 test "it filters broken threads" do
1207 user1 = insert(:user)
1208 user2 = insert(:user)
1209 user3 = insert(:user)
1211 {:ok, user1, user3} = User.follow(user1, user3)
1212 assert User.following?(user1, user3)
1214 {:ok, user2, user3} = User.follow(user2, user3)
1215 assert User.following?(user2, user3)
1217 {:ok, user3, user2} = User.follow(user3, user2)
1218 assert User.following?(user3, user2)
1220 {:ok, public_activity} = CommonAPI.post(user3, %{status: "hi 1"})
1222 {:ok, private_activity_1} = CommonAPI.post(user3, %{status: "hi 2", visibility: "private"})
1224 {:ok, private_activity_2} =
1225 CommonAPI.post(user2, %{
1227 visibility: "private",
1228 in_reply_to_status_id: private_activity_1.id
1231 {:ok, private_activity_3} =
1232 CommonAPI.post(user3, %{
1234 visibility: "private",
1235 in_reply_to_status_id: private_activity_2.id
1239 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1240 |> Enum.map(fn a -> a.id end)
1242 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1244 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1246 assert length(activities) == 3
1249 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{user: user1})
1250 |> Enum.map(fn a -> a.id end)
1252 assert [public_activity.id, private_activity_1.id] == activities
1253 assert length(activities) == 2
1257 describe "flag/1" do
1259 reporter = insert(:user)
1260 target_account = insert(:user)
1262 {:ok, activity} = CommonAPI.post(target_account, %{status: content})
1263 context = Utils.generate_context_id()
1265 reporter_ap_id = reporter.ap_id
1266 target_ap_id = target_account.ap_id
1267 activity_ap_id = activity.data["id"]
1269 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1275 target_account: target_account,
1276 reported_activity: activity,
1278 activity_ap_id: activity_ap_id,
1279 activity_with_object: activity_with_object,
1280 reporter_ap_id: reporter_ap_id,
1281 target_ap_id: target_ap_id
1285 test "it can create a Flag activity",
1289 target_account: target_account,
1290 reported_activity: reported_activity,
1292 activity_ap_id: activity_ap_id,
1293 activity_with_object: activity_with_object,
1294 reporter_ap_id: reporter_ap_id,
1295 target_ap_id: target_ap_id
1297 assert {:ok, activity} =
1301 account: target_account,
1302 statuses: [reported_activity],
1308 "id" => activity_ap_id,
1309 "content" => content,
1310 "published" => activity_with_object.object.data["published"],
1312 AccountView.render("show.json", %{user: target_account, skip_visibility_check: true})
1316 actor: ^reporter_ap_id,
1319 "content" => ^content,
1320 "context" => ^context,
1321 "object" => [^target_ap_id, ^note_obj]
1326 test_with_mock "strips status data from Flag, before federating it",
1330 target_account: target_account,
1331 reported_activity: reported_activity,
1341 account: target_account,
1342 statuses: [reported_activity],
1347 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1349 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1352 test_with_mock "reverts on error",
1356 target_account: target_account,
1357 reported_activity: reported_activity,
1362 maybe_federate: fn _ -> {:error, :reverted} end do
1363 assert {:error, :reverted} =
1367 account: target_account,
1368 statuses: [reported_activity],
1372 assert Repo.aggregate(Activity, :count, :id) == 1
1373 assert Repo.aggregate(Object, :count, :id) == 2
1374 assert Repo.aggregate(Notification, :count, :id) == 0
1378 test "fetch_activities/2 returns activities addressed to a list " do
1379 user = insert(:user)
1380 member = insert(:user)
1381 {:ok, list} = Pleroma.List.create("foo", user)
1382 {:ok, list} = Pleroma.List.follow(list, member)
1384 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1386 activity = Repo.preload(activity, :bookmark)
1387 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1389 assert ActivityPub.fetch_activities([], %{user: user}) == [activity]
1393 File.read!("test/fixtures/avatar_data_uri")
1396 describe "fetch_activities_bounded" do
1397 test "fetches private posts for followed users" do
1398 user = insert(:user)
1401 CommonAPI.post(user, %{
1402 status: "thought I looked cute might delete later :3",
1403 visibility: "private"
1406 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1407 assert result.id == activity.id
1410 test "fetches only public posts for other users" do
1411 user = insert(:user)
1412 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe", visibility: "public"})
1414 {:ok, _private_activity} =
1415 CommonAPI.post(user, %{
1416 status: "why is tenshi eating a corndog so cute?",
1417 visibility: "private"
1420 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1421 assert result.id == activity.id
1425 describe "fetch_follow_information_for_user" do
1426 test "syncronizes following/followers counters" do
1430 follower_address: "http://localhost:4001/users/fuser2/followers",
1431 following_address: "http://localhost:4001/users/fuser2/following"
1434 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1435 assert info.follower_count == 527
1436 assert info.following_count == 267
1439 test "detects hidden followers" do
1442 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1443 %Tesla.Env{status: 403, body: ""}
1446 apply(HttpRequestMock, :request, [env])
1453 follower_address: "http://localhost:4001/users/masto_closed/followers",
1454 following_address: "http://localhost:4001/users/masto_closed/following"
1457 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1458 assert follow_info.hide_followers == true
1459 assert follow_info.hide_follows == false
1462 test "detects hidden follows" do
1465 "http://localhost:4001/users/masto_closed/following?page=1" ->
1466 %Tesla.Env{status: 403, body: ""}
1469 apply(HttpRequestMock, :request, [env])
1476 follower_address: "http://localhost:4001/users/masto_closed/followers",
1477 following_address: "http://localhost:4001/users/masto_closed/following"
1480 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1481 assert follow_info.hide_followers == false
1482 assert follow_info.hide_follows == true
1485 test "detects hidden follows/followers for friendica" do
1489 follower_address: "http://localhost:8080/followers/fuser3",
1490 following_address: "http://localhost:8080/following/fuser3"
1493 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1494 assert follow_info.hide_followers == true
1495 assert follow_info.follower_count == 296
1496 assert follow_info.following_count == 32
1497 assert follow_info.hide_follows == true
1500 test "doesn't crash when follower and following counters are hidden" do
1503 "http://localhost:4001/users/masto_hidden_counters/following" ->
1506 "@context" => "https://www.w3.org/ns/activitystreams",
1507 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1509 headers: HttpRequestMock.activitypub_object_headers()
1512 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1513 %Tesla.Env{status: 403, body: ""}
1515 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1518 "@context" => "https://www.w3.org/ns/activitystreams",
1519 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1521 headers: HttpRequestMock.activitypub_object_headers()
1524 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1525 %Tesla.Env{status: 403, body: ""}
1532 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1533 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1536 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1538 assert follow_info.hide_followers == true
1539 assert follow_info.follower_count == 0
1540 assert follow_info.hide_follows == true
1541 assert follow_info.following_count == 0
1545 describe "fetch_favourites/3" do
1546 test "returns a favourite activities sorted by adds to favorite" do
1547 user = insert(:user)
1548 other_user = insert(:user)
1549 user1 = insert(:user)
1550 user2 = insert(:user)
1551 {:ok, a1} = CommonAPI.post(user1, %{status: "bla"})
1552 {:ok, _a2} = CommonAPI.post(user2, %{status: "traps are happy"})
1553 {:ok, a3} = CommonAPI.post(user2, %{status: "Trees Are "})
1554 {:ok, a4} = CommonAPI.post(user2, %{status: "Agent Smith "})
1555 {:ok, a5} = CommonAPI.post(user1, %{status: "Red or Blue "})
1557 {:ok, _} = CommonAPI.favorite(user, a4.id)
1558 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1559 {:ok, _} = CommonAPI.favorite(user, a3.id)
1560 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1561 {:ok, _} = CommonAPI.favorite(user, a5.id)
1562 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1563 {:ok, _} = CommonAPI.favorite(user, a1.id)
1564 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1565 result = ActivityPub.fetch_favourites(user)
1567 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1569 result = ActivityPub.fetch_favourites(user, %{limit: 2})
1570 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1574 describe "Move activity" do
1576 %{ap_id: old_ap_id} = old_user = insert(:user)
1577 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1578 follower = insert(:user)
1579 follower_move_opted_out = insert(:user, allow_following_move: false)
1581 User.follow(follower, old_user)
1582 User.follow(follower_move_opted_out, old_user)
1584 assert User.following?(follower, old_user)
1585 assert User.following?(follower_move_opted_out, old_user)
1587 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1592 "actor" => ^old_ap_id,
1593 "object" => ^old_ap_id,
1594 "target" => ^new_ap_id,
1601 "op" => "move_following",
1602 "origin_id" => old_user.id,
1603 "target_id" => new_user.id
1606 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1608 Pleroma.Workers.BackgroundWorker.perform(%Oban.Job{args: params})
1610 refute User.following?(follower, old_user)
1611 assert User.following?(follower, new_user)
1613 assert User.following?(follower_move_opted_out, old_user)
1614 refute User.following?(follower_move_opted_out, new_user)
1616 activity = %Activity{activity | object: nil}
1618 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1620 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1623 test "old user must be in the new user's `also_known_as` list" do
1624 old_user = insert(:user)
1625 new_user = insert(:user)
1627 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1628 ActivityPub.move(old_user, new_user)
1632 test "doesn't retrieve replies activities with exclude_replies" do
1633 user = insert(:user)
1635 {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
1637 {:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id})
1639 [result] = ActivityPub.fetch_public_activities(%{exclude_replies: true})
1641 assert result.id == activity.id
1643 assert length(ActivityPub.fetch_public_activities()) == 2
1646 describe "replies filtering with public messages" do
1647 setup :public_messages
1649 test "public timeline", %{users: %{u1: user}} do
1652 |> Map.put(:type, ["Create", "Announce"])
1653 |> Map.put(:local_only, false)
1654 |> Map.put(:blocking_user, user)
1655 |> Map.put(:muting_user, user)
1656 |> Map.put(:reply_filtering_user, user)
1657 |> ActivityPub.fetch_public_activities()
1658 |> Enum.map(& &1.id)
1660 assert length(activities_ids) == 16
1663 test "public timeline with reply_visibility `following`", %{
1669 activities: activities
1673 |> Map.put(:type, ["Create", "Announce"])
1674 |> Map.put(:local_only, false)
1675 |> Map.put(:blocking_user, user)
1676 |> Map.put(:muting_user, user)
1677 |> Map.put(:reply_visibility, "following")
1678 |> Map.put(:reply_filtering_user, user)
1679 |> ActivityPub.fetch_public_activities()
1680 |> Enum.map(& &1.id)
1682 assert length(activities_ids) == 14
1685 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1687 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1690 test "public timeline with reply_visibility `self`", %{
1696 activities: activities
1700 |> Map.put(:type, ["Create", "Announce"])
1701 |> Map.put(:local_only, false)
1702 |> Map.put(:blocking_user, user)
1703 |> Map.put(:muting_user, user)
1704 |> Map.put(:reply_visibility, "self")
1705 |> Map.put(:reply_filtering_user, user)
1706 |> ActivityPub.fetch_public_activities()
1707 |> Enum.map(& &1.id)
1709 assert length(activities_ids) == 10
1710 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1711 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1714 test "home timeline", %{
1716 activities: activities,
1724 |> Map.put(:type, ["Create", "Announce"])
1725 |> Map.put(:blocking_user, user)
1726 |> Map.put(:muting_user, user)
1727 |> Map.put(:user, user)
1728 |> Map.put(:reply_filtering_user, user)
1731 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1732 |> Enum.map(& &1.id)
1734 assert length(activities_ids) == 13
1749 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1752 test "home timeline with reply_visibility `following`", %{
1754 activities: activities,
1762 |> Map.put(:type, ["Create", "Announce"])
1763 |> Map.put(:blocking_user, user)
1764 |> Map.put(:muting_user, user)
1765 |> Map.put(:user, user)
1766 |> Map.put(:reply_visibility, "following")
1767 |> Map.put(:reply_filtering_user, user)
1770 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1771 |> Enum.map(& &1.id)
1773 assert length(activities_ids) == 11
1788 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1791 test "home timeline with reply_visibility `self`", %{
1793 activities: activities,
1801 |> Map.put(:type, ["Create", "Announce"])
1802 |> Map.put(:blocking_user, user)
1803 |> Map.put(:muting_user, user)
1804 |> Map.put(:user, user)
1805 |> Map.put(:reply_visibility, "self")
1806 |> Map.put(:reply_filtering_user, user)
1809 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1810 |> Enum.map(& &1.id)
1812 assert length(activities_ids) == 9
1825 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1828 test "filtering out announces where the user is the actor of the announced message" do
1829 user = insert(:user)
1830 other_user = insert(:user)
1831 third_user = insert(:user)
1832 User.follow(user, other_user)
1834 {:ok, post} = CommonAPI.post(user, %{status: "yo"})
1835 {:ok, other_post} = CommonAPI.post(third_user, %{status: "yo"})
1836 {:ok, _announce} = CommonAPI.repeat(post.id, other_user)
1837 {:ok, _announce} = CommonAPI.repeat(post.id, third_user)
1838 {:ok, announce} = CommonAPI.repeat(other_post.id, other_user)
1845 [user.ap_id | User.following(user)]
1846 |> ActivityPub.fetch_activities(params)
1848 assert length(results) == 3
1852 announce_filtering_user: user
1856 [user.ap_id | User.following(user)]
1857 |> ActivityPub.fetch_activities(params)
1859 assert result.id == announce.id
1863 describe "replies filtering with private messages" do
1864 setup :private_messages
1866 test "public timeline", %{users: %{u1: user}} do
1869 |> Map.put(:type, ["Create", "Announce"])
1870 |> Map.put(:local_only, false)
1871 |> Map.put(:blocking_user, user)
1872 |> Map.put(:muting_user, user)
1873 |> Map.put(:user, user)
1874 |> ActivityPub.fetch_public_activities()
1875 |> Enum.map(& &1.id)
1877 assert activities_ids == []
1880 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1883 |> Map.put(:type, ["Create", "Announce"])
1884 |> Map.put(:local_only, false)
1885 |> Map.put(:blocking_user, user)
1886 |> Map.put(:muting_user, user)
1887 |> Map.put(:reply_visibility, "following")
1888 |> Map.put(:reply_filtering_user, user)
1889 |> Map.put(:user, user)
1890 |> ActivityPub.fetch_public_activities()
1891 |> Enum.map(& &1.id)
1893 assert activities_ids == []
1896 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
1899 |> Map.put(:type, ["Create", "Announce"])
1900 |> Map.put(:local_only, false)
1901 |> Map.put(:blocking_user, user)
1902 |> Map.put(:muting_user, user)
1903 |> Map.put(:reply_visibility, "self")
1904 |> Map.put(:reply_filtering_user, user)
1905 |> Map.put(:user, user)
1906 |> ActivityPub.fetch_public_activities()
1907 |> Enum.map(& &1.id)
1909 assert activities_ids == []
1913 |> Map.put(:reply_visibility, "self")
1914 |> Map.put(:reply_filtering_user, nil)
1915 |> ActivityPub.fetch_public_activities()
1917 assert activities_ids == []
1920 test "home timeline", %{users: %{u1: user}} do
1923 |> Map.put(:type, ["Create", "Announce"])
1924 |> Map.put(:blocking_user, user)
1925 |> Map.put(:muting_user, user)
1926 |> Map.put(:user, user)
1929 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1930 |> Enum.map(& &1.id)
1932 assert length(activities_ids) == 12
1935 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1938 |> Map.put(:type, ["Create", "Announce"])
1939 |> Map.put(:blocking_user, user)
1940 |> Map.put(:muting_user, user)
1941 |> Map.put(:user, user)
1942 |> Map.put(:reply_visibility, "following")
1943 |> Map.put(:reply_filtering_user, user)
1946 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1947 |> Enum.map(& &1.id)
1949 assert length(activities_ids) == 12
1952 test "home timeline with default reply_visibility `self`", %{
1954 activities: activities,
1962 |> Map.put(:type, ["Create", "Announce"])
1963 |> Map.put(:blocking_user, user)
1964 |> Map.put(:muting_user, user)
1965 |> Map.put(:user, user)
1966 |> Map.put(:reply_visibility, "self")
1967 |> Map.put(:reply_filtering_user, user)
1970 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1971 |> Enum.map(& &1.id)
1973 assert length(activities_ids) == 10
1976 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
1978 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1982 defp public_messages(_) do
1983 [u1, u2, u3, u4] = insert_list(4, :user)
1984 {:ok, u1, u2} = User.follow(u1, u2)
1985 {:ok, u2, u1} = User.follow(u2, u1)
1986 {:ok, u1, u4} = User.follow(u1, u4)
1987 {:ok, u4, u1} = User.follow(u4, u1)
1989 {:ok, u2, u3} = User.follow(u2, u3)
1990 {:ok, u3, u2} = User.follow(u3, u2)
1992 {:ok, a1} = CommonAPI.post(u1, %{status: "Status"})
1995 CommonAPI.post(u2, %{
1996 status: "@#{u1.nickname} reply from u2 to u1",
1997 in_reply_to_status_id: a1.id
2001 CommonAPI.post(u3, %{
2002 status: "@#{u1.nickname} reply from u3 to u1",
2003 in_reply_to_status_id: a1.id
2007 CommonAPI.post(u4, %{
2008 status: "@#{u1.nickname} reply from u4 to u1",
2009 in_reply_to_status_id: a1.id
2012 {:ok, a2} = CommonAPI.post(u2, %{status: "Status"})
2015 CommonAPI.post(u1, %{
2016 status: "@#{u2.nickname} reply from u1 to u2",
2017 in_reply_to_status_id: a2.id
2021 CommonAPI.post(u3, %{
2022 status: "@#{u2.nickname} reply from u3 to u2",
2023 in_reply_to_status_id: a2.id
2027 CommonAPI.post(u4, %{
2028 status: "@#{u2.nickname} reply from u4 to u2",
2029 in_reply_to_status_id: a2.id
2032 {:ok, a3} = CommonAPI.post(u3, %{status: "Status"})
2035 CommonAPI.post(u1, %{
2036 status: "@#{u3.nickname} reply from u1 to u3",
2037 in_reply_to_status_id: a3.id
2041 CommonAPI.post(u2, %{
2042 status: "@#{u3.nickname} reply from u2 to u3",
2043 in_reply_to_status_id: a3.id
2047 CommonAPI.post(u4, %{
2048 status: "@#{u3.nickname} reply from u4 to u3",
2049 in_reply_to_status_id: a3.id
2052 {:ok, a4} = CommonAPI.post(u4, %{status: "Status"})
2055 CommonAPI.post(u1, %{
2056 status: "@#{u4.nickname} reply from u1 to u4",
2057 in_reply_to_status_id: a4.id
2061 CommonAPI.post(u2, %{
2062 status: "@#{u4.nickname} reply from u2 to u4",
2063 in_reply_to_status_id: a4.id
2067 CommonAPI.post(u3, %{
2068 status: "@#{u4.nickname} reply from u3 to u4",
2069 in_reply_to_status_id: a4.id
2073 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2074 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2075 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2076 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
2077 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
2078 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
2081 defp private_messages(_) do
2082 [u1, u2, u3, u4] = insert_list(4, :user)
2083 {:ok, u1, u2} = User.follow(u1, u2)
2084 {:ok, u2, u1} = User.follow(u2, u1)
2085 {:ok, u1, u3} = User.follow(u1, u3)
2086 {:ok, u3, u1} = User.follow(u3, u1)
2087 {:ok, u1, u4} = User.follow(u1, u4)
2088 {:ok, u4, u1} = User.follow(u4, u1)
2090 {:ok, u2, u3} = User.follow(u2, u3)
2091 {:ok, u3, u2} = User.follow(u3, u2)
2093 {:ok, a1} = CommonAPI.post(u1, %{status: "Status", visibility: "private"})
2096 CommonAPI.post(u2, %{
2097 status: "@#{u1.nickname} reply from u2 to u1",
2098 in_reply_to_status_id: a1.id,
2099 visibility: "private"
2103 CommonAPI.post(u3, %{
2104 status: "@#{u1.nickname} reply from u3 to u1",
2105 in_reply_to_status_id: a1.id,
2106 visibility: "private"
2110 CommonAPI.post(u4, %{
2111 status: "@#{u1.nickname} reply from u4 to u1",
2112 in_reply_to_status_id: a1.id,
2113 visibility: "private"
2116 {:ok, a2} = CommonAPI.post(u2, %{status: "Status", visibility: "private"})
2119 CommonAPI.post(u1, %{
2120 status: "@#{u2.nickname} reply from u1 to u2",
2121 in_reply_to_status_id: a2.id,
2122 visibility: "private"
2126 CommonAPI.post(u3, %{
2127 status: "@#{u2.nickname} reply from u3 to u2",
2128 in_reply_to_status_id: a2.id,
2129 visibility: "private"
2132 {:ok, a3} = CommonAPI.post(u3, %{status: "Status", visibility: "private"})
2135 CommonAPI.post(u1, %{
2136 status: "@#{u3.nickname} reply from u1 to u3",
2137 in_reply_to_status_id: a3.id,
2138 visibility: "private"
2142 CommonAPI.post(u2, %{
2143 status: "@#{u3.nickname} reply from u2 to u3",
2144 in_reply_to_status_id: a3.id,
2145 visibility: "private"
2148 {:ok, a4} = CommonAPI.post(u4, %{status: "Status", visibility: "private"})
2151 CommonAPI.post(u1, %{
2152 status: "@#{u4.nickname} reply from u1 to u4",
2153 in_reply_to_status_id: a4.id,
2154 visibility: "private"
2158 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2159 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2160 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2161 u2: %{r1: r2_1.id, r2: r2_2.id},
2162 u3: %{r1: r3_1.id, r2: r3_2.id},
2166 describe "maybe_update_follow_information/1" do
2168 clear_config([:instance, :external_user_synchronization], true)
2172 ap_id: "https://gensokyo.2hu/users/raymoo",
2173 following_address: "https://gensokyo.2hu/users/following",
2174 follower_address: "https://gensokyo.2hu/users/followers",
2181 test "logs an error when it can't fetch the info", %{user: user} do
2182 assert capture_log(fn ->
2183 ActivityPub.maybe_update_follow_information(user)
2184 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2187 test "just returns the input if the user type is Application", %{
2192 |> Map.put(:type, "Application")
2194 refute capture_log(fn ->
2195 assert ^user = ActivityPub.maybe_update_follow_information(user)
2196 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2199 test "it just returns the input if the user has no following/follower addresses", %{
2204 |> Map.put(:following_address, nil)
2205 |> Map.put(:follower_address, nil)
2207 refute capture_log(fn ->
2208 assert ^user = ActivityPub.maybe_update_follow_information(user)
2209 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2213 describe "global activity expiration" do
2214 test "creates an activity expiration for local Create activities" do
2215 clear_config([:mrf, :policies], Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy)
2217 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
2218 {:ok, follow} = ActivityBuilder.insert(%{"type" => "Follow", "context" => "3hu"})
2221 worker: Pleroma.Workers.PurgeExpiredActivity,
2222 args: %{activity_id: activity.id},
2224 activity.inserted_at
2225 |> DateTime.from_naive!("Etc/UTC")
2226 |> Timex.shift(days: 365)
2230 worker: Pleroma.Workers.PurgeExpiredActivity,
2231 args: %{activity_id: follow.id}
2236 describe "handling of clashing nicknames" do
2237 test "renames an existing user with a clashing nickname and a different ap id" do
2242 nickname: "admin@mastodon.example.org",
2243 ap_id: "http://mastodon.example.org/users/harinezumigari"
2247 nickname: orig_user.nickname,
2248 ap_id: orig_user.ap_id <> "part_2"
2250 |> ActivityPub.maybe_handle_clashing_nickname()
2252 user = User.get_by_id(orig_user.id)
2254 assert user.nickname == "#{orig_user.id}.admin@mastodon.example.org"
2257 test "does nothing with a clashing nickname and the same ap id" do
2262 nickname: "admin@mastodon.example.org",
2263 ap_id: "http://mastodon.example.org/users/harinezumigari"
2267 nickname: orig_user.nickname,
2268 ap_id: orig_user.ap_id
2270 |> ActivityPub.maybe_handle_clashing_nickname()
2272 user = User.get_by_id(orig_user.id)
2274 assert user.nickname == orig_user.nickname
2278 describe "reply filtering" do
2279 test "`following` still contains announcements by friends" do
2280 user = insert(:user)
2281 followed = insert(:user)
2282 not_followed = insert(:user)
2284 User.follow(user, followed)
2286 {:ok, followed_post} = CommonAPI.post(followed, %{status: "Hello"})
2288 {:ok, not_followed_to_followed} =
2289 CommonAPI.post(not_followed, %{
2290 status: "Also hello",
2291 in_reply_to_status_id: followed_post.id
2294 {:ok, retoot} = CommonAPI.repeat(not_followed_to_followed.id, followed)
2298 |> Map.put(:type, ["Create", "Announce"])
2299 |> Map.put(:blocking_user, user)
2300 |> Map.put(:muting_user, user)
2301 |> Map.put(:reply_filtering_user, user)
2302 |> Map.put(:reply_visibility, "following")
2303 |> Map.put(:announce_filtering_user, user)
2304 |> Map.put(:user, user)
2307 [user.ap_id | User.following(user)]
2308 |> ActivityPub.fetch_activities(params)
2310 followed_post_id = followed_post.id
2311 retoot_id = retoot.id
2313 assert [%{id: ^followed_post_id}, %{id: ^retoot_id}] = activities
2315 assert length(activities) == 2
2318 # This test is skipped because, while this is the desired behavior,
2319 # there seems to be no good way to achieve it with the method that
2320 # we currently use for detecting to who a reply is directed.
2321 # This is a TODO and should be fixed by a later rewrite of the code
2324 test "`following` still contains self-replies by friends" do
2325 user = insert(:user)
2326 followed = insert(:user)
2327 not_followed = insert(:user)
2329 User.follow(user, followed)
2331 {:ok, followed_post} = CommonAPI.post(followed, %{status: "Hello"})
2332 {:ok, not_followed_post} = CommonAPI.post(not_followed, %{status: "Also hello"})
2334 {:ok, _followed_to_not_followed} =
2335 CommonAPI.post(followed, %{status: "sup", in_reply_to_status_id: not_followed_post.id})
2337 {:ok, _followed_self_reply} =
2338 CommonAPI.post(followed, %{status: "Also cofe", in_reply_to_status_id: followed_post.id})
2342 |> Map.put(:type, ["Create", "Announce"])
2343 |> Map.put(:blocking_user, user)
2344 |> Map.put(:muting_user, user)
2345 |> Map.put(:reply_filtering_user, user)
2346 |> Map.put(:reply_visibility, "following")
2347 |> Map.put(:announce_filtering_user, user)
2348 |> Map.put(:user, user)
2351 [user.ap_id | User.following(user)]
2352 |> ActivityPub.fetch_activities(params)
2354 assert length(activities) == 2
2358 test "allow fetching of accounts with an empty string name field" do
2360 %{method: :get, url: "https://princess.cat/users/mewmew"} ->
2361 file = File.read!("test/fixtures/mewmew_no_name.json")
2362 %Tesla.Env{status: 200, body: file, headers: HttpRequestMock.activitypub_object_headers()}
2365 {:ok, user} = ActivityPub.make_user_from_ap_id("https://princess.cat/users/mewmew")
2366 assert user.name == " "