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 "building a user from his ap id" do
91 test "it returns a user" do
92 user_id = "http://mastodon.example.org/users/admin"
93 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
94 assert user.ap_id == user_id
95 assert user.nickname == "admin@mastodon.example.org"
96 assert user.info.source_data
97 assert user.info.ap_enabled
98 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
101 test "it fetches the appropriate tag-restricted posts" do
104 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
105 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
106 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
108 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
111 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
114 ActivityPub.fetch_activities([], %{
116 "tag" => ["test", "essais"],
117 "tag_reject" => ["reject"]
121 ActivityPub.fetch_activities([], %{
124 "tag_all" => ["test", "reject"]
127 assert fetch_one == [status_one, status_three]
128 assert fetch_two == [status_one, status_two, status_three]
129 assert fetch_three == [status_one, status_two]
130 assert fetch_four == [status_three]
134 describe "insertion" do
135 test "drops activities beyond a certain limit" do
136 limit = Pleroma.Config.get([:instance, :remote_limit])
139 :crypto.strong_rand_bytes(limit + 1)
141 |> binary_part(0, limit + 1)
146 "content" => random_text
150 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
153 test "doesn't drop activities with content being null" do
157 "actor" => user.ap_id,
160 "actor" => user.ap_id,
167 assert {:ok, _} = ActivityPub.insert(data)
170 test "returns the activity if one with the same id is already in" do
171 activity = insert(:note_activity)
172 {:ok, new_activity} = ActivityPub.insert(activity.data)
174 assert activity.id == new_activity.id
177 test "inserts a given map into the activity database, giving it an id if it has none." do
181 "actor" => user.ap_id,
184 "actor" => user.ap_id,
191 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
192 assert activity.data["ok"] == data["ok"]
193 assert is_binary(activity.data["id"])
199 "actor" => user.ap_id,
201 "context" => "blabla",
203 "actor" => user.ap_id,
210 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
211 assert activity.data["ok"] == data["ok"]
212 assert activity.data["id"] == given_id
213 assert activity.data["context"] == "blabla"
214 assert activity.data["context_id"]
217 test "adds a context when none is there" do
221 "actor" => user.ap_id,
224 "actor" => user.ap_id,
231 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
232 object = Pleroma.Object.normalize(activity)
234 assert is_binary(activity.data["context"])
235 assert is_binary(object.data["context"])
236 assert activity.data["context_id"]
237 assert object.data["context_id"]
240 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
244 "actor" => user.ap_id,
247 "actor" => user.ap_id,
254 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
255 assert object = Object.normalize(activity)
256 assert is_binary(object.data["id"])
260 describe "create activities" do
261 test "removes doubled 'to' recipients" do
265 ActivityPub.create(%{
266 to: ["user1", "user1", "user2"],
270 "to" => ["user1", "user1", "user2"],
272 "content" => "testing"
276 assert activity.data["to"] == ["user1", "user2"]
277 assert activity.actor == user.ap_id
278 assert activity.recipients == ["user1", "user2", user.ap_id]
281 test "increases user note count only for public activities" do
285 CommonAPI.post(User.get_cached_by_id(user.id), %{
287 "visibility" => "public"
291 CommonAPI.post(User.get_cached_by_id(user.id), %{
293 "visibility" => "unlisted"
297 CommonAPI.post(User.get_cached_by_id(user.id), %{
299 "visibility" => "private"
303 CommonAPI.post(User.get_cached_by_id(user.id), %{
305 "visibility" => "direct"
308 user = User.get_cached_by_id(user.id)
309 assert user.info.note_count == 2
312 test "increases replies count" do
314 user2 = insert(:user)
316 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
317 ap_id = activity.data["id"]
318 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
321 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
322 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
323 assert object.data["repliesCount"] == 1
326 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
327 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
328 assert object.data["repliesCount"] == 2
331 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
332 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
333 assert object.data["repliesCount"] == 2
336 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
337 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
338 assert object.data["repliesCount"] == 2
342 describe "fetch activities for recipients" do
343 test "retrieve the activities for certain recipients" do
344 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
345 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
346 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
348 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
349 assert length(activities) == 2
350 assert activities == [activity_one, activity_two]
354 describe "fetch activities in context" do
355 test "retrieves activities that have a given context" do
356 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
357 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
358 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
359 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
360 activity_five = insert(:note_activity)
363 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
365 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
366 assert activities == [activity_two, activity]
370 test "doesn't return blocked activities" do
371 activity_one = insert(:note_activity)
372 activity_two = insert(:note_activity)
373 activity_three = insert(:note_activity)
375 booster = insert(:user)
376 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
379 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
381 assert Enum.member?(activities, activity_two)
382 assert Enum.member?(activities, activity_three)
383 refute Enum.member?(activities, activity_one)
385 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
388 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
390 assert Enum.member?(activities, activity_two)
391 assert Enum.member?(activities, activity_three)
392 assert Enum.member?(activities, activity_one)
394 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
395 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
396 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
397 activity_three = Activity.get_by_id(activity_three.id)
400 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
402 assert Enum.member?(activities, activity_two)
403 refute Enum.member?(activities, activity_three)
404 refute Enum.member?(activities, boost_activity)
405 assert Enum.member?(activities, activity_one)
408 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
410 assert Enum.member?(activities, activity_two)
411 assert Enum.member?(activities, activity_three)
412 assert Enum.member?(activities, boost_activity)
413 assert Enum.member?(activities, activity_one)
416 test "doesn't return transitive interactions concerning blocked users" do
417 blocker = insert(:user)
418 blockee = insert(:user)
419 friend = insert(:user)
421 {:ok, blocker} = User.block(blocker, blockee)
423 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
425 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
427 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
429 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
431 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
433 assert Enum.member?(activities, activity_one)
434 refute Enum.member?(activities, activity_two)
435 refute Enum.member?(activities, activity_three)
436 refute Enum.member?(activities, activity_four)
439 test "doesn't return announce activities concerning blocked users" do
440 blocker = insert(:user)
441 blockee = insert(:user)
442 friend = insert(:user)
444 {:ok, blocker} = User.block(blocker, blockee)
446 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
448 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
450 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
453 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
454 |> Enum.map(fn act -> act.id end)
456 assert Enum.member?(activities, activity_one.id)
457 refute Enum.member?(activities, activity_two.id)
458 refute Enum.member?(activities, activity_three.id)
461 test "doesn't return activities from blocked domains" do
462 domain = "dogwhistle.zone"
463 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
464 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
465 activity = insert(:note_activity, %{note: note})
467 {:ok, user} = User.block_domain(user, domain)
470 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
472 refute activity in activities
474 followed_user = insert(:user)
475 ActivityPub.follow(user, followed_user)
476 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
479 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
481 refute repeat_activity in activities
484 test "doesn't return muted activities" do
485 activity_one = insert(:note_activity)
486 activity_two = insert(:note_activity)
487 activity_three = insert(:note_activity)
489 booster = insert(:user)
490 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
493 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
495 assert Enum.member?(activities, activity_two)
496 assert Enum.member?(activities, activity_three)
497 refute Enum.member?(activities, activity_one)
499 # Calling with 'with_muted' will deliver muted activities, too.
501 ActivityPub.fetch_activities([], %{
502 "muting_user" => user,
503 "with_muted" => true,
504 "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.unmute(user, %User{ap_id: activity_one.data["actor"]})
514 ActivityPub.fetch_activities([], %{"muting_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.mute(user, %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([], %{"muting_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)
533 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
535 assert Enum.member?(activities, activity_two)
536 assert Enum.member?(activities, activity_three)
537 assert Enum.member?(activities, boost_activity)
538 assert Enum.member?(activities, activity_one)
541 test "doesn't return thread muted activities" do
543 _activity_one = insert(:note_activity)
544 note_two = insert(:note, data: %{"context" => "suya.."})
545 activity_two = insert(:note_activity, note: note_two)
547 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
549 assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user})
552 test "returns thread muted activities when with_muted is set" do
554 _activity_one = insert(:note_activity)
555 note_two = insert(:note, data: %{"context" => "suya.."})
556 activity_two = insert(:note_activity, note: note_two)
558 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
560 assert [_activity_two, _activity_one] =
561 ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true})
564 test "does include announces on request" do
565 activity_three = insert(:note_activity)
567 booster = insert(:user)
569 {:ok, user} = User.follow(user, booster)
571 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
573 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
575 assert announce_activity.id == announce.id
578 test "excludes reblogs on request" do
580 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
581 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
583 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
585 assert activity == expected_activity
588 describe "public fetch activities" do
589 test "doesn't retrieve unlisted activities" do
592 {:ok, _unlisted_activity} =
593 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
595 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
597 [activity] = ActivityPub.fetch_public_activities()
599 assert activity == listed_activity
602 test "retrieves public activities" do
603 _activities = ActivityPub.fetch_public_activities()
605 %{public: public} = ActivityBuilder.public_and_non_public()
607 activities = ActivityPub.fetch_public_activities()
608 assert length(activities) == 1
609 assert Enum.at(activities, 0) == public
612 test "retrieves a maximum of 20 activities" do
613 activities = ActivityBuilder.insert_list(30)
614 last_expected = List.last(activities)
616 activities = ActivityPub.fetch_public_activities()
617 last = List.last(activities)
619 assert length(activities) == 20
620 assert last == last_expected
623 test "retrieves ids starting from a since_id" do
624 activities = ActivityBuilder.insert_list(30)
625 later_activities = ActivityBuilder.insert_list(10)
626 since_id = List.last(activities).id
627 last_expected = List.last(later_activities)
629 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
630 last = List.last(activities)
632 assert length(activities) == 10
633 assert last == last_expected
636 test "retrieves ids up to max_id" do
637 _first_activities = ActivityBuilder.insert_list(10)
638 activities = ActivityBuilder.insert_list(20)
639 later_activities = ActivityBuilder.insert_list(10)
640 max_id = List.first(later_activities).id
641 last_expected = List.last(activities)
643 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
644 last = List.last(activities)
646 assert length(activities) == 20
647 assert last == last_expected
650 test "doesn't return reblogs for users for whom reblogs have been muted" do
651 activity = insert(:note_activity)
653 booster = insert(:user)
654 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
656 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
658 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
660 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
663 test "returns reblogs for users for whom reblogs have not been muted" do
664 activity = insert(:note_activity)
666 booster = insert(:user)
667 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
668 {:ok, user} = CommonAPI.show_reblogs(user, booster)
670 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
672 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
674 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
678 describe "like an object" do
679 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
680 Pleroma.Config.put([:instance, :federating], true)
681 note_activity = insert(:note_activity)
682 assert object_activity = Object.normalize(note_activity)
686 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
687 assert called(Pleroma.Web.Federator.publish(like_activity))
690 test "returns exist activity if object already liked" do
691 note_activity = insert(:note_activity)
692 assert object_activity = Object.normalize(note_activity)
696 {:ok, like_activity, _object} = ActivityPub.like(user, object_activity)
698 {:ok, like_activity_exist, _object} = ActivityPub.like(user, object_activity)
699 assert like_activity == like_activity_exist
702 test "adds a like activity to the db" do
703 note_activity = insert(:note_activity)
704 assert object = Object.normalize(note_activity)
707 user_two = insert(:user)
709 {:ok, like_activity, object} = ActivityPub.like(user, object)
711 assert like_activity.data["actor"] == user.ap_id
712 assert like_activity.data["type"] == "Like"
713 assert like_activity.data["object"] == object.data["id"]
714 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
715 assert like_activity.data["context"] == object.data["context"]
716 assert object.data["like_count"] == 1
717 assert object.data["likes"] == [user.ap_id]
719 # Just return the original activity if the user already liked it.
720 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
722 assert like_activity == same_like_activity
723 assert object.data["likes"] == [user.ap_id]
724 assert object.data["like_count"] == 1
726 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
727 assert object.data["like_count"] == 2
731 describe "unliking" do
732 test_with_mock "sends an activity to federation", Pleroma.Web.Federator, [:passthrough], [] do
733 Pleroma.Config.put([:instance, :federating], true)
735 note_activity = insert(:note_activity)
736 object = Object.normalize(note_activity)
739 {:ok, object} = ActivityPub.unlike(user, object)
740 refute called(Pleroma.Web.Federator.publish())
742 {:ok, _like_activity, object} = ActivityPub.like(user, object)
743 assert object.data["like_count"] == 1
745 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
746 assert object.data["like_count"] == 0
748 assert called(Pleroma.Web.Federator.publish(unlike_activity))
751 test "unliking a previously liked object" do
752 note_activity = insert(:note_activity)
753 object = Object.normalize(note_activity)
756 # Unliking something that hasn't been liked does nothing
757 {:ok, object} = ActivityPub.unlike(user, object)
758 assert object.data["like_count"] == 0
760 {:ok, like_activity, object} = ActivityPub.like(user, object)
761 assert object.data["like_count"] == 1
763 {:ok, _, _, object} = ActivityPub.unlike(user, object)
764 assert object.data["like_count"] == 0
766 assert Activity.get_by_id(like_activity.id) == nil
770 describe "announcing an object" do
771 test "adds an announce activity to the db" do
772 note_activity = insert(:note_activity)
773 object = Object.normalize(note_activity)
776 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
777 assert object.data["announcement_count"] == 1
778 assert object.data["announcements"] == [user.ap_id]
780 assert announce_activity.data["to"] == [
781 User.ap_followers(user),
782 note_activity.data["actor"]
785 assert announce_activity.data["object"] == object.data["id"]
786 assert announce_activity.data["actor"] == user.ap_id
787 assert announce_activity.data["context"] == object.data["context"]
791 describe "unannouncing an object" do
792 test "unannouncing a previously announced object" do
793 note_activity = insert(:note_activity)
794 object = Object.normalize(note_activity)
797 # Unannouncing an object that is not announced does nothing
798 # {:ok, object} = ActivityPub.unannounce(user, object)
799 # assert object.data["announcement_count"] == 0
801 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
802 assert object.data["announcement_count"] == 1
804 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
805 assert object.data["announcement_count"] == 0
807 assert unannounce_activity.data["to"] == [
808 User.ap_followers(user),
809 announce_activity.data["actor"]
812 assert unannounce_activity.data["type"] == "Undo"
813 assert unannounce_activity.data["object"] == announce_activity.data
814 assert unannounce_activity.data["actor"] == user.ap_id
815 assert unannounce_activity.data["context"] == announce_activity.data["context"]
817 assert Activity.get_by_id(announce_activity.id) == nil
821 describe "uploading files" do
822 test "copies the file to the configured folder" do
824 content_type: "image/jpg",
825 path: Path.absname("test/fixtures/image.jpg"),
826 filename: "an_image.jpg"
829 {:ok, %Object{} = object} = ActivityPub.upload(file)
830 assert object.data["name"] == "an_image.jpg"
833 test "works with base64 encoded images" do
838 {:ok, %Object{}} = ActivityPub.upload(file)
842 describe "fetch the latest Follow" do
843 test "fetches the latest Follow activity" do
844 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
845 follower = Repo.get_by(User, ap_id: activity.data["actor"])
846 followed = Repo.get_by(User, ap_id: activity.data["object"])
848 assert activity == Utils.fetch_latest_follow(follower, followed)
852 describe "following / unfollowing" do
853 test "creates a follow activity" do
854 follower = insert(:user)
855 followed = insert(:user)
857 {:ok, activity} = ActivityPub.follow(follower, followed)
858 assert activity.data["type"] == "Follow"
859 assert activity.data["actor"] == follower.ap_id
860 assert activity.data["object"] == followed.ap_id
863 test "creates an undo activity for the last follow" do
864 follower = insert(:user)
865 followed = insert(:user)
867 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
868 {:ok, activity} = ActivityPub.unfollow(follower, followed)
870 assert activity.data["type"] == "Undo"
871 assert activity.data["actor"] == follower.ap_id
873 embedded_object = activity.data["object"]
874 assert is_map(embedded_object)
875 assert embedded_object["type"] == "Follow"
876 assert embedded_object["object"] == followed.ap_id
877 assert embedded_object["id"] == follow_activity.data["id"]
881 describe "blocking / unblocking" do
882 test "creates a block activity" do
883 blocker = insert(:user)
884 blocked = insert(:user)
886 {:ok, activity} = ActivityPub.block(blocker, blocked)
888 assert activity.data["type"] == "Block"
889 assert activity.data["actor"] == blocker.ap_id
890 assert activity.data["object"] == blocked.ap_id
893 test "creates an undo activity for the last block" do
894 blocker = insert(:user)
895 blocked = insert(:user)
897 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
898 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
900 assert activity.data["type"] == "Undo"
901 assert activity.data["actor"] == blocker.ap_id
903 embedded_object = activity.data["object"]
904 assert is_map(embedded_object)
905 assert embedded_object["type"] == "Block"
906 assert embedded_object["object"] == blocked.ap_id
907 assert embedded_object["id"] == block_activity.data["id"]
911 describe "deletion" do
912 test "it creates a delete activity and deletes the original object" do
913 note = insert(:note_activity)
914 object = Object.normalize(note)
915 {:ok, delete} = ActivityPub.delete(object)
917 assert delete.data["type"] == "Delete"
918 assert delete.data["actor"] == note.data["actor"]
919 assert delete.data["object"] == object.data["id"]
921 assert Activity.get_by_id(delete.id) != nil
923 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
926 test "decrements user note count only for public activities" do
927 user = insert(:user, info: %{note_count: 10})
930 CommonAPI.post(User.get_cached_by_id(user.id), %{
932 "visibility" => "public"
936 CommonAPI.post(User.get_cached_by_id(user.id), %{
938 "visibility" => "unlisted"
942 CommonAPI.post(User.get_cached_by_id(user.id), %{
944 "visibility" => "private"
948 CommonAPI.post(User.get_cached_by_id(user.id), %{
950 "visibility" => "direct"
953 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
954 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
955 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
956 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
958 user = User.get_cached_by_id(user.id)
959 assert user.info.note_count == 10
962 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
964 note = insert(:note_activity)
965 object = Object.normalize(note)
971 "actor" => object.data["actor"],
972 "id" => object.data["id"],
973 "to" => [user.ap_id],
977 |> Object.update_and_set_cache()
979 {:ok, delete} = ActivityPub.delete(object)
981 assert user.ap_id in delete.data["to"]
984 test "decreases reply count" do
986 user2 = insert(:user)
988 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
989 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
990 ap_id = activity.data["id"]
992 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
993 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
994 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
995 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
997 _ = CommonAPI.delete(direct_reply.id, user2)
998 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
999 assert object.data["repliesCount"] == 2
1001 _ = CommonAPI.delete(private_reply.id, user2)
1002 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1003 assert object.data["repliesCount"] == 2
1005 _ = CommonAPI.delete(public_reply.id, user2)
1006 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1007 assert object.data["repliesCount"] == 1
1009 _ = CommonAPI.delete(unlisted_reply.id, user2)
1010 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
1011 assert object.data["repliesCount"] == 0
1015 describe "timeline post-processing" do
1016 test "it filters broken threads" do
1017 user1 = insert(:user)
1018 user2 = insert(:user)
1019 user3 = insert(:user)
1021 {:ok, user1} = User.follow(user1, user3)
1022 assert User.following?(user1, user3)
1024 {:ok, user2} = User.follow(user2, user3)
1025 assert User.following?(user2, user3)
1027 {:ok, user3} = User.follow(user3, user2)
1028 assert User.following?(user3, user2)
1030 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
1032 {:ok, private_activity_1} =
1033 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
1035 {:ok, private_activity_2} =
1036 CommonAPI.post(user2, %{
1038 "visibility" => "private",
1039 "in_reply_to_status_id" => private_activity_1.id
1042 {:ok, private_activity_3} =
1043 CommonAPI.post(user3, %{
1045 "visibility" => "private",
1046 "in_reply_to_status_id" => private_activity_2.id
1050 ActivityPub.fetch_activities([user1.ap_id | user1.following])
1051 |> Enum.map(fn a -> a.id end)
1053 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1055 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1057 assert length(activities) == 3
1060 ActivityPub.fetch_activities([user1.ap_id | user1.following], %{"user" => user1})
1061 |> Enum.map(fn a -> a.id end)
1063 assert [public_activity.id, private_activity_1.id] == activities
1064 assert length(activities) == 2
1068 describe "update" do
1069 test "it creates an update activity with the new user data" do
1070 user = insert(:user)
1071 {:ok, user} = User.ensure_keys_present(user)
1072 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1075 ActivityPub.update(%{
1076 actor: user_data["id"],
1077 to: [user.follower_address],
1082 assert update.data["actor"] == user.ap_id
1083 assert update.data["to"] == [user.follower_address]
1084 assert embedded_object = update.data["object"]
1085 assert embedded_object["id"] == user_data["id"]
1086 assert embedded_object["type"] == user_data["type"]
1090 test "returned pinned statuses" do
1091 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
1092 user = insert(:user)
1094 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1095 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1096 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1098 CommonAPI.pin(activity_one.id, user)
1099 user = refresh_record(user)
1101 CommonAPI.pin(activity_two.id, user)
1102 user = refresh_record(user)
1104 CommonAPI.pin(activity_three.id, user)
1105 user = refresh_record(user)
1107 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1109 assert 3 = length(activities)
1112 test "it can create a Flag activity" do
1113 reporter = insert(:user)
1114 target_account = insert(:user)
1115 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
1116 context = Utils.generate_context_id()
1119 reporter_ap_id = reporter.ap_id
1120 target_ap_id = target_account.ap_id
1121 activity_ap_id = activity.data["id"]
1123 assert {:ok, activity} =
1127 account: target_account,
1128 statuses: [activity],
1133 actor: ^reporter_ap_id,
1136 "content" => ^content,
1137 "context" => ^context,
1138 "object" => [^target_ap_id, ^activity_ap_id]
1143 test "fetch_activities/2 returns activities addressed to a list " do
1144 user = insert(:user)
1145 member = insert(:user)
1146 {:ok, list} = Pleroma.List.create("foo", user)
1147 {:ok, list} = Pleroma.List.follow(list, member)
1150 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1152 activity = Repo.preload(activity, :bookmark)
1153 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1155 assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity]
1159 File.read!("test/fixtures/avatar_data_uri")
1162 describe "fetch_activities_bounded" do
1163 test "fetches private posts for followed users" do
1164 user = insert(:user)
1167 CommonAPI.post(user, %{
1168 "status" => "thought I looked cute might delete later :3",
1169 "visibility" => "private"
1172 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1173 assert result.id == activity.id
1176 test "fetches only public posts for other users" do
1177 user = insert(:user)
1178 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe", "visibility" => "public"})
1180 {:ok, _private_activity} =
1181 CommonAPI.post(user, %{
1182 "status" => "why is tenshi eating a corndog so cute?",
1183 "visibility" => "private"
1186 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1187 assert result.id == activity.id
1191 describe "fetch_follow_information_for_user" do
1192 test "syncronizes following/followers counters" do
1196 follower_address: "http://localhost:4001/users/fuser2/followers",
1197 following_address: "http://localhost:4001/users/fuser2/following"
1200 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1201 assert info.follower_count == 527
1202 assert info.following_count == 267
1205 test "detects hidden followers" do
1208 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1209 %Tesla.Env{status: 403, body: ""}
1212 apply(HttpRequestMock, :request, [env])
1219 follower_address: "http://localhost:4001/users/masto_closed/followers",
1220 following_address: "http://localhost:4001/users/masto_closed/following"
1223 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1224 assert info.hide_followers == true
1225 assert info.hide_follows == false
1228 test "detects hidden follows" do
1231 "http://localhost:4001/users/masto_closed/following?page=1" ->
1232 %Tesla.Env{status: 403, body: ""}
1235 apply(HttpRequestMock, :request, [env])
1242 follower_address: "http://localhost:4001/users/masto_closed/followers",
1243 following_address: "http://localhost:4001/users/masto_closed/following"
1246 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1247 assert info.hide_followers == false
1248 assert info.hide_follows == true