1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Builders.ActivityBuilder
12 alias Pleroma.Notification
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.AdminAPI.AccountView
18 alias Pleroma.Web.CommonAPI
20 import ExUnit.CaptureLog
22 import Pleroma.Factory
26 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
30 setup do: clear_config([:instance, :federating])
32 describe "streaming out participations" do
33 test "it streams them out" do
35 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
37 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
40 conversation.participations
41 |> Repo.preload(:user)
43 with_mock Pleroma.Web.Streamer,
44 stream: fn _, _ -> nil end do
45 ActivityPub.stream_out_participations(conversation.participations)
47 assert called(Pleroma.Web.Streamer.stream("participation", participations))
51 test "streams them out on activity creation" do
52 user_one = insert(:user)
53 user_two = insert(:user)
55 with_mock Pleroma.Web.Streamer,
56 stream: fn _, _ -> nil end do
58 CommonAPI.post(user_one, %{
59 status: "@#{user_two.nickname}",
64 activity.data["context"]
65 |> Pleroma.Conversation.get_for_ap_id()
66 |> Repo.preload(participations: :user)
68 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
73 describe "fetching restricted by visibility" do
74 test "it restricts by the appropriate visibility" do
77 {:ok, public_activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
79 {:ok, direct_activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
81 {:ok, unlisted_activity} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
83 {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
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 fetches the appropriate tag-restricted posts" do
190 {:ok, status_one} = CommonAPI.post(user, %{status: ". #test"})
191 {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
192 {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #reject"})
194 fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"})
196 fetch_two = ActivityPub.fetch_activities([], %{type: "Create", tag: ["test", "essais"]})
199 ActivityPub.fetch_activities([], %{
201 tag: ["test", "essais"],
202 tag_reject: ["reject"]
206 ActivityPub.fetch_activities([], %{
209 tag_all: ["test", "reject"]
212 assert fetch_one == [status_one, status_three]
213 assert fetch_two == [status_one, status_two, status_three]
214 assert fetch_three == [status_one, status_two]
215 assert fetch_four == [status_three]
219 describe "insertion" do
220 test "drops activities beyond a certain limit" do
221 limit = Config.get([:instance, :remote_limit])
224 :crypto.strong_rand_bytes(limit + 1)
226 |> binary_part(0, limit + 1)
231 "content" => random_text
235 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
238 test "doesn't drop activities with content being null" do
242 "actor" => user.ap_id,
245 "actor" => user.ap_id,
252 assert {:ok, _} = ActivityPub.insert(data)
255 test "returns the activity if one with the same id is already in" do
256 activity = insert(:note_activity)
257 {:ok, new_activity} = ActivityPub.insert(activity.data)
259 assert activity.id == new_activity.id
262 test "inserts a given map into the activity database, giving it an id if it has none." do
266 "actor" => user.ap_id,
269 "actor" => user.ap_id,
276 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
277 assert activity.data["ok"] == data["ok"]
278 assert is_binary(activity.data["id"])
284 "actor" => user.ap_id,
286 "context" => "blabla",
288 "actor" => user.ap_id,
295 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
296 assert activity.data["ok"] == data["ok"]
297 assert activity.data["id"] == given_id
298 assert activity.data["context"] == "blabla"
299 assert activity.data["context_id"]
302 test "adds a context when none is there" do
306 "actor" => user.ap_id,
309 "actor" => user.ap_id,
316 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
317 object = Pleroma.Object.normalize(activity)
319 assert is_binary(activity.data["context"])
320 assert is_binary(object.data["context"])
321 assert activity.data["context_id"]
322 assert object.data["context_id"]
325 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
329 "actor" => user.ap_id,
332 "actor" => user.ap_id,
339 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
340 assert object = Object.normalize(activity)
341 assert is_binary(object.data["id"])
345 describe "listen activities" do
346 test "does not increase user note count" do
350 ActivityPub.listen(%{
351 to: ["https://www.w3.org/ns/activitystreams#Public"],
355 "actor" => user.ap_id,
356 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
358 "title" => "lain radio episode 1",
364 assert activity.actor == user.ap_id
366 user = User.get_cached_by_id(user.id)
367 assert user.note_count == 0
370 test "can be fetched into a timeline" do
371 _listen_activity_1 = insert(:listen)
372 _listen_activity_2 = insert(:listen)
373 _listen_activity_3 = insert(:listen)
375 timeline = ActivityPub.fetch_activities([], %{type: ["Listen"]})
377 assert length(timeline) == 3
381 describe "create activities" do
382 test "it reverts create" do
385 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
386 assert {:error, :reverted} =
387 ActivityPub.create(%{
388 to: ["user1", "user2"],
392 "to" => ["user1", "user2"],
394 "content" => "testing"
399 assert Repo.aggregate(Activity, :count, :id) == 0
400 assert Repo.aggregate(Object, :count, :id) == 0
403 test "removes doubled 'to' recipients" do
407 ActivityPub.create(%{
408 to: ["user1", "user1", "user2"],
412 "to" => ["user1", "user1", "user2"],
414 "content" => "testing"
418 assert activity.data["to"] == ["user1", "user2"]
419 assert activity.actor == user.ap_id
420 assert activity.recipients == ["user1", "user2", user.ap_id]
423 test "increases user note count only for public activities" do
427 CommonAPI.post(User.get_cached_by_id(user.id), %{
433 CommonAPI.post(User.get_cached_by_id(user.id), %{
435 visibility: "unlisted"
439 CommonAPI.post(User.get_cached_by_id(user.id), %{
441 visibility: "private"
445 CommonAPI.post(User.get_cached_by_id(user.id), %{
450 user = User.get_cached_by_id(user.id)
451 assert user.note_count == 2
454 test "increases replies count" do
456 user2 = insert(:user)
458 {:ok, activity} = CommonAPI.post(user, %{status: "1", visibility: "public"})
459 ap_id = activity.data["id"]
460 reply_data = %{status: "1", in_reply_to_status_id: activity.id}
463 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "public"))
464 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
465 assert object.data["repliesCount"] == 1
468 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "unlisted"))
469 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
470 assert object.data["repliesCount"] == 2
473 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "private"))
474 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
475 assert object.data["repliesCount"] == 2
478 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "direct"))
479 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
480 assert object.data["repliesCount"] == 2
484 describe "fetch activities for recipients" do
485 test "retrieve the activities for certain recipients" do
486 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
487 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
488 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
490 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
491 assert length(activities) == 2
492 assert activities == [activity_one, activity_two]
496 describe "fetch activities in context" do
497 test "retrieves activities that have a given context" do
498 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
499 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
500 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
501 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
502 activity_five = insert(:note_activity)
505 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
507 activities = ActivityPub.fetch_activities_for_context("2hu", %{blocking_user: user})
508 assert activities == [activity_two, activity]
511 test "doesn't return activities with filtered words" do
513 user_two = insert(:user)
514 insert(:filter, user: user, phrase: "test", hide: true)
516 {:ok, %{id: id1, data: %{"context" => context}}} = CommonAPI.post(user, %{status: "1"})
518 {:ok, %{id: id2}} = CommonAPI.post(user_two, %{status: "2", in_reply_to_status_id: id1})
520 {:ok, %{id: id3} = user_activity} =
521 CommonAPI.post(user, %{status: "3 test?", in_reply_to_status_id: id2})
523 {:ok, %{id: id4} = filtered_activity} =
524 CommonAPI.post(user_two, %{status: "4 test!", in_reply_to_status_id: id3})
526 {:ok, _} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
530 |> ActivityPub.fetch_activities_for_context(%{user: user})
533 assert length(activities) == 4
534 assert user_activity.id in activities
535 refute filtered_activity.id in activities
539 test "doesn't return blocked activities" do
540 activity_one = insert(:note_activity)
541 activity_two = insert(:note_activity)
542 activity_three = insert(:note_activity)
544 booster = insert(:user)
545 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
547 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
549 assert Enum.member?(activities, activity_two)
550 assert Enum.member?(activities, activity_three)
551 refute Enum.member?(activities, activity_one)
553 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
555 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
557 assert Enum.member?(activities, activity_two)
558 assert Enum.member?(activities, activity_three)
559 assert Enum.member?(activities, activity_one)
561 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
562 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
563 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
564 activity_three = Activity.get_by_id(activity_three.id)
566 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
568 assert Enum.member?(activities, activity_two)
569 refute Enum.member?(activities, activity_three)
570 refute Enum.member?(activities, boost_activity)
571 assert Enum.member?(activities, activity_one)
573 activities = ActivityPub.fetch_activities([], %{blocking_user: nil, skip_preload: true})
575 assert Enum.member?(activities, activity_two)
576 assert Enum.member?(activities, activity_three)
577 assert Enum.member?(activities, boost_activity)
578 assert Enum.member?(activities, activity_one)
581 test "doesn't return transitive interactions concerning blocked users" do
582 blocker = insert(:user)
583 blockee = insert(:user)
584 friend = insert(:user)
586 {:ok, _user_relationship} = User.block(blocker, blockee)
588 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
590 {:ok, activity_two} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
592 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
594 {:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
596 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker})
598 assert Enum.member?(activities, activity_one)
599 refute Enum.member?(activities, activity_two)
600 refute Enum.member?(activities, activity_three)
601 refute Enum.member?(activities, activity_four)
604 test "doesn't return announce activities with blocked users in 'to'" do
605 blocker = insert(:user)
606 blockee = insert(:user)
607 friend = insert(:user)
609 {:ok, _user_relationship} = User.block(blocker, blockee)
611 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
613 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
615 {:ok, activity_three} = CommonAPI.repeat(activity_two.id, friend)
618 ActivityPub.fetch_activities([], %{blocking_user: blocker})
619 |> Enum.map(fn act -> act.id end)
621 assert Enum.member?(activities, activity_one.id)
622 refute Enum.member?(activities, activity_two.id)
623 refute Enum.member?(activities, activity_three.id)
626 test "doesn't return announce activities with blocked users in 'cc'" do
627 blocker = insert(:user)
628 blockee = insert(:user)
629 friend = insert(:user)
631 {:ok, _user_relationship} = User.block(blocker, blockee)
633 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
635 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
637 assert object = Pleroma.Object.normalize(activity_two)
640 "actor" => friend.ap_id,
641 "object" => object.data["id"],
642 "context" => object.data["context"],
643 "type" => "Announce",
644 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
645 "cc" => [blockee.ap_id]
648 assert {:ok, activity_three} = ActivityPub.insert(data)
651 ActivityPub.fetch_activities([], %{blocking_user: blocker})
652 |> Enum.map(fn act -> act.id end)
654 assert Enum.member?(activities, activity_one.id)
655 refute Enum.member?(activities, activity_two.id)
656 refute Enum.member?(activities, activity_three.id)
659 test "doesn't return activities from blocked domains" do
660 domain = "dogwhistle.zone"
661 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
662 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
663 activity = insert(:note_activity, %{note: note})
665 {:ok, user} = User.block_domain(user, domain)
667 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
669 refute activity in activities
671 followed_user = insert(:user)
672 CommonAPI.follow(user, followed_user)
673 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user)
675 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
677 refute repeat_activity in activities
680 test "does return activities from followed users on blocked domains" do
681 domain = "meanies.social"
682 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
683 blocker = insert(:user)
685 {:ok, blocker} = User.follow(blocker, domain_user)
686 {:ok, blocker} = User.block_domain(blocker, domain)
688 assert User.following?(blocker, domain_user)
689 assert User.blocks_domain?(blocker, domain_user)
690 refute User.blocks?(blocker, domain_user)
692 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
693 activity = insert(:note_activity, %{note: note})
695 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
697 assert activity in activities
699 # And check that if the guy we DO follow boosts someone else from their domain,
700 # that should be hidden
701 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
702 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
703 bad_activity = insert(:note_activity, %{note: bad_note})
704 {:ok, repeat_activity} = CommonAPI.repeat(bad_activity.id, domain_user)
706 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
708 refute repeat_activity in activities
711 test "doesn't return muted activities" do
712 activity_one = insert(:note_activity)
713 activity_two = insert(:note_activity)
714 activity_three = insert(:note_activity)
716 booster = insert(:user)
718 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
719 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
721 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
723 assert Enum.member?(activities, activity_two)
724 assert Enum.member?(activities, activity_three)
725 refute Enum.member?(activities, activity_one)
727 # Calling with 'with_muted' will deliver muted activities, too.
729 ActivityPub.fetch_activities([], %{
735 assert Enum.member?(activities, activity_two)
736 assert Enum.member?(activities, activity_three)
737 assert Enum.member?(activities, activity_one)
739 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
741 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
743 assert Enum.member?(activities, activity_two)
744 assert Enum.member?(activities, activity_three)
745 assert Enum.member?(activities, activity_one)
747 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
748 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
749 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
750 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
751 activity_three = Activity.get_by_id(activity_three.id)
753 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
755 assert Enum.member?(activities, activity_two)
756 refute Enum.member?(activities, activity_three)
757 refute Enum.member?(activities, boost_activity)
758 assert Enum.member?(activities, activity_one)
760 activities = ActivityPub.fetch_activities([], %{muting_user: nil, skip_preload: true})
762 assert Enum.member?(activities, activity_two)
763 assert Enum.member?(activities, activity_three)
764 assert Enum.member?(activities, boost_activity)
765 assert Enum.member?(activities, activity_one)
768 test "doesn't return thread muted activities" do
770 _activity_one = insert(:note_activity)
771 note_two = insert(:note, data: %{"context" => "suya.."})
772 activity_two = insert(:note_activity, note: note_two)
774 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
776 assert [_activity_one] = ActivityPub.fetch_activities([], %{muting_user: user})
779 test "returns thread muted activities when with_muted is set" do
781 _activity_one = insert(:note_activity)
782 note_two = insert(:note, data: %{"context" => "suya.."})
783 activity_two = insert(:note_activity, note: note_two)
785 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
787 assert [_activity_two, _activity_one] =
788 ActivityPub.fetch_activities([], %{muting_user: user, with_muted: true})
791 test "does include announces on request" do
792 activity_three = insert(:note_activity)
794 booster = insert(:user)
796 {:ok, user} = User.follow(user, booster)
798 {:ok, announce} = CommonAPI.repeat(activity_three.id, booster)
800 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
802 assert announce_activity.id == announce.id
805 test "excludes reblogs on request" do
807 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
808 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
810 [activity] = ActivityPub.fetch_user_activities(user, nil, %{exclude_reblogs: true})
812 assert activity == expected_activity
815 describe "irreversible filters" do
818 user_two = insert(:user)
820 insert(:filter, user: user_two, phrase: "cofe", hide: true)
821 insert(:filter, user: user_two, phrase: "ok boomer", hide: true)
822 insert(:filter, user: user_two, phrase: "test", hide: false)
825 type: ["Create", "Announce"],
829 {:ok, %{user: user, user_two: user_two, params: params}}
832 test "it returns statuses if they don't contain exact filter words", %{
836 {:ok, _} = CommonAPI.post(user, %{status: "hey"})
837 {:ok, _} = CommonAPI.post(user, %{status: "got cofefe?"})
838 {:ok, _} = CommonAPI.post(user, %{status: "I am not a boomer"})
839 {:ok, _} = CommonAPI.post(user, %{status: "ok boomers"})
840 {:ok, _} = CommonAPI.post(user, %{status: "ccofee is not a word"})
841 {:ok, _} = CommonAPI.post(user, %{status: "this is a test"})
843 activities = ActivityPub.fetch_activities([], params)
845 assert Enum.count(activities) == 6
848 test "it does not filter user's own statuses", %{user_two: user_two, params: params} do
849 {:ok, _} = CommonAPI.post(user_two, %{status: "Give me some cofe!"})
850 {:ok, _} = CommonAPI.post(user_two, %{status: "ok boomer"})
852 activities = ActivityPub.fetch_activities([], params)
854 assert Enum.count(activities) == 2
857 test "it excludes statuses with filter words", %{user: user, params: params} do
858 {:ok, _} = CommonAPI.post(user, %{status: "Give me some cofe!"})
859 {:ok, _} = CommonAPI.post(user, %{status: "ok boomer"})
860 {:ok, _} = CommonAPI.post(user, %{status: "is it a cOfE?"})
861 {:ok, _} = CommonAPI.post(user, %{status: "cofe is all I need"})
862 {:ok, _} = CommonAPI.post(user, %{status: "— ok BOOMER\n"})
864 activities = ActivityPub.fetch_activities([], params)
866 assert Enum.empty?(activities)
869 test "it returns all statuses if user does not have any filters" do
870 another_user = insert(:user)
871 {:ok, _} = CommonAPI.post(another_user, %{status: "got cofe?"})
872 {:ok, _} = CommonAPI.post(another_user, %{status: "test!"})
875 ActivityPub.fetch_activities([], %{
876 type: ["Create", "Announce"],
880 assert Enum.count(activities) == 2
884 describe "public fetch activities" do
885 test "doesn't retrieve unlisted activities" do
888 {:ok, _unlisted_activity} = CommonAPI.post(user, %{status: "yeah", visibility: "unlisted"})
890 {:ok, listed_activity} = CommonAPI.post(user, %{status: "yeah"})
892 [activity] = ActivityPub.fetch_public_activities()
894 assert activity == listed_activity
897 test "retrieves public activities" do
898 _activities = ActivityPub.fetch_public_activities()
900 %{public: public} = ActivityBuilder.public_and_non_public()
902 activities = ActivityPub.fetch_public_activities()
903 assert length(activities) == 1
904 assert Enum.at(activities, 0) == public
907 test "retrieves a maximum of 20 activities" do
908 ActivityBuilder.insert_list(10)
909 expected_activities = ActivityBuilder.insert_list(20)
911 activities = ActivityPub.fetch_public_activities()
913 assert collect_ids(activities) == collect_ids(expected_activities)
914 assert length(activities) == 20
917 test "retrieves ids starting from a since_id" do
918 activities = ActivityBuilder.insert_list(30)
919 expected_activities = ActivityBuilder.insert_list(10)
920 since_id = List.last(activities).id
922 activities = ActivityPub.fetch_public_activities(%{since_id: since_id})
924 assert collect_ids(activities) == collect_ids(expected_activities)
925 assert length(activities) == 10
928 test "retrieves ids up to max_id" do
929 ActivityBuilder.insert_list(10)
930 expected_activities = ActivityBuilder.insert_list(20)
934 |> ActivityBuilder.insert_list()
937 activities = ActivityPub.fetch_public_activities(%{max_id: max_id})
939 assert length(activities) == 20
940 assert collect_ids(activities) == collect_ids(expected_activities)
943 test "paginates via offset/limit" do
944 _first_part_activities = ActivityBuilder.insert_list(10)
945 second_part_activities = ActivityBuilder.insert_list(10)
947 later_activities = ActivityBuilder.insert_list(10)
949 activities = ActivityPub.fetch_public_activities(%{page: "2", page_size: "20"}, :offset)
951 assert length(activities) == 20
953 assert collect_ids(activities) ==
954 collect_ids(second_part_activities) ++ collect_ids(later_activities)
957 test "doesn't return reblogs for users for whom reblogs have been muted" do
958 activity = insert(:note_activity)
960 booster = insert(:user)
961 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
963 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
965 activities = ActivityPub.fetch_activities([], %{muting_user: user})
967 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
970 test "returns reblogs for users for whom reblogs have not been muted" do
971 activity = insert(:note_activity)
973 booster = insert(:user)
974 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
975 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
977 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
979 activities = ActivityPub.fetch_activities([], %{muting_user: user})
981 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
985 describe "uploading files" do
986 test "copies the file to the configured folder" do
988 content_type: "image/jpg",
989 path: Path.absname("test/fixtures/image.jpg"),
990 filename: "an_image.jpg"
993 {:ok, %Object{} = object} = ActivityPub.upload(file)
994 assert object.data["name"] == "an_image.jpg"
997 test "works with base64 encoded images" do
1002 {:ok, %Object{}} = ActivityPub.upload(file)
1006 describe "fetch the latest Follow" do
1007 test "fetches the latest Follow activity" do
1008 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1009 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1010 followed = Repo.get_by(User, ap_id: activity.data["object"])
1012 assert activity == Utils.fetch_latest_follow(follower, followed)
1016 describe "unfollowing" do
1017 test "it reverts unfollow activity" do
1018 follower = insert(:user)
1019 followed = insert(:user)
1021 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1023 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1024 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
1027 activity = Activity.get_by_id(follow_activity.id)
1028 assert activity.data["type"] == "Follow"
1029 assert activity.data["actor"] == follower.ap_id
1031 assert activity.data["object"] == followed.ap_id
1034 test "creates an undo activity for the last follow" do
1035 follower = insert(:user)
1036 followed = insert(:user)
1038 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1039 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1041 assert activity.data["type"] == "Undo"
1042 assert activity.data["actor"] == follower.ap_id
1044 embedded_object = activity.data["object"]
1045 assert is_map(embedded_object)
1046 assert embedded_object["type"] == "Follow"
1047 assert embedded_object["object"] == followed.ap_id
1048 assert embedded_object["id"] == follow_activity.data["id"]
1051 test "creates an undo activity for a pending follow request" do
1052 follower = insert(:user)
1053 followed = insert(:user, %{locked: true})
1055 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1056 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1058 assert activity.data["type"] == "Undo"
1059 assert activity.data["actor"] == follower.ap_id
1061 embedded_object = activity.data["object"]
1062 assert is_map(embedded_object)
1063 assert embedded_object["type"] == "Follow"
1064 assert embedded_object["object"] == followed.ap_id
1065 assert embedded_object["id"] == follow_activity.data["id"]
1069 describe "timeline post-processing" do
1070 test "it filters broken threads" do
1071 user1 = insert(:user)
1072 user2 = insert(:user)
1073 user3 = insert(:user)
1075 {:ok, user1} = User.follow(user1, user3)
1076 assert User.following?(user1, user3)
1078 {:ok, user2} = User.follow(user2, user3)
1079 assert User.following?(user2, user3)
1081 {:ok, user3} = User.follow(user3, user2)
1082 assert User.following?(user3, user2)
1084 {:ok, public_activity} = CommonAPI.post(user3, %{status: "hi 1"})
1086 {:ok, private_activity_1} = CommonAPI.post(user3, %{status: "hi 2", visibility: "private"})
1088 {:ok, private_activity_2} =
1089 CommonAPI.post(user2, %{
1091 visibility: "private",
1092 in_reply_to_status_id: private_activity_1.id
1095 {:ok, private_activity_3} =
1096 CommonAPI.post(user3, %{
1098 visibility: "private",
1099 in_reply_to_status_id: private_activity_2.id
1103 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1104 |> Enum.map(fn a -> a.id end)
1106 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1108 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1110 assert length(activities) == 3
1113 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{user: user1})
1114 |> Enum.map(fn a -> a.id end)
1116 assert [public_activity.id, private_activity_1.id] == activities
1117 assert length(activities) == 2
1121 describe "flag/1" do
1123 reporter = insert(:user)
1124 target_account = insert(:user)
1126 {:ok, activity} = CommonAPI.post(target_account, %{status: content})
1127 context = Utils.generate_context_id()
1129 reporter_ap_id = reporter.ap_id
1130 target_ap_id = target_account.ap_id
1131 activity_ap_id = activity.data["id"]
1133 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1139 target_account: target_account,
1140 reported_activity: activity,
1142 activity_ap_id: activity_ap_id,
1143 activity_with_object: activity_with_object,
1144 reporter_ap_id: reporter_ap_id,
1145 target_ap_id: target_ap_id
1149 test "it can create a Flag activity",
1153 target_account: target_account,
1154 reported_activity: reported_activity,
1156 activity_ap_id: activity_ap_id,
1157 activity_with_object: activity_with_object,
1158 reporter_ap_id: reporter_ap_id,
1159 target_ap_id: target_ap_id
1161 assert {:ok, activity} =
1165 account: target_account,
1166 statuses: [reported_activity],
1172 "id" => activity_ap_id,
1173 "content" => content,
1174 "published" => activity_with_object.object.data["published"],
1175 "actor" => AccountView.render("show.json", %{user: target_account})
1179 actor: ^reporter_ap_id,
1182 "content" => ^content,
1183 "context" => ^context,
1184 "object" => [^target_ap_id, ^note_obj]
1189 test_with_mock "strips status data from Flag, before federating it",
1193 target_account: target_account,
1194 reported_activity: reported_activity,
1204 account: target_account,
1205 statuses: [reported_activity],
1210 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1212 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1216 test "fetch_activities/2 returns activities addressed to a list " do
1217 user = insert(:user)
1218 member = insert(:user)
1219 {:ok, list} = Pleroma.List.create("foo", user)
1220 {:ok, list} = Pleroma.List.follow(list, member)
1222 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1224 activity = Repo.preload(activity, :bookmark)
1225 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1227 assert ActivityPub.fetch_activities([], %{user: user}) == [activity]
1231 File.read!("test/fixtures/avatar_data_uri")
1234 describe "fetch_activities_bounded" do
1235 test "fetches private posts for followed users" do
1236 user = insert(:user)
1239 CommonAPI.post(user, %{
1240 status: "thought I looked cute might delete later :3",
1241 visibility: "private"
1244 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1245 assert result.id == activity.id
1248 test "fetches only public posts for other users" do
1249 user = insert(:user)
1250 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe", visibility: "public"})
1252 {:ok, _private_activity} =
1253 CommonAPI.post(user, %{
1254 status: "why is tenshi eating a corndog so cute?",
1255 visibility: "private"
1258 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1259 assert result.id == activity.id
1263 describe "fetch_follow_information_for_user" do
1264 test "syncronizes following/followers counters" do
1268 follower_address: "http://localhost:4001/users/fuser2/followers",
1269 following_address: "http://localhost:4001/users/fuser2/following"
1272 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1273 assert info.follower_count == 527
1274 assert info.following_count == 267
1277 test "detects hidden followers" do
1280 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1281 %Tesla.Env{status: 403, body: ""}
1284 apply(HttpRequestMock, :request, [env])
1291 follower_address: "http://localhost:4001/users/masto_closed/followers",
1292 following_address: "http://localhost:4001/users/masto_closed/following"
1295 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1296 assert follow_info.hide_followers == true
1297 assert follow_info.hide_follows == false
1300 test "detects hidden follows" do
1303 "http://localhost:4001/users/masto_closed/following?page=1" ->
1304 %Tesla.Env{status: 403, body: ""}
1307 apply(HttpRequestMock, :request, [env])
1314 follower_address: "http://localhost:4001/users/masto_closed/followers",
1315 following_address: "http://localhost:4001/users/masto_closed/following"
1318 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1319 assert follow_info.hide_followers == false
1320 assert follow_info.hide_follows == true
1323 test "detects hidden follows/followers for friendica" do
1327 follower_address: "http://localhost:8080/followers/fuser3",
1328 following_address: "http://localhost:8080/following/fuser3"
1331 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1332 assert follow_info.hide_followers == true
1333 assert follow_info.follower_count == 296
1334 assert follow_info.following_count == 32
1335 assert follow_info.hide_follows == true
1338 test "doesn't crash when follower and following counters are hidden" do
1341 "http://localhost:4001/users/masto_hidden_counters/following" ->
1343 "@context" => "https://www.w3.org/ns/activitystreams",
1344 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1347 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1348 %Tesla.Env{status: 403, body: ""}
1350 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1352 "@context" => "https://www.w3.org/ns/activitystreams",
1353 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1356 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1357 %Tesla.Env{status: 403, body: ""}
1364 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1365 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1368 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1370 assert follow_info.hide_followers == true
1371 assert follow_info.follower_count == 0
1372 assert follow_info.hide_follows == true
1373 assert follow_info.following_count == 0
1377 describe "fetch_favourites/3" do
1378 test "returns a favourite activities sorted by adds to favorite" do
1379 user = insert(:user)
1380 other_user = insert(:user)
1381 user1 = insert(:user)
1382 user2 = insert(:user)
1383 {:ok, a1} = CommonAPI.post(user1, %{status: "bla"})
1384 {:ok, _a2} = CommonAPI.post(user2, %{status: "traps are happy"})
1385 {:ok, a3} = CommonAPI.post(user2, %{status: "Trees Are "})
1386 {:ok, a4} = CommonAPI.post(user2, %{status: "Agent Smith "})
1387 {:ok, a5} = CommonAPI.post(user1, %{status: "Red or Blue "})
1389 {:ok, _} = CommonAPI.favorite(user, a4.id)
1390 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1391 {:ok, _} = CommonAPI.favorite(user, a3.id)
1392 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1393 {:ok, _} = CommonAPI.favorite(user, a5.id)
1394 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1395 {:ok, _} = CommonAPI.favorite(user, a1.id)
1396 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1397 result = ActivityPub.fetch_favourites(user)
1399 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1401 result = ActivityPub.fetch_favourites(user, %{limit: 2})
1402 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1406 describe "Move activity" do
1408 %{ap_id: old_ap_id} = old_user = insert(:user)
1409 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1410 follower = insert(:user)
1411 follower_move_opted_out = insert(:user, allow_following_move: false)
1413 User.follow(follower, old_user)
1414 User.follow(follower_move_opted_out, old_user)
1416 assert User.following?(follower, old_user)
1417 assert User.following?(follower_move_opted_out, old_user)
1419 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1424 "actor" => ^old_ap_id,
1425 "object" => ^old_ap_id,
1426 "target" => ^new_ap_id,
1433 "op" => "move_following",
1434 "origin_id" => old_user.id,
1435 "target_id" => new_user.id
1438 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1440 Pleroma.Workers.BackgroundWorker.perform(params, nil)
1442 refute User.following?(follower, old_user)
1443 assert User.following?(follower, new_user)
1445 assert User.following?(follower_move_opted_out, old_user)
1446 refute User.following?(follower_move_opted_out, new_user)
1448 activity = %Activity{activity | object: nil}
1450 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1452 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1455 test "old user must be in the new user's `also_known_as` list" do
1456 old_user = insert(:user)
1457 new_user = insert(:user)
1459 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1460 ActivityPub.move(old_user, new_user)
1464 test "doesn't retrieve replies activities with exclude_replies" do
1465 user = insert(:user)
1467 {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
1469 {:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id})
1471 [result] = ActivityPub.fetch_public_activities(%{exclude_replies: true})
1473 assert result.id == activity.id
1475 assert length(ActivityPub.fetch_public_activities()) == 2
1478 describe "replies filtering with public messages" do
1479 setup :public_messages
1481 test "public timeline", %{users: %{u1: user}} do
1484 |> Map.put(:type, ["Create", "Announce"])
1485 |> Map.put(:local_only, false)
1486 |> Map.put(:blocking_user, user)
1487 |> Map.put(:muting_user, user)
1488 |> Map.put(:reply_filtering_user, user)
1489 |> ActivityPub.fetch_public_activities()
1490 |> Enum.map(& &1.id)
1492 assert length(activities_ids) == 16
1495 test "public timeline with reply_visibility `following`", %{
1501 activities: activities
1505 |> Map.put(:type, ["Create", "Announce"])
1506 |> Map.put(:local_only, false)
1507 |> Map.put(:blocking_user, user)
1508 |> Map.put(:muting_user, user)
1509 |> Map.put(:reply_visibility, "following")
1510 |> Map.put(:reply_filtering_user, user)
1511 |> ActivityPub.fetch_public_activities()
1512 |> Enum.map(& &1.id)
1514 assert length(activities_ids) == 14
1517 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1519 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1522 test "public timeline with reply_visibility `self`", %{
1528 activities: activities
1532 |> Map.put(:type, ["Create", "Announce"])
1533 |> Map.put(:local_only, false)
1534 |> Map.put(:blocking_user, user)
1535 |> Map.put(:muting_user, user)
1536 |> Map.put(:reply_visibility, "self")
1537 |> Map.put(:reply_filtering_user, user)
1538 |> ActivityPub.fetch_public_activities()
1539 |> Enum.map(& &1.id)
1541 assert length(activities_ids) == 10
1542 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1543 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1546 test "home timeline", %{
1548 activities: activities,
1556 |> Map.put(:type, ["Create", "Announce"])
1557 |> Map.put(:blocking_user, user)
1558 |> Map.put(:muting_user, user)
1559 |> Map.put(:user, user)
1560 |> Map.put(:reply_filtering_user, user)
1563 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1564 |> Enum.map(& &1.id)
1566 assert length(activities_ids) == 13
1581 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1584 test "home timeline with reply_visibility `following`", %{
1586 activities: activities,
1594 |> Map.put(:type, ["Create", "Announce"])
1595 |> Map.put(:blocking_user, user)
1596 |> Map.put(:muting_user, user)
1597 |> Map.put(:user, user)
1598 |> Map.put(:reply_visibility, "following")
1599 |> Map.put(:reply_filtering_user, user)
1602 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1603 |> Enum.map(& &1.id)
1605 assert length(activities_ids) == 11
1620 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1623 test "home timeline with reply_visibility `self`", %{
1625 activities: activities,
1633 |> Map.put(:type, ["Create", "Announce"])
1634 |> Map.put(:blocking_user, user)
1635 |> Map.put(:muting_user, user)
1636 |> Map.put(:user, user)
1637 |> Map.put(:reply_visibility, "self")
1638 |> Map.put(:reply_filtering_user, user)
1641 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1642 |> Enum.map(& &1.id)
1644 assert length(activities_ids) == 9
1657 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1660 test "filtering out announces where the user is the actor of the announced message" do
1661 user = insert(:user)
1662 other_user = insert(:user)
1663 third_user = insert(:user)
1664 User.follow(user, other_user)
1666 {:ok, post} = CommonAPI.post(user, %{status: "yo"})
1667 {:ok, other_post} = CommonAPI.post(third_user, %{status: "yo"})
1668 {:ok, _announce} = CommonAPI.repeat(post.id, other_user)
1669 {:ok, _announce} = CommonAPI.repeat(post.id, third_user)
1670 {:ok, announce} = CommonAPI.repeat(other_post.id, other_user)
1677 [user.ap_id | User.following(user)]
1678 |> ActivityPub.fetch_activities(params)
1680 assert length(results) == 3
1684 announce_filtering_user: user
1688 [user.ap_id | User.following(user)]
1689 |> ActivityPub.fetch_activities(params)
1691 assert result.id == announce.id
1695 describe "replies filtering with private messages" do
1696 setup :private_messages
1698 test "public timeline", %{users: %{u1: user}} do
1701 |> Map.put(:type, ["Create", "Announce"])
1702 |> Map.put(:local_only, false)
1703 |> Map.put(:blocking_user, user)
1704 |> Map.put(:muting_user, user)
1705 |> Map.put(:user, user)
1706 |> ActivityPub.fetch_public_activities()
1707 |> Enum.map(& &1.id)
1709 assert activities_ids == []
1712 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1715 |> Map.put(:type, ["Create", "Announce"])
1716 |> Map.put(:local_only, false)
1717 |> Map.put(:blocking_user, user)
1718 |> Map.put(:muting_user, user)
1719 |> Map.put(:reply_visibility, "following")
1720 |> Map.put(:reply_filtering_user, user)
1721 |> Map.put(:user, user)
1722 |> ActivityPub.fetch_public_activities()
1723 |> Enum.map(& &1.id)
1725 assert activities_ids == []
1728 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
1731 |> Map.put(:type, ["Create", "Announce"])
1732 |> Map.put(:local_only, false)
1733 |> Map.put(:blocking_user, user)
1734 |> Map.put(:muting_user, user)
1735 |> Map.put(:reply_visibility, "self")
1736 |> Map.put(:reply_filtering_user, user)
1737 |> Map.put(:user, user)
1738 |> ActivityPub.fetch_public_activities()
1739 |> Enum.map(& &1.id)
1741 assert activities_ids == []
1744 test "home timeline", %{users: %{u1: user}} do
1747 |> Map.put(:type, ["Create", "Announce"])
1748 |> Map.put(:blocking_user, user)
1749 |> Map.put(:muting_user, user)
1750 |> Map.put(:user, user)
1753 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1754 |> Enum.map(& &1.id)
1756 assert length(activities_ids) == 12
1759 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
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) == 12
1776 test "home timeline with default reply_visibility `self`", %{
1778 activities: activities,
1786 |> Map.put(:type, ["Create", "Announce"])
1787 |> Map.put(:blocking_user, user)
1788 |> Map.put(:muting_user, user)
1789 |> Map.put(:user, user)
1790 |> Map.put(:reply_visibility, "self")
1791 |> Map.put(:reply_filtering_user, user)
1794 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1795 |> Enum.map(& &1.id)
1797 assert length(activities_ids) == 10
1800 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
1802 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1806 defp public_messages(_) do
1807 [u1, u2, u3, u4] = insert_list(4, :user)
1808 {:ok, u1} = User.follow(u1, u2)
1809 {:ok, u2} = User.follow(u2, u1)
1810 {:ok, u1} = User.follow(u1, u4)
1811 {:ok, u4} = User.follow(u4, u1)
1813 {:ok, u2} = User.follow(u2, u3)
1814 {:ok, u3} = User.follow(u3, u2)
1816 {:ok, a1} = CommonAPI.post(u1, %{status: "Status"})
1819 CommonAPI.post(u2, %{
1820 status: "@#{u1.nickname} reply from u2 to u1",
1821 in_reply_to_status_id: a1.id
1825 CommonAPI.post(u3, %{
1826 status: "@#{u1.nickname} reply from u3 to u1",
1827 in_reply_to_status_id: a1.id
1831 CommonAPI.post(u4, %{
1832 status: "@#{u1.nickname} reply from u4 to u1",
1833 in_reply_to_status_id: a1.id
1836 {:ok, a2} = CommonAPI.post(u2, %{status: "Status"})
1839 CommonAPI.post(u1, %{
1840 status: "@#{u2.nickname} reply from u1 to u2",
1841 in_reply_to_status_id: a2.id
1845 CommonAPI.post(u3, %{
1846 status: "@#{u2.nickname} reply from u3 to u2",
1847 in_reply_to_status_id: a2.id
1851 CommonAPI.post(u4, %{
1852 status: "@#{u2.nickname} reply from u4 to u2",
1853 in_reply_to_status_id: a2.id
1856 {:ok, a3} = CommonAPI.post(u3, %{status: "Status"})
1859 CommonAPI.post(u1, %{
1860 status: "@#{u3.nickname} reply from u1 to u3",
1861 in_reply_to_status_id: a3.id
1865 CommonAPI.post(u2, %{
1866 status: "@#{u3.nickname} reply from u2 to u3",
1867 in_reply_to_status_id: a3.id
1871 CommonAPI.post(u4, %{
1872 status: "@#{u3.nickname} reply from u4 to u3",
1873 in_reply_to_status_id: a3.id
1876 {:ok, a4} = CommonAPI.post(u4, %{status: "Status"})
1879 CommonAPI.post(u1, %{
1880 status: "@#{u4.nickname} reply from u1 to u4",
1881 in_reply_to_status_id: a4.id
1885 CommonAPI.post(u2, %{
1886 status: "@#{u4.nickname} reply from u2 to u4",
1887 in_reply_to_status_id: a4.id
1891 CommonAPI.post(u3, %{
1892 status: "@#{u4.nickname} reply from u3 to u4",
1893 in_reply_to_status_id: a4.id
1897 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
1898 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
1899 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
1900 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
1901 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
1902 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
1905 defp private_messages(_) do
1906 [u1, u2, u3, u4] = insert_list(4, :user)
1907 {:ok, u1} = User.follow(u1, u2)
1908 {:ok, u2} = User.follow(u2, u1)
1909 {:ok, u1} = User.follow(u1, u3)
1910 {:ok, u3} = User.follow(u3, u1)
1911 {:ok, u1} = User.follow(u1, u4)
1912 {:ok, u4} = User.follow(u4, u1)
1914 {:ok, u2} = User.follow(u2, u3)
1915 {:ok, u3} = User.follow(u3, u2)
1917 {:ok, a1} = CommonAPI.post(u1, %{status: "Status", visibility: "private"})
1920 CommonAPI.post(u2, %{
1921 status: "@#{u1.nickname} reply from u2 to u1",
1922 in_reply_to_status_id: a1.id,
1923 visibility: "private"
1927 CommonAPI.post(u3, %{
1928 status: "@#{u1.nickname} reply from u3 to u1",
1929 in_reply_to_status_id: a1.id,
1930 visibility: "private"
1934 CommonAPI.post(u4, %{
1935 status: "@#{u1.nickname} reply from u4 to u1",
1936 in_reply_to_status_id: a1.id,
1937 visibility: "private"
1940 {:ok, a2} = CommonAPI.post(u2, %{status: "Status", visibility: "private"})
1943 CommonAPI.post(u1, %{
1944 status: "@#{u2.nickname} reply from u1 to u2",
1945 in_reply_to_status_id: a2.id,
1946 visibility: "private"
1950 CommonAPI.post(u3, %{
1951 status: "@#{u2.nickname} reply from u3 to u2",
1952 in_reply_to_status_id: a2.id,
1953 visibility: "private"
1956 {:ok, a3} = CommonAPI.post(u3, %{status: "Status", visibility: "private"})
1959 CommonAPI.post(u1, %{
1960 status: "@#{u3.nickname} reply from u1 to u3",
1961 in_reply_to_status_id: a3.id,
1962 visibility: "private"
1966 CommonAPI.post(u2, %{
1967 status: "@#{u3.nickname} reply from u2 to u3",
1968 in_reply_to_status_id: a3.id,
1969 visibility: "private"
1972 {:ok, a4} = CommonAPI.post(u4, %{status: "Status", visibility: "private"})
1975 CommonAPI.post(u1, %{
1976 status: "@#{u4.nickname} reply from u1 to u4",
1977 in_reply_to_status_id: a4.id,
1978 visibility: "private"
1982 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
1983 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
1984 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
1985 u2: %{r1: r2_1.id, r2: r2_2.id},
1986 u3: %{r1: r3_1.id, r2: r3_2.id},
1990 describe "maybe_update_follow_information/1" do
1992 clear_config([:instance, :external_user_synchronization], true)
1996 ap_id: "https://gensokyo.2hu/users/raymoo",
1997 following_address: "https://gensokyo.2hu/users/following",
1998 follower_address: "https://gensokyo.2hu/users/followers",
2005 test "logs an error when it can't fetch the info", %{user: user} do
2006 assert capture_log(fn ->
2007 ActivityPub.maybe_update_follow_information(user)
2008 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2011 test "just returns the input if the user type is Application", %{
2016 |> Map.put(:type, "Application")
2018 refute capture_log(fn ->
2019 assert ^user = ActivityPub.maybe_update_follow_information(user)
2020 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2023 test "it just returns the input if the user has no following/follower addresses", %{
2028 |> Map.put(:following_address, nil)
2029 |> Map.put(:follower_address, nil)
2031 refute capture_log(fn ->
2032 assert ^user = ActivityPub.maybe_update_follow_information(user)
2033 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2037 describe "global activity expiration" do
2038 setup do: clear_config([:mrf, :policies])
2040 test "creates an activity expiration for local Create activities" do
2043 Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy
2046 {:ok, %{id: id_create}} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
2047 {:ok, _follow} = ActivityBuilder.insert(%{"type" => "Follow", "context" => "3hu"})
2049 assert [%{activity_id: ^id_create}] = Pleroma.ActivityExpiration |> Repo.all()