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 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
195 test "it fetches the appropriate tag-restricted posts" do
198 {:ok, status_one} = CommonAPI.post(user, %{status: ". #test"})
199 {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
200 {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #reject"})
202 fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"})
204 fetch_two = ActivityPub.fetch_activities([], %{type: "Create", tag: ["test", "essais"]})
207 ActivityPub.fetch_activities([], %{
209 tag: ["test", "essais"],
210 tag_reject: ["reject"]
214 ActivityPub.fetch_activities([], %{
217 tag_all: ["test", "reject"]
220 assert fetch_one == [status_one, status_three]
221 assert fetch_two == [status_one, status_two, status_three]
222 assert fetch_three == [status_one, status_two]
223 assert fetch_four == [status_three]
226 describe "insertion" do
227 test "drops activities beyond a certain limit" do
228 limit = Config.get([:instance, :remote_limit])
231 :crypto.strong_rand_bytes(limit + 1)
233 |> binary_part(0, limit + 1)
238 "content" => random_text
242 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
245 test "doesn't drop activities with content being null" do
249 "actor" => user.ap_id,
252 "actor" => user.ap_id,
259 assert {:ok, _} = ActivityPub.insert(data)
262 test "returns the activity if one with the same id is already in" do
263 activity = insert(:note_activity)
264 {:ok, new_activity} = ActivityPub.insert(activity.data)
266 assert activity.id == new_activity.id
269 test "inserts a given map into the activity database, giving it an id if it has none." do
273 "actor" => user.ap_id,
276 "actor" => user.ap_id,
283 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
284 assert activity.data["ok"] == data["ok"]
285 assert is_binary(activity.data["id"])
291 "actor" => user.ap_id,
293 "context" => "blabla",
295 "actor" => user.ap_id,
302 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
303 assert activity.data["ok"] == data["ok"]
304 assert activity.data["id"] == given_id
305 assert activity.data["context"] == "blabla"
306 assert activity.data["context_id"]
309 test "adds a context when none is there" do
313 "actor" => user.ap_id,
316 "actor" => user.ap_id,
323 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
324 object = Pleroma.Object.normalize(activity)
326 assert is_binary(activity.data["context"])
327 assert is_binary(object.data["context"])
328 assert activity.data["context_id"]
329 assert object.data["context_id"]
332 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
336 "actor" => user.ap_id,
339 "actor" => user.ap_id,
346 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
347 assert object = Object.normalize(activity)
348 assert is_binary(object.data["id"])
352 describe "listen activities" do
353 test "does not increase user note count" do
357 ActivityPub.listen(%{
358 to: ["https://www.w3.org/ns/activitystreams#Public"],
362 "actor" => user.ap_id,
363 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
365 "title" => "lain radio episode 1",
371 assert activity.actor == user.ap_id
373 user = User.get_cached_by_id(user.id)
374 assert user.note_count == 0
377 test "can be fetched into a timeline" do
378 _listen_activity_1 = insert(:listen)
379 _listen_activity_2 = insert(:listen)
380 _listen_activity_3 = insert(:listen)
382 timeline = ActivityPub.fetch_activities([], %{type: ["Listen"]})
384 assert length(timeline) == 3
388 describe "create activities" do
389 test "it reverts create" do
392 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
393 assert {:error, :reverted} =
394 ActivityPub.create(%{
395 to: ["user1", "user2"],
399 "to" => ["user1", "user2"],
401 "content" => "testing"
406 assert Repo.aggregate(Activity, :count, :id) == 0
407 assert Repo.aggregate(Object, :count, :id) == 0
410 test "removes doubled 'to' recipients" do
414 ActivityPub.create(%{
415 to: ["user1", "user1", "user2"],
419 "to" => ["user1", "user1", "user2"],
421 "content" => "testing"
425 assert activity.data["to"] == ["user1", "user2"]
426 assert activity.actor == user.ap_id
427 assert activity.recipients == ["user1", "user2", user.ap_id]
430 test "increases user note count only for public activities" do
434 CommonAPI.post(User.get_cached_by_id(user.id), %{
440 CommonAPI.post(User.get_cached_by_id(user.id), %{
442 visibility: "unlisted"
446 CommonAPI.post(User.get_cached_by_id(user.id), %{
448 visibility: "private"
452 CommonAPI.post(User.get_cached_by_id(user.id), %{
457 user = User.get_cached_by_id(user.id)
458 assert user.note_count == 2
461 test "increases replies count" do
463 user2 = insert(:user)
465 {:ok, activity} = CommonAPI.post(user, %{status: "1", visibility: "public"})
466 ap_id = activity.data["id"]
467 reply_data = %{status: "1", in_reply_to_status_id: activity.id}
470 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "public"))
471 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
472 assert object.data["repliesCount"] == 1
475 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "unlisted"))
476 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
477 assert object.data["repliesCount"] == 2
480 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "private"))
481 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
482 assert object.data["repliesCount"] == 2
485 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, :visibility, "direct"))
486 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
487 assert object.data["repliesCount"] == 2
491 describe "fetch activities for recipients" do
492 test "retrieve the activities for certain recipients" do
493 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
494 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
495 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
497 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
498 assert length(activities) == 2
499 assert activities == [activity_one, activity_two]
503 describe "fetch activities in context" do
504 test "retrieves activities that have a given context" do
505 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
506 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
507 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
508 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
509 activity_five = insert(:note_activity)
512 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
514 activities = ActivityPub.fetch_activities_for_context("2hu", %{blocking_user: user})
515 assert activities == [activity_two, activity]
518 test "doesn't return activities with filtered words" do
520 user_two = insert(:user)
521 insert(:filter, user: user, phrase: "test", hide: true)
523 {:ok, %{id: id1, data: %{"context" => context}}} = CommonAPI.post(user, %{status: "1"})
525 {:ok, %{id: id2}} = CommonAPI.post(user_two, %{status: "2", in_reply_to_status_id: id1})
527 {:ok, %{id: id3} = user_activity} =
528 CommonAPI.post(user, %{status: "3 test?", in_reply_to_status_id: id2})
530 {:ok, %{id: id4} = filtered_activity} =
531 CommonAPI.post(user_two, %{status: "4 test!", in_reply_to_status_id: id3})
533 {:ok, _} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
537 |> ActivityPub.fetch_activities_for_context(%{user: user})
540 assert length(activities) == 4
541 assert user_activity.id in activities
542 refute filtered_activity.id in activities
546 test "doesn't return blocked activities" do
547 activity_one = insert(:note_activity)
548 activity_two = insert(:note_activity)
549 activity_three = insert(:note_activity)
551 booster = insert(:user)
552 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
554 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
556 assert Enum.member?(activities, activity_two)
557 assert Enum.member?(activities, activity_three)
558 refute Enum.member?(activities, activity_one)
560 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
562 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
564 assert Enum.member?(activities, activity_two)
565 assert Enum.member?(activities, activity_three)
566 assert Enum.member?(activities, activity_one)
568 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
569 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
570 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
571 activity_three = Activity.get_by_id(activity_three.id)
573 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
575 assert Enum.member?(activities, activity_two)
576 refute Enum.member?(activities, activity_three)
577 refute Enum.member?(activities, boost_activity)
578 assert Enum.member?(activities, activity_one)
580 activities = ActivityPub.fetch_activities([], %{blocking_user: nil, skip_preload: true})
582 assert Enum.member?(activities, activity_two)
583 assert Enum.member?(activities, activity_three)
584 assert Enum.member?(activities, boost_activity)
585 assert Enum.member?(activities, activity_one)
588 test "doesn't return transitive interactions concerning blocked users" do
589 blocker = insert(:user)
590 blockee = insert(:user)
591 friend = insert(:user)
593 {:ok, _user_relationship} = User.block(blocker, blockee)
595 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
597 {:ok, activity_two} = CommonAPI.post(friend, %{status: "hey! @#{blockee.nickname}"})
599 {:ok, activity_three} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
601 {:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
603 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker})
605 assert Enum.member?(activities, activity_one)
606 refute Enum.member?(activities, activity_two)
607 refute Enum.member?(activities, activity_three)
608 refute Enum.member?(activities, activity_four)
611 test "doesn't return announce activities with blocked users in 'to'" do
612 blocker = insert(:user)
613 blockee = insert(:user)
614 friend = insert(:user)
616 {:ok, _user_relationship} = User.block(blocker, blockee)
618 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
620 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
622 {:ok, activity_three} = CommonAPI.repeat(activity_two.id, friend)
625 ActivityPub.fetch_activities([], %{blocking_user: blocker})
626 |> Enum.map(fn act -> act.id end)
628 assert Enum.member?(activities, activity_one.id)
629 refute Enum.member?(activities, activity_two.id)
630 refute Enum.member?(activities, activity_three.id)
633 test "doesn't return announce activities with blocked users in 'cc'" do
634 blocker = insert(:user)
635 blockee = insert(:user)
636 friend = insert(:user)
638 {:ok, _user_relationship} = User.block(blocker, blockee)
640 {:ok, activity_one} = CommonAPI.post(friend, %{status: "hey!"})
642 {:ok, activity_two} = CommonAPI.post(blockee, %{status: "hey! @#{friend.nickname}"})
644 assert object = Pleroma.Object.normalize(activity_two)
647 "actor" => friend.ap_id,
648 "object" => object.data["id"],
649 "context" => object.data["context"],
650 "type" => "Announce",
651 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
652 "cc" => [blockee.ap_id]
655 assert {:ok, activity_three} = ActivityPub.insert(data)
658 ActivityPub.fetch_activities([], %{blocking_user: blocker})
659 |> Enum.map(fn act -> act.id end)
661 assert Enum.member?(activities, activity_one.id)
662 refute Enum.member?(activities, activity_two.id)
663 refute Enum.member?(activities, activity_three.id)
666 test "doesn't return activities from blocked domains" do
667 domain = "dogwhistle.zone"
668 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
669 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
670 activity = insert(:note_activity, %{note: note})
672 {:ok, user} = User.block_domain(user, domain)
674 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
676 refute activity in activities
678 followed_user = insert(:user)
679 CommonAPI.follow(user, followed_user)
680 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user)
682 activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
684 refute repeat_activity in activities
687 test "does return activities from followed users on blocked domains" do
688 domain = "meanies.social"
689 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
690 blocker = insert(:user)
692 {:ok, blocker} = User.follow(blocker, domain_user)
693 {:ok, blocker} = User.block_domain(blocker, domain)
695 assert User.following?(blocker, domain_user)
696 assert User.blocks_domain?(blocker, domain_user)
697 refute User.blocks?(blocker, domain_user)
699 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
700 activity = insert(:note_activity, %{note: note})
702 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
704 assert activity in activities
706 # And check that if the guy we DO follow boosts someone else from their domain,
707 # that should be hidden
708 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
709 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
710 bad_activity = insert(:note_activity, %{note: bad_note})
711 {:ok, repeat_activity} = CommonAPI.repeat(bad_activity.id, domain_user)
713 activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
715 refute repeat_activity in activities
718 test "doesn't return muted activities" do
719 activity_one = insert(:note_activity)
720 activity_two = insert(:note_activity)
721 activity_three = insert(:note_activity)
723 booster = insert(:user)
725 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
726 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
728 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
730 assert Enum.member?(activities, activity_two)
731 assert Enum.member?(activities, activity_three)
732 refute Enum.member?(activities, activity_one)
734 # Calling with 'with_muted' will deliver muted activities, too.
736 ActivityPub.fetch_activities([], %{
742 assert Enum.member?(activities, activity_two)
743 assert Enum.member?(activities, activity_three)
744 assert Enum.member?(activities, activity_one)
746 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
748 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
750 assert Enum.member?(activities, activity_two)
751 assert Enum.member?(activities, activity_three)
752 assert Enum.member?(activities, activity_one)
754 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
755 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
756 {:ok, %{data: %{"object" => id}}} = CommonAPI.repeat(activity_three.id, booster)
757 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
758 activity_three = Activity.get_by_id(activity_three.id)
760 activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
762 assert Enum.member?(activities, activity_two)
763 refute Enum.member?(activities, activity_three)
764 refute Enum.member?(activities, boost_activity)
765 assert Enum.member?(activities, activity_one)
767 activities = ActivityPub.fetch_activities([], %{muting_user: nil, skip_preload: true})
769 assert Enum.member?(activities, activity_two)
770 assert Enum.member?(activities, activity_three)
771 assert Enum.member?(activities, boost_activity)
772 assert Enum.member?(activities, activity_one)
775 test "doesn't return thread muted activities" do
777 _activity_one = insert(:note_activity)
778 note_two = insert(:note, data: %{"context" => "suya.."})
779 activity_two = insert(:note_activity, note: note_two)
781 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
783 assert [_activity_one] = ActivityPub.fetch_activities([], %{muting_user: user})
786 test "returns thread muted activities when with_muted is set" do
788 _activity_one = insert(:note_activity)
789 note_two = insert(:note, data: %{"context" => "suya.."})
790 activity_two = insert(:note_activity, note: note_two)
792 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
794 assert [_activity_two, _activity_one] =
795 ActivityPub.fetch_activities([], %{muting_user: user, with_muted: true})
798 test "does include announces on request" do
799 activity_three = insert(:note_activity)
801 booster = insert(:user)
803 {:ok, user} = User.follow(user, booster)
805 {:ok, announce} = CommonAPI.repeat(activity_three.id, booster)
807 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
809 assert announce_activity.id == announce.id
812 test "excludes reblogs on request" do
814 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
815 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
817 [activity] = ActivityPub.fetch_user_activities(user, nil, %{exclude_reblogs: true})
819 assert activity == expected_activity
822 describe "irreversible filters" do
825 user_two = insert(:user)
827 insert(:filter, user: user_two, phrase: "cofe", hide: true)
828 insert(:filter, user: user_two, phrase: "ok boomer", hide: true)
829 insert(:filter, user: user_two, phrase: "test", hide: false)
832 type: ["Create", "Announce"],
836 {:ok, %{user: user, user_two: user_two, params: params}}
839 test "it returns statuses if they don't contain exact filter words", %{
843 {:ok, _} = CommonAPI.post(user, %{status: "hey"})
844 {:ok, _} = CommonAPI.post(user, %{status: "got cofefe?"})
845 {:ok, _} = CommonAPI.post(user, %{status: "I am not a boomer"})
846 {:ok, _} = CommonAPI.post(user, %{status: "ok boomers"})
847 {:ok, _} = CommonAPI.post(user, %{status: "ccofee is not a word"})
848 {:ok, _} = CommonAPI.post(user, %{status: "this is a test"})
850 activities = ActivityPub.fetch_activities([], params)
852 assert Enum.count(activities) == 6
855 test "it does not filter user's own statuses", %{user_two: user_two, params: params} do
856 {:ok, _} = CommonAPI.post(user_two, %{status: "Give me some cofe!"})
857 {:ok, _} = CommonAPI.post(user_two, %{status: "ok boomer"})
859 activities = ActivityPub.fetch_activities([], params)
861 assert Enum.count(activities) == 2
864 test "it excludes statuses with filter words", %{user: user, params: params} do
865 {:ok, _} = CommonAPI.post(user, %{status: "Give me some cofe!"})
866 {:ok, _} = CommonAPI.post(user, %{status: "ok boomer"})
867 {:ok, _} = CommonAPI.post(user, %{status: "is it a cOfE?"})
868 {:ok, _} = CommonAPI.post(user, %{status: "cofe is all I need"})
869 {:ok, _} = CommonAPI.post(user, %{status: "— ok BOOMER\n"})
871 activities = ActivityPub.fetch_activities([], params)
873 assert Enum.empty?(activities)
876 test "it returns all statuses if user does not have any filters" do
877 another_user = insert(:user)
878 {:ok, _} = CommonAPI.post(another_user, %{status: "got cofe?"})
879 {:ok, _} = CommonAPI.post(another_user, %{status: "test!"})
882 ActivityPub.fetch_activities([], %{
883 type: ["Create", "Announce"],
887 assert Enum.count(activities) == 2
891 describe "public fetch activities" do
892 test "doesn't retrieve unlisted activities" do
895 {:ok, _unlisted_activity} = CommonAPI.post(user, %{status: "yeah", visibility: "unlisted"})
897 {:ok, listed_activity} = CommonAPI.post(user, %{status: "yeah"})
899 [activity] = ActivityPub.fetch_public_activities()
901 assert activity == listed_activity
904 test "retrieves public activities" do
905 _activities = ActivityPub.fetch_public_activities()
907 %{public: public} = ActivityBuilder.public_and_non_public()
909 activities = ActivityPub.fetch_public_activities()
910 assert length(activities) == 1
911 assert Enum.at(activities, 0) == public
914 test "retrieves a maximum of 20 activities" do
915 ActivityBuilder.insert_list(10)
916 expected_activities = ActivityBuilder.insert_list(20)
918 activities = ActivityPub.fetch_public_activities()
920 assert collect_ids(activities) == collect_ids(expected_activities)
921 assert length(activities) == 20
924 test "retrieves ids starting from a since_id" do
925 activities = ActivityBuilder.insert_list(30)
926 expected_activities = ActivityBuilder.insert_list(10)
927 since_id = List.last(activities).id
929 activities = ActivityPub.fetch_public_activities(%{since_id: since_id})
931 assert collect_ids(activities) == collect_ids(expected_activities)
932 assert length(activities) == 10
935 test "retrieves ids up to max_id" do
936 ActivityBuilder.insert_list(10)
937 expected_activities = ActivityBuilder.insert_list(20)
941 |> ActivityBuilder.insert_list()
944 activities = ActivityPub.fetch_public_activities(%{max_id: max_id})
946 assert length(activities) == 20
947 assert collect_ids(activities) == collect_ids(expected_activities)
950 test "paginates via offset/limit" do
951 _first_part_activities = ActivityBuilder.insert_list(10)
952 second_part_activities = ActivityBuilder.insert_list(10)
954 later_activities = ActivityBuilder.insert_list(10)
956 activities = ActivityPub.fetch_public_activities(%{page: "2", page_size: "20"}, :offset)
958 assert length(activities) == 20
960 assert collect_ids(activities) ==
961 collect_ids(second_part_activities) ++ collect_ids(later_activities)
964 test "doesn't return reblogs for users for whom reblogs have been muted" do
965 activity = insert(:note_activity)
967 booster = insert(:user)
968 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
970 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
972 activities = ActivityPub.fetch_activities([], %{muting_user: user})
974 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
977 test "returns reblogs for users for whom reblogs have not been muted" do
978 activity = insert(:note_activity)
980 booster = insert(:user)
981 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
982 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
984 {:ok, activity} = CommonAPI.repeat(activity.id, booster)
986 activities = ActivityPub.fetch_activities([], %{muting_user: user})
988 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
992 describe "uploading files" do
993 test "copies the file to the configured folder" do
995 content_type: "image/jpg",
996 path: Path.absname("test/fixtures/image.jpg"),
997 filename: "an_image.jpg"
1000 {:ok, %Object{} = object} = ActivityPub.upload(file)
1001 assert object.data["name"] == "an_image.jpg"
1004 test "works with base64 encoded images" do
1009 {:ok, %Object{}} = ActivityPub.upload(file)
1013 describe "fetch the latest Follow" do
1014 test "fetches the latest Follow activity" do
1015 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1016 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1017 followed = Repo.get_by(User, ap_id: activity.data["object"])
1019 assert activity == Utils.fetch_latest_follow(follower, followed)
1023 describe "unfollowing" do
1024 test "it reverts unfollow activity" do
1025 follower = insert(:user)
1026 followed = insert(:user)
1028 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1030 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1031 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
1034 activity = Activity.get_by_id(follow_activity.id)
1035 assert activity.data["type"] == "Follow"
1036 assert activity.data["actor"] == follower.ap_id
1038 assert activity.data["object"] == followed.ap_id
1041 test "creates an undo activity for the last follow" do
1042 follower = insert(:user)
1043 followed = insert(:user)
1045 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1046 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1048 assert activity.data["type"] == "Undo"
1049 assert activity.data["actor"] == follower.ap_id
1051 embedded_object = activity.data["object"]
1052 assert is_map(embedded_object)
1053 assert embedded_object["type"] == "Follow"
1054 assert embedded_object["object"] == followed.ap_id
1055 assert embedded_object["id"] == follow_activity.data["id"]
1058 test "creates an undo activity for a pending follow request" do
1059 follower = insert(:user)
1060 followed = insert(:user, %{locked: true})
1062 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, followed)
1063 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1065 assert activity.data["type"] == "Undo"
1066 assert activity.data["actor"] == follower.ap_id
1068 embedded_object = activity.data["object"]
1069 assert is_map(embedded_object)
1070 assert embedded_object["type"] == "Follow"
1071 assert embedded_object["object"] == followed.ap_id
1072 assert embedded_object["id"] == follow_activity.data["id"]
1076 describe "timeline post-processing" do
1077 test "it filters broken threads" do
1078 user1 = insert(:user)
1079 user2 = insert(:user)
1080 user3 = insert(:user)
1082 {:ok, user1} = User.follow(user1, user3)
1083 assert User.following?(user1, user3)
1085 {:ok, user2} = User.follow(user2, user3)
1086 assert User.following?(user2, user3)
1088 {:ok, user3} = User.follow(user3, user2)
1089 assert User.following?(user3, user2)
1091 {:ok, public_activity} = CommonAPI.post(user3, %{status: "hi 1"})
1093 {:ok, private_activity_1} = CommonAPI.post(user3, %{status: "hi 2", visibility: "private"})
1095 {:ok, private_activity_2} =
1096 CommonAPI.post(user2, %{
1098 visibility: "private",
1099 in_reply_to_status_id: private_activity_1.id
1102 {:ok, private_activity_3} =
1103 CommonAPI.post(user3, %{
1105 visibility: "private",
1106 in_reply_to_status_id: private_activity_2.id
1110 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1111 |> Enum.map(fn a -> a.id end)
1113 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1115 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1117 assert length(activities) == 3
1120 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{user: user1})
1121 |> Enum.map(fn a -> a.id end)
1123 assert [public_activity.id, private_activity_1.id] == activities
1124 assert length(activities) == 2
1128 describe "flag/1" do
1130 reporter = insert(:user)
1131 target_account = insert(:user)
1133 {:ok, activity} = CommonAPI.post(target_account, %{status: content})
1134 context = Utils.generate_context_id()
1136 reporter_ap_id = reporter.ap_id
1137 target_ap_id = target_account.ap_id
1138 activity_ap_id = activity.data["id"]
1140 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1146 target_account: target_account,
1147 reported_activity: activity,
1149 activity_ap_id: activity_ap_id,
1150 activity_with_object: activity_with_object,
1151 reporter_ap_id: reporter_ap_id,
1152 target_ap_id: target_ap_id
1156 test "it can create a Flag activity",
1160 target_account: target_account,
1161 reported_activity: reported_activity,
1163 activity_ap_id: activity_ap_id,
1164 activity_with_object: activity_with_object,
1165 reporter_ap_id: reporter_ap_id,
1166 target_ap_id: target_ap_id
1168 assert {:ok, activity} =
1172 account: target_account,
1173 statuses: [reported_activity],
1179 "id" => activity_ap_id,
1180 "content" => content,
1181 "published" => activity_with_object.object.data["published"],
1183 AccountView.render("show.json", %{user: target_account, skip_visibility_check: true})
1187 actor: ^reporter_ap_id,
1190 "content" => ^content,
1191 "context" => ^context,
1192 "object" => [^target_ap_id, ^note_obj]
1197 test_with_mock "strips status data from Flag, before federating it",
1201 target_account: target_account,
1202 reported_activity: reported_activity,
1212 account: target_account,
1213 statuses: [reported_activity],
1218 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1220 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1224 test "fetch_activities/2 returns activities addressed to a list " do
1225 user = insert(:user)
1226 member = insert(:user)
1227 {:ok, list} = Pleroma.List.create("foo", user)
1228 {:ok, list} = Pleroma.List.follow(list, member)
1230 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
1232 activity = Repo.preload(activity, :bookmark)
1233 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1235 assert ActivityPub.fetch_activities([], %{user: user}) == [activity]
1239 File.read!("test/fixtures/avatar_data_uri")
1242 describe "fetch_activities_bounded" do
1243 test "fetches private posts for followed users" do
1244 user = insert(:user)
1247 CommonAPI.post(user, %{
1248 status: "thought I looked cute might delete later :3",
1249 visibility: "private"
1252 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1253 assert result.id == activity.id
1256 test "fetches only public posts for other users" do
1257 user = insert(:user)
1258 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe", visibility: "public"})
1260 {:ok, _private_activity} =
1261 CommonAPI.post(user, %{
1262 status: "why is tenshi eating a corndog so cute?",
1263 visibility: "private"
1266 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1267 assert result.id == activity.id
1271 describe "fetch_follow_information_for_user" do
1272 test "syncronizes following/followers counters" do
1276 follower_address: "http://localhost:4001/users/fuser2/followers",
1277 following_address: "http://localhost:4001/users/fuser2/following"
1280 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1281 assert info.follower_count == 527
1282 assert info.following_count == 267
1285 test "detects hidden followers" do
1288 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1289 %Tesla.Env{status: 403, body: ""}
1292 apply(HttpRequestMock, :request, [env])
1299 follower_address: "http://localhost:4001/users/masto_closed/followers",
1300 following_address: "http://localhost:4001/users/masto_closed/following"
1303 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1304 assert follow_info.hide_followers == true
1305 assert follow_info.hide_follows == false
1308 test "detects hidden follows" do
1311 "http://localhost:4001/users/masto_closed/following?page=1" ->
1312 %Tesla.Env{status: 403, body: ""}
1315 apply(HttpRequestMock, :request, [env])
1322 follower_address: "http://localhost:4001/users/masto_closed/followers",
1323 following_address: "http://localhost:4001/users/masto_closed/following"
1326 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1327 assert follow_info.hide_followers == false
1328 assert follow_info.hide_follows == true
1331 test "detects hidden follows/followers for friendica" do
1335 follower_address: "http://localhost:8080/followers/fuser3",
1336 following_address: "http://localhost:8080/following/fuser3"
1339 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1340 assert follow_info.hide_followers == true
1341 assert follow_info.follower_count == 296
1342 assert follow_info.following_count == 32
1343 assert follow_info.hide_follows == true
1346 test "doesn't crash when follower and following counters are hidden" do
1349 "http://localhost:4001/users/masto_hidden_counters/following" ->
1351 "@context" => "https://www.w3.org/ns/activitystreams",
1352 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1355 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1356 %Tesla.Env{status: 403, body: ""}
1358 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1360 "@context" => "https://www.w3.org/ns/activitystreams",
1361 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1364 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1365 %Tesla.Env{status: 403, body: ""}
1372 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1373 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1376 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1378 assert follow_info.hide_followers == true
1379 assert follow_info.follower_count == 0
1380 assert follow_info.hide_follows == true
1381 assert follow_info.following_count == 0
1385 describe "fetch_favourites/3" do
1386 test "returns a favourite activities sorted by adds to favorite" do
1387 user = insert(:user)
1388 other_user = insert(:user)
1389 user1 = insert(:user)
1390 user2 = insert(:user)
1391 {:ok, a1} = CommonAPI.post(user1, %{status: "bla"})
1392 {:ok, _a2} = CommonAPI.post(user2, %{status: "traps are happy"})
1393 {:ok, a3} = CommonAPI.post(user2, %{status: "Trees Are "})
1394 {:ok, a4} = CommonAPI.post(user2, %{status: "Agent Smith "})
1395 {:ok, a5} = CommonAPI.post(user1, %{status: "Red or Blue "})
1397 {:ok, _} = CommonAPI.favorite(user, a4.id)
1398 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1399 {:ok, _} = CommonAPI.favorite(user, a3.id)
1400 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1401 {:ok, _} = CommonAPI.favorite(user, a5.id)
1402 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1403 {:ok, _} = CommonAPI.favorite(user, a1.id)
1404 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1405 result = ActivityPub.fetch_favourites(user)
1407 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1409 result = ActivityPub.fetch_favourites(user, %{limit: 2})
1410 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1414 describe "Move activity" do
1416 %{ap_id: old_ap_id} = old_user = insert(:user)
1417 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1418 follower = insert(:user)
1419 follower_move_opted_out = insert(:user, allow_following_move: false)
1421 User.follow(follower, old_user)
1422 User.follow(follower_move_opted_out, old_user)
1424 assert User.following?(follower, old_user)
1425 assert User.following?(follower_move_opted_out, old_user)
1427 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1432 "actor" => ^old_ap_id,
1433 "object" => ^old_ap_id,
1434 "target" => ^new_ap_id,
1441 "op" => "move_following",
1442 "origin_id" => old_user.id,
1443 "target_id" => new_user.id
1446 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1448 Pleroma.Workers.BackgroundWorker.perform(%Oban.Job{args: params})
1450 refute User.following?(follower, old_user)
1451 assert User.following?(follower, new_user)
1453 assert User.following?(follower_move_opted_out, old_user)
1454 refute User.following?(follower_move_opted_out, new_user)
1456 activity = %Activity{activity | object: nil}
1458 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1460 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1463 test "old user must be in the new user's `also_known_as` list" do
1464 old_user = insert(:user)
1465 new_user = insert(:user)
1467 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1468 ActivityPub.move(old_user, new_user)
1472 test "doesn't retrieve replies activities with exclude_replies" do
1473 user = insert(:user)
1475 {:ok, activity} = CommonAPI.post(user, %{status: "yeah"})
1477 {:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id})
1479 [result] = ActivityPub.fetch_public_activities(%{exclude_replies: true})
1481 assert result.id == activity.id
1483 assert length(ActivityPub.fetch_public_activities()) == 2
1486 describe "replies filtering with public messages" do
1487 setup :public_messages
1489 test "public timeline", %{users: %{u1: user}} do
1492 |> Map.put(:type, ["Create", "Announce"])
1493 |> Map.put(:local_only, false)
1494 |> Map.put(:blocking_user, user)
1495 |> Map.put(:muting_user, user)
1496 |> Map.put(:reply_filtering_user, user)
1497 |> ActivityPub.fetch_public_activities()
1498 |> Enum.map(& &1.id)
1500 assert length(activities_ids) == 16
1503 test "public timeline with reply_visibility `following`", %{
1509 activities: activities
1513 |> Map.put(:type, ["Create", "Announce"])
1514 |> Map.put(:local_only, false)
1515 |> Map.put(:blocking_user, user)
1516 |> Map.put(:muting_user, user)
1517 |> Map.put(:reply_visibility, "following")
1518 |> Map.put(:reply_filtering_user, user)
1519 |> ActivityPub.fetch_public_activities()
1520 |> Enum.map(& &1.id)
1522 assert length(activities_ids) == 14
1525 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1527 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1530 test "public timeline with reply_visibility `self`", %{
1536 activities: activities
1540 |> Map.put(:type, ["Create", "Announce"])
1541 |> Map.put(:local_only, false)
1542 |> Map.put(:blocking_user, user)
1543 |> Map.put(:muting_user, user)
1544 |> Map.put(:reply_visibility, "self")
1545 |> Map.put(:reply_filtering_user, user)
1546 |> ActivityPub.fetch_public_activities()
1547 |> Enum.map(& &1.id)
1549 assert length(activities_ids) == 10
1550 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1551 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1554 test "home timeline", %{
1556 activities: activities,
1564 |> Map.put(:type, ["Create", "Announce"])
1565 |> Map.put(:blocking_user, user)
1566 |> Map.put(:muting_user, user)
1567 |> Map.put(:user, user)
1568 |> Map.put(:reply_filtering_user, user)
1571 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1572 |> Enum.map(& &1.id)
1574 assert length(activities_ids) == 13
1589 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1592 test "home timeline with reply_visibility `following`", %{
1594 activities: activities,
1602 |> Map.put(:type, ["Create", "Announce"])
1603 |> Map.put(:blocking_user, user)
1604 |> Map.put(:muting_user, user)
1605 |> Map.put(:user, user)
1606 |> Map.put(:reply_visibility, "following")
1607 |> Map.put(:reply_filtering_user, user)
1610 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1611 |> Enum.map(& &1.id)
1613 assert length(activities_ids) == 11
1628 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1631 test "home timeline with reply_visibility `self`", %{
1633 activities: activities,
1641 |> Map.put(:type, ["Create", "Announce"])
1642 |> Map.put(:blocking_user, user)
1643 |> Map.put(:muting_user, user)
1644 |> Map.put(:user, user)
1645 |> Map.put(:reply_visibility, "self")
1646 |> Map.put(:reply_filtering_user, user)
1649 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1650 |> Enum.map(& &1.id)
1652 assert length(activities_ids) == 9
1665 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1668 test "filtering out announces where the user is the actor of the announced message" do
1669 user = insert(:user)
1670 other_user = insert(:user)
1671 third_user = insert(:user)
1672 User.follow(user, other_user)
1674 {:ok, post} = CommonAPI.post(user, %{status: "yo"})
1675 {:ok, other_post} = CommonAPI.post(third_user, %{status: "yo"})
1676 {:ok, _announce} = CommonAPI.repeat(post.id, other_user)
1677 {:ok, _announce} = CommonAPI.repeat(post.id, third_user)
1678 {:ok, announce} = CommonAPI.repeat(other_post.id, other_user)
1685 [user.ap_id | User.following(user)]
1686 |> ActivityPub.fetch_activities(params)
1688 assert length(results) == 3
1692 announce_filtering_user: user
1696 [user.ap_id | User.following(user)]
1697 |> ActivityPub.fetch_activities(params)
1699 assert result.id == announce.id
1703 describe "replies filtering with private messages" do
1704 setup :private_messages
1706 test "public timeline", %{users: %{u1: user}} do
1709 |> Map.put(:type, ["Create", "Announce"])
1710 |> Map.put(:local_only, false)
1711 |> Map.put(:blocking_user, user)
1712 |> Map.put(:muting_user, user)
1713 |> Map.put(:user, user)
1714 |> ActivityPub.fetch_public_activities()
1715 |> Enum.map(& &1.id)
1717 assert activities_ids == []
1720 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1723 |> Map.put(:type, ["Create", "Announce"])
1724 |> Map.put(:local_only, false)
1725 |> Map.put(:blocking_user, user)
1726 |> Map.put(:muting_user, user)
1727 |> Map.put(:reply_visibility, "following")
1728 |> Map.put(:reply_filtering_user, user)
1729 |> Map.put(:user, user)
1730 |> ActivityPub.fetch_public_activities()
1731 |> Enum.map(& &1.id)
1733 assert activities_ids == []
1736 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
1739 |> Map.put(:type, ["Create", "Announce"])
1740 |> Map.put(:local_only, false)
1741 |> Map.put(:blocking_user, user)
1742 |> Map.put(:muting_user, user)
1743 |> Map.put(:reply_visibility, "self")
1744 |> Map.put(:reply_filtering_user, user)
1745 |> Map.put(:user, user)
1746 |> ActivityPub.fetch_public_activities()
1747 |> Enum.map(& &1.id)
1749 assert activities_ids == []
1752 test "home timeline", %{users: %{u1: user}} do
1755 |> Map.put(:type, ["Create", "Announce"])
1756 |> Map.put(:blocking_user, user)
1757 |> Map.put(:muting_user, user)
1758 |> Map.put(:user, user)
1761 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1762 |> Enum.map(& &1.id)
1764 assert length(activities_ids) == 12
1767 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1770 |> Map.put(:type, ["Create", "Announce"])
1771 |> Map.put(:blocking_user, user)
1772 |> Map.put(:muting_user, user)
1773 |> Map.put(:user, user)
1774 |> Map.put(:reply_visibility, "following")
1775 |> Map.put(:reply_filtering_user, user)
1778 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1779 |> Enum.map(& &1.id)
1781 assert length(activities_ids) == 12
1784 test "home timeline with default reply_visibility `self`", %{
1786 activities: activities,
1794 |> Map.put(:type, ["Create", "Announce"])
1795 |> Map.put(:blocking_user, user)
1796 |> Map.put(:muting_user, user)
1797 |> Map.put(:user, user)
1798 |> Map.put(:reply_visibility, "self")
1799 |> Map.put(:reply_filtering_user, user)
1802 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1803 |> Enum.map(& &1.id)
1805 assert length(activities_ids) == 10
1808 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
1810 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1814 defp public_messages(_) do
1815 [u1, u2, u3, u4] = insert_list(4, :user)
1816 {:ok, u1} = User.follow(u1, u2)
1817 {:ok, u2} = User.follow(u2, u1)
1818 {:ok, u1} = User.follow(u1, u4)
1819 {:ok, u4} = User.follow(u4, u1)
1821 {:ok, u2} = User.follow(u2, u3)
1822 {:ok, u3} = User.follow(u3, u2)
1824 {:ok, a1} = CommonAPI.post(u1, %{status: "Status"})
1827 CommonAPI.post(u2, %{
1828 status: "@#{u1.nickname} reply from u2 to u1",
1829 in_reply_to_status_id: a1.id
1833 CommonAPI.post(u3, %{
1834 status: "@#{u1.nickname} reply from u3 to u1",
1835 in_reply_to_status_id: a1.id
1839 CommonAPI.post(u4, %{
1840 status: "@#{u1.nickname} reply from u4 to u1",
1841 in_reply_to_status_id: a1.id
1844 {:ok, a2} = CommonAPI.post(u2, %{status: "Status"})
1847 CommonAPI.post(u1, %{
1848 status: "@#{u2.nickname} reply from u1 to u2",
1849 in_reply_to_status_id: a2.id
1853 CommonAPI.post(u3, %{
1854 status: "@#{u2.nickname} reply from u3 to u2",
1855 in_reply_to_status_id: a2.id
1859 CommonAPI.post(u4, %{
1860 status: "@#{u2.nickname} reply from u4 to u2",
1861 in_reply_to_status_id: a2.id
1864 {:ok, a3} = CommonAPI.post(u3, %{status: "Status"})
1867 CommonAPI.post(u1, %{
1868 status: "@#{u3.nickname} reply from u1 to u3",
1869 in_reply_to_status_id: a3.id
1873 CommonAPI.post(u2, %{
1874 status: "@#{u3.nickname} reply from u2 to u3",
1875 in_reply_to_status_id: a3.id
1879 CommonAPI.post(u4, %{
1880 status: "@#{u3.nickname} reply from u4 to u3",
1881 in_reply_to_status_id: a3.id
1884 {:ok, a4} = CommonAPI.post(u4, %{status: "Status"})
1887 CommonAPI.post(u1, %{
1888 status: "@#{u4.nickname} reply from u1 to u4",
1889 in_reply_to_status_id: a4.id
1893 CommonAPI.post(u2, %{
1894 status: "@#{u4.nickname} reply from u2 to u4",
1895 in_reply_to_status_id: a4.id
1899 CommonAPI.post(u3, %{
1900 status: "@#{u4.nickname} reply from u3 to u4",
1901 in_reply_to_status_id: a4.id
1905 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
1906 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
1907 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
1908 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
1909 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
1910 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
1913 defp private_messages(_) do
1914 [u1, u2, u3, u4] = insert_list(4, :user)
1915 {:ok, u1} = User.follow(u1, u2)
1916 {:ok, u2} = User.follow(u2, u1)
1917 {:ok, u1} = User.follow(u1, u3)
1918 {:ok, u3} = User.follow(u3, u1)
1919 {:ok, u1} = User.follow(u1, u4)
1920 {:ok, u4} = User.follow(u4, u1)
1922 {:ok, u2} = User.follow(u2, u3)
1923 {:ok, u3} = User.follow(u3, u2)
1925 {:ok, a1} = CommonAPI.post(u1, %{status: "Status", visibility: "private"})
1928 CommonAPI.post(u2, %{
1929 status: "@#{u1.nickname} reply from u2 to u1",
1930 in_reply_to_status_id: a1.id,
1931 visibility: "private"
1935 CommonAPI.post(u3, %{
1936 status: "@#{u1.nickname} reply from u3 to u1",
1937 in_reply_to_status_id: a1.id,
1938 visibility: "private"
1942 CommonAPI.post(u4, %{
1943 status: "@#{u1.nickname} reply from u4 to u1",
1944 in_reply_to_status_id: a1.id,
1945 visibility: "private"
1948 {:ok, a2} = CommonAPI.post(u2, %{status: "Status", visibility: "private"})
1951 CommonAPI.post(u1, %{
1952 status: "@#{u2.nickname} reply from u1 to u2",
1953 in_reply_to_status_id: a2.id,
1954 visibility: "private"
1958 CommonAPI.post(u3, %{
1959 status: "@#{u2.nickname} reply from u3 to u2",
1960 in_reply_to_status_id: a2.id,
1961 visibility: "private"
1964 {:ok, a3} = CommonAPI.post(u3, %{status: "Status", visibility: "private"})
1967 CommonAPI.post(u1, %{
1968 status: "@#{u3.nickname} reply from u1 to u3",
1969 in_reply_to_status_id: a3.id,
1970 visibility: "private"
1974 CommonAPI.post(u2, %{
1975 status: "@#{u3.nickname} reply from u2 to u3",
1976 in_reply_to_status_id: a3.id,
1977 visibility: "private"
1980 {:ok, a4} = CommonAPI.post(u4, %{status: "Status", visibility: "private"})
1983 CommonAPI.post(u1, %{
1984 status: "@#{u4.nickname} reply from u1 to u4",
1985 in_reply_to_status_id: a4.id,
1986 visibility: "private"
1990 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
1991 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
1992 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
1993 u2: %{r1: r2_1.id, r2: r2_2.id},
1994 u3: %{r1: r3_1.id, r2: r3_2.id},
1998 describe "maybe_update_follow_information/1" do
2000 clear_config([:instance, :external_user_synchronization], true)
2004 ap_id: "https://gensokyo.2hu/users/raymoo",
2005 following_address: "https://gensokyo.2hu/users/following",
2006 follower_address: "https://gensokyo.2hu/users/followers",
2013 test "logs an error when it can't fetch the info", %{user: user} do
2014 assert capture_log(fn ->
2015 ActivityPub.maybe_update_follow_information(user)
2016 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2019 test "just returns the input if the user type is Application", %{
2024 |> Map.put(:type, "Application")
2026 refute capture_log(fn ->
2027 assert ^user = ActivityPub.maybe_update_follow_information(user)
2028 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2031 test "it just returns the input if the user has no following/follower addresses", %{
2036 |> Map.put(:following_address, nil)
2037 |> Map.put(:follower_address, nil)
2039 refute capture_log(fn ->
2040 assert ^user = ActivityPub.maybe_update_follow_information(user)
2041 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2045 describe "global activity expiration" do
2046 setup do: clear_config([:mrf, :policies])
2048 test "creates an activity expiration for local Create activities" do
2051 Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy
2054 {:ok, %{id: id_create}} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
2055 {:ok, _follow} = ActivityBuilder.insert(%{"type" => "Follow", "context" => "3hu"})
2057 assert [%{activity_id: ^id_create}] = Pleroma.ActivityExpiration |> Repo.all()
2061 describe "handling of clashing nicknames" do
2062 test "renames an existing user with a clashing nickname and a different ap id" do
2067 nickname: "admin@mastodon.example.org",
2068 ap_id: "http://mastodon.example.org/users/harinezumigari"
2072 nickname: orig_user.nickname,
2073 ap_id: orig_user.ap_id <> "part_2"
2075 |> ActivityPub.maybe_handle_clashing_nickname()
2077 user = User.get_by_id(orig_user.id)
2079 assert user.nickname == "#{orig_user.id}.admin@mastodon.example.org"
2082 test "does nothing with a clashing nickname and the same ap id" do
2087 nickname: "admin@mastodon.example.org",
2088 ap_id: "http://mastodon.example.org/users/harinezumigari"
2092 nickname: orig_user.nickname,
2093 ap_id: orig_user.ap_id
2095 |> ActivityPub.maybe_handle_clashing_nickname()
2097 user = User.get_by_id(orig_user.id)
2099 assert user.nickname == orig_user.nickname