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))
46 describe "fetching restricted by visibility" do
47 test "it restricts by the appropriate visibility" do
50 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
52 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
54 {:ok, unlisted_activity} =
55 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
57 {:ok, private_activity} =
58 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
61 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
63 assert activities == [direct_activity]
66 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
68 assert activities == [unlisted_activity]
71 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
73 assert activities == [private_activity]
76 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
78 assert activities == [public_activity]
81 ActivityPub.fetch_activities([], %{
82 :visibility => ~w[private public],
83 "actor_id" => user.ap_id
86 assert activities == [public_activity, private_activity]
90 describe "fetching excluded by visibility" do
91 test "it excludes by the appropriate visibility" do
94 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
96 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
98 {:ok, unlisted_activity} =
99 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
101 {:ok, private_activity} =
102 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
105 ActivityPub.fetch_activities([], %{
106 "exclude_visibilities" => "direct",
107 "actor_id" => user.ap_id
110 assert public_activity in activities
111 assert unlisted_activity in activities
112 assert private_activity in activities
113 refute direct_activity in activities
116 ActivityPub.fetch_activities([], %{
117 "exclude_visibilities" => "unlisted",
118 "actor_id" => user.ap_id
121 assert public_activity in activities
122 refute unlisted_activity in activities
123 assert private_activity in activities
124 assert direct_activity in activities
127 ActivityPub.fetch_activities([], %{
128 "exclude_visibilities" => "private",
129 "actor_id" => user.ap_id
132 assert public_activity in activities
133 assert unlisted_activity in activities
134 refute private_activity in activities
135 assert direct_activity in activities
138 ActivityPub.fetch_activities([], %{
139 "exclude_visibilities" => "public",
140 "actor_id" => user.ap_id
143 refute public_activity in activities
144 assert unlisted_activity in activities
145 assert private_activity in activities
146 assert direct_activity in activities
150 describe "building a user from his ap id" do
151 test "it returns a user" do
152 user_id = "http://mastodon.example.org/users/admin"
153 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
154 assert user.ap_id == user_id
155 assert user.nickname == "admin@mastodon.example.org"
156 assert user.info.source_data
157 assert user.info.ap_enabled
158 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
161 test "it fetches the appropriate tag-restricted posts" do
164 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
165 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
166 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
168 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
171 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
174 ActivityPub.fetch_activities([], %{
176 "tag" => ["test", "essais"],
177 "tag_reject" => ["reject"]
181 ActivityPub.fetch_activities([], %{
184 "tag_all" => ["test", "reject"]
187 assert fetch_one == [status_one, status_three]
188 assert fetch_two == [status_one, status_two, status_three]
189 assert fetch_three == [status_one, status_two]
190 assert fetch_four == [status_three]
194 describe "insertion" do
195 test "drops activities beyond a certain limit" do
196 limit = Pleroma.Config.get([:instance, :remote_limit])
199 :crypto.strong_rand_bytes(limit + 1)
201 |> binary_part(0, limit + 1)
206 "content" => random_text
210 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
213 test "doesn't drop activities with content being null" do
217 "actor" => user.ap_id,
220 "actor" => user.ap_id,
227 assert {:ok, _} = ActivityPub.insert(data)
230 test "returns the activity if one with the same id is already in" do
231 activity = insert(:note_activity)
232 {:ok, new_activity} = ActivityPub.insert(activity.data)
234 assert activity.id == new_activity.id
237 test "inserts a given map into the activity database, giving it an id if it has none." do
241 "actor" => user.ap_id,
244 "actor" => user.ap_id,
251 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
252 assert activity.data["ok"] == data["ok"]
253 assert is_binary(activity.data["id"])
259 "actor" => user.ap_id,
261 "context" => "blabla",
263 "actor" => user.ap_id,
270 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
271 assert activity.data["ok"] == data["ok"]
272 assert activity.data["id"] == given_id
273 assert activity.data["context"] == "blabla"
274 assert activity.data["context_id"]
277 test "adds a context when none is there" do
281 "actor" => user.ap_id,
284 "actor" => user.ap_id,
291 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
292 object = Pleroma.Object.normalize(activity)
294 assert is_binary(activity.data["context"])
295 assert is_binary(object.data["context"])
296 assert activity.data["context_id"]
297 assert object.data["context_id"]
300 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
304 "actor" => user.ap_id,
307 "actor" => user.ap_id,
314 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
315 assert object = Object.normalize(activity)
316 assert is_binary(object.data["id"])
320 describe "listen activities" do
321 test "does not increase user note count" do
325 ActivityPub.listen(%{
326 to: ["https://www.w3.org/ns/activitystreams#Public"],
330 "actor" => user.ap_id,
331 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
333 "title" => "lain radio episode 1",
339 assert activity.actor == user.ap_id
341 user = User.get_cached_by_id(user.id)
342 assert user.info.note_count == 0
345 test "can be fetched into a timeline" do
346 _listen_activity_1 = insert(:listen)
347 _listen_activity_2 = insert(:listen)
348 _listen_activity_3 = insert(:listen)
350 timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]})
352 assert length(timeline) == 3
356 describe "create activities" do
357 test "removes doubled 'to' recipients" do
361 ActivityPub.create(%{
362 to: ["user1", "user1", "user2"],
366 "to" => ["user1", "user1", "user2"],
368 "content" => "testing"
372 assert activity.data["to"] == ["user1", "user2"]
373 assert activity.actor == user.ap_id
374 assert activity.recipients == ["user1", "user2", user.ap_id]
377 test "increases user note count only for public activities" do
381 CommonAPI.post(User.get_cached_by_id(user.id), %{
383 "visibility" => "public"
387 CommonAPI.post(User.get_cached_by_id(user.id), %{
389 "visibility" => "unlisted"
393 CommonAPI.post(User.get_cached_by_id(user.id), %{
395 "visibility" => "private"
399 CommonAPI.post(User.get_cached_by_id(user.id), %{
401 "visibility" => "direct"
404 user = User.get_cached_by_id(user.id)
405 assert user.info.note_count == 2
408 test "increases replies count" do
410 user2 = insert(:user)
412 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
413 ap_id = activity.data["id"]
414 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
417 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
418 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
419 assert object.data["repliesCount"] == 1
422 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
423 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
424 assert object.data["repliesCount"] == 2
427 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
428 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
429 assert object.data["repliesCount"] == 2
432 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
433 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
434 assert object.data["repliesCount"] == 2
438 describe "fetch activities for recipients" do
439 test "retrieve the activities for certain recipients" do
440 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
441 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
442 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
444 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
445 assert length(activities) == 2
446 assert activities == [activity_one, activity_two]
450 describe "fetch activities in context" do
451 test "retrieves activities that have a given context" do
452 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
453 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
454 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
455 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
456 activity_five = insert(:note_activity)
459 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
461 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
462 assert activities == [activity_two, activity]
466 test "doesn't return blocked activities" do
467 activity_one = insert(:note_activity)
468 activity_two = insert(:note_activity)
469 activity_three = insert(:note_activity)
471 booster = insert(:user)
472 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
475 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
477 assert Enum.member?(activities, activity_two)
478 assert Enum.member?(activities, activity_three)
479 refute Enum.member?(activities, activity_one)
481 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
484 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
486 assert Enum.member?(activities, activity_two)
487 assert Enum.member?(activities, activity_three)
488 assert Enum.member?(activities, activity_one)
490 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
491 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
492 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
493 activity_three = Activity.get_by_id(activity_three.id)
496 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
498 assert Enum.member?(activities, activity_two)
499 refute Enum.member?(activities, activity_three)
500 refute Enum.member?(activities, boost_activity)
501 assert Enum.member?(activities, activity_one)
504 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
506 assert Enum.member?(activities, activity_two)
507 assert Enum.member?(activities, activity_three)
508 assert Enum.member?(activities, boost_activity)
509 assert Enum.member?(activities, activity_one)
512 test "doesn't return transitive interactions concerning blocked users" do
513 blocker = insert(:user)
514 blockee = insert(:user)
515 friend = insert(:user)
517 {:ok, blocker} = User.block(blocker, blockee)
519 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
521 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
523 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
525 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
527 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
529 assert Enum.member?(activities, activity_one)
530 refute Enum.member?(activities, activity_two)
531 refute Enum.member?(activities, activity_three)
532 refute Enum.member?(activities, activity_four)
535 test "doesn't return announce activities concerning blocked users" do
536 blocker = insert(:user)
537 blockee = insert(:user)
538 friend = insert(:user)
540 {:ok, blocker} = User.block(blocker, blockee)
542 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
544 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
546 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
549 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
550 |> Enum.map(fn act -> act.id end)
552 assert Enum.member?(activities, activity_one.id)
553 refute Enum.member?(activities, activity_two.id)
554 refute Enum.member?(activities, activity_three.id)
557 test "doesn't return activities from blocked domains" do
558 domain = "dogwhistle.zone"
559 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
560 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
561 activity = insert(:note_activity, %{note: note})
563 {:ok, user} = User.block_domain(user, domain)
566 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
568 refute activity in activities
570 followed_user = insert(:user)
571 ActivityPub.follow(user, followed_user)
572 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
575 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
577 refute repeat_activity in activities
580 test "doesn't return muted activities" do
581 activity_one = insert(:note_activity)
582 activity_two = insert(:note_activity)
583 activity_three = insert(:note_activity)
585 booster = insert(:user)
586 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
589 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
591 assert Enum.member?(activities, activity_two)
592 assert Enum.member?(activities, activity_three)
593 refute Enum.member?(activities, activity_one)
595 # Calling with 'with_muted' will deliver muted activities, too.
597 ActivityPub.fetch_activities([], %{
598 "muting_user" => user,
599 "with_muted" => true,
600 "skip_preload" => true
603 assert Enum.member?(activities, activity_two)
604 assert Enum.member?(activities, activity_three)
605 assert Enum.member?(activities, activity_one)
607 {:ok, user} = User.unmute(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 assert Enum.member?(activities, activity_one)
616 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
617 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
618 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
619 activity_three = Activity.get_by_id(activity_three.id)
622 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
624 assert Enum.member?(activities, activity_two)
625 refute Enum.member?(activities, activity_three)
626 refute Enum.member?(activities, boost_activity)
627 assert Enum.member?(activities, activity_one)
629 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
631 assert Enum.member?(activities, activity_two)
632 assert Enum.member?(activities, activity_three)
633 assert Enum.member?(activities, boost_activity)
634 assert Enum.member?(activities, activity_one)
637 test "doesn't return thread muted activities" do
639 _activity_one = insert(:note_activity)
640 note_two = insert(:note, data: %{"context" => "suya.."})
641 activity_two = insert(:note_activity, note: note_two)
643 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
645 assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user})
648 test "returns thread muted activities when with_muted is set" do
650 _activity_one = insert(:note_activity)
651 note_two = insert(:note, data: %{"context" => "suya.."})
652 activity_two = insert(:note_activity, note: note_two)
654 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
656 assert [_activity_two, _activity_one] =
657 ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true})
660 test "does include announces on request" do
661 activity_three = insert(:note_activity)
663 booster = insert(:user)
665 {:ok, user} = User.follow(user, booster)
667 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
669 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
671 assert announce_activity.id == announce.id
674 test "excludes reblogs on request" do
676 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
677 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
679 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
681 assert activity == expected_activity
684 describe "public fetch activities" do
685 test "doesn't retrieve unlisted activities" do
688 {:ok, _unlisted_activity} =
689 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
691 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
693 [activity] = ActivityPub.fetch_public_activities()
695 assert activity == listed_activity
698 test "retrieves public activities" do
699 _activities = ActivityPub.fetch_public_activities()
701 %{public: public} = ActivityBuilder.public_and_non_public()
703 activities = ActivityPub.fetch_public_activities()
704 assert length(activities) == 1
705 assert Enum.at(activities, 0) == public
708 test "retrieves a maximum of 20 activities" do
709 activities = ActivityBuilder.insert_list(30)
710 last_expected = List.last(activities)
712 activities = ActivityPub.fetch_public_activities()
713 last = List.last(activities)
715 assert length(activities) == 20
716 assert last == last_expected
719 test "retrieves ids starting from a since_id" do
720 activities = ActivityBuilder.insert_list(30)
721 later_activities = ActivityBuilder.insert_list(10)
722 since_id = List.last(activities).id
723 last_expected = List.last(later_activities)
725 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
726 last = List.last(activities)
728 assert length(activities) == 10
729 assert last == last_expected
732 test "retrieves ids up to max_id" do
733 _first_activities = ActivityBuilder.insert_list(10)
734 activities = ActivityBuilder.insert_list(20)
735 later_activities = ActivityBuilder.insert_list(10)
736 max_id = List.first(later_activities).id
737 last_expected = List.last(activities)
739 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
740 last = List.last(activities)
742 assert length(activities) == 20
743 assert last == last_expected
746 test "paginates via offset/limit" do
747 _first_activities = ActivityBuilder.insert_list(10)
748 activities = ActivityBuilder.insert_list(10)
749 _later_activities = ActivityBuilder.insert_list(10)
750 first_expected = List.first(activities)
753 ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
755 first = List.first(activities)
757 assert length(activities) == 20
758 assert first == first_expected
761 test "doesn't return reblogs for users for whom reblogs have been muted" do
762 activity = insert(:note_activity)
764 booster = insert(:user)
765 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
767 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
769 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
771 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
774 test "returns reblogs for users for whom reblogs have not been muted" do
775 activity = insert(:note_activity)
777 booster = insert(:user)
778 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
779 {:ok, user} = CommonAPI.show_reblogs(user, booster)
781 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
783 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
785 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
789 describe "like an object" do
790 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
791 Pleroma.Config.put([:instance, :federating], true)
792 note_activity = insert(:note_activity)
793 assert object_activity = Object.normalize(note_activity)
797 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
798 assert called(Pleroma.Web.Federator.publish(like_activity))
801 test "returns exist activity if object already liked" do
802 note_activity = insert(:note_activity)
803 assert object_activity = Object.normalize(note_activity)
807 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
809 {:ok, like_activity_exist, _object} = ActivityPub.like(user, object_activity)
810 assert like_activity == like_activity_exist
813 test "adds a like activity to the db" do
814 note_activity = insert(:note_activity)
815 assert object = Object.normalize(note_activity)
818 user_two = insert(:user)
820 {:ok, like_activity, object} = ActivityPub.like(user, object)
822 assert like_activity.data["actor"] == user.ap_id
823 assert like_activity.data["type"] == "Like"
824 assert like_activity.data["object"] == object.data["id"]
825 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
826 assert like_activity.data["context"] == object.data["context"]
827 assert object.data["like_count"] == 1
828 assert object.data["likes"] == [user.ap_id]
830 # Just return the original activity if the user already liked it.
831 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
833 assert like_activity == same_like_activity
834 assert object.data["likes"] == [user.ap_id]
835 assert object.data["like_count"] == 1
837 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
838 assert object.data["like_count"] == 2
842 describe "unliking" do
843 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
844 Pleroma.Config.put([:instance, :federating], true)
846 note_activity = insert(:note_activity)
847 object = Object.normalize(note_activity)
850 {:ok, object} = ActivityPub.unlike(user, object)
851 refute called(Pleroma.Web.Federator.publish())
853 {:ok, _like_activity, object} = ActivityPub.like(user, object)
854 assert object.data["like_count"] == 1
856 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
857 assert object.data["like_count"] == 0
859 assert called(Pleroma.Web.Federator.publish(unlike_activity))
862 test "unliking a previously liked object" do
863 note_activity = insert(:note_activity)
864 object = Object.normalize(note_activity)
867 # Unliking something that hasn't been liked does nothing
868 {:ok, object} = ActivityPub.unlike(user, object)
869 assert object.data["like_count"] == 0
871 {:ok, like_activity, object} = ActivityPub.like(user, object)
872 assert object.data["like_count"] == 1
874 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
875 assert object.data["like_count"] == 0
877 assert Activity.get_by_id(like_activity.id) == nil
878 assert note_activity.actor in unlike_activity.recipients
882 describe "announcing an object" do
883 test "adds an announce activity to the db" do
884 note_activity = insert(:note_activity)
885 object = Object.normalize(note_activity)
888 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
889 assert object.data["announcement_count"] == 1
890 assert object.data["announcements"] == [user.ap_id]
892 assert announce_activity.data["to"] == [
893 User.ap_followers(user),
894 note_activity.data["actor"]
897 assert announce_activity.data["object"] == object.data["id"]
898 assert announce_activity.data["actor"] == user.ap_id
899 assert announce_activity.data["context"] == object.data["context"]
903 describe "announcing a private object" do
904 test "adds an announce activity to the db if the audience is not widened" do
906 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
907 object = Object.normalize(note_activity)
909 {:ok, announce_activity, object} = ActivityPub.announce(user, object, nil, true, false)
911 assert announce_activity.data["to"] == [User.ap_followers(user)]
913 assert announce_activity.data["object"] == object.data["id"]
914 assert announce_activity.data["actor"] == user.ap_id
915 assert announce_activity.data["context"] == object.data["context"]
918 test "does not add an announce activity to the db if the audience is widened" do
920 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
921 object = Object.normalize(note_activity)
923 assert {:error, _} = ActivityPub.announce(user, object, nil, true, true)
926 test "does not add an announce activity to the db if the announcer is not the author" do
928 announcer = insert(:user)
929 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
930 object = Object.normalize(note_activity)
932 assert {:error, _} = ActivityPub.announce(announcer, object, nil, true, false)
936 describe "unannouncing an object" do
937 test "unannouncing a previously announced object" do
938 note_activity = insert(:note_activity)
939 object = Object.normalize(note_activity)
942 # Unannouncing an object that is not announced does nothing
943 # {:ok, object} = ActivityPub.unannounce(user, object)
944 # assert object.data["announcement_count"] == 0
946 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
947 assert object.data["announcement_count"] == 1
949 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
950 assert object.data["announcement_count"] == 0
952 assert unannounce_activity.data["to"] == [
953 User.ap_followers(user),
957 assert unannounce_activity.data["type"] == "Undo"
958 assert unannounce_activity.data["object"] == announce_activity.data
959 assert unannounce_activity.data["actor"] == user.ap_id
960 assert unannounce_activity.data["context"] == announce_activity.data["context"]
962 assert Activity.get_by_id(announce_activity.id) == nil
966 describe "uploading files" do
967 test "copies the file to the configured folder" do
969 content_type: "image/jpg",
970 path: Path.absname("test/fixtures/image.jpg"),
971 filename: "an_image.jpg"
974 {:ok, %Object{} = object} = ActivityPub.upload(file)
975 assert object.data["name"] == "an_image.jpg"
978 test "works with base64 encoded images" do
983 {:ok, %Object{}} = ActivityPub.upload(file)
987 describe "fetch the latest Follow" do
988 test "fetches the latest Follow activity" do
989 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
990 follower = Repo.get_by(User, ap_id: activity.data["actor"])
991 followed = Repo.get_by(User, ap_id: activity.data["object"])
993 assert activity == Utils.fetch_latest_follow(follower, followed)
997 describe "following / unfollowing" do
998 test "creates a follow activity" do
999 follower = insert(:user)
1000 followed = insert(:user)
1002 {:ok, activity} = ActivityPub.follow(follower, followed)
1003 assert activity.data["type"] == "Follow"
1004 assert activity.data["actor"] == follower.ap_id
1005 assert activity.data["object"] == followed.ap_id
1008 test "creates an undo activity for the last follow" do
1009 follower = insert(:user)
1010 followed = insert(:user)
1012 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1013 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1015 assert activity.data["type"] == "Undo"
1016 assert activity.data["actor"] == follower.ap_id
1018 embedded_object = activity.data["object"]
1019 assert is_map(embedded_object)
1020 assert embedded_object["type"] == "Follow"
1021 assert embedded_object["object"] == followed.ap_id
1022 assert embedded_object["id"] == follow_activity.data["id"]
1026 describe "blocking / unblocking" do
1027 test "creates a block activity" do
1028 blocker = insert(:user)
1029 blocked = insert(:user)
1031 {:ok, activity} = ActivityPub.block(blocker, blocked)
1033 assert activity.data["type"] == "Block"
1034 assert activity.data["actor"] == blocker.ap_id
1035 assert activity.data["object"] == blocked.ap_id
1038 test "creates an undo activity for the last block" do
1039 blocker = insert(:user)
1040 blocked = insert(:user)
1042 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
1043 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
1045 assert activity.data["type"] == "Undo"
1046 assert activity.data["actor"] == blocker.ap_id
1048 embedded_object = activity.data["object"]
1049 assert is_map(embedded_object)
1050 assert embedded_object["type"] == "Block"
1051 assert embedded_object["object"] == blocked.ap_id
1052 assert embedded_object["id"] == block_activity.data["id"]
1056 describe "deletion" do
1057 test "it creates a delete activity and deletes the original object" do
1058 note = insert(:note_activity)
1059 object = Object.normalize(note)
1060 {:ok, delete} = ActivityPub.delete(object)
1062 assert delete.data["type"] == "Delete"
1063 assert delete.data["actor"] == note.data["actor"]
1064 assert delete.data["object"] == object.data["id"]
1066 assert Activity.get_by_id(delete.id) != nil
1068 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
1071 test "decrements user note count only for public activities" do
1072 user = insert(:user, info: %{note_count: 10})
1075 CommonAPI.post(User.get_cached_by_id(user.id), %{
1077 "visibility" => "public"
1081 CommonAPI.post(User.get_cached_by_id(user.id), %{
1083 "visibility" => "unlisted"
1087 CommonAPI.post(User.get_cached_by_id(user.id), %{
1089 "visibility" => "private"
1093 CommonAPI.post(User.get_cached_by_id(user.id), %{
1095 "visibility" => "direct"
1098 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
1099 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
1100 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
1101 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
1103 user = User.get_cached_by_id(user.id)
1104 assert user.info.note_count == 10
1107 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
1108 user = insert(:user)
1109 note = insert(:note_activity)
1110 object = Object.normalize(note)
1116 "actor" => object.data["actor"],
1117 "id" => object.data["id"],
1118 "to" => [user.ap_id],
1122 |> Object.update_and_set_cache()
1124 {:ok, delete} = ActivityPub.delete(object)
1126 assert user.ap_id in delete.data["to"]
1129 test "decreases reply count" do
1130 user = insert(:user)
1131 user2 = insert(:user)
1133 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
1134 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
1135 ap_id = activity.data["id"]
1137 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
1138 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
1139 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
1140 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
1142 _ = CommonAPI.delete(direct_reply.id, user2)
1143 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1144 assert object.data["repliesCount"] == 2
1146 _ = CommonAPI.delete(private_reply.id, user2)
1147 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1148 assert object.data["repliesCount"] == 2
1150 _ = CommonAPI.delete(public_reply.id, user2)
1151 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1152 assert object.data["repliesCount"] == 1
1154 _ = CommonAPI.delete(unlisted_reply.id, user2)
1155 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1156 assert object.data["repliesCount"] == 0
1160 describe "timeline post-processing" do
1161 test "it filters broken threads" do
1162 user1 = insert(:user)
1163 user2 = insert(:user)
1164 user3 = insert(:user)
1166 {:ok, user1} = User.follow(user1, user3)
1167 assert User.following?(user1, user3)
1169 {:ok, user2} = User.follow(user2, user3)
1170 assert User.following?(user2, user3)
1172 {:ok, user3} = User.follow(user3, user2)
1173 assert User.following?(user3, user2)
1175 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
1177 {:ok, private_activity_1} =
1178 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
1180 {:ok, private_activity_2} =
1181 CommonAPI.post(user2, %{
1183 "visibility" => "private",
1184 "in_reply_to_status_id" => private_activity_1.id
1187 {:ok, private_activity_3} =
1188 CommonAPI.post(user3, %{
1190 "visibility" => "private",
1191 "in_reply_to_status_id" => private_activity_2.id
1195 ActivityPub.fetch_activities([user1.ap_id | user1.following])
1196 |> Enum.map(fn a -> a.id end)
1198 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1200 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1202 assert length(activities) == 3
1205 ActivityPub.fetch_activities([user1.ap_id | user1.following], %{"user" => user1})
1206 |> Enum.map(fn a -> a.id end)
1208 assert [public_activity.id, private_activity_1.id] == activities
1209 assert length(activities) == 2
1213 describe "update" do
1214 test "it creates an update activity with the new user data" do
1215 user = insert(:user)
1216 {:ok, user} = User.ensure_keys_present(user)
1217 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1220 ActivityPub.update(%{
1221 actor: user_data["id"],
1222 to: [user.follower_address],
1227 assert update.data["actor"] == user.ap_id
1228 assert update.data["to"] == [user.follower_address]
1229 assert embedded_object = update.data["object"]
1230 assert embedded_object["id"] == user_data["id"]
1231 assert embedded_object["type"] == user_data["type"]
1235 test "returned pinned statuses" do
1236 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
1237 user = insert(:user)
1239 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1240 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1241 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1243 CommonAPI.pin(activity_one.id, user)
1244 user = refresh_record(user)
1246 CommonAPI.pin(activity_two.id, user)
1247 user = refresh_record(user)
1249 CommonAPI.pin(activity_three.id, user)
1250 user = refresh_record(user)
1252 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1254 assert 3 = length(activities)
1257 test "it can create a Flag activity" do
1258 reporter = insert(:user)
1259 target_account = insert(:user)
1260 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
1261 context = Utils.generate_context_id()
1264 reporter_ap_id = reporter.ap_id
1265 target_ap_id = target_account.ap_id
1266 activity_ap_id = activity.data["id"]
1268 assert {:ok, activity} =
1272 account: target_account,
1273 statuses: [activity],
1278 actor: ^reporter_ap_id,
1281 "content" => ^content,
1282 "context" => ^context,
1283 "object" => [^target_ap_id, ^activity_ap_id]
1288 test "fetch_activities/2 returns activities addressed to a list " do
1289 user = insert(:user)
1290 member = insert(:user)
1291 {:ok, list} = Pleroma.List.create("foo", user)
1292 {:ok, list} = Pleroma.List.follow(list, member)
1295 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1297 activity = Repo.preload(activity, :bookmark)
1298 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1300 assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity]
1304 File.read!("test/fixtures/avatar_data_uri")
1307 describe "fetch_activities_bounded" do
1308 test "fetches private posts for followed users" do
1309 user = insert(:user)
1312 CommonAPI.post(user, %{
1313 "status" => "thought I looked cute might delete later :3",
1314 "visibility" => "private"
1317 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1318 assert result.id == activity.id
1321 test "fetches only public posts for other users" do
1322 user = insert(:user)
1323 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe", "visibility" => "public"})
1325 {:ok, _private_activity} =
1326 CommonAPI.post(user, %{
1327 "status" => "why is tenshi eating a corndog so cute?",
1328 "visibility" => "private"
1331 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1332 assert result.id == activity.id
1336 describe "fetch_follow_information_for_user" do
1337 test "syncronizes following/followers counters" do
1341 follower_address: "http://localhost:4001/users/fuser2/followers",
1342 following_address: "http://localhost:4001/users/fuser2/following"
1345 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1346 assert info.follower_count == 527
1347 assert info.following_count == 267
1350 test "detects hidden followers" do
1353 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1354 %Tesla.Env{status: 403, body: ""}
1357 apply(HttpRequestMock, :request, [env])
1364 follower_address: "http://localhost:4001/users/masto_closed/followers",
1365 following_address: "http://localhost:4001/users/masto_closed/following"
1368 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1369 assert info.hide_followers == true
1370 assert info.hide_follows == false
1373 test "detects hidden follows" do
1376 "http://localhost:4001/users/masto_closed/following?page=1" ->
1377 %Tesla.Env{status: 403, body: ""}
1380 apply(HttpRequestMock, :request, [env])
1387 follower_address: "http://localhost:4001/users/masto_closed/followers",
1388 following_address: "http://localhost:4001/users/masto_closed/following"
1391 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1392 assert info.hide_followers == false
1393 assert info.hide_follows == true