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
9 alias Pleroma.Instances
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Publisher
14 alias Pleroma.Web.ActivityPub.Utils
15 alias Pleroma.Web.CommonAPI
17 import Pleroma.Factory
22 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
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 Enum.each(participations, fn participation ->
42 assert called(Pleroma.Web.Streamer.stream("participation", participation))
48 describe "fetching restricted by visibility" do
49 test "it restricts by the appropriate visibility" do
52 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
54 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
56 {:ok, unlisted_activity} =
57 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
59 {:ok, private_activity} =
60 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
63 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
65 assert activities == [direct_activity]
68 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
70 assert activities == [unlisted_activity]
73 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
75 assert activities == [private_activity]
78 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
80 assert activities == [public_activity]
83 ActivityPub.fetch_activities([], %{
84 :visibility => ~w[private public],
85 "actor_id" => user.ap_id
88 assert activities == [public_activity, private_activity]
92 describe "building a user from his ap id" do
93 test "it returns a user" do
94 user_id = "http://mastodon.example.org/users/admin"
95 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
96 assert user.ap_id == user_id
97 assert user.nickname == "admin@mastodon.example.org"
98 assert user.info.source_data
99 assert user.info.ap_enabled
100 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
103 test "it fetches the appropriate tag-restricted posts" do
106 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
107 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
108 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
110 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
113 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
116 ActivityPub.fetch_activities([], %{
118 "tag" => ["test", "essais"],
119 "tag_reject" => ["reject"]
123 ActivityPub.fetch_activities([], %{
126 "tag_all" => ["test", "reject"]
129 assert fetch_one == [status_one, status_three]
130 assert fetch_two == [status_one, status_two, status_three]
131 assert fetch_three == [status_one, status_two]
132 assert fetch_four == [status_three]
136 describe "insertion" do
137 test "drops activities beyond a certain limit" do
138 limit = Pleroma.Config.get([:instance, :remote_limit])
141 :crypto.strong_rand_bytes(limit + 1)
143 |> binary_part(0, limit + 1)
148 "content" => random_text
152 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
155 test "doesn't drop activities with content being null" do
159 "actor" => user.ap_id,
162 "actor" => user.ap_id,
169 assert {:ok, _} = ActivityPub.insert(data)
172 test "returns the activity if one with the same id is already in" do
173 activity = insert(:note_activity)
174 {:ok, new_activity} = ActivityPub.insert(activity.data)
176 assert activity.id == new_activity.id
179 test "inserts a given map into the activity database, giving it an id if it has none." do
183 "actor" => user.ap_id,
186 "actor" => user.ap_id,
193 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
194 assert activity.data["ok"] == data["ok"]
195 assert is_binary(activity.data["id"])
201 "actor" => user.ap_id,
203 "context" => "blabla",
205 "actor" => user.ap_id,
212 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
213 assert activity.data["ok"] == data["ok"]
214 assert activity.data["id"] == given_id
215 assert activity.data["context"] == "blabla"
216 assert activity.data["context_id"]
219 test "adds a context when none is there" do
223 "actor" => user.ap_id,
226 "actor" => user.ap_id,
233 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
234 object = Pleroma.Object.normalize(activity)
236 assert is_binary(activity.data["context"])
237 assert is_binary(object.data["context"])
238 assert activity.data["context_id"]
239 assert object.data["context_id"]
242 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
246 "actor" => user.ap_id,
249 "actor" => user.ap_id,
256 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
257 object = Object.normalize(activity.data["object"])
259 assert is_binary(object.data["id"])
260 assert %Object{} = Object.get_by_ap_id(activity.data["object"])
264 describe "create activities" do
265 test "removes doubled 'to' recipients" do
269 ActivityPub.create(%{
270 to: ["user1", "user1", "user2"],
274 "to" => ["user1", "user1", "user2"],
276 "content" => "testing"
280 assert activity.data["to"] == ["user1", "user2"]
281 assert activity.actor == user.ap_id
282 assert activity.recipients == ["user1", "user2", user.ap_id]
285 test "increases user note count only for public activities" do
289 CommonAPI.post(User.get_cached_by_id(user.id), %{
291 "visibility" => "public"
295 CommonAPI.post(User.get_cached_by_id(user.id), %{
297 "visibility" => "unlisted"
301 CommonAPI.post(User.get_cached_by_id(user.id), %{
303 "visibility" => "private"
307 CommonAPI.post(User.get_cached_by_id(user.id), %{
309 "visibility" => "direct"
312 user = User.get_cached_by_id(user.id)
313 assert user.info.note_count == 2
316 test "increases replies count" do
318 user2 = insert(:user)
320 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
321 ap_id = activity.data["id"]
322 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
325 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
326 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
327 assert object.data["repliesCount"] == 1
330 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
331 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
332 assert object.data["repliesCount"] == 2
335 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
336 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
337 assert object.data["repliesCount"] == 2
340 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
341 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
342 assert object.data["repliesCount"] == 2
346 describe "fetch activities for recipients" do
347 test "retrieve the activities for certain recipients" do
348 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
349 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
350 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
352 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
353 assert length(activities) == 2
354 assert activities == [activity_one, activity_two]
358 describe "fetch activities in context" do
359 test "retrieves activities that have a given context" do
360 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
361 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
362 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
363 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
364 activity_five = insert(:note_activity)
367 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
369 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
370 assert activities == [activity_two, activity]
374 test "doesn't return blocked activities" do
375 activity_one = insert(:note_activity)
376 activity_two = insert(:note_activity)
377 activity_three = insert(:note_activity)
379 booster = insert(:user)
380 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
383 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
385 assert Enum.member?(activities, activity_two)
386 assert Enum.member?(activities, activity_three)
387 refute Enum.member?(activities, activity_one)
389 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
392 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
394 assert Enum.member?(activities, activity_two)
395 assert Enum.member?(activities, activity_three)
396 assert Enum.member?(activities, activity_one)
398 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
399 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
400 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
401 activity_three = Activity.get_by_id(activity_three.id)
404 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
406 assert Enum.member?(activities, activity_two)
407 refute Enum.member?(activities, activity_three)
408 refute Enum.member?(activities, boost_activity)
409 assert Enum.member?(activities, activity_one)
412 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
414 assert Enum.member?(activities, activity_two)
415 assert Enum.member?(activities, activity_three)
416 assert Enum.member?(activities, boost_activity)
417 assert Enum.member?(activities, activity_one)
420 test "doesn't return transitive interactions concerning blocked users" do
421 blocker = insert(:user)
422 blockee = insert(:user)
423 friend = insert(:user)
425 {:ok, blocker} = User.block(blocker, blockee)
427 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
429 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
431 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
433 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
435 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
437 assert Enum.member?(activities, activity_one)
438 refute Enum.member?(activities, activity_two)
439 refute Enum.member?(activities, activity_three)
440 refute Enum.member?(activities, activity_four)
443 test "doesn't return announce activities concerning blocked users" do
444 blocker = insert(:user)
445 blockee = insert(:user)
446 friend = insert(:user)
448 {:ok, blocker} = User.block(blocker, blockee)
450 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
452 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
454 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
457 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
458 |> Enum.map(fn act -> act.id end)
460 assert Enum.member?(activities, activity_one.id)
461 refute Enum.member?(activities, activity_two.id)
462 refute Enum.member?(activities, activity_three.id)
465 test "doesn't return muted activities" do
466 activity_one = insert(:note_activity)
467 activity_two = insert(:note_activity)
468 activity_three = insert(:note_activity)
470 booster = insert(:user)
471 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
474 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
476 assert Enum.member?(activities, activity_two)
477 assert Enum.member?(activities, activity_three)
478 refute Enum.member?(activities, activity_one)
480 # Calling with 'with_muted' will deliver muted activities, too.
482 ActivityPub.fetch_activities([], %{
483 "muting_user" => user,
484 "with_muted" => true,
485 "skip_preload" => true
488 assert Enum.member?(activities, activity_two)
489 assert Enum.member?(activities, activity_three)
490 assert Enum.member?(activities, activity_one)
492 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
495 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
497 assert Enum.member?(activities, activity_two)
498 assert Enum.member?(activities, activity_three)
499 assert Enum.member?(activities, activity_one)
501 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
502 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
503 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
504 activity_three = Activity.get_by_id(activity_three.id)
507 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
509 assert Enum.member?(activities, activity_two)
510 refute Enum.member?(activities, activity_three)
511 refute Enum.member?(activities, boost_activity)
512 assert Enum.member?(activities, activity_one)
514 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
516 assert Enum.member?(activities, activity_two)
517 assert Enum.member?(activities, activity_three)
518 assert Enum.member?(activities, boost_activity)
519 assert Enum.member?(activities, activity_one)
522 test "does include announces on request" do
523 activity_three = insert(:note_activity)
525 booster = insert(:user)
527 {:ok, user} = User.follow(user, booster)
529 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
531 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
533 assert announce_activity.id == announce.id
536 test "excludes reblogs on request" do
538 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
539 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
541 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
543 assert activity == expected_activity
546 describe "public fetch activities" do
547 test "doesn't retrieve unlisted activities" do
550 {:ok, _unlisted_activity} =
551 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
553 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
555 [activity] = ActivityPub.fetch_public_activities()
557 assert activity == listed_activity
560 test "retrieves public activities" do
561 _activities = ActivityPub.fetch_public_activities()
563 %{public: public} = ActivityBuilder.public_and_non_public()
565 activities = ActivityPub.fetch_public_activities()
566 assert length(activities) == 1
567 assert Enum.at(activities, 0) == public
570 test "retrieves a maximum of 20 activities" do
571 activities = ActivityBuilder.insert_list(30)
572 last_expected = List.last(activities)
574 activities = ActivityPub.fetch_public_activities()
575 last = List.last(activities)
577 assert length(activities) == 20
578 assert last == last_expected
581 test "retrieves ids starting from a since_id" do
582 activities = ActivityBuilder.insert_list(30)
583 later_activities = ActivityBuilder.insert_list(10)
584 since_id = List.last(activities).id
585 last_expected = List.last(later_activities)
587 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
588 last = List.last(activities)
590 assert length(activities) == 10
591 assert last == last_expected
594 test "retrieves ids up to max_id" do
595 _first_activities = ActivityBuilder.insert_list(10)
596 activities = ActivityBuilder.insert_list(20)
597 later_activities = ActivityBuilder.insert_list(10)
598 max_id = List.first(later_activities).id
599 last_expected = List.last(activities)
601 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
602 last = List.last(activities)
604 assert length(activities) == 20
605 assert last == last_expected
608 test "doesn't return reblogs for users for whom reblogs have been muted" do
609 activity = insert(:note_activity)
611 booster = insert(:user)
612 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
614 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
616 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
618 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
621 test "returns reblogs for users for whom reblogs have not been muted" do
622 activity = insert(:note_activity)
624 booster = insert(:user)
625 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
626 {:ok, user} = CommonAPI.show_reblogs(user, booster)
628 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
630 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
632 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
636 describe "like an object" do
637 test "adds a like activity to the db" do
638 note_activity = insert(:note_activity)
639 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
641 user_two = insert(:user)
643 {:ok, like_activity, object} = ActivityPub.like(user, object)
645 assert like_activity.data["actor"] == user.ap_id
646 assert like_activity.data["type"] == "Like"
647 assert like_activity.data["object"] == object.data["id"]
648 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
649 assert like_activity.data["context"] == object.data["context"]
650 assert object.data["like_count"] == 1
651 assert object.data["likes"] == [user.ap_id]
653 # Just return the original activity if the user already liked it.
654 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
656 assert like_activity == same_like_activity
657 assert object.data["likes"] == [user.ap_id]
659 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
660 assert note_activity.data["object"]["like_count"] == 1
662 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
663 assert object.data["like_count"] == 2
667 describe "unliking" do
668 test "unliking a previously liked object" do
669 note_activity = insert(:note_activity)
670 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
673 # Unliking something that hasn't been liked does nothing
674 {:ok, object} = ActivityPub.unlike(user, object)
675 assert object.data["like_count"] == 0
677 {:ok, like_activity, object} = ActivityPub.like(user, object)
678 assert object.data["like_count"] == 1
680 {:ok, _, _, object} = ActivityPub.unlike(user, object)
681 assert object.data["like_count"] == 0
683 assert Activity.get_by_id(like_activity.id) == nil
687 describe "announcing an object" do
688 test "adds an announce activity to the db" do
689 note_activity = insert(:note_activity)
690 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
693 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
694 assert object.data["announcement_count"] == 1
695 assert object.data["announcements"] == [user.ap_id]
697 assert announce_activity.data["to"] == [
698 User.ap_followers(user),
699 note_activity.data["actor"]
702 assert announce_activity.data["object"] == object.data["id"]
703 assert announce_activity.data["actor"] == user.ap_id
704 assert announce_activity.data["context"] == object.data["context"]
708 describe "unannouncing an object" do
709 test "unannouncing a previously announced object" do
710 note_activity = insert(:note_activity)
711 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
714 # Unannouncing an object that is not announced does nothing
715 # {:ok, object} = ActivityPub.unannounce(user, object)
716 # assert object.data["announcement_count"] == 0
718 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
719 assert object.data["announcement_count"] == 1
721 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
722 assert object.data["announcement_count"] == 0
724 assert unannounce_activity.data["to"] == [
725 User.ap_followers(user),
726 announce_activity.data["actor"]
729 assert unannounce_activity.data["type"] == "Undo"
730 assert unannounce_activity.data["object"] == announce_activity.data
731 assert unannounce_activity.data["actor"] == user.ap_id
732 assert unannounce_activity.data["context"] == announce_activity.data["context"]
734 assert Activity.get_by_id(announce_activity.id) == nil
738 describe "uploading files" do
739 test "copies the file to the configured folder" do
741 content_type: "image/jpg",
742 path: Path.absname("test/fixtures/image.jpg"),
743 filename: "an_image.jpg"
746 {:ok, %Object{} = object} = ActivityPub.upload(file)
747 assert object.data["name"] == "an_image.jpg"
750 test "works with base64 encoded images" do
755 {:ok, %Object{}} = ActivityPub.upload(file)
759 describe "fetch the latest Follow" do
760 test "fetches the latest Follow activity" do
761 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
762 follower = Repo.get_by(User, ap_id: activity.data["actor"])
763 followed = Repo.get_by(User, ap_id: activity.data["object"])
765 assert activity == Utils.fetch_latest_follow(follower, followed)
769 describe "following / unfollowing" do
770 test "creates a follow activity" do
771 follower = insert(:user)
772 followed = insert(:user)
774 {:ok, activity} = ActivityPub.follow(follower, followed)
775 assert activity.data["type"] == "Follow"
776 assert activity.data["actor"] == follower.ap_id
777 assert activity.data["object"] == followed.ap_id
780 test "creates an undo activity for the last follow" do
781 follower = insert(:user)
782 followed = insert(:user)
784 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
785 {:ok, activity} = ActivityPub.unfollow(follower, followed)
787 assert activity.data["type"] == "Undo"
788 assert activity.data["actor"] == follower.ap_id
790 assert is_map(activity.data["object"])
791 assert activity.data["object"]["type"] == "Follow"
792 assert activity.data["object"]["object"] == followed.ap_id
793 assert activity.data["object"]["id"] == follow_activity.data["id"]
797 describe "blocking / unblocking" do
798 test "creates a block activity" do
799 blocker = insert(:user)
800 blocked = insert(:user)
802 {:ok, activity} = ActivityPub.block(blocker, blocked)
804 assert activity.data["type"] == "Block"
805 assert activity.data["actor"] == blocker.ap_id
806 assert activity.data["object"] == blocked.ap_id
809 test "creates an undo activity for the last block" do
810 blocker = insert(:user)
811 blocked = insert(:user)
813 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
814 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
816 assert activity.data["type"] == "Undo"
817 assert activity.data["actor"] == blocker.ap_id
819 assert is_map(activity.data["object"])
820 assert activity.data["object"]["type"] == "Block"
821 assert activity.data["object"]["object"] == blocked.ap_id
822 assert activity.data["object"]["id"] == block_activity.data["id"]
826 describe "deletion" do
827 test "it creates a delete activity and deletes the original object" do
828 note = insert(:note_activity)
829 object = Object.get_by_ap_id(note.data["object"]["id"])
830 {:ok, delete} = ActivityPub.delete(object)
832 assert delete.data["type"] == "Delete"
833 assert delete.data["actor"] == note.data["actor"]
834 assert delete.data["object"] == note.data["object"]["id"]
836 assert Activity.get_by_id(delete.id) != nil
838 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
841 test "decrements user note count only for public activities" do
842 user = insert(:user, info: %{note_count: 10})
845 CommonAPI.post(User.get_cached_by_id(user.id), %{
847 "visibility" => "public"
851 CommonAPI.post(User.get_cached_by_id(user.id), %{
853 "visibility" => "unlisted"
857 CommonAPI.post(User.get_cached_by_id(user.id), %{
859 "visibility" => "private"
863 CommonAPI.post(User.get_cached_by_id(user.id), %{
865 "visibility" => "direct"
868 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
869 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
870 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
871 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
873 user = User.get_cached_by_id(user.id)
874 assert user.info.note_count == 10
877 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
879 note = insert(:note_activity)
882 Object.get_by_ap_id(note.data["object"]["id"])
885 "actor" => note.data["object"]["actor"],
886 "id" => note.data["object"]["id"],
887 "to" => [user.ap_id],
891 |> Object.update_and_set_cache()
893 {:ok, delete} = ActivityPub.delete(object)
895 assert user.ap_id in delete.data["to"]
898 test "decreases reply count" do
900 user2 = insert(:user)
902 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
903 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
904 ap_id = activity.data["id"]
906 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
907 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
908 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
909 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
911 _ = CommonAPI.delete(direct_reply.id, user2)
912 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
913 assert object.data["repliesCount"] == 2
915 _ = CommonAPI.delete(private_reply.id, user2)
916 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
917 assert object.data["repliesCount"] == 2
919 _ = CommonAPI.delete(public_reply.id, user2)
920 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
921 assert object.data["repliesCount"] == 1
923 _ = CommonAPI.delete(unlisted_reply.id, user2)
924 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
925 assert object.data["repliesCount"] == 0
929 describe "timeline post-processing" do
930 test "it filters broken threads" do
931 user1 = insert(:user)
932 user2 = insert(:user)
933 user3 = insert(:user)
935 {:ok, user1} = User.follow(user1, user3)
936 assert User.following?(user1, user3)
938 {:ok, user2} = User.follow(user2, user3)
939 assert User.following?(user2, user3)
941 {:ok, user3} = User.follow(user3, user2)
942 assert User.following?(user3, user2)
944 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
946 {:ok, private_activity_1} =
947 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
949 {:ok, private_activity_2} =
950 CommonAPI.post(user2, %{
952 "visibility" => "private",
953 "in_reply_to_status_id" => private_activity_1.id
956 {:ok, private_activity_3} =
957 CommonAPI.post(user3, %{
959 "visibility" => "private",
960 "in_reply_to_status_id" => private_activity_2.id
964 ActivityPub.fetch_activities([user1.ap_id | user1.following])
965 |> Enum.map(fn a -> a.id end)
967 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
969 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
971 assert length(activities) == 3
974 ActivityPub.fetch_activities([user1.ap_id | user1.following], %{"user" => user1})
975 |> Enum.map(fn a -> a.id end)
977 assert [public_activity.id, private_activity_1.id] == activities
978 assert length(activities) == 2
983 test "it creates an update activity with the new user data" do
985 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
986 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
989 ActivityPub.update(%{
990 actor: user_data["id"],
991 to: [user.follower_address],
996 assert update.data["actor"] == user.ap_id
997 assert update.data["to"] == [user.follower_address]
998 assert update.data["object"]["id"] == user_data["id"]
999 assert update.data["object"]["type"] == user_data["type"]
1003 test "returned pinned statuses" do
1004 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
1005 user = insert(:user)
1007 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1008 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1009 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1011 CommonAPI.pin(activity_one.id, user)
1012 user = refresh_record(user)
1014 CommonAPI.pin(activity_two.id, user)
1015 user = refresh_record(user)
1017 CommonAPI.pin(activity_three.id, user)
1018 user = refresh_record(user)
1020 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1022 assert 3 = length(activities)
1025 test "it can create a Flag activity" do
1026 reporter = insert(:user)
1027 target_account = insert(:user)
1028 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
1029 context = Utils.generate_context_id()
1032 reporter_ap_id = reporter.ap_id
1033 target_ap_id = target_account.ap_id
1034 activity_ap_id = activity.data["id"]
1036 assert {:ok, activity} =
1040 account: target_account,
1041 statuses: [activity],
1046 actor: ^reporter_ap_id,
1049 "content" => ^content,
1050 "context" => ^context,
1051 "object" => [^target_ap_id, ^activity_ap_id]
1056 describe "publish_one/1" do
1057 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
1061 actor = insert(:user)
1062 inbox = "http://200.site/users/nick1/inbox"
1064 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1066 assert called(Instances.set_reachable(inbox))
1069 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
1073 actor = insert(:user)
1074 inbox = "http://200.site/users/nick1/inbox"
1077 Publisher.publish_one(%{
1082 unreachable_since: NaiveDateTime.utc_now()
1085 assert called(Instances.set_reachable(inbox))
1088 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1092 actor = insert(:user)
1093 inbox = "http://200.site/users/nick1/inbox"
1096 Publisher.publish_one(%{
1101 unreachable_since: nil
1104 refute called(Instances.set_reachable(inbox))
1107 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1111 actor = insert(:user)
1112 inbox = "http://404.site/users/nick1/inbox"
1114 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1116 assert called(Instances.set_unreachable(inbox))
1119 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1123 actor = insert(:user)
1124 inbox = "http://connrefused.site/users/nick1/inbox"
1126 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1128 assert called(Instances.set_unreachable(inbox))
1131 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1135 actor = insert(:user)
1136 inbox = "http://200.site/users/nick1/inbox"
1138 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1140 refute called(Instances.set_unreachable(inbox))
1143 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1147 actor = insert(:user)
1148 inbox = "http://connrefused.site/users/nick1/inbox"
1150 assert {:error, _} =
1151 Publisher.publish_one(%{
1156 unreachable_since: NaiveDateTime.utc_now()
1159 refute called(Instances.set_unreachable(inbox))
1164 File.read!("test/fixtures/avatar_data_uri")