1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Builders.ActivityBuilder
11 alias Pleroma.Notification
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Utils
16 alias Pleroma.Web.CommonAPI
18 import Pleroma.Factory
23 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 clear_config([:instance, :federating])
29 describe "streaming out participations" do
30 test "it streams them out" do
32 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
34 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
37 conversation.participations
38 |> Repo.preload(:user)
40 with_mock Pleroma.Web.Streamer,
41 stream: fn _, _ -> nil end do
42 ActivityPub.stream_out_participations(conversation.participations)
44 assert called(Pleroma.Web.Streamer.stream("participation", participations))
48 test "streams them out on activity creation" do
49 user_one = insert(:user)
50 user_two = insert(:user)
52 with_mock Pleroma.Web.Streamer,
53 stream: fn _, _ -> nil end do
55 CommonAPI.post(user_one, %{
56 "status" => "@#{user_two.nickname}",
57 "visibility" => "direct"
61 activity.data["context"]
62 |> Pleroma.Conversation.get_for_ap_id()
63 |> Repo.preload(participations: :user)
65 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
70 describe "fetching restricted by visibility" do
71 test "it restricts by the appropriate visibility" do
74 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
76 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
78 {:ok, unlisted_activity} =
79 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
81 {:ok, private_activity} =
82 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
85 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]
100 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
102 assert activities == [public_activity]
105 ActivityPub.fetch_activities([], %{
106 :visibility => ~w[private public],
107 "actor_id" => user.ap_id
110 assert activities == [public_activity, private_activity]
114 describe "fetching excluded by visibility" do
115 test "it excludes by the appropriate visibility" do
118 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
120 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
122 {:ok, unlisted_activity} =
123 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
125 {:ok, private_activity} =
126 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
129 ActivityPub.fetch_activities([], %{
130 "exclude_visibilities" => "direct",
131 "actor_id" => user.ap_id
134 assert public_activity in activities
135 assert unlisted_activity in activities
136 assert private_activity in activities
137 refute direct_activity in activities
140 ActivityPub.fetch_activities([], %{
141 "exclude_visibilities" => "unlisted",
142 "actor_id" => user.ap_id
145 assert public_activity in activities
146 refute unlisted_activity in activities
147 assert private_activity in activities
148 assert direct_activity in activities
151 ActivityPub.fetch_activities([], %{
152 "exclude_visibilities" => "private",
153 "actor_id" => user.ap_id
156 assert public_activity in activities
157 assert unlisted_activity in activities
158 refute private_activity in activities
159 assert direct_activity in activities
162 ActivityPub.fetch_activities([], %{
163 "exclude_visibilities" => "public",
164 "actor_id" => user.ap_id
167 refute public_activity in activities
168 assert unlisted_activity in activities
169 assert private_activity in activities
170 assert direct_activity in activities
174 describe "building a user from his ap id" do
175 test "it returns a user" do
176 user_id = "http://mastodon.example.org/users/admin"
177 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
178 assert user.ap_id == user_id
179 assert user.nickname == "admin@mastodon.example.org"
180 assert user.source_data
181 assert user.ap_enabled
182 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
185 test "it returns a user that is invisible" do
186 user_id = "http://mastodon.example.org/users/relay"
187 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
188 assert User.invisible?(user)
191 test "it fetches the appropriate tag-restricted posts" do
194 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
195 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
196 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
198 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
201 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
204 ActivityPub.fetch_activities([], %{
206 "tag" => ["test", "essais"],
207 "tag_reject" => ["reject"]
211 ActivityPub.fetch_activities([], %{
214 "tag_all" => ["test", "reject"]
217 assert fetch_one == [status_one, status_three]
218 assert fetch_two == [status_one, status_two, status_three]
219 assert fetch_three == [status_one, status_two]
220 assert fetch_four == [status_three]
224 describe "insertion" do
225 test "drops activities beyond a certain limit" do
226 limit = Pleroma.Config.get([:instance, :remote_limit])
229 :crypto.strong_rand_bytes(limit + 1)
231 |> binary_part(0, limit + 1)
236 "content" => random_text
240 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
243 test "doesn't drop activities with content being null" do
247 "actor" => user.ap_id,
250 "actor" => user.ap_id,
257 assert {:ok, _} = ActivityPub.insert(data)
260 test "returns the activity if one with the same id is already in" do
261 activity = insert(:note_activity)
262 {:ok, new_activity} = ActivityPub.insert(activity.data)
264 assert activity.id == new_activity.id
267 test "inserts a given map into the activity database, giving it an id if it has none." do
271 "actor" => user.ap_id,
274 "actor" => user.ap_id,
281 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
282 assert activity.data["ok"] == data["ok"]
283 assert is_binary(activity.data["id"])
289 "actor" => user.ap_id,
291 "context" => "blabla",
293 "actor" => user.ap_id,
300 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
301 assert activity.data["ok"] == data["ok"]
302 assert activity.data["id"] == given_id
303 assert activity.data["context"] == "blabla"
304 assert activity.data["context_id"]
307 test "adds a context when none is there" do
311 "actor" => user.ap_id,
314 "actor" => user.ap_id,
321 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
322 object = Pleroma.Object.normalize(activity)
324 assert is_binary(activity.data["context"])
325 assert is_binary(object.data["context"])
326 assert activity.data["context_id"]
327 assert object.data["context_id"]
330 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
334 "actor" => user.ap_id,
337 "actor" => user.ap_id,
344 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
345 assert object = Object.normalize(activity)
346 assert is_binary(object.data["id"])
350 describe "listen activities" do
351 test "does not increase user note count" do
355 ActivityPub.listen(%{
356 to: ["https://www.w3.org/ns/activitystreams#Public"],
360 "actor" => user.ap_id,
361 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
363 "title" => "lain radio episode 1",
369 assert activity.actor == user.ap_id
371 user = User.get_cached_by_id(user.id)
372 assert user.note_count == 0
375 test "can be fetched into a timeline" do
376 _listen_activity_1 = insert(:listen)
377 _listen_activity_2 = insert(:listen)
378 _listen_activity_3 = insert(:listen)
380 timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]})
382 assert length(timeline) == 3
386 describe "create activities" do
387 test "removes doubled 'to' recipients" do
391 ActivityPub.create(%{
392 to: ["user1", "user1", "user2"],
396 "to" => ["user1", "user1", "user2"],
398 "content" => "testing"
402 assert activity.data["to"] == ["user1", "user2"]
403 assert activity.actor == user.ap_id
404 assert activity.recipients == ["user1", "user2", user.ap_id]
407 test "increases user note count only for public activities" do
411 CommonAPI.post(User.get_cached_by_id(user.id), %{
413 "visibility" => "public"
417 CommonAPI.post(User.get_cached_by_id(user.id), %{
419 "visibility" => "unlisted"
423 CommonAPI.post(User.get_cached_by_id(user.id), %{
425 "visibility" => "private"
429 CommonAPI.post(User.get_cached_by_id(user.id), %{
431 "visibility" => "direct"
434 user = User.get_cached_by_id(user.id)
435 assert user.note_count == 2
438 test "increases replies count" do
440 user2 = insert(:user)
442 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
443 ap_id = activity.data["id"]
444 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
447 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
448 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
449 assert object.data["repliesCount"] == 1
452 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
453 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
454 assert object.data["repliesCount"] == 2
457 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
458 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
459 assert object.data["repliesCount"] == 2
462 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
463 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
464 assert object.data["repliesCount"] == 2
468 describe "fetch activities for recipients" do
469 test "retrieve the activities for certain recipients" do
470 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
471 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
472 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
474 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
475 assert length(activities) == 2
476 assert activities == [activity_one, activity_two]
480 describe "fetch activities in context" do
481 test "retrieves activities that have a given context" do
482 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
483 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
484 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
485 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
486 activity_five = insert(:note_activity)
489 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
491 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
492 assert activities == [activity_two, activity]
496 test "doesn't return blocked activities" do
497 activity_one = insert(:note_activity)
498 activity_two = insert(:note_activity)
499 activity_three = insert(:note_activity)
501 booster = insert(:user)
502 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
505 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
507 assert Enum.member?(activities, activity_two)
508 assert Enum.member?(activities, activity_three)
509 refute Enum.member?(activities, activity_one)
511 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
514 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
516 assert Enum.member?(activities, activity_two)
517 assert Enum.member?(activities, activity_three)
518 assert Enum.member?(activities, activity_one)
520 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
521 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
522 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
523 activity_three = Activity.get_by_id(activity_three.id)
526 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
528 assert Enum.member?(activities, activity_two)
529 refute Enum.member?(activities, activity_three)
530 refute Enum.member?(activities, boost_activity)
531 assert Enum.member?(activities, activity_one)
534 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
536 assert Enum.member?(activities, activity_two)
537 assert Enum.member?(activities, activity_three)
538 assert Enum.member?(activities, boost_activity)
539 assert Enum.member?(activities, activity_one)
542 test "doesn't return transitive interactions concerning blocked users" do
543 blocker = insert(:user)
544 blockee = insert(:user)
545 friend = insert(:user)
547 {:ok, blocker} = User.block(blocker, blockee)
549 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
551 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
553 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
555 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
557 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
559 assert Enum.member?(activities, activity_one)
560 refute Enum.member?(activities, activity_two)
561 refute Enum.member?(activities, activity_three)
562 refute Enum.member?(activities, activity_four)
565 test "doesn't return announce activities concerning blocked users" do
566 blocker = insert(:user)
567 blockee = insert(:user)
568 friend = insert(:user)
570 {:ok, blocker} = User.block(blocker, blockee)
572 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
574 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
576 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
579 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
580 |> Enum.map(fn act -> act.id end)
582 assert Enum.member?(activities, activity_one.id)
583 refute Enum.member?(activities, activity_two.id)
584 refute Enum.member?(activities, activity_three.id)
587 test "doesn't return activities from blocked domains" do
588 domain = "dogwhistle.zone"
589 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
590 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
591 activity = insert(:note_activity, %{note: note})
593 {:ok, user} = User.block_domain(user, domain)
596 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
598 refute activity in activities
600 followed_user = insert(:user)
601 ActivityPub.follow(user, followed_user)
602 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
605 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
607 refute repeat_activity in activities
610 test "doesn't return muted activities" do
611 activity_one = insert(:note_activity)
612 activity_two = insert(:note_activity)
613 activity_three = insert(:note_activity)
615 booster = insert(:user)
616 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
619 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
621 assert Enum.member?(activities, activity_two)
622 assert Enum.member?(activities, activity_three)
623 refute Enum.member?(activities, activity_one)
625 # Calling with 'with_muted' will deliver muted activities, too.
627 ActivityPub.fetch_activities([], %{
628 "muting_user" => user,
629 "with_muted" => true,
630 "skip_preload" => true
633 assert Enum.member?(activities, activity_two)
634 assert Enum.member?(activities, activity_three)
635 assert Enum.member?(activities, activity_one)
637 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
640 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
642 assert Enum.member?(activities, activity_two)
643 assert Enum.member?(activities, activity_three)
644 assert Enum.member?(activities, activity_one)
646 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
647 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
648 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
649 activity_three = Activity.get_by_id(activity_three.id)
652 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
654 assert Enum.member?(activities, activity_two)
655 refute Enum.member?(activities, activity_three)
656 refute Enum.member?(activities, boost_activity)
657 assert Enum.member?(activities, activity_one)
659 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
661 assert Enum.member?(activities, activity_two)
662 assert Enum.member?(activities, activity_three)
663 assert Enum.member?(activities, boost_activity)
664 assert Enum.member?(activities, activity_one)
667 test "doesn't return thread muted activities" do
669 _activity_one = insert(:note_activity)
670 note_two = insert(:note, data: %{"context" => "suya.."})
671 activity_two = insert(:note_activity, note: note_two)
673 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
675 assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user})
678 test "returns thread muted activities when with_muted is set" do
680 _activity_one = insert(:note_activity)
681 note_two = insert(:note, data: %{"context" => "suya.."})
682 activity_two = insert(:note_activity, note: note_two)
684 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
686 assert [_activity_two, _activity_one] =
687 ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true})
690 test "does include announces on request" do
691 activity_three = insert(:note_activity)
693 booster = insert(:user)
695 {:ok, user} = User.follow(user, booster)
697 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
699 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
701 assert announce_activity.id == announce.id
704 test "excludes reblogs on request" do
706 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
707 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
709 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
711 assert activity == expected_activity
714 describe "public fetch activities" do
715 test "doesn't retrieve unlisted activities" do
718 {:ok, _unlisted_activity} =
719 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
721 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
723 [activity] = ActivityPub.fetch_public_activities()
725 assert activity == listed_activity
728 test "retrieves public activities" do
729 _activities = ActivityPub.fetch_public_activities()
731 %{public: public} = ActivityBuilder.public_and_non_public()
733 activities = ActivityPub.fetch_public_activities()
734 assert length(activities) == 1
735 assert Enum.at(activities, 0) == public
738 test "retrieves a maximum of 20 activities" do
739 activities = ActivityBuilder.insert_list(30)
740 last_expected = List.last(activities)
742 activities = ActivityPub.fetch_public_activities()
743 last = List.last(activities)
745 assert length(activities) == 20
746 assert last == last_expected
749 test "retrieves ids starting from a since_id" do
750 activities = ActivityBuilder.insert_list(30)
751 later_activities = ActivityBuilder.insert_list(10)
752 since_id = List.last(activities).id
753 last_expected = List.last(later_activities)
755 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
756 last = List.last(activities)
758 assert length(activities) == 10
759 assert last == last_expected
762 test "retrieves ids up to max_id" do
763 _first_activities = ActivityBuilder.insert_list(10)
764 activities = ActivityBuilder.insert_list(20)
765 later_activities = ActivityBuilder.insert_list(10)
766 max_id = List.first(later_activities).id
767 last_expected = List.last(activities)
769 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
770 last = List.last(activities)
772 assert length(activities) == 20
773 assert last == last_expected
776 test "paginates via offset/limit" do
777 _first_activities = ActivityBuilder.insert_list(10)
778 activities = ActivityBuilder.insert_list(10)
779 _later_activities = ActivityBuilder.insert_list(10)
780 first_expected = List.first(activities)
783 ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
785 first = List.first(activities)
787 assert length(activities) == 20
788 assert first == first_expected
791 test "doesn't return reblogs for users for whom reblogs have been muted" do
792 activity = insert(:note_activity)
794 booster = insert(:user)
795 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
797 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
799 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
801 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
804 test "returns reblogs for users for whom reblogs have not been muted" do
805 activity = insert(:note_activity)
807 booster = insert(:user)
808 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
809 {:ok, user} = CommonAPI.show_reblogs(user, booster)
811 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
813 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
815 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
819 describe "like an object" do
820 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
821 Pleroma.Config.put([:instance, :federating], true)
822 note_activity = insert(:note_activity)
823 assert object_activity = Object.normalize(note_activity)
827 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
828 assert called(Pleroma.Web.Federator.publish(like_activity))
831 test "returns exist activity if object already liked" do
832 note_activity = insert(:note_activity)
833 assert object_activity = Object.normalize(note_activity)
837 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
839 {:ok, like_activity_exist, _object} = ActivityPub.like(user, object_activity)
840 assert like_activity == like_activity_exist
843 test "adds a like activity to the db" do
844 note_activity = insert(:note_activity)
845 assert object = Object.normalize(note_activity)
848 user_two = insert(:user)
850 {:ok, like_activity, object} = ActivityPub.like(user, object)
852 assert like_activity.data["actor"] == user.ap_id
853 assert like_activity.data["type"] == "Like"
854 assert like_activity.data["object"] == object.data["id"]
855 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
856 assert like_activity.data["context"] == object.data["context"]
857 assert object.data["like_count"] == 1
858 assert object.data["likes"] == [user.ap_id]
860 # Just return the original activity if the user already liked it.
861 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
863 assert like_activity == same_like_activity
864 assert object.data["likes"] == [user.ap_id]
865 assert object.data["like_count"] == 1
867 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
868 assert object.data["like_count"] == 2
872 describe "unliking" do
873 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
874 Pleroma.Config.put([:instance, :federating], true)
876 note_activity = insert(:note_activity)
877 object = Object.normalize(note_activity)
880 {:ok, object} = ActivityPub.unlike(user, object)
881 refute called(Pleroma.Web.Federator.publish())
883 {:ok, _like_activity, object} = ActivityPub.like(user, object)
884 assert object.data["like_count"] == 1
886 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
887 assert object.data["like_count"] == 0
889 assert called(Pleroma.Web.Federator.publish(unlike_activity))
892 test "unliking a previously liked object" do
893 note_activity = insert(:note_activity)
894 object = Object.normalize(note_activity)
897 # Unliking something that hasn't been liked does nothing
898 {:ok, object} = ActivityPub.unlike(user, object)
899 assert object.data["like_count"] == 0
901 {:ok, like_activity, object} = ActivityPub.like(user, object)
902 assert object.data["like_count"] == 1
904 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
905 assert object.data["like_count"] == 0
907 assert Activity.get_by_id(like_activity.id) == nil
908 assert note_activity.actor in unlike_activity.recipients
912 describe "announcing an object" do
913 test "adds an announce activity to the db" do
914 note_activity = insert(:note_activity)
915 object = Object.normalize(note_activity)
918 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
919 assert object.data["announcement_count"] == 1
920 assert object.data["announcements"] == [user.ap_id]
922 assert announce_activity.data["to"] == [
923 User.ap_followers(user),
924 note_activity.data["actor"]
927 assert announce_activity.data["object"] == object.data["id"]
928 assert announce_activity.data["actor"] == user.ap_id
929 assert announce_activity.data["context"] == object.data["context"]
933 describe "announcing a private object" do
934 test "adds an announce activity to the db if the audience is not widened" do
936 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
937 object = Object.normalize(note_activity)
939 {:ok, announce_activity, object} = ActivityPub.announce(user, object, nil, true, false)
941 assert announce_activity.data["to"] == [User.ap_followers(user)]
943 assert announce_activity.data["object"] == object.data["id"]
944 assert announce_activity.data["actor"] == user.ap_id
945 assert announce_activity.data["context"] == object.data["context"]
948 test "does not add an announce activity to the db if the audience is widened" do
950 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
951 object = Object.normalize(note_activity)
953 assert {:error, _} = ActivityPub.announce(user, object, nil, true, true)
956 test "does not add an announce activity to the db if the announcer is not the author" do
958 announcer = insert(:user)
959 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
960 object = Object.normalize(note_activity)
962 assert {:error, _} = ActivityPub.announce(announcer, object, nil, true, false)
966 describe "unannouncing an object" do
967 test "unannouncing a previously announced object" do
968 note_activity = insert(:note_activity)
969 object = Object.normalize(note_activity)
972 # Unannouncing an object that is not announced does nothing
973 # {:ok, object} = ActivityPub.unannounce(user, object)
974 # assert object.data["announcement_count"] == 0
976 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
977 assert object.data["announcement_count"] == 1
979 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
980 assert object.data["announcement_count"] == 0
982 assert unannounce_activity.data["to"] == [
983 User.ap_followers(user),
987 assert unannounce_activity.data["type"] == "Undo"
988 assert unannounce_activity.data["object"] == announce_activity.data
989 assert unannounce_activity.data["actor"] == user.ap_id
990 assert unannounce_activity.data["context"] == announce_activity.data["context"]
992 assert Activity.get_by_id(announce_activity.id) == nil
996 describe "uploading files" do
997 test "copies the file to the configured folder" do
999 content_type: "image/jpg",
1000 path: Path.absname("test/fixtures/image.jpg"),
1001 filename: "an_image.jpg"
1004 {:ok, %Object{} = object} = ActivityPub.upload(file)
1005 assert object.data["name"] == "an_image.jpg"
1008 test "works with base64 encoded images" do
1013 {:ok, %Object{}} = ActivityPub.upload(file)
1017 describe "fetch the latest Follow" do
1018 test "fetches the latest Follow activity" do
1019 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1020 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1021 followed = Repo.get_by(User, ap_id: activity.data["object"])
1023 assert activity == Utils.fetch_latest_follow(follower, followed)
1027 describe "following / unfollowing" do
1028 test "creates a follow activity" do
1029 follower = insert(:user)
1030 followed = insert(:user)
1032 {:ok, activity} = ActivityPub.follow(follower, followed)
1033 assert activity.data["type"] == "Follow"
1034 assert activity.data["actor"] == follower.ap_id
1035 assert activity.data["object"] == followed.ap_id
1038 test "creates an undo activity for the last follow" do
1039 follower = insert(:user)
1040 followed = insert(:user)
1042 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1043 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1045 assert activity.data["type"] == "Undo"
1046 assert activity.data["actor"] == follower.ap_id
1048 embedded_object = activity.data["object"]
1049 assert is_map(embedded_object)
1050 assert embedded_object["type"] == "Follow"
1051 assert embedded_object["object"] == followed.ap_id
1052 assert embedded_object["id"] == follow_activity.data["id"]
1056 describe "blocking / unblocking" do
1057 test "creates a block activity" do
1058 blocker = insert(:user)
1059 blocked = insert(:user)
1061 {:ok, activity} = ActivityPub.block(blocker, blocked)
1063 assert activity.data["type"] == "Block"
1064 assert activity.data["actor"] == blocker.ap_id
1065 assert activity.data["object"] == blocked.ap_id
1068 test "creates an undo activity for the last block" do
1069 blocker = insert(:user)
1070 blocked = insert(:user)
1072 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
1073 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
1075 assert activity.data["type"] == "Undo"
1076 assert activity.data["actor"] == blocker.ap_id
1078 embedded_object = activity.data["object"]
1079 assert is_map(embedded_object)
1080 assert embedded_object["type"] == "Block"
1081 assert embedded_object["object"] == blocked.ap_id
1082 assert embedded_object["id"] == block_activity.data["id"]
1086 describe "deletion" do
1087 test "it creates a delete activity and deletes the original object" do
1088 note = insert(:note_activity)
1089 object = Object.normalize(note)
1090 {:ok, delete} = ActivityPub.delete(object)
1092 assert delete.data["type"] == "Delete"
1093 assert delete.data["actor"] == note.data["actor"]
1094 assert delete.data["object"] == object.data["id"]
1096 assert Activity.get_by_id(delete.id) != nil
1098 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
1101 test "decrements user note count only for public activities" do
1102 user = insert(:user, note_count: 10)
1105 CommonAPI.post(User.get_cached_by_id(user.id), %{
1107 "visibility" => "public"
1111 CommonAPI.post(User.get_cached_by_id(user.id), %{
1113 "visibility" => "unlisted"
1117 CommonAPI.post(User.get_cached_by_id(user.id), %{
1119 "visibility" => "private"
1123 CommonAPI.post(User.get_cached_by_id(user.id), %{
1125 "visibility" => "direct"
1128 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
1129 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
1130 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
1131 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
1133 user = User.get_cached_by_id(user.id)
1134 assert user.note_count == 10
1137 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
1138 user = insert(:user)
1139 note = insert(:note_activity)
1140 object = Object.normalize(note)
1146 "actor" => object.data["actor"],
1147 "id" => object.data["id"],
1148 "to" => [user.ap_id],
1152 |> Object.update_and_set_cache()
1154 {:ok, delete} = ActivityPub.delete(object)
1156 assert user.ap_id in delete.data["to"]
1159 test "decreases reply count" do
1160 user = insert(:user)
1161 user2 = insert(:user)
1163 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
1164 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
1165 ap_id = activity.data["id"]
1167 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
1168 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
1169 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
1170 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
1172 _ = CommonAPI.delete(direct_reply.id, user2)
1173 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1174 assert object.data["repliesCount"] == 2
1176 _ = CommonAPI.delete(private_reply.id, user2)
1177 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1178 assert object.data["repliesCount"] == 2
1180 _ = CommonAPI.delete(public_reply.id, user2)
1181 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1182 assert object.data["repliesCount"] == 1
1184 _ = CommonAPI.delete(unlisted_reply.id, user2)
1185 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1186 assert object.data["repliesCount"] == 0
1190 describe "timeline post-processing" do
1191 test "it filters broken threads" do
1192 user1 = insert(:user)
1193 user2 = insert(:user)
1194 user3 = insert(:user)
1196 {:ok, user1} = User.follow(user1, user3)
1197 assert User.following?(user1, user3)
1199 {:ok, user2} = User.follow(user2, user3)
1200 assert User.following?(user2, user3)
1202 {:ok, user3} = User.follow(user3, user2)
1203 assert User.following?(user3, user2)
1205 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
1207 {:ok, private_activity_1} =
1208 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
1210 {:ok, private_activity_2} =
1211 CommonAPI.post(user2, %{
1213 "visibility" => "private",
1214 "in_reply_to_status_id" => private_activity_1.id
1217 {:ok, private_activity_3} =
1218 CommonAPI.post(user3, %{
1220 "visibility" => "private",
1221 "in_reply_to_status_id" => private_activity_2.id
1225 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1226 |> Enum.map(fn a -> a.id end)
1228 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1230 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1232 assert length(activities) == 3
1235 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{"user" => user1})
1236 |> Enum.map(fn a -> a.id end)
1238 assert [public_activity.id, private_activity_1.id] == activities
1239 assert length(activities) == 2
1243 describe "update" do
1244 test "it creates an update activity with the new user data" do
1245 user = insert(:user)
1246 {:ok, user} = User.ensure_keys_present(user)
1247 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1250 ActivityPub.update(%{
1251 actor: user_data["id"],
1252 to: [user.follower_address],
1257 assert update.data["actor"] == user.ap_id
1258 assert update.data["to"] == [user.follower_address]
1259 assert embedded_object = update.data["object"]
1260 assert embedded_object["id"] == user_data["id"]
1261 assert embedded_object["type"] == user_data["type"]
1265 test "returned pinned statuses" do
1266 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
1267 user = insert(:user)
1269 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1270 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1271 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1273 CommonAPI.pin(activity_one.id, user)
1274 user = refresh_record(user)
1276 CommonAPI.pin(activity_two.id, user)
1277 user = refresh_record(user)
1279 CommonAPI.pin(activity_three.id, user)
1280 user = refresh_record(user)
1282 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1284 assert 3 = length(activities)
1287 test "it can create a Flag activity" do
1288 reporter = insert(:user)
1289 target_account = insert(:user)
1290 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
1291 context = Utils.generate_context_id()
1294 reporter_ap_id = reporter.ap_id
1295 target_ap_id = target_account.ap_id
1296 activity_ap_id = activity.data["id"]
1298 assert {:ok, activity} =
1302 account: target_account,
1303 statuses: [activity],
1308 actor: ^reporter_ap_id,
1311 "content" => ^content,
1312 "context" => ^context,
1313 "object" => [^target_ap_id, ^activity_ap_id]
1318 test "fetch_activities/2 returns activities addressed to a list " do
1319 user = insert(:user)
1320 member = insert(:user)
1321 {:ok, list} = Pleroma.List.create("foo", user)
1322 {:ok, list} = Pleroma.List.follow(list, member)
1325 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1327 activity = Repo.preload(activity, :bookmark)
1328 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1330 assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity]
1334 File.read!("test/fixtures/avatar_data_uri")
1337 describe "fetch_activities_bounded" do
1338 test "fetches private posts for followed users" do
1339 user = insert(:user)
1342 CommonAPI.post(user, %{
1343 "status" => "thought I looked cute might delete later :3",
1344 "visibility" => "private"
1347 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1348 assert result.id == activity.id
1351 test "fetches only public posts for other users" do
1352 user = insert(:user)
1353 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe", "visibility" => "public"})
1355 {:ok, _private_activity} =
1356 CommonAPI.post(user, %{
1357 "status" => "why is tenshi eating a corndog so cute?",
1358 "visibility" => "private"
1361 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1362 assert result.id == activity.id
1366 describe "fetch_follow_information_for_user" do
1367 test "syncronizes following/followers counters" do
1371 follower_address: "http://localhost:4001/users/fuser2/followers",
1372 following_address: "http://localhost:4001/users/fuser2/following"
1375 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1376 assert info.follower_count == 527
1377 assert info.following_count == 267
1380 test "detects hidden followers" do
1383 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1384 %Tesla.Env{status: 403, body: ""}
1387 apply(HttpRequestMock, :request, [env])
1394 follower_address: "http://localhost:4001/users/masto_closed/followers",
1395 following_address: "http://localhost:4001/users/masto_closed/following"
1398 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1399 assert follow_info.hide_followers == true
1400 assert follow_info.hide_follows == false
1403 test "detects hidden follows" do
1406 "http://localhost:4001/users/masto_closed/following?page=1" ->
1407 %Tesla.Env{status: 403, body: ""}
1410 apply(HttpRequestMock, :request, [env])
1417 follower_address: "http://localhost:4001/users/masto_closed/followers",
1418 following_address: "http://localhost:4001/users/masto_closed/following"
1421 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1422 assert follow_info.hide_followers == false
1423 assert follow_info.hide_follows == true
1427 describe "Move activity" do
1429 %{ap_id: old_ap_id} = old_user = insert(:user)
1430 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1431 follower = insert(:user)
1432 follower_move_opted_out = insert(:user, allow_following_move: false)
1434 User.follow(follower, old_user)
1435 User.follow(follower_move_opted_out, old_user)
1437 assert User.following?(follower, old_user)
1438 assert User.following?(follower_move_opted_out, old_user)
1440 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1445 "actor" => ^old_ap_id,
1446 "object" => ^old_ap_id,
1447 "target" => ^new_ap_id,
1454 "op" => "move_following",
1455 "origin_id" => old_user.id,
1456 "target_id" => new_user.id
1459 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1461 Pleroma.Workers.BackgroundWorker.perform(params, nil)
1463 refute User.following?(follower, old_user)
1464 assert User.following?(follower, new_user)
1466 assert User.following?(follower_move_opted_out, old_user)
1467 refute User.following?(follower_move_opted_out, new_user)
1469 activity = %Activity{activity | object: nil}
1471 assert [%Notification{activity: ^activity}] =
1472 Notification.for_user_since(follower, ~N[2019-04-13 11:22:33])
1474 assert [%Notification{activity: ^activity}] =
1475 Notification.for_user_since(follower_move_opted_out, ~N[2019-04-13 11:22:33])
1478 test "old user must be in the new user's `also_known_as` list" do
1479 old_user = insert(:user)
1480 new_user = insert(:user)
1482 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1483 ActivityPub.move(old_user, new_user)