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 activities from blocked domains" do
466 domain = "dogwhistle.zone"
467 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
468 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
469 activity = insert(:note_activity, %{note: note})
471 {:ok, user} = User.block_domain(user, domain)
474 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
476 refute activity in activities
479 test "doesn't return muted activities" do
480 activity_one = insert(:note_activity)
481 activity_two = insert(:note_activity)
482 activity_three = insert(:note_activity)
484 booster = insert(:user)
485 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
488 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
490 assert Enum.member?(activities, activity_two)
491 assert Enum.member?(activities, activity_three)
492 refute Enum.member?(activities, activity_one)
494 # Calling with 'with_muted' will deliver muted activities, too.
496 ActivityPub.fetch_activities([], %{
497 "muting_user" => user,
498 "with_muted" => true,
499 "skip_preload" => true
502 assert Enum.member?(activities, activity_two)
503 assert Enum.member?(activities, activity_three)
504 assert Enum.member?(activities, activity_one)
506 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
509 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
511 assert Enum.member?(activities, activity_two)
512 assert Enum.member?(activities, activity_three)
513 assert Enum.member?(activities, activity_one)
515 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
516 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
517 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
518 activity_three = Activity.get_by_id(activity_three.id)
521 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
523 assert Enum.member?(activities, activity_two)
524 refute Enum.member?(activities, activity_three)
525 refute Enum.member?(activities, boost_activity)
526 assert Enum.member?(activities, activity_one)
528 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
530 assert Enum.member?(activities, activity_two)
531 assert Enum.member?(activities, activity_three)
532 assert Enum.member?(activities, boost_activity)
533 assert Enum.member?(activities, activity_one)
536 test "does include announces on request" do
537 activity_three = insert(:note_activity)
539 booster = insert(:user)
541 {:ok, user} = User.follow(user, booster)
543 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
545 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
547 assert announce_activity.id == announce.id
550 test "excludes reblogs on request" do
552 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
553 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
555 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
557 assert activity == expected_activity
560 describe "public fetch activities" do
561 test "doesn't retrieve unlisted activities" do
564 {:ok, _unlisted_activity} =
565 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
567 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
569 [activity] = ActivityPub.fetch_public_activities()
571 assert activity == listed_activity
574 test "retrieves public activities" do
575 _activities = ActivityPub.fetch_public_activities()
577 %{public: public} = ActivityBuilder.public_and_non_public()
579 activities = ActivityPub.fetch_public_activities()
580 assert length(activities) == 1
581 assert Enum.at(activities, 0) == public
584 test "retrieves a maximum of 20 activities" do
585 activities = ActivityBuilder.insert_list(30)
586 last_expected = List.last(activities)
588 activities = ActivityPub.fetch_public_activities()
589 last = List.last(activities)
591 assert length(activities) == 20
592 assert last == last_expected
595 test "retrieves ids starting from a since_id" do
596 activities = ActivityBuilder.insert_list(30)
597 later_activities = ActivityBuilder.insert_list(10)
598 since_id = List.last(activities).id
599 last_expected = List.last(later_activities)
601 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
602 last = List.last(activities)
604 assert length(activities) == 10
605 assert last == last_expected
608 test "retrieves ids up to max_id" do
609 _first_activities = ActivityBuilder.insert_list(10)
610 activities = ActivityBuilder.insert_list(20)
611 later_activities = ActivityBuilder.insert_list(10)
612 max_id = List.first(later_activities).id
613 last_expected = List.last(activities)
615 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
616 last = List.last(activities)
618 assert length(activities) == 20
619 assert last == last_expected
622 test "doesn't return reblogs for users for whom reblogs have been muted" do
623 activity = insert(:note_activity)
625 booster = insert(:user)
626 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
628 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
630 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
632 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
635 test "returns reblogs for users for whom reblogs have not been muted" do
636 activity = insert(:note_activity)
638 booster = insert(:user)
639 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
640 {:ok, user} = CommonAPI.show_reblogs(user, booster)
642 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
644 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
646 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
650 describe "like an object" do
651 test "adds a like activity to the db" do
652 note_activity = insert(:note_activity)
653 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
655 user_two = insert(:user)
657 {:ok, like_activity, object} = ActivityPub.like(user, object)
659 assert like_activity.data["actor"] == user.ap_id
660 assert like_activity.data["type"] == "Like"
661 assert like_activity.data["object"] == object.data["id"]
662 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
663 assert like_activity.data["context"] == object.data["context"]
664 assert object.data["like_count"] == 1
665 assert object.data["likes"] == [user.ap_id]
667 # Just return the original activity if the user already liked it.
668 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
670 assert like_activity == same_like_activity
671 assert object.data["likes"] == [user.ap_id]
673 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
674 assert note_activity.data["object"]["like_count"] == 1
676 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
677 assert object.data["like_count"] == 2
681 describe "unliking" do
682 test "unliking a previously liked object" do
683 note_activity = insert(:note_activity)
684 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
687 # Unliking something that hasn't been liked does nothing
688 {:ok, object} = ActivityPub.unlike(user, object)
689 assert object.data["like_count"] == 0
691 {:ok, like_activity, object} = ActivityPub.like(user, object)
692 assert object.data["like_count"] == 1
694 {:ok, _, _, object} = ActivityPub.unlike(user, object)
695 assert object.data["like_count"] == 0
697 assert Activity.get_by_id(like_activity.id) == nil
701 describe "announcing an object" do
702 test "adds an announce activity to the db" do
703 note_activity = insert(:note_activity)
704 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
707 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
708 assert object.data["announcement_count"] == 1
709 assert object.data["announcements"] == [user.ap_id]
711 assert announce_activity.data["to"] == [
712 User.ap_followers(user),
713 note_activity.data["actor"]
716 assert announce_activity.data["object"] == object.data["id"]
717 assert announce_activity.data["actor"] == user.ap_id
718 assert announce_activity.data["context"] == object.data["context"]
722 describe "unannouncing an object" do
723 test "unannouncing a previously announced object" do
724 note_activity = insert(:note_activity)
725 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
728 # Unannouncing an object that is not announced does nothing
729 # {:ok, object} = ActivityPub.unannounce(user, object)
730 # assert object.data["announcement_count"] == 0
732 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
733 assert object.data["announcement_count"] == 1
735 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
736 assert object.data["announcement_count"] == 0
738 assert unannounce_activity.data["to"] == [
739 User.ap_followers(user),
740 announce_activity.data["actor"]
743 assert unannounce_activity.data["type"] == "Undo"
744 assert unannounce_activity.data["object"] == announce_activity.data
745 assert unannounce_activity.data["actor"] == user.ap_id
746 assert unannounce_activity.data["context"] == announce_activity.data["context"]
748 assert Activity.get_by_id(announce_activity.id) == nil
752 describe "uploading files" do
753 test "copies the file to the configured folder" do
755 content_type: "image/jpg",
756 path: Path.absname("test/fixtures/image.jpg"),
757 filename: "an_image.jpg"
760 {:ok, %Object{} = object} = ActivityPub.upload(file)
761 assert object.data["name"] == "an_image.jpg"
764 test "works with base64 encoded images" do
769 {:ok, %Object{}} = ActivityPub.upload(file)
773 describe "fetch the latest Follow" do
774 test "fetches the latest Follow activity" do
775 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
776 follower = Repo.get_by(User, ap_id: activity.data["actor"])
777 followed = Repo.get_by(User, ap_id: activity.data["object"])
779 assert activity == Utils.fetch_latest_follow(follower, followed)
783 describe "following / unfollowing" do
784 test "creates a follow activity" do
785 follower = insert(:user)
786 followed = insert(:user)
788 {:ok, activity} = ActivityPub.follow(follower, followed)
789 assert activity.data["type"] == "Follow"
790 assert activity.data["actor"] == follower.ap_id
791 assert activity.data["object"] == followed.ap_id
794 test "creates an undo activity for the last follow" do
795 follower = insert(:user)
796 followed = insert(:user)
798 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
799 {:ok, activity} = ActivityPub.unfollow(follower, followed)
801 assert activity.data["type"] == "Undo"
802 assert activity.data["actor"] == follower.ap_id
804 assert is_map(activity.data["object"])
805 assert activity.data["object"]["type"] == "Follow"
806 assert activity.data["object"]["object"] == followed.ap_id
807 assert activity.data["object"]["id"] == follow_activity.data["id"]
811 describe "blocking / unblocking" do
812 test "creates a block activity" do
813 blocker = insert(:user)
814 blocked = insert(:user)
816 {:ok, activity} = ActivityPub.block(blocker, blocked)
818 assert activity.data["type"] == "Block"
819 assert activity.data["actor"] == blocker.ap_id
820 assert activity.data["object"] == blocked.ap_id
823 test "creates an undo activity for the last block" do
824 blocker = insert(:user)
825 blocked = insert(:user)
827 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
828 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
830 assert activity.data["type"] == "Undo"
831 assert activity.data["actor"] == blocker.ap_id
833 assert is_map(activity.data["object"])
834 assert activity.data["object"]["type"] == "Block"
835 assert activity.data["object"]["object"] == blocked.ap_id
836 assert activity.data["object"]["id"] == block_activity.data["id"]
840 describe "deletion" do
841 test "it creates a delete activity and deletes the original object" do
842 note = insert(:note_activity)
843 object = Object.get_by_ap_id(note.data["object"]["id"])
844 {:ok, delete} = ActivityPub.delete(object)
846 assert delete.data["type"] == "Delete"
847 assert delete.data["actor"] == note.data["actor"]
848 assert delete.data["object"] == note.data["object"]["id"]
850 assert Activity.get_by_id(delete.id) != nil
852 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
855 test "decrements user note count only for public activities" do
856 user = insert(:user, info: %{note_count: 10})
859 CommonAPI.post(User.get_cached_by_id(user.id), %{
861 "visibility" => "public"
865 CommonAPI.post(User.get_cached_by_id(user.id), %{
867 "visibility" => "unlisted"
871 CommonAPI.post(User.get_cached_by_id(user.id), %{
873 "visibility" => "private"
877 CommonAPI.post(User.get_cached_by_id(user.id), %{
879 "visibility" => "direct"
882 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
883 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
884 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
885 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
887 user = User.get_cached_by_id(user.id)
888 assert user.info.note_count == 10
891 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
893 note = insert(:note_activity)
896 Object.get_by_ap_id(note.data["object"]["id"])
899 "actor" => note.data["object"]["actor"],
900 "id" => note.data["object"]["id"],
901 "to" => [user.ap_id],
905 |> Object.update_and_set_cache()
907 {:ok, delete} = ActivityPub.delete(object)
909 assert user.ap_id in delete.data["to"]
912 test "decreases reply count" do
914 user2 = insert(:user)
916 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
917 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
918 ap_id = activity.data["id"]
920 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
921 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
922 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
923 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
925 _ = CommonAPI.delete(direct_reply.id, user2)
926 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
927 assert object.data["repliesCount"] == 2
929 _ = CommonAPI.delete(private_reply.id, user2)
930 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
931 assert object.data["repliesCount"] == 2
933 _ = CommonAPI.delete(public_reply.id, user2)
934 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
935 assert object.data["repliesCount"] == 1
937 _ = CommonAPI.delete(unlisted_reply.id, user2)
938 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
939 assert object.data["repliesCount"] == 0
943 describe "timeline post-processing" do
944 test "it filters broken threads" do
945 user1 = insert(:user)
946 user2 = insert(:user)
947 user3 = insert(:user)
949 {:ok, user1} = User.follow(user1, user3)
950 assert User.following?(user1, user3)
952 {:ok, user2} = User.follow(user2, user3)
953 assert User.following?(user2, user3)
955 {:ok, user3} = User.follow(user3, user2)
956 assert User.following?(user3, user2)
958 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
960 {:ok, private_activity_1} =
961 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
963 {:ok, private_activity_2} =
964 CommonAPI.post(user2, %{
966 "visibility" => "private",
967 "in_reply_to_status_id" => private_activity_1.id
970 {:ok, private_activity_3} =
971 CommonAPI.post(user3, %{
973 "visibility" => "private",
974 "in_reply_to_status_id" => private_activity_2.id
977 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
979 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
981 assert [public_activity, private_activity_1, private_activity_3] == activities
983 assert length(activities) == 3
985 activities = ActivityPub.contain_timeline(activities, user1)
987 assert [public_activity, private_activity_1] == activities
988 assert length(activities) == 2
993 test "it creates an update activity with the new user data" do
995 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
996 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
999 ActivityPub.update(%{
1000 actor: user_data["id"],
1001 to: [user.follower_address],
1006 assert update.data["actor"] == user.ap_id
1007 assert update.data["to"] == [user.follower_address]
1008 assert update.data["object"]["id"] == user_data["id"]
1009 assert update.data["object"]["type"] == user_data["type"]
1013 test "returned pinned statuses" do
1014 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
1015 user = insert(:user)
1017 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1018 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1019 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1021 CommonAPI.pin(activity_one.id, user)
1022 user = refresh_record(user)
1024 CommonAPI.pin(activity_two.id, user)
1025 user = refresh_record(user)
1027 CommonAPI.pin(activity_three.id, user)
1028 user = refresh_record(user)
1030 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1032 assert 3 = length(activities)
1035 test "it can create a Flag activity" do
1036 reporter = insert(:user)
1037 target_account = insert(:user)
1038 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
1039 context = Utils.generate_context_id()
1042 reporter_ap_id = reporter.ap_id
1043 target_ap_id = target_account.ap_id
1044 activity_ap_id = activity.data["id"]
1046 assert {:ok, activity} =
1050 account: target_account,
1051 statuses: [activity],
1056 actor: ^reporter_ap_id,
1059 "content" => ^content,
1060 "context" => ^context,
1061 "object" => [^target_ap_id, ^activity_ap_id]
1066 describe "publish_one/1" do
1067 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
1071 actor = insert(:user)
1072 inbox = "http://200.site/users/nick1/inbox"
1074 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1076 assert called(Instances.set_reachable(inbox))
1079 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
1083 actor = insert(:user)
1084 inbox = "http://200.site/users/nick1/inbox"
1087 Publisher.publish_one(%{
1092 unreachable_since: NaiveDateTime.utc_now()
1095 assert called(Instances.set_reachable(inbox))
1098 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1102 actor = insert(:user)
1103 inbox = "http://200.site/users/nick1/inbox"
1106 Publisher.publish_one(%{
1111 unreachable_since: nil
1114 refute called(Instances.set_reachable(inbox))
1117 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1121 actor = insert(:user)
1122 inbox = "http://404.site/users/nick1/inbox"
1124 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1126 assert called(Instances.set_unreachable(inbox))
1129 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1133 actor = insert(:user)
1134 inbox = "http://connrefused.site/users/nick1/inbox"
1136 assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1138 assert called(Instances.set_unreachable(inbox))
1141 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1145 actor = insert(:user)
1146 inbox = "http://200.site/users/nick1/inbox"
1148 assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1150 refute called(Instances.set_unreachable(inbox))
1153 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1157 actor = insert(:user)
1158 inbox = "http://connrefused.site/users/nick1/inbox"
1160 assert {:error, _} =
1161 Publisher.publish_one(%{
1166 unreachable_since: NaiveDateTime.utc_now()
1169 refute called(Instances.set_unreachable(inbox))
1174 File.read!("test/fixtures/avatar_data_uri")