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.Utils
14 alias Pleroma.Web.CommonAPI
16 import Pleroma.Factory
21 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
25 describe "fetching restricted by visibility" do
26 test "it restricts by the appropriate visibility" do
29 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
31 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
33 {:ok, unlisted_activity} =
34 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
36 {:ok, private_activity} =
37 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
40 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
42 assert activities == [direct_activity]
45 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
47 assert activities == [unlisted_activity]
50 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
52 assert activities == [private_activity]
55 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
57 assert activities == [public_activity]
60 ActivityPub.fetch_activities([], %{
61 :visibility => ~w[private public],
62 "actor_id" => user.ap_id
65 assert activities == [public_activity, private_activity]
69 describe "building a user from his ap id" do
70 test "it returns a user" do
71 user_id = "http://mastodon.example.org/users/admin"
72 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
73 assert user.ap_id == user_id
74 assert user.nickname == "admin@mastodon.example.org"
75 assert user.info.source_data
76 assert user.info.ap_enabled
77 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
80 test "it fetches the appropriate tag-restricted posts" do
83 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
84 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
85 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
87 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
90 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
93 ActivityPub.fetch_activities([], %{
95 "tag" => ["test", "essais"],
96 "tag_reject" => ["reject"]
100 ActivityPub.fetch_activities([], %{
103 "tag_all" => ["test", "reject"]
106 assert fetch_one == [status_one, status_three]
107 assert fetch_two == [status_one, status_two, status_three]
108 assert fetch_three == [status_one, status_two]
109 assert fetch_four == [status_three]
113 describe "insertion" do
114 test "drops activities beyond a certain limit" do
115 limit = Pleroma.Config.get([:instance, :remote_limit])
118 :crypto.strong_rand_bytes(limit + 1)
120 |> binary_part(0, limit + 1)
125 "content" => random_text
129 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
132 test "doesn't drop activities with content being null" do
140 assert {:ok, _} = ActivityPub.insert(data)
143 test "returns the activity if one with the same id is already in" do
144 activity = insert(:note_activity)
145 {:ok, new_activity} = ActivityPub.insert(activity.data)
147 assert activity.id == new_activity.id
150 test "inserts a given map into the activity database, giving it an id if it has none." do
155 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
156 assert activity.data["ok"] == data["ok"]
157 assert is_binary(activity.data["id"])
164 "context" => "blabla"
167 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
168 assert activity.data["ok"] == data["ok"]
169 assert activity.data["id"] == given_id
170 assert activity.data["context"] == "blabla"
171 assert activity.data["context_id"]
174 test "adds a context when none is there" do
182 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
184 assert is_binary(activity.data["context"])
185 assert is_binary(activity.data["object"]["context"])
186 assert activity.data["context_id"]
187 assert activity.data["object"]["context_id"]
190 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
198 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
199 object = Object.normalize(activity.data["object"])
201 assert is_binary(object.data["id"])
202 assert %Object{} = Object.get_by_ap_id(activity.data["object"])
206 describe "create activities" do
207 test "removes doubled 'to' recipients" do
211 ActivityPub.create(%{
212 to: ["user1", "user1", "user2"],
216 "to" => ["user1", "user1", "user2"],
218 "content" => "testing"
222 assert activity.data["to"] == ["user1", "user2"]
223 assert activity.actor == user.ap_id
224 assert activity.recipients == ["user1", "user2", user.ap_id]
227 test "increases user note count only for public activities" do
231 CommonAPI.post(User.get_cached_by_id(user.id), %{
233 "visibility" => "public"
237 CommonAPI.post(User.get_cached_by_id(user.id), %{
239 "visibility" => "unlisted"
243 CommonAPI.post(User.get_cached_by_id(user.id), %{
245 "visibility" => "private"
249 CommonAPI.post(User.get_cached_by_id(user.id), %{
251 "visibility" => "direct"
254 user = User.get_cached_by_id(user.id)
255 assert user.info.note_count == 2
258 test "increases replies count" do
260 user2 = insert(:user)
262 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
263 ap_id = activity.data["id"]
264 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
267 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
268 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
269 assert object.data["repliesCount"] == 1
272 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
273 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
274 assert object.data["repliesCount"] == 2
277 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
278 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
279 assert object.data["repliesCount"] == 2
282 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
283 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
284 assert object.data["repliesCount"] == 2
288 describe "fetch activities for recipients" do
289 test "retrieve the activities for certain recipients" do
290 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
291 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
292 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
294 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
295 assert length(activities) == 2
296 assert activities == [activity_one, activity_two]
300 describe "fetch activities in context" do
301 test "retrieves activities that have a given context" do
302 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
303 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
304 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
305 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
306 activity_five = insert(:note_activity)
309 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
311 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
312 assert activities == [activity_two, activity]
316 test "doesn't return blocked activities" do
317 activity_one = insert(:note_activity)
318 activity_two = insert(:note_activity)
319 activity_three = insert(:note_activity)
321 booster = insert(:user)
322 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
325 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
327 assert Enum.member?(activities, activity_two)
328 assert Enum.member?(activities, activity_three)
329 refute Enum.member?(activities, activity_one)
331 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
334 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
336 assert Enum.member?(activities, activity_two)
337 assert Enum.member?(activities, activity_three)
338 assert Enum.member?(activities, activity_one)
340 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
341 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
342 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
343 activity_three = Activity.get_by_id(activity_three.id)
346 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
348 assert Enum.member?(activities, activity_two)
349 refute Enum.member?(activities, activity_three)
350 refute Enum.member?(activities, boost_activity)
351 assert Enum.member?(activities, activity_one)
354 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
356 assert Enum.member?(activities, activity_two)
357 assert Enum.member?(activities, activity_three)
358 assert Enum.member?(activities, boost_activity)
359 assert Enum.member?(activities, activity_one)
362 test "doesn't return transitive interactions concerning blocked users" do
363 blocker = insert(:user)
364 blockee = insert(:user)
365 friend = insert(:user)
367 {:ok, blocker} = User.block(blocker, blockee)
369 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
371 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
373 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
375 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
377 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
379 assert Enum.member?(activities, activity_one)
380 refute Enum.member?(activities, activity_two)
381 refute Enum.member?(activities, activity_three)
382 refute Enum.member?(activities, activity_four)
385 test "doesn't return announce activities concerning blocked users" do
386 blocker = insert(:user)
387 blockee = insert(:user)
388 friend = insert(:user)
390 {:ok, blocker} = User.block(blocker, blockee)
392 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
394 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
396 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
399 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
400 |> Enum.map(fn act -> act.id end)
402 assert Enum.member?(activities, activity_one.id)
403 refute Enum.member?(activities, activity_two.id)
404 refute Enum.member?(activities, activity_three.id)
407 test "doesn't return muted activities" do
408 activity_one = insert(:note_activity)
409 activity_two = insert(:note_activity)
410 activity_three = insert(:note_activity)
412 booster = insert(:user)
413 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
416 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
418 assert Enum.member?(activities, activity_two)
419 assert Enum.member?(activities, activity_three)
420 refute Enum.member?(activities, activity_one)
422 # Calling with 'with_muted' will deliver muted activities, too.
424 ActivityPub.fetch_activities([], %{
425 "muting_user" => user,
426 "with_muted" => true,
427 "skip_preload" => true
430 assert Enum.member?(activities, activity_two)
431 assert Enum.member?(activities, activity_three)
432 assert Enum.member?(activities, activity_one)
434 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
437 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
439 assert Enum.member?(activities, activity_two)
440 assert Enum.member?(activities, activity_three)
441 assert Enum.member?(activities, activity_one)
443 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
444 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
445 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
446 activity_three = Activity.get_by_id(activity_three.id)
449 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
451 assert Enum.member?(activities, activity_two)
452 refute Enum.member?(activities, activity_three)
453 refute Enum.member?(activities, boost_activity)
454 assert Enum.member?(activities, activity_one)
456 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
458 assert Enum.member?(activities, activity_two)
459 assert Enum.member?(activities, activity_three)
460 assert Enum.member?(activities, boost_activity)
461 assert Enum.member?(activities, activity_one)
464 test "does include announces on request" do
465 activity_three = insert(:note_activity)
467 booster = insert(:user)
469 {:ok, user} = User.follow(user, booster)
471 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
473 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
475 assert announce_activity.id == announce.id
478 test "excludes reblogs on request" do
480 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
481 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
483 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
485 assert activity == expected_activity
488 describe "public fetch activities" do
489 test "doesn't retrieve unlisted activities" do
492 {:ok, _unlisted_activity} =
493 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
495 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
497 [activity] = ActivityPub.fetch_public_activities()
499 assert activity == listed_activity
502 test "retrieves public activities" do
503 _activities = ActivityPub.fetch_public_activities()
505 %{public: public} = ActivityBuilder.public_and_non_public()
507 activities = ActivityPub.fetch_public_activities()
508 assert length(activities) == 1
509 assert Enum.at(activities, 0) == public
512 test "retrieves a maximum of 20 activities" do
513 activities = ActivityBuilder.insert_list(30)
514 last_expected = List.last(activities)
516 activities = ActivityPub.fetch_public_activities()
517 last = List.last(activities)
519 assert length(activities) == 20
520 assert last == last_expected
523 test "retrieves ids starting from a since_id" do
524 activities = ActivityBuilder.insert_list(30)
525 later_activities = ActivityBuilder.insert_list(10)
526 since_id = List.last(activities).id
527 last_expected = List.last(later_activities)
529 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
530 last = List.last(activities)
532 assert length(activities) == 10
533 assert last == last_expected
536 test "retrieves ids up to max_id" do
537 _first_activities = ActivityBuilder.insert_list(10)
538 activities = ActivityBuilder.insert_list(20)
539 later_activities = ActivityBuilder.insert_list(10)
540 max_id = List.first(later_activities).id
541 last_expected = List.last(activities)
543 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
544 last = List.last(activities)
546 assert length(activities) == 20
547 assert last == last_expected
550 test "doesn't return reblogs for users for whom reblogs have been muted" do
551 activity = insert(:note_activity)
553 booster = insert(:user)
554 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
556 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
558 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
560 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
563 test "returns reblogs for users for whom reblogs have not been muted" do
564 activity = insert(:note_activity)
566 booster = insert(:user)
567 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
568 {:ok, user} = CommonAPI.show_reblogs(user, booster)
570 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
572 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
574 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
578 describe "like an object" do
579 test "adds a like activity to the db" do
580 note_activity = insert(:note_activity)
581 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
583 user_two = insert(:user)
585 {:ok, like_activity, object} = ActivityPub.like(user, object)
587 assert like_activity.data["actor"] == user.ap_id
588 assert like_activity.data["type"] == "Like"
589 assert like_activity.data["object"] == object.data["id"]
590 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
591 assert like_activity.data["context"] == object.data["context"]
592 assert object.data["like_count"] == 1
593 assert object.data["likes"] == [user.ap_id]
595 # Just return the original activity if the user already liked it.
596 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
598 assert like_activity == same_like_activity
599 assert object.data["likes"] == [user.ap_id]
601 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
602 assert note_activity.data["object"]["like_count"] == 1
604 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
605 assert object.data["like_count"] == 2
609 describe "unliking" do
610 test "unliking a previously liked object" do
611 note_activity = insert(:note_activity)
612 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
615 # Unliking something that hasn't been liked does nothing
616 {:ok, object} = ActivityPub.unlike(user, object)
617 assert object.data["like_count"] == 0
619 {:ok, like_activity, object} = ActivityPub.like(user, object)
620 assert object.data["like_count"] == 1
622 {:ok, _, _, object} = ActivityPub.unlike(user, object)
623 assert object.data["like_count"] == 0
625 assert Activity.get_by_id(like_activity.id) == nil
629 describe "announcing an object" do
630 test "adds an announce activity to the db" do
631 note_activity = insert(:note_activity)
632 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
635 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
636 assert object.data["announcement_count"] == 1
637 assert object.data["announcements"] == [user.ap_id]
639 assert announce_activity.data["to"] == [
640 User.ap_followers(user),
641 note_activity.data["actor"]
644 assert announce_activity.data["object"] == object.data["id"]
645 assert announce_activity.data["actor"] == user.ap_id
646 assert announce_activity.data["context"] == object.data["context"]
650 describe "unannouncing an object" do
651 test "unannouncing a previously announced object" do
652 note_activity = insert(:note_activity)
653 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
656 # Unannouncing an object that is not announced does nothing
657 # {:ok, object} = ActivityPub.unannounce(user, object)
658 # assert object.data["announcement_count"] == 0
660 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
661 assert object.data["announcement_count"] == 1
663 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
664 assert object.data["announcement_count"] == 0
666 assert unannounce_activity.data["to"] == [
667 User.ap_followers(user),
668 announce_activity.data["actor"]
671 assert unannounce_activity.data["type"] == "Undo"
672 assert unannounce_activity.data["object"] == announce_activity.data
673 assert unannounce_activity.data["actor"] == user.ap_id
674 assert unannounce_activity.data["context"] == announce_activity.data["context"]
676 assert Activity.get_by_id(announce_activity.id) == nil
680 describe "uploading files" do
681 test "copies the file to the configured folder" do
683 content_type: "image/jpg",
684 path: Path.absname("test/fixtures/image.jpg"),
685 filename: "an_image.jpg"
688 {:ok, %Object{} = object} = ActivityPub.upload(file)
689 assert object.data["name"] == "an_image.jpg"
692 test "works with base64 encoded images" do
697 {:ok, %Object{}} = ActivityPub.upload(file)
701 describe "fetch the latest Follow" do
702 test "fetches the latest Follow activity" do
703 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
704 follower = Repo.get_by(User, ap_id: activity.data["actor"])
705 followed = Repo.get_by(User, ap_id: activity.data["object"])
707 assert activity == Utils.fetch_latest_follow(follower, followed)
711 describe "following / unfollowing" do
712 test "creates a follow activity" do
713 follower = insert(:user)
714 followed = insert(:user)
716 {:ok, activity} = ActivityPub.follow(follower, followed)
717 assert activity.data["type"] == "Follow"
718 assert activity.data["actor"] == follower.ap_id
719 assert activity.data["object"] == followed.ap_id
722 test "creates an undo activity for the last follow" do
723 follower = insert(:user)
724 followed = insert(:user)
726 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
727 {:ok, activity} = ActivityPub.unfollow(follower, followed)
729 assert activity.data["type"] == "Undo"
730 assert activity.data["actor"] == follower.ap_id
732 assert is_map(activity.data["object"])
733 assert activity.data["object"]["type"] == "Follow"
734 assert activity.data["object"]["object"] == followed.ap_id
735 assert activity.data["object"]["id"] == follow_activity.data["id"]
739 describe "blocking / unblocking" do
740 test "creates a block activity" do
741 blocker = insert(:user)
742 blocked = insert(:user)
744 {:ok, activity} = ActivityPub.block(blocker, blocked)
746 assert activity.data["type"] == "Block"
747 assert activity.data["actor"] == blocker.ap_id
748 assert activity.data["object"] == blocked.ap_id
751 test "creates an undo activity for the last block" do
752 blocker = insert(:user)
753 blocked = insert(:user)
755 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
756 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
758 assert activity.data["type"] == "Undo"
759 assert activity.data["actor"] == blocker.ap_id
761 assert is_map(activity.data["object"])
762 assert activity.data["object"]["type"] == "Block"
763 assert activity.data["object"]["object"] == blocked.ap_id
764 assert activity.data["object"]["id"] == block_activity.data["id"]
768 describe "deletion" do
769 test "it creates a delete activity and deletes the original object" do
770 note = insert(:note_activity)
771 object = Object.get_by_ap_id(note.data["object"]["id"])
772 {:ok, delete} = ActivityPub.delete(object)
774 assert delete.data["type"] == "Delete"
775 assert delete.data["actor"] == note.data["actor"]
776 assert delete.data["object"] == note.data["object"]["id"]
778 assert Activity.get_by_id(delete.id) != nil
780 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
783 test "decrements user note count only for public activities" do
784 user = insert(:user, info: %{note_count: 10})
787 CommonAPI.post(User.get_cached_by_id(user.id), %{
789 "visibility" => "public"
793 CommonAPI.post(User.get_cached_by_id(user.id), %{
795 "visibility" => "unlisted"
799 CommonAPI.post(User.get_cached_by_id(user.id), %{
801 "visibility" => "private"
805 CommonAPI.post(User.get_cached_by_id(user.id), %{
807 "visibility" => "direct"
810 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
811 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
812 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
813 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
815 user = User.get_cached_by_id(user.id)
816 assert user.info.note_count == 10
819 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
821 note = insert(:note_activity)
824 Object.get_by_ap_id(note.data["object"]["id"])
827 "actor" => note.data["object"]["actor"],
828 "id" => note.data["object"]["id"],
829 "to" => [user.ap_id],
833 |> Object.update_and_set_cache()
835 {:ok, delete} = ActivityPub.delete(object)
837 assert user.ap_id in delete.data["to"]
840 test "decreases reply count" do
842 user2 = insert(:user)
844 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
845 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
846 ap_id = activity.data["id"]
848 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
849 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
850 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
851 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
853 _ = CommonAPI.delete(direct_reply.id, user2)
854 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
855 assert object.data["repliesCount"] == 2
857 _ = CommonAPI.delete(private_reply.id, user2)
858 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
859 assert object.data["repliesCount"] == 2
861 _ = CommonAPI.delete(public_reply.id, user2)
862 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
863 assert object.data["repliesCount"] == 1
865 _ = CommonAPI.delete(unlisted_reply.id, user2)
866 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
867 assert object.data["repliesCount"] == 0
871 describe "timeline post-processing" do
872 test "it filters broken threads" do
873 user1 = insert(:user)
874 user2 = insert(:user)
875 user3 = insert(:user)
877 {:ok, user1} = User.follow(user1, user3)
878 assert User.following?(user1, user3)
880 {:ok, user2} = User.follow(user2, user3)
881 assert User.following?(user2, user3)
883 {:ok, user3} = User.follow(user3, user2)
884 assert User.following?(user3, user2)
886 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
888 {:ok, private_activity_1} =
889 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
891 {:ok, private_activity_2} =
892 CommonAPI.post(user2, %{
894 "visibility" => "private",
895 "in_reply_to_status_id" => private_activity_1.id
898 {:ok, private_activity_3} =
899 CommonAPI.post(user3, %{
901 "visibility" => "private",
902 "in_reply_to_status_id" => private_activity_2.id
905 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
907 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
909 assert [public_activity, private_activity_1, private_activity_3] ==
912 assert length(activities) == 3
914 activities = ActivityPub.contain_timeline(activities, user1)
916 assert [public_activity, private_activity_1] == activities
917 assert length(activities) == 2
922 test "it creates an update activity with the new user data" do
924 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
925 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
928 ActivityPub.update(%{
929 actor: user_data["id"],
930 to: [user.follower_address],
935 assert update.data["actor"] == user.ap_id
936 assert update.data["to"] == [user.follower_address]
937 assert update.data["object"]["id"] == user_data["id"]
938 assert update.data["object"]["type"] == user_data["type"]
942 test "returned pinned statuses" do
943 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
946 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
947 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
948 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
950 CommonAPI.pin(activity_one.id, user)
951 user = refresh_record(user)
953 CommonAPI.pin(activity_two.id, user)
954 user = refresh_record(user)
956 CommonAPI.pin(activity_three.id, user)
957 user = refresh_record(user)
959 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
961 assert 3 = length(activities)
964 test "it can create a Flag activity" do
965 reporter = insert(:user)
966 target_account = insert(:user)
967 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
968 context = Utils.generate_context_id()
971 reporter_ap_id = reporter.ap_id
972 target_ap_id = target_account.ap_id
973 activity_ap_id = activity.data["id"]
975 assert {:ok, activity} =
979 account: target_account,
980 statuses: [activity],
985 actor: ^reporter_ap_id,
988 "content" => ^content,
989 "context" => ^context,
990 "object" => [^target_ap_id, ^activity_ap_id]
995 describe "publish_one/1" do
996 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
1000 actor = insert(:user)
1001 inbox = "http://200.site/users/nick1/inbox"
1003 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1005 assert called(Instances.set_reachable(inbox))
1008 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
1012 actor = insert(:user)
1013 inbox = "http://200.site/users/nick1/inbox"
1016 ActivityPub.publish_one(%{
1021 unreachable_since: NaiveDateTime.utc_now()
1024 assert called(Instances.set_reachable(inbox))
1027 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1031 actor = insert(:user)
1032 inbox = "http://200.site/users/nick1/inbox"
1035 ActivityPub.publish_one(%{
1040 unreachable_since: nil
1043 refute called(Instances.set_reachable(inbox))
1046 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1050 actor = insert(:user)
1051 inbox = "http://404.site/users/nick1/inbox"
1053 assert {:error, _} =
1054 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1056 assert called(Instances.set_unreachable(inbox))
1059 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1063 actor = insert(:user)
1064 inbox = "http://connrefused.site/users/nick1/inbox"
1066 assert {:error, _} =
1067 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1069 assert called(Instances.set_unreachable(inbox))
1072 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1076 actor = insert(:user)
1077 inbox = "http://200.site/users/nick1/inbox"
1079 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1081 refute called(Instances.set_unreachable(inbox))
1084 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1088 actor = insert(:user)
1089 inbox = "http://connrefused.site/users/nick1/inbox"
1091 assert {:error, _} =
1092 ActivityPub.publish_one(%{
1097 unreachable_since: NaiveDateTime.utc_now()
1100 refute called(Instances.set_unreachable(inbox))
1105 File.read!("test/fixtures/avatar_data_uri")