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
136 "actor" => user.ap_id,
139 "actor" => user.ap_id,
146 assert {:ok, _} = ActivityPub.insert(data)
149 test "returns the activity if one with the same id is already in" do
150 activity = insert(:note_activity)
151 {:ok, new_activity} = ActivityPub.insert(activity.data)
153 assert activity.id == new_activity.id
156 test "inserts a given map into the activity database, giving it an id if it has none." do
160 "actor" => user.ap_id,
163 "actor" => user.ap_id,
170 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
171 assert activity.data["ok"] == data["ok"]
172 assert is_binary(activity.data["id"])
178 "actor" => user.ap_id,
180 "context" => "blabla",
182 "actor" => user.ap_id,
189 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
190 assert activity.data["ok"] == data["ok"]
191 assert activity.data["id"] == given_id
192 assert activity.data["context"] == "blabla"
193 assert activity.data["context_id"]
196 test "adds a context when none is there" do
200 "actor" => user.ap_id,
203 "actor" => user.ap_id,
210 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
211 object = Pleroma.Object.normalize(activity)
213 assert is_binary(activity.data["context"])
214 assert is_binary(object.data["context"])
215 assert activity.data["context_id"]
216 assert object.data["context_id"]
219 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
223 "actor" => user.ap_id,
226 "actor" => user.ap_id,
233 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
234 object = Object.normalize(activity.data["object"])
236 assert is_binary(object.data["id"])
237 assert %Object{} = Object.get_by_ap_id(activity.data["object"])
241 describe "create activities" do
242 test "removes doubled 'to' recipients" do
246 ActivityPub.create(%{
247 to: ["user1", "user1", "user2"],
251 "to" => ["user1", "user1", "user2"],
253 "content" => "testing"
257 assert activity.data["to"] == ["user1", "user2"]
258 assert activity.actor == user.ap_id
259 assert activity.recipients == ["user1", "user2", user.ap_id]
262 test "increases user note count only for public activities" do
266 CommonAPI.post(User.get_cached_by_id(user.id), %{
268 "visibility" => "public"
272 CommonAPI.post(User.get_cached_by_id(user.id), %{
274 "visibility" => "unlisted"
278 CommonAPI.post(User.get_cached_by_id(user.id), %{
280 "visibility" => "private"
284 CommonAPI.post(User.get_cached_by_id(user.id), %{
286 "visibility" => "direct"
289 user = User.get_cached_by_id(user.id)
290 assert user.info.note_count == 2
293 test "increases replies count" do
295 user2 = insert(:user)
297 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
298 ap_id = activity.data["id"]
299 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
302 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
303 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
304 assert object.data["repliesCount"] == 1
307 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
308 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
309 assert object.data["repliesCount"] == 2
312 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
313 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
314 assert object.data["repliesCount"] == 2
317 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
318 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
319 assert object.data["repliesCount"] == 2
323 describe "fetch activities for recipients" do
324 test "retrieve the activities for certain recipients" do
325 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
326 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
327 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
329 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
330 assert length(activities) == 2
331 assert activities == [activity_one, activity_two]
335 describe "fetch activities in context" do
336 test "retrieves activities that have a given context" do
337 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
338 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
339 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
340 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
341 activity_five = insert(:note_activity)
344 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
346 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
347 assert activities == [activity_two, activity]
351 test "doesn't return blocked activities" do
352 activity_one = insert(:note_activity)
353 activity_two = insert(:note_activity)
354 activity_three = insert(:note_activity)
356 booster = insert(:user)
357 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
360 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
362 assert Enum.member?(activities, activity_two)
363 assert Enum.member?(activities, activity_three)
364 refute Enum.member?(activities, activity_one)
366 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
369 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
371 assert Enum.member?(activities, activity_two)
372 assert Enum.member?(activities, activity_three)
373 assert Enum.member?(activities, activity_one)
375 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
376 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
377 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
378 activity_three = Activity.get_by_id(activity_three.id)
381 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
383 assert Enum.member?(activities, activity_two)
384 refute Enum.member?(activities, activity_three)
385 refute Enum.member?(activities, boost_activity)
386 assert Enum.member?(activities, activity_one)
389 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
391 assert Enum.member?(activities, activity_two)
392 assert Enum.member?(activities, activity_three)
393 assert Enum.member?(activities, boost_activity)
394 assert Enum.member?(activities, activity_one)
397 test "doesn't return transitive interactions concerning blocked users" do
398 blocker = insert(:user)
399 blockee = insert(:user)
400 friend = insert(:user)
402 {:ok, blocker} = User.block(blocker, blockee)
404 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
406 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
408 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
410 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
412 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
414 assert Enum.member?(activities, activity_one)
415 refute Enum.member?(activities, activity_two)
416 refute Enum.member?(activities, activity_three)
417 refute Enum.member?(activities, activity_four)
420 test "doesn't return announce activities 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(blockee, %{"status" => "hey! @#{friend.nickname}"})
431 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
434 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
435 |> Enum.map(fn act -> act.id end)
437 assert Enum.member?(activities, activity_one.id)
438 refute Enum.member?(activities, activity_two.id)
439 refute Enum.member?(activities, activity_three.id)
442 test "doesn't return muted activities" do
443 activity_one = insert(:note_activity)
444 activity_two = insert(:note_activity)
445 activity_three = insert(:note_activity)
447 booster = insert(:user)
448 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
451 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
453 assert Enum.member?(activities, activity_two)
454 assert Enum.member?(activities, activity_three)
455 refute Enum.member?(activities, activity_one)
457 # Calling with 'with_muted' will deliver muted activities, too.
459 ActivityPub.fetch_activities([], %{
460 "muting_user" => user,
461 "with_muted" => true,
462 "skip_preload" => true
465 assert Enum.member?(activities, activity_two)
466 assert Enum.member?(activities, activity_three)
467 assert Enum.member?(activities, activity_one)
469 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
472 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
474 assert Enum.member?(activities, activity_two)
475 assert Enum.member?(activities, activity_three)
476 assert Enum.member?(activities, activity_one)
478 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
479 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
480 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
481 activity_three = Activity.get_by_id(activity_three.id)
484 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
486 assert Enum.member?(activities, activity_two)
487 refute Enum.member?(activities, activity_three)
488 refute Enum.member?(activities, boost_activity)
489 assert Enum.member?(activities, activity_one)
491 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
493 assert Enum.member?(activities, activity_two)
494 assert Enum.member?(activities, activity_three)
495 assert Enum.member?(activities, boost_activity)
496 assert Enum.member?(activities, activity_one)
499 test "does include announces on request" do
500 activity_three = insert(:note_activity)
502 booster = insert(:user)
504 {:ok, user} = User.follow(user, booster)
506 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
508 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
510 assert announce_activity.id == announce.id
513 test "excludes reblogs on request" do
515 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
516 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
518 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
520 assert activity == expected_activity
523 describe "public fetch activities" do
524 test "doesn't retrieve unlisted activities" do
527 {:ok, _unlisted_activity} =
528 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
530 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
532 [activity] = ActivityPub.fetch_public_activities()
534 assert activity == listed_activity
537 test "retrieves public activities" do
538 _activities = ActivityPub.fetch_public_activities()
540 %{public: public} = ActivityBuilder.public_and_non_public()
542 activities = ActivityPub.fetch_public_activities()
543 assert length(activities) == 1
544 assert Enum.at(activities, 0) == public
547 test "retrieves a maximum of 20 activities" do
548 activities = ActivityBuilder.insert_list(30)
549 last_expected = List.last(activities)
551 activities = ActivityPub.fetch_public_activities()
552 last = List.last(activities)
554 assert length(activities) == 20
555 assert last == last_expected
558 test "retrieves ids starting from a since_id" do
559 activities = ActivityBuilder.insert_list(30)
560 later_activities = ActivityBuilder.insert_list(10)
561 since_id = List.last(activities).id
562 last_expected = List.last(later_activities)
564 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
565 last = List.last(activities)
567 assert length(activities) == 10
568 assert last == last_expected
571 test "retrieves ids up to max_id" do
572 _first_activities = ActivityBuilder.insert_list(10)
573 activities = ActivityBuilder.insert_list(20)
574 later_activities = ActivityBuilder.insert_list(10)
575 max_id = List.first(later_activities).id
576 last_expected = List.last(activities)
578 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
579 last = List.last(activities)
581 assert length(activities) == 20
582 assert last == last_expected
585 test "doesn't return reblogs for users for whom reblogs have been muted" do
586 activity = insert(:note_activity)
588 booster = insert(:user)
589 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
591 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
593 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
595 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
598 test "returns reblogs for users for whom reblogs have not been muted" do
599 activity = insert(:note_activity)
601 booster = insert(:user)
602 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
603 {:ok, user} = CommonAPI.show_reblogs(user, booster)
605 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
607 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
609 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
613 describe "like an object" do
614 test "adds a like activity to the db" do
615 note_activity = insert(:note_activity)
616 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
618 user_two = insert(:user)
620 {:ok, like_activity, object} = ActivityPub.like(user, object)
622 assert like_activity.data["actor"] == user.ap_id
623 assert like_activity.data["type"] == "Like"
624 assert like_activity.data["object"] == object.data["id"]
625 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
626 assert like_activity.data["context"] == object.data["context"]
627 assert object.data["like_count"] == 1
628 assert object.data["likes"] == [user.ap_id]
630 # Just return the original activity if the user already liked it.
631 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
633 assert like_activity == same_like_activity
634 assert object.data["likes"] == [user.ap_id]
636 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
637 assert note_activity.data["object"]["like_count"] == 1
639 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
640 assert object.data["like_count"] == 2
644 describe "unliking" do
645 test "unliking a previously liked object" do
646 note_activity = insert(:note_activity)
647 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
650 # Unliking something that hasn't been liked does nothing
651 {:ok, object} = ActivityPub.unlike(user, object)
652 assert object.data["like_count"] == 0
654 {:ok, like_activity, object} = ActivityPub.like(user, object)
655 assert object.data["like_count"] == 1
657 {:ok, _, _, object} = ActivityPub.unlike(user, object)
658 assert object.data["like_count"] == 0
660 assert Activity.get_by_id(like_activity.id) == nil
664 describe "announcing an object" do
665 test "adds an announce activity to the db" do
666 note_activity = insert(:note_activity)
667 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
670 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
671 assert object.data["announcement_count"] == 1
672 assert object.data["announcements"] == [user.ap_id]
674 assert announce_activity.data["to"] == [
675 User.ap_followers(user),
676 note_activity.data["actor"]
679 assert announce_activity.data["object"] == object.data["id"]
680 assert announce_activity.data["actor"] == user.ap_id
681 assert announce_activity.data["context"] == object.data["context"]
685 describe "unannouncing an object" do
686 test "unannouncing a previously announced object" do
687 note_activity = insert(:note_activity)
688 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
691 # Unannouncing an object that is not announced does nothing
692 # {:ok, object} = ActivityPub.unannounce(user, object)
693 # assert object.data["announcement_count"] == 0
695 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
696 assert object.data["announcement_count"] == 1
698 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
699 assert object.data["announcement_count"] == 0
701 assert unannounce_activity.data["to"] == [
702 User.ap_followers(user),
703 announce_activity.data["actor"]
706 assert unannounce_activity.data["type"] == "Undo"
707 assert unannounce_activity.data["object"] == announce_activity.data
708 assert unannounce_activity.data["actor"] == user.ap_id
709 assert unannounce_activity.data["context"] == announce_activity.data["context"]
711 assert Activity.get_by_id(announce_activity.id) == nil
715 describe "uploading files" do
716 test "copies the file to the configured folder" do
718 content_type: "image/jpg",
719 path: Path.absname("test/fixtures/image.jpg"),
720 filename: "an_image.jpg"
723 {:ok, %Object{} = object} = ActivityPub.upload(file)
724 assert object.data["name"] == "an_image.jpg"
727 test "works with base64 encoded images" do
732 {:ok, %Object{}} = ActivityPub.upload(file)
736 describe "fetch the latest Follow" do
737 test "fetches the latest Follow activity" do
738 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
739 follower = Repo.get_by(User, ap_id: activity.data["actor"])
740 followed = Repo.get_by(User, ap_id: activity.data["object"])
742 assert activity == Utils.fetch_latest_follow(follower, followed)
746 describe "following / unfollowing" do
747 test "creates a follow activity" do
748 follower = insert(:user)
749 followed = insert(:user)
751 {:ok, activity} = ActivityPub.follow(follower, followed)
752 assert activity.data["type"] == "Follow"
753 assert activity.data["actor"] == follower.ap_id
754 assert activity.data["object"] == followed.ap_id
757 test "creates an undo activity for the last follow" do
758 follower = insert(:user)
759 followed = insert(:user)
761 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
762 {:ok, activity} = ActivityPub.unfollow(follower, followed)
764 assert activity.data["type"] == "Undo"
765 assert activity.data["actor"] == follower.ap_id
767 assert is_map(activity.data["object"])
768 assert activity.data["object"]["type"] == "Follow"
769 assert activity.data["object"]["object"] == followed.ap_id
770 assert activity.data["object"]["id"] == follow_activity.data["id"]
774 describe "blocking / unblocking" do
775 test "creates a block activity" do
776 blocker = insert(:user)
777 blocked = insert(:user)
779 {:ok, activity} = ActivityPub.block(blocker, blocked)
781 assert activity.data["type"] == "Block"
782 assert activity.data["actor"] == blocker.ap_id
783 assert activity.data["object"] == blocked.ap_id
786 test "creates an undo activity for the last block" do
787 blocker = insert(:user)
788 blocked = insert(:user)
790 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
791 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
793 assert activity.data["type"] == "Undo"
794 assert activity.data["actor"] == blocker.ap_id
796 assert is_map(activity.data["object"])
797 assert activity.data["object"]["type"] == "Block"
798 assert activity.data["object"]["object"] == blocked.ap_id
799 assert activity.data["object"]["id"] == block_activity.data["id"]
803 describe "deletion" do
804 test "it creates a delete activity and deletes the original object" do
805 note = insert(:note_activity)
806 object = Object.get_by_ap_id(note.data["object"]["id"])
807 {:ok, delete} = ActivityPub.delete(object)
809 assert delete.data["type"] == "Delete"
810 assert delete.data["actor"] == note.data["actor"]
811 assert delete.data["object"] == note.data["object"]["id"]
813 assert Activity.get_by_id(delete.id) != nil
815 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
818 test "decrements user note count only for public activities" do
819 user = insert(:user, info: %{note_count: 10})
822 CommonAPI.post(User.get_cached_by_id(user.id), %{
824 "visibility" => "public"
828 CommonAPI.post(User.get_cached_by_id(user.id), %{
830 "visibility" => "unlisted"
834 CommonAPI.post(User.get_cached_by_id(user.id), %{
836 "visibility" => "private"
840 CommonAPI.post(User.get_cached_by_id(user.id), %{
842 "visibility" => "direct"
845 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
846 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
847 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
848 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
850 user = User.get_cached_by_id(user.id)
851 assert user.info.note_count == 10
854 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
856 note = insert(:note_activity)
859 Object.get_by_ap_id(note.data["object"]["id"])
862 "actor" => note.data["object"]["actor"],
863 "id" => note.data["object"]["id"],
864 "to" => [user.ap_id],
868 |> Object.update_and_set_cache()
870 {:ok, delete} = ActivityPub.delete(object)
872 assert user.ap_id in delete.data["to"]
875 test "decreases reply count" do
877 user2 = insert(:user)
879 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
880 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
881 ap_id = activity.data["id"]
883 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
884 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
885 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
886 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
888 _ = CommonAPI.delete(direct_reply.id, user2)
889 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
890 assert object.data["repliesCount"] == 2
892 _ = CommonAPI.delete(private_reply.id, user2)
893 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
894 assert object.data["repliesCount"] == 2
896 _ = CommonAPI.delete(public_reply.id, user2)
897 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
898 assert object.data["repliesCount"] == 1
900 _ = CommonAPI.delete(unlisted_reply.id, user2)
901 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
902 assert object.data["repliesCount"] == 0
906 describe "timeline post-processing" do
907 test "it filters broken threads" do
908 user1 = insert(:user)
909 user2 = insert(:user)
910 user3 = insert(:user)
912 {:ok, user1} = User.follow(user1, user3)
913 assert User.following?(user1, user3)
915 {:ok, user2} = User.follow(user2, user3)
916 assert User.following?(user2, user3)
918 {:ok, user3} = User.follow(user3, user2)
919 assert User.following?(user3, user2)
921 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
923 {:ok, private_activity_1} =
924 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
926 {:ok, private_activity_2} =
927 CommonAPI.post(user2, %{
929 "visibility" => "private",
930 "in_reply_to_status_id" => private_activity_1.id
933 {:ok, private_activity_3} =
934 CommonAPI.post(user3, %{
936 "visibility" => "private",
937 "in_reply_to_status_id" => private_activity_2.id
940 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
942 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
944 assert [public_activity, private_activity_1, private_activity_3] ==
947 assert length(activities) == 3
949 activities = ActivityPub.contain_timeline(activities, user1)
951 assert [public_activity, private_activity_1] == activities
952 assert length(activities) == 2
957 test "it creates an update activity with the new user data" do
959 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
960 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
963 ActivityPub.update(%{
964 actor: user_data["id"],
965 to: [user.follower_address],
970 assert update.data["actor"] == user.ap_id
971 assert update.data["to"] == [user.follower_address]
972 assert update.data["object"]["id"] == user_data["id"]
973 assert update.data["object"]["type"] == user_data["type"]
977 test "returned pinned statuses" do
978 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
981 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
982 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
983 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
985 CommonAPI.pin(activity_one.id, user)
986 user = refresh_record(user)
988 CommonAPI.pin(activity_two.id, user)
989 user = refresh_record(user)
991 CommonAPI.pin(activity_three.id, user)
992 user = refresh_record(user)
994 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
996 assert 3 = length(activities)
999 test "it can create a Flag activity" do
1000 reporter = insert(:user)
1001 target_account = insert(:user)
1002 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
1003 context = Utils.generate_context_id()
1006 reporter_ap_id = reporter.ap_id
1007 target_ap_id = target_account.ap_id
1008 activity_ap_id = activity.data["id"]
1010 assert {:ok, activity} =
1014 account: target_account,
1015 statuses: [activity],
1020 actor: ^reporter_ap_id,
1023 "content" => ^content,
1024 "context" => ^context,
1025 "object" => [^target_ap_id, ^activity_ap_id]
1030 describe "publish_one/1" do
1031 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
1035 actor = insert(:user)
1036 inbox = "http://200.site/users/nick1/inbox"
1038 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1040 assert called(Instances.set_reachable(inbox))
1043 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
1047 actor = insert(:user)
1048 inbox = "http://200.site/users/nick1/inbox"
1051 ActivityPub.publish_one(%{
1056 unreachable_since: NaiveDateTime.utc_now()
1059 assert called(Instances.set_reachable(inbox))
1062 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1066 actor = insert(:user)
1067 inbox = "http://200.site/users/nick1/inbox"
1070 ActivityPub.publish_one(%{
1075 unreachable_since: nil
1078 refute called(Instances.set_reachable(inbox))
1081 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1085 actor = insert(:user)
1086 inbox = "http://404.site/users/nick1/inbox"
1088 assert {:error, _} =
1089 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1091 assert called(Instances.set_unreachable(inbox))
1094 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1098 actor = insert(:user)
1099 inbox = "http://connrefused.site/users/nick1/inbox"
1101 assert {:error, _} =
1102 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1104 assert called(Instances.set_unreachable(inbox))
1107 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1111 actor = insert(:user)
1112 inbox = "http://200.site/users/nick1/inbox"
1114 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1116 refute called(Instances.set_unreachable(inbox))
1119 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1123 actor = insert(:user)
1124 inbox = "http://connrefused.site/users/nick1/inbox"
1126 assert {:error, _} =
1127 ActivityPub.publish_one(%{
1132 unreachable_since: NaiveDateTime.utc_now()
1135 refute called(Instances.set_unreachable(inbox))
1140 File.read!("test/fixtures/avatar_data_uri")