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
8 alias Pleroma.Builders.ActivityBuilder
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.CommonAPI
15 import Pleroma.Factory
20 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
24 clear_config([:instance, :federating])
26 describe "streaming out participations" do
27 test "it streams them out" do
29 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
31 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
34 conversation.participations
35 |> Repo.preload(:user)
37 with_mock Pleroma.Web.Streamer,
38 stream: fn _, _ -> nil end do
39 ActivityPub.stream_out_participations(conversation.participations)
41 assert called(Pleroma.Web.Streamer.stream("participation", participations))
45 test "streams them out on activity creation" do
46 user_one = insert(:user)
47 user_two = insert(:user)
49 with_mock Pleroma.Web.Streamer,
50 stream: fn _, _ -> nil end do
52 CommonAPI.post(user_one, %{
53 "status" => "@#{user_two.nickname}",
54 "visibility" => "direct"
58 activity.data["context"]
59 |> Pleroma.Conversation.get_for_ap_id()
60 |> Repo.preload(participations: :user)
62 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
67 describe "fetching restricted by visibility" do
68 test "it restricts by the appropriate visibility" do
71 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
73 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
75 {:ok, unlisted_activity} =
76 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
78 {:ok, private_activity} =
79 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
82 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
84 assert activities == [direct_activity]
87 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
89 assert activities == [unlisted_activity]
92 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
94 assert activities == [private_activity]
97 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
99 assert activities == [public_activity]
102 ActivityPub.fetch_activities([], %{
103 :visibility => ~w[private public],
104 "actor_id" => user.ap_id
107 assert activities == [public_activity, private_activity]
111 describe "fetching excluded by visibility" do
112 test "it excludes by the appropriate visibility" do
115 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
117 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
119 {:ok, unlisted_activity} =
120 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
122 {:ok, private_activity} =
123 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
126 ActivityPub.fetch_activities([], %{
127 "exclude_visibilities" => "direct",
128 "actor_id" => user.ap_id
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",
139 "actor_id" => user.ap_id
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",
150 "actor_id" => user.ap_id
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",
161 "actor_id" => user.ap_id
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.info.source_data
178 assert user.info.ap_enabled
179 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
182 test "it fetches the appropriate tag-restricted posts" do
185 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
186 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
187 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
189 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
192 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
195 ActivityPub.fetch_activities([], %{
197 "tag" => ["test", "essais"],
198 "tag_reject" => ["reject"]
202 ActivityPub.fetch_activities([], %{
205 "tag_all" => ["test", "reject"]
208 assert fetch_one == [status_one, status_three]
209 assert fetch_two == [status_one, status_two, status_three]
210 assert fetch_three == [status_one, status_two]
211 assert fetch_four == [status_three]
215 describe "insertion" do
216 test "drops activities beyond a certain limit" do
217 limit = Pleroma.Config.get([:instance, :remote_limit])
220 :crypto.strong_rand_bytes(limit + 1)
222 |> binary_part(0, limit + 1)
227 "content" => random_text
231 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
234 test "doesn't drop activities with content being null" do
238 "actor" => user.ap_id,
241 "actor" => user.ap_id,
248 assert {:ok, _} = ActivityPub.insert(data)
251 test "returns the activity if one with the same id is already in" do
252 activity = insert(:note_activity)
253 {:ok, new_activity} = ActivityPub.insert(activity.data)
255 assert activity.id == new_activity.id
258 test "inserts a given map into the activity database, giving it an id if it has none." do
262 "actor" => user.ap_id,
265 "actor" => user.ap_id,
272 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
273 assert activity.data["ok"] == data["ok"]
274 assert is_binary(activity.data["id"])
280 "actor" => user.ap_id,
282 "context" => "blabla",
284 "actor" => user.ap_id,
291 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
292 assert activity.data["ok"] == data["ok"]
293 assert activity.data["id"] == given_id
294 assert activity.data["context"] == "blabla"
295 assert activity.data["context_id"]
298 test "adds a context when none is there" do
302 "actor" => user.ap_id,
305 "actor" => user.ap_id,
312 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
313 object = Pleroma.Object.normalize(activity)
315 assert is_binary(activity.data["context"])
316 assert is_binary(object.data["context"])
317 assert activity.data["context_id"]
318 assert object.data["context_id"]
321 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
325 "actor" => user.ap_id,
328 "actor" => user.ap_id,
335 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
336 assert object = Object.normalize(activity)
337 assert is_binary(object.data["id"])
341 describe "listen activities" do
342 test "does not increase user note count" do
346 ActivityPub.listen(%{
347 to: ["https://www.w3.org/ns/activitystreams#Public"],
351 "actor" => user.ap_id,
352 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
354 "title" => "lain radio episode 1",
360 assert activity.actor == user.ap_id
362 user = User.get_cached_by_id(user.id)
363 assert user.info.note_count == 0
366 test "can be fetched into a timeline" do
367 _listen_activity_1 = insert(:listen)
368 _listen_activity_2 = insert(:listen)
369 _listen_activity_3 = insert(:listen)
371 timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]})
373 assert length(timeline) == 3
377 describe "create activities" do
378 test "removes doubled 'to' recipients" do
382 ActivityPub.create(%{
383 to: ["user1", "user1", "user2"],
387 "to" => ["user1", "user1", "user2"],
389 "content" => "testing"
393 assert activity.data["to"] == ["user1", "user2"]
394 assert activity.actor == user.ap_id
395 assert activity.recipients == ["user1", "user2", user.ap_id]
398 test "increases user note count only for public activities" do
402 CommonAPI.post(User.get_cached_by_id(user.id), %{
404 "visibility" => "public"
408 CommonAPI.post(User.get_cached_by_id(user.id), %{
410 "visibility" => "unlisted"
414 CommonAPI.post(User.get_cached_by_id(user.id), %{
416 "visibility" => "private"
420 CommonAPI.post(User.get_cached_by_id(user.id), %{
422 "visibility" => "direct"
425 user = User.get_cached_by_id(user.id)
426 assert user.info.note_count == 2
429 test "increases replies count" do
431 user2 = insert(:user)
433 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
434 ap_id = activity.data["id"]
435 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
438 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
439 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
440 assert object.data["repliesCount"] == 1
443 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
444 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
445 assert object.data["repliesCount"] == 2
448 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
449 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
450 assert object.data["repliesCount"] == 2
453 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
454 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
455 assert object.data["repliesCount"] == 2
459 describe "fetch activities for recipients" do
460 test "retrieve the activities for certain recipients" do
461 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
462 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
463 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
465 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
466 assert length(activities) == 2
467 assert activities == [activity_one, activity_two]
471 describe "fetch activities in context" do
472 test "retrieves activities that have a given context" do
473 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
474 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
475 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
476 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
477 activity_five = insert(:note_activity)
480 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
482 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
483 assert activities == [activity_two, activity]
487 test "doesn't return blocked activities" do
488 activity_one = insert(:note_activity)
489 activity_two = insert(:note_activity)
490 activity_three = insert(:note_activity)
492 booster = insert(:user)
493 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
496 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
498 assert Enum.member?(activities, activity_two)
499 assert Enum.member?(activities, activity_three)
500 refute Enum.member?(activities, activity_one)
502 {:ok, user} = User.unblock(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 assert Enum.member?(activities, activity_one)
511 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
512 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
513 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
514 activity_three = Activity.get_by_id(activity_three.id)
517 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
519 assert Enum.member?(activities, activity_two)
520 refute Enum.member?(activities, activity_three)
521 refute Enum.member?(activities, boost_activity)
522 assert Enum.member?(activities, activity_one)
525 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
527 assert Enum.member?(activities, activity_two)
528 assert Enum.member?(activities, activity_three)
529 assert Enum.member?(activities, boost_activity)
530 assert Enum.member?(activities, activity_one)
533 test "doesn't return transitive interactions concerning blocked users" do
534 blocker = insert(:user)
535 blockee = insert(:user)
536 friend = insert(:user)
538 {:ok, blocker} = User.block(blocker, blockee)
540 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
542 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
544 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
546 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
548 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
550 assert Enum.member?(activities, activity_one)
551 refute Enum.member?(activities, activity_two)
552 refute Enum.member?(activities, activity_three)
553 refute Enum.member?(activities, activity_four)
556 test "doesn't return announce activities concerning blocked users" do
557 blocker = insert(:user)
558 blockee = insert(:user)
559 friend = insert(:user)
561 {:ok, blocker} = User.block(blocker, blockee)
563 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
565 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
567 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
570 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
571 |> Enum.map(fn act -> act.id end)
573 assert Enum.member?(activities, activity_one.id)
574 refute Enum.member?(activities, activity_two.id)
575 refute Enum.member?(activities, activity_three.id)
578 test "doesn't return activities from blocked domains" do
579 domain = "dogwhistle.zone"
580 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
581 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
582 activity = insert(:note_activity, %{note: note})
584 {:ok, user} = User.block_domain(user, domain)
587 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
589 refute activity in activities
591 followed_user = insert(:user)
592 ActivityPub.follow(user, followed_user)
593 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
596 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
598 refute repeat_activity in activities
601 test "doesn't return muted activities" do
602 activity_one = insert(:note_activity)
603 activity_two = insert(:note_activity)
604 activity_three = insert(:note_activity)
606 booster = insert(:user)
607 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
610 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
612 assert Enum.member?(activities, activity_two)
613 assert Enum.member?(activities, activity_three)
614 refute Enum.member?(activities, activity_one)
616 # Calling with 'with_muted' will deliver muted activities, too.
618 ActivityPub.fetch_activities([], %{
619 "muting_user" => user,
620 "with_muted" => true,
621 "skip_preload" => true
624 assert Enum.member?(activities, activity_two)
625 assert Enum.member?(activities, activity_three)
626 assert Enum.member?(activities, activity_one)
628 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
631 ActivityPub.fetch_activities([], %{"muting_user" => user, "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.mute(user, %User{ap_id: activity_three.data["actor"]})
638 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
639 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
640 activity_three = Activity.get_by_id(activity_three.id)
643 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
645 assert Enum.member?(activities, activity_two)
646 refute Enum.member?(activities, activity_three)
647 refute Enum.member?(activities, boost_activity)
648 assert Enum.member?(activities, activity_one)
650 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
652 assert Enum.member?(activities, activity_two)
653 assert Enum.member?(activities, activity_three)
654 assert Enum.member?(activities, boost_activity)
655 assert Enum.member?(activities, activity_one)
658 test "doesn't return thread muted activities" do
660 _activity_one = insert(:note_activity)
661 note_two = insert(:note, data: %{"context" => "suya.."})
662 activity_two = insert(:note_activity, note: note_two)
664 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
666 assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user})
669 test "returns thread muted activities when with_muted is set" do
671 _activity_one = insert(:note_activity)
672 note_two = insert(:note, data: %{"context" => "suya.."})
673 activity_two = insert(:note_activity, note: note_two)
675 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
677 assert [_activity_two, _activity_one] =
678 ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true})
681 test "does include announces on request" do
682 activity_three = insert(:note_activity)
684 booster = insert(:user)
686 {:ok, user} = User.follow(user, booster)
688 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
690 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
692 assert announce_activity.id == announce.id
695 test "excludes reblogs on request" do
697 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
698 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
700 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
702 assert activity == expected_activity
705 describe "public fetch activities" do
706 test "doesn't retrieve unlisted activities" do
709 {:ok, _unlisted_activity} =
710 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
712 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
714 [activity] = ActivityPub.fetch_public_activities()
716 assert activity == listed_activity
719 test "retrieves public activities" do
720 _activities = ActivityPub.fetch_public_activities()
722 %{public: public} = ActivityBuilder.public_and_non_public()
724 activities = ActivityPub.fetch_public_activities()
725 assert length(activities) == 1
726 assert Enum.at(activities, 0) == public
729 test "retrieves a maximum of 20 activities" do
730 activities = ActivityBuilder.insert_list(30)
731 last_expected = List.last(activities)
733 activities = ActivityPub.fetch_public_activities()
734 last = List.last(activities)
736 assert length(activities) == 20
737 assert last == last_expected
740 test "retrieves ids starting from a since_id" do
741 activities = ActivityBuilder.insert_list(30)
742 later_activities = ActivityBuilder.insert_list(10)
743 since_id = List.last(activities).id
744 last_expected = List.last(later_activities)
746 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
747 last = List.last(activities)
749 assert length(activities) == 10
750 assert last == last_expected
753 test "retrieves ids up to max_id" do
754 _first_activities = ActivityBuilder.insert_list(10)
755 activities = ActivityBuilder.insert_list(20)
756 later_activities = ActivityBuilder.insert_list(10)
757 max_id = List.first(later_activities).id
758 last_expected = List.last(activities)
760 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
761 last = List.last(activities)
763 assert length(activities) == 20
764 assert last == last_expected
767 test "paginates via offset/limit" do
768 _first_activities = ActivityBuilder.insert_list(10)
769 activities = ActivityBuilder.insert_list(10)
770 _later_activities = ActivityBuilder.insert_list(10)
771 first_expected = List.first(activities)
774 ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
776 first = List.first(activities)
778 assert length(activities) == 20
779 assert first == first_expected
782 test "doesn't return reblogs for users for whom reblogs have been muted" do
783 activity = insert(:note_activity)
785 booster = insert(:user)
786 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
788 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
790 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
792 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
795 test "returns reblogs for users for whom reblogs have not been muted" do
796 activity = insert(:note_activity)
798 booster = insert(:user)
799 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
800 {:ok, user} = CommonAPI.show_reblogs(user, booster)
802 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
804 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
806 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
810 describe "like an object" do
811 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
812 Pleroma.Config.put([:instance, :federating], true)
813 note_activity = insert(:note_activity)
814 assert object_activity = Object.normalize(note_activity)
818 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
819 assert called(Pleroma.Web.Federator.publish(like_activity))
822 test "returns exist activity if object already liked" do
823 note_activity = insert(:note_activity)
824 assert object_activity = Object.normalize(note_activity)
828 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
830 {:ok, like_activity_exist, _object} = ActivityPub.like(user, object_activity)
831 assert like_activity == like_activity_exist
834 test "adds a like activity to the db" do
835 note_activity = insert(:note_activity)
836 assert object = Object.normalize(note_activity)
839 user_two = insert(:user)
841 {:ok, like_activity, object} = ActivityPub.like(user, object)
843 assert like_activity.data["actor"] == user.ap_id
844 assert like_activity.data["type"] == "Like"
845 assert like_activity.data["object"] == object.data["id"]
846 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
847 assert like_activity.data["context"] == object.data["context"]
848 assert object.data["like_count"] == 1
849 assert object.data["likes"] == [user.ap_id]
851 # Just return the original activity if the user already liked it.
852 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
854 assert like_activity == same_like_activity
855 assert object.data["likes"] == [user.ap_id]
856 assert object.data["like_count"] == 1
858 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
859 assert object.data["like_count"] == 2
863 describe "unliking" do
864 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
865 Pleroma.Config.put([:instance, :federating], true)
867 note_activity = insert(:note_activity)
868 object = Object.normalize(note_activity)
871 {:ok, object} = ActivityPub.unlike(user, object)
872 refute called(Pleroma.Web.Federator.publish())
874 {:ok, _like_activity, object} = ActivityPub.like(user, object)
875 assert object.data["like_count"] == 1
877 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
878 assert object.data["like_count"] == 0
880 assert called(Pleroma.Web.Federator.publish(unlike_activity))
883 test "unliking a previously liked object" do
884 note_activity = insert(:note_activity)
885 object = Object.normalize(note_activity)
888 # Unliking something that hasn't been liked does nothing
889 {:ok, object} = ActivityPub.unlike(user, object)
890 assert object.data["like_count"] == 0
892 {:ok, like_activity, object} = ActivityPub.like(user, object)
893 assert object.data["like_count"] == 1
895 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
896 assert object.data["like_count"] == 0
898 assert Activity.get_by_id(like_activity.id) == nil
899 assert note_activity.actor in unlike_activity.recipients
903 describe "announcing an object" do
904 test "adds an announce activity to the db" do
905 note_activity = insert(:note_activity)
906 object = Object.normalize(note_activity)
909 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
910 assert object.data["announcement_count"] == 1
911 assert object.data["announcements"] == [user.ap_id]
913 assert announce_activity.data["to"] == [
914 User.ap_followers(user),
915 note_activity.data["actor"]
918 assert announce_activity.data["object"] == object.data["id"]
919 assert announce_activity.data["actor"] == user.ap_id
920 assert announce_activity.data["context"] == object.data["context"]
924 describe "announcing a private object" do
925 test "adds an announce activity to the db if the audience is not widened" do
927 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
928 object = Object.normalize(note_activity)
930 {:ok, announce_activity, object} = ActivityPub.announce(user, object, nil, true, false)
932 assert announce_activity.data["to"] == [User.ap_followers(user)]
934 assert announce_activity.data["object"] == object.data["id"]
935 assert announce_activity.data["actor"] == user.ap_id
936 assert announce_activity.data["context"] == object.data["context"]
939 test "does not add an announce activity to the db if the audience is widened" do
941 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
942 object = Object.normalize(note_activity)
944 assert {:error, _} = ActivityPub.announce(user, object, nil, true, true)
947 test "does not add an announce activity to the db if the announcer is not the author" do
949 announcer = insert(:user)
950 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
951 object = Object.normalize(note_activity)
953 assert {:error, _} = ActivityPub.announce(announcer, object, nil, true, false)
957 describe "unannouncing an object" do
958 test "unannouncing a previously announced object" do
959 note_activity = insert(:note_activity)
960 object = Object.normalize(note_activity)
963 # Unannouncing an object that is not announced does nothing
964 # {:ok, object} = ActivityPub.unannounce(user, object)
965 # assert object.data["announcement_count"] == 0
967 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
968 assert object.data["announcement_count"] == 1
970 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
971 assert object.data["announcement_count"] == 0
973 assert unannounce_activity.data["to"] == [
974 User.ap_followers(user),
978 assert unannounce_activity.data["type"] == "Undo"
979 assert unannounce_activity.data["object"] == announce_activity.data
980 assert unannounce_activity.data["actor"] == user.ap_id
981 assert unannounce_activity.data["context"] == announce_activity.data["context"]
983 assert Activity.get_by_id(announce_activity.id) == nil
987 describe "uploading files" do
988 test "copies the file to the configured folder" do
990 content_type: "image/jpg",
991 path: Path.absname("test/fixtures/image.jpg"),
992 filename: "an_image.jpg"
995 {:ok, %Object{} = object} = ActivityPub.upload(file)
996 assert object.data["name"] == "an_image.jpg"
999 test "works with base64 encoded images" do
1004 {:ok, %Object{}} = ActivityPub.upload(file)
1008 describe "fetch the latest Follow" do
1009 test "fetches the latest Follow activity" do
1010 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1011 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1012 followed = Repo.get_by(User, ap_id: activity.data["object"])
1014 assert activity == Utils.fetch_latest_follow(follower, followed)
1018 describe "following / unfollowing" do
1019 test "creates a follow activity" do
1020 follower = insert(:user)
1021 followed = insert(:user)
1023 {:ok, activity} = ActivityPub.follow(follower, followed)
1024 assert activity.data["type"] == "Follow"
1025 assert activity.data["actor"] == follower.ap_id
1026 assert activity.data["object"] == followed.ap_id
1029 test "creates an undo activity for the last follow" do
1030 follower = insert(:user)
1031 followed = insert(:user)
1033 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1034 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1036 assert activity.data["type"] == "Undo"
1037 assert activity.data["actor"] == follower.ap_id
1039 embedded_object = activity.data["object"]
1040 assert is_map(embedded_object)
1041 assert embedded_object["type"] == "Follow"
1042 assert embedded_object["object"] == followed.ap_id
1043 assert embedded_object["id"] == follow_activity.data["id"]
1047 describe "blocking / unblocking" do
1048 test "creates a block activity" do
1049 blocker = insert(:user)
1050 blocked = insert(:user)
1052 {:ok, activity} = ActivityPub.block(blocker, blocked)
1054 assert activity.data["type"] == "Block"
1055 assert activity.data["actor"] == blocker.ap_id
1056 assert activity.data["object"] == blocked.ap_id
1059 test "creates an undo activity for the last block" do
1060 blocker = insert(:user)
1061 blocked = insert(:user)
1063 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
1064 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
1066 assert activity.data["type"] == "Undo"
1067 assert activity.data["actor"] == blocker.ap_id
1069 embedded_object = activity.data["object"]
1070 assert is_map(embedded_object)
1071 assert embedded_object["type"] == "Block"
1072 assert embedded_object["object"] == blocked.ap_id
1073 assert embedded_object["id"] == block_activity.data["id"]
1077 describe "deletion" do
1078 test "it creates a delete activity and deletes the original object" do
1079 note = insert(:note_activity)
1080 object = Object.normalize(note)
1081 {:ok, delete} = ActivityPub.delete(object)
1083 assert delete.data["type"] == "Delete"
1084 assert delete.data["actor"] == note.data["actor"]
1085 assert delete.data["object"] == object.data["id"]
1087 assert Activity.get_by_id(delete.id) != nil
1089 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
1092 test "decrements user note count only for public activities" do
1093 user = insert(:user, info: %{note_count: 10})
1096 CommonAPI.post(User.get_cached_by_id(user.id), %{
1098 "visibility" => "public"
1102 CommonAPI.post(User.get_cached_by_id(user.id), %{
1104 "visibility" => "unlisted"
1108 CommonAPI.post(User.get_cached_by_id(user.id), %{
1110 "visibility" => "private"
1114 CommonAPI.post(User.get_cached_by_id(user.id), %{
1116 "visibility" => "direct"
1119 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
1120 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
1121 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
1122 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
1124 user = User.get_cached_by_id(user.id)
1125 assert user.info.note_count == 10
1128 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
1129 user = insert(:user)
1130 note = insert(:note_activity)
1131 object = Object.normalize(note)
1137 "actor" => object.data["actor"],
1138 "id" => object.data["id"],
1139 "to" => [user.ap_id],
1143 |> Object.update_and_set_cache()
1145 {:ok, delete} = ActivityPub.delete(object)
1147 assert user.ap_id in delete.data["to"]
1150 test "decreases reply count" do
1151 user = insert(:user)
1152 user2 = insert(:user)
1154 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
1155 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
1156 ap_id = activity.data["id"]
1158 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
1159 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
1160 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
1161 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
1163 _ = CommonAPI.delete(direct_reply.id, user2)
1164 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1165 assert object.data["repliesCount"] == 2
1167 _ = CommonAPI.delete(private_reply.id, user2)
1168 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1169 assert object.data["repliesCount"] == 2
1171 _ = CommonAPI.delete(public_reply.id, user2)
1172 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1173 assert object.data["repliesCount"] == 1
1175 _ = CommonAPI.delete(unlisted_reply.id, user2)
1176 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1177 assert object.data["repliesCount"] == 0
1181 describe "timeline post-processing" do
1182 test "it filters broken threads" do
1183 user1 = insert(:user)
1184 user2 = insert(:user)
1185 user3 = insert(:user)
1187 {:ok, user1} = User.follow(user1, user3)
1188 assert User.following?(user1, user3)
1190 {:ok, user2} = User.follow(user2, user3)
1191 assert User.following?(user2, user3)
1193 {:ok, user3} = User.follow(user3, user2)
1194 assert User.following?(user3, user2)
1196 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
1198 {:ok, private_activity_1} =
1199 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
1201 {:ok, private_activity_2} =
1202 CommonAPI.post(user2, %{
1204 "visibility" => "private",
1205 "in_reply_to_status_id" => private_activity_1.id
1208 {:ok, private_activity_3} =
1209 CommonAPI.post(user3, %{
1211 "visibility" => "private",
1212 "in_reply_to_status_id" => private_activity_2.id
1216 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1217 |> Enum.map(fn a -> a.id end)
1219 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1221 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1223 assert length(activities) == 3
1226 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{"user" => user1})
1227 |> Enum.map(fn a -> a.id end)
1229 assert [public_activity.id, private_activity_1.id] == activities
1230 assert length(activities) == 2
1234 describe "update" do
1235 test "it creates an update activity with the new user data" do
1236 user = insert(:user)
1237 {:ok, user} = User.ensure_keys_present(user)
1238 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1241 ActivityPub.update(%{
1242 actor: user_data["id"],
1243 to: [user.follower_address],
1248 assert update.data["actor"] == user.ap_id
1249 assert update.data["to"] == [user.follower_address]
1250 assert embedded_object = update.data["object"]
1251 assert embedded_object["id"] == user_data["id"]
1252 assert embedded_object["type"] == user_data["type"]
1256 test "returned pinned statuses" do
1257 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
1258 user = insert(:user)
1260 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1261 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1262 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1264 CommonAPI.pin(activity_one.id, user)
1265 user = refresh_record(user)
1267 CommonAPI.pin(activity_two.id, user)
1268 user = refresh_record(user)
1270 CommonAPI.pin(activity_three.id, user)
1271 user = refresh_record(user)
1273 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1275 assert 3 = length(activities)
1278 test "it can create a Flag activity" do
1279 reporter = insert(:user)
1280 target_account = insert(:user)
1281 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
1282 context = Utils.generate_context_id()
1285 reporter_ap_id = reporter.ap_id
1286 target_ap_id = target_account.ap_id
1287 activity_ap_id = activity.data["id"]
1289 assert {:ok, activity} =
1293 account: target_account,
1294 statuses: [activity],
1299 actor: ^reporter_ap_id,
1302 "content" => ^content,
1303 "context" => ^context,
1304 "object" => [^target_ap_id, ^activity_ap_id]
1309 test "fetch_activities/2 returns activities addressed to a list " do
1310 user = insert(:user)
1311 member = insert(:user)
1312 {:ok, list} = Pleroma.List.create("foo", user)
1313 {:ok, list} = Pleroma.List.follow(list, member)
1316 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1318 activity = Repo.preload(activity, :bookmark)
1319 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1321 assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity]
1325 File.read!("test/fixtures/avatar_data_uri")
1328 describe "fetch_activities_bounded" do
1329 test "fetches private posts for followed users" do
1330 user = insert(:user)
1333 CommonAPI.post(user, %{
1334 "status" => "thought I looked cute might delete later :3",
1335 "visibility" => "private"
1338 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1339 assert result.id == activity.id
1342 test "fetches only public posts for other users" do
1343 user = insert(:user)
1344 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe", "visibility" => "public"})
1346 {:ok, _private_activity} =
1347 CommonAPI.post(user, %{
1348 "status" => "why is tenshi eating a corndog so cute?",
1349 "visibility" => "private"
1352 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1353 assert result.id == activity.id
1357 describe "fetch_follow_information_for_user" do
1358 test "syncronizes following/followers counters" do
1362 follower_address: "http://localhost:4001/users/fuser2/followers",
1363 following_address: "http://localhost:4001/users/fuser2/following"
1366 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1367 assert info.follower_count == 527
1368 assert info.following_count == 267
1371 test "detects hidden followers" do
1374 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1375 %Tesla.Env{status: 403, body: ""}
1378 apply(HttpRequestMock, :request, [env])
1385 follower_address: "http://localhost:4001/users/masto_closed/followers",
1386 following_address: "http://localhost:4001/users/masto_closed/following"
1389 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1390 assert info.hide_followers == true
1391 assert info.hide_follows == false
1394 test "detects hidden follows" do
1397 "http://localhost:4001/users/masto_closed/following?page=1" ->
1398 %Tesla.Env{status: 403, body: ""}
1401 apply(HttpRequestMock, :request, [env])
1408 follower_address: "http://localhost:4001/users/masto_closed/followers",
1409 following_address: "http://localhost:4001/users/masto_closed/following"
1412 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1413 assert info.hide_followers == false
1414 assert info.hide_follows == true