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([], %{"tag" => "test"})
88 fetch_two = ActivityPub.fetch_activities([], %{"tag" => ["test", "essais"]})
91 ActivityPub.fetch_activities([], %{
92 "tag" => ["test", "essais"],
93 "tag_reject" => ["reject"]
97 ActivityPub.fetch_activities([], %{
99 "tag_all" => ["test", "reject"]
102 assert fetch_one == [status_one, status_three]
103 assert fetch_two == [status_one, status_two, status_three]
104 assert fetch_three == [status_one, status_two]
105 assert fetch_four == [status_three]
109 describe "insertion" do
110 test "drops activities beyond a certain limit" do
111 limit = Pleroma.Config.get([:instance, :remote_limit])
114 :crypto.strong_rand_bytes(limit + 1)
116 |> binary_part(0, limit + 1)
121 "content" => random_text
125 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
128 test "doesn't drop activities with content being null" do
132 "actor" => user.ap_id,
135 "actor" => user.ap_id,
142 assert {:ok, _} = ActivityPub.insert(data)
145 test "returns the activity if one with the same id is already in" do
146 activity = insert(:note_activity)
147 {:ok, new_activity} = ActivityPub.insert(activity.data)
149 assert activity.id == new_activity.id
152 test "inserts a given map into the activity database, giving it an id if it has none." do
156 "actor" => user.ap_id,
159 "actor" => user.ap_id,
166 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
167 assert activity.data["ok"] == data["ok"]
168 assert is_binary(activity.data["id"])
174 "actor" => user.ap_id,
176 "context" => "blabla",
178 "actor" => user.ap_id,
185 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
186 assert activity.data["ok"] == data["ok"]
187 assert activity.data["id"] == given_id
188 assert activity.data["context"] == "blabla"
189 assert activity.data["context_id"]
192 test "adds a context when none is there" do
196 "actor" => user.ap_id,
199 "actor" => user.ap_id,
206 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
208 assert is_binary(activity.data["context"])
209 assert is_binary(activity.data["object"]["context"])
210 assert activity.data["context_id"]
211 assert activity.data["object"]["context_id"]
214 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
218 "actor" => user.ap_id,
221 "actor" => user.ap_id,
228 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
229 assert is_binary(activity.data["object"]["id"])
230 assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"])
234 describe "create activities" do
235 test "removes doubled 'to' recipients" do
239 ActivityPub.create(%{
240 to: ["user1", "user1", "user2"],
246 assert activity.data["to"] == ["user1", "user2"]
247 assert activity.actor == user.ap_id
248 assert activity.recipients == ["user1", "user2", user.ap_id]
251 test "increases user note count only for public activities" do
255 CommonAPI.post(User.get_by_id(user.id), %{"status" => "1", "visibility" => "public"})
258 CommonAPI.post(User.get_by_id(user.id), %{"status" => "2", "visibility" => "unlisted"})
261 CommonAPI.post(User.get_by_id(user.id), %{"status" => "2", "visibility" => "private"})
264 CommonAPI.post(User.get_by_id(user.id), %{"status" => "3", "visibility" => "direct"})
266 user = User.get_by_id(user.id)
267 assert user.info.note_count == 2
270 test "increases replies count" do
272 user2 = insert(:user)
274 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
275 ap_id = activity.data["id"]
276 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
279 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
280 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
281 assert data["object"]["repliesCount"] == 1
282 assert object.data["repliesCount"] == 1
285 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
286 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
287 assert data["object"]["repliesCount"] == 2
288 assert object.data["repliesCount"] == 2
291 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
292 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
293 assert data["object"]["repliesCount"] == 2
294 assert object.data["repliesCount"] == 2
297 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
298 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
299 assert data["object"]["repliesCount"] == 2
300 assert object.data["repliesCount"] == 2
304 describe "fetch activities for recipients" do
305 test "retrieve the activities for certain recipients" do
306 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
307 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
308 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
310 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
311 assert length(activities) == 2
312 assert activities == [activity_one, activity_two]
316 describe "fetch activities in context" do
317 test "retrieves activities that have a given context" do
318 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
319 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
320 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
321 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
322 activity_five = insert(:note_activity)
325 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
327 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
328 assert activities == [activity_two, activity]
332 test "doesn't return blocked activities" do
333 activity_one = insert(:note_activity)
334 activity_two = insert(:note_activity)
335 activity_three = insert(:note_activity)
337 booster = insert(:user)
338 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
341 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
343 assert Enum.member?(activities, activity_two)
344 assert Enum.member?(activities, activity_three)
345 refute Enum.member?(activities, activity_one)
347 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
350 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
352 assert Enum.member?(activities, activity_two)
353 assert Enum.member?(activities, activity_three)
354 assert Enum.member?(activities, activity_one)
356 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
357 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
358 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
359 activity_three = Activity.get_by_id(activity_three.id)
362 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
364 assert Enum.member?(activities, activity_two)
365 refute Enum.member?(activities, activity_three)
366 refute Enum.member?(activities, boost_activity)
367 assert Enum.member?(activities, activity_one)
370 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
372 assert Enum.member?(activities, activity_two)
373 assert Enum.member?(activities, activity_three)
374 assert Enum.member?(activities, boost_activity)
375 assert Enum.member?(activities, activity_one)
378 test "doesn't return muted activities" do
379 activity_one = insert(:note_activity)
380 activity_two = insert(:note_activity)
381 activity_three = insert(:note_activity)
383 booster = insert(:user)
384 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
387 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
389 assert Enum.member?(activities, activity_two)
390 assert Enum.member?(activities, activity_three)
391 refute Enum.member?(activities, activity_one)
393 # Calling with 'with_muted' will deliver muted activities, too.
395 ActivityPub.fetch_activities([], %{
396 "muting_user" => user,
397 "with_muted" => true,
398 "skip_preload" => true
401 assert Enum.member?(activities, activity_two)
402 assert Enum.member?(activities, activity_three)
403 assert Enum.member?(activities, activity_one)
405 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
408 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
410 assert Enum.member?(activities, activity_two)
411 assert Enum.member?(activities, activity_three)
412 assert Enum.member?(activities, activity_one)
414 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
415 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
416 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
417 activity_three = Activity.get_by_id(activity_three.id)
420 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
422 assert Enum.member?(activities, activity_two)
423 refute Enum.member?(activities, activity_three)
424 refute Enum.member?(activities, boost_activity)
425 assert Enum.member?(activities, activity_one)
427 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
429 assert Enum.member?(activities, activity_two)
430 assert Enum.member?(activities, activity_three)
431 assert Enum.member?(activities, boost_activity)
432 assert Enum.member?(activities, activity_one)
435 test "does include announces on request" do
436 activity_three = insert(:note_activity)
438 booster = insert(:user)
440 {:ok, user} = User.follow(user, booster)
442 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
444 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
446 assert announce_activity.id == announce.id
449 test "excludes reblogs on request" do
451 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
452 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
454 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
456 assert activity == expected_activity
459 describe "public fetch activities" do
460 test "doesn't retrieve unlisted activities" do
463 {:ok, _unlisted_activity} =
464 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
466 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
468 [activity] = ActivityPub.fetch_public_activities()
470 assert activity == listed_activity
473 test "retrieves public activities" do
474 _activities = ActivityPub.fetch_public_activities()
476 %{public: public} = ActivityBuilder.public_and_non_public()
478 activities = ActivityPub.fetch_public_activities()
479 assert length(activities) == 1
480 assert Enum.at(activities, 0) == public
483 test "retrieves a maximum of 20 activities" do
484 activities = ActivityBuilder.insert_list(30)
485 last_expected = List.last(activities)
487 activities = ActivityPub.fetch_public_activities()
488 last = List.last(activities)
490 assert length(activities) == 20
491 assert last == last_expected
494 test "retrieves ids starting from a since_id" do
495 activities = ActivityBuilder.insert_list(30)
496 later_activities = ActivityBuilder.insert_list(10)
497 since_id = List.last(activities).id
498 last_expected = List.last(later_activities)
500 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
501 last = List.last(activities)
503 assert length(activities) == 10
504 assert last == last_expected
507 test "retrieves ids up to max_id" do
508 _first_activities = ActivityBuilder.insert_list(10)
509 activities = ActivityBuilder.insert_list(20)
510 later_activities = ActivityBuilder.insert_list(10)
511 max_id = List.first(later_activities).id
512 last_expected = List.last(activities)
514 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
515 last = List.last(activities)
517 assert length(activities) == 20
518 assert last == last_expected
521 test "doesn't return reblogs for users for whom reblogs have been muted" do
522 activity = insert(:note_activity)
524 booster = insert(:user)
525 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
527 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
529 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
531 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
534 test "returns reblogs for users for whom reblogs have not been muted" do
535 activity = insert(:note_activity)
537 booster = insert(:user)
538 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
539 {:ok, user} = CommonAPI.show_reblogs(user, booster)
541 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
543 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
545 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
549 describe "like an object" do
550 test "adds a like activity to the db" do
551 note_activity = insert(:note_activity)
552 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
554 user_two = insert(:user)
556 {:ok, like_activity, object} = ActivityPub.like(user, object)
558 assert like_activity.data["actor"] == user.ap_id
559 assert like_activity.data["type"] == "Like"
560 assert like_activity.data["object"] == object.data["id"]
561 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
562 assert like_activity.data["context"] == object.data["context"]
563 assert object.data["like_count"] == 1
564 assert object.data["likes"] == [user.ap_id]
566 # Just return the original activity if the user already liked it.
567 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
569 assert like_activity == same_like_activity
570 assert object.data["likes"] == [user.ap_id]
572 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
573 assert note_activity.data["object"]["like_count"] == 1
575 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
576 assert object.data["like_count"] == 2
580 describe "unliking" do
581 test "unliking a previously liked object" do
582 note_activity = insert(:note_activity)
583 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
586 # Unliking something that hasn't been liked does nothing
587 {:ok, object} = ActivityPub.unlike(user, object)
588 assert object.data["like_count"] == 0
590 {:ok, like_activity, object} = ActivityPub.like(user, object)
591 assert object.data["like_count"] == 1
593 {:ok, _, _, object} = ActivityPub.unlike(user, object)
594 assert object.data["like_count"] == 0
596 assert Activity.get_by_id(like_activity.id) == nil
600 describe "announcing an object" do
601 test "adds an announce activity to the db" do
602 note_activity = insert(:note_activity)
603 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
606 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
607 assert object.data["announcement_count"] == 1
608 assert object.data["announcements"] == [user.ap_id]
610 assert announce_activity.data["to"] == [
611 User.ap_followers(user),
612 note_activity.data["actor"]
615 assert announce_activity.data["object"] == object.data["id"]
616 assert announce_activity.data["actor"] == user.ap_id
617 assert announce_activity.data["context"] == object.data["context"]
621 describe "unannouncing an object" do
622 test "unannouncing a previously announced object" do
623 note_activity = insert(:note_activity)
624 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
627 # Unannouncing an object that is not announced does nothing
628 # {:ok, object} = ActivityPub.unannounce(user, object)
629 # assert object.data["announcement_count"] == 0
631 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
632 assert object.data["announcement_count"] == 1
634 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
635 assert object.data["announcement_count"] == 0
637 assert unannounce_activity.data["to"] == [
638 User.ap_followers(user),
639 announce_activity.data["actor"]
642 assert unannounce_activity.data["type"] == "Undo"
643 assert unannounce_activity.data["object"] == announce_activity.data
644 assert unannounce_activity.data["actor"] == user.ap_id
645 assert unannounce_activity.data["context"] == announce_activity.data["context"]
647 assert Activity.get_by_id(announce_activity.id) == nil
651 describe "uploading files" do
652 test "copies the file to the configured folder" do
654 content_type: "image/jpg",
655 path: Path.absname("test/fixtures/image.jpg"),
656 filename: "an_image.jpg"
659 {:ok, %Object{} = object} = ActivityPub.upload(file)
660 assert object.data["name"] == "an_image.jpg"
663 test "works with base64 encoded images" do
668 {:ok, %Object{}} = ActivityPub.upload(file)
672 describe "fetching an object" do
673 test "it fetches an object" do
675 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
677 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
678 assert activity.data["id"]
680 {:ok, object_again} =
681 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
683 assert [attachment] = object.data["attachment"]
684 assert is_list(attachment["url"])
686 assert object == object_again
689 test "it works with objects only available via Ostatus" do
690 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
691 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
692 assert activity.data["id"]
694 {:ok, object_again} =
695 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
697 assert object == object_again
700 test "it correctly stitches up conversations between ostatus and ap" do
701 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
702 {:ok, object} = ActivityPub.fetch_object_from_id(last)
704 object = Object.get_by_ap_id(object.data["inReplyTo"])
709 describe "following / unfollowing" do
710 test "creates a follow activity" do
711 follower = insert(:user)
712 followed = insert(:user)
714 {:ok, activity} = ActivityPub.follow(follower, followed)
715 assert activity.data["type"] == "Follow"
716 assert activity.data["actor"] == follower.ap_id
717 assert activity.data["object"] == followed.ap_id
720 test "creates an undo activity for the last follow" do
721 follower = insert(:user)
722 followed = insert(:user)
724 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
725 {:ok, activity} = ActivityPub.unfollow(follower, followed)
727 assert activity.data["type"] == "Undo"
728 assert activity.data["actor"] == follower.ap_id
730 assert is_map(activity.data["object"])
731 assert activity.data["object"]["type"] == "Follow"
732 assert activity.data["object"]["object"] == followed.ap_id
733 assert activity.data["object"]["id"] == follow_activity.data["id"]
737 describe "blocking / unblocking" do
738 test "creates a block activity" do
739 blocker = insert(:user)
740 blocked = insert(:user)
742 {:ok, activity} = ActivityPub.block(blocker, blocked)
744 assert activity.data["type"] == "Block"
745 assert activity.data["actor"] == blocker.ap_id
746 assert activity.data["object"] == blocked.ap_id
749 test "creates an undo activity for the last block" do
750 blocker = insert(:user)
751 blocked = insert(:user)
753 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
754 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
756 assert activity.data["type"] == "Undo"
757 assert activity.data["actor"] == blocker.ap_id
759 assert is_map(activity.data["object"])
760 assert activity.data["object"]["type"] == "Block"
761 assert activity.data["object"]["object"] == blocked.ap_id
762 assert activity.data["object"]["id"] == block_activity.data["id"]
766 describe "deletion" do
767 test "it creates a delete activity and deletes the original object" do
768 note = insert(:note_activity)
769 object = Object.get_by_ap_id(note.data["object"]["id"])
770 {:ok, delete} = ActivityPub.delete(object)
772 assert delete.data["type"] == "Delete"
773 assert delete.data["actor"] == note.data["actor"]
774 assert delete.data["object"] == note.data["object"]["id"]
776 assert Activity.get_by_id(delete.id) != nil
778 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
781 test "decrements user note count only for public activities" do
782 user = insert(:user, info: %{note_count: 10})
785 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "public"})
788 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "unlisted"})
791 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "private"})
794 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "direct"})
796 {:ok, _} = a1.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
797 {:ok, _} = a2.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
798 {:ok, _} = a3.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
799 {:ok, _} = a4.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
801 user = User.get_by_id(user.id)
802 assert user.info.note_count == 10
805 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
807 note = insert(:note_activity)
810 Object.get_by_ap_id(note.data["object"]["id"])
813 "actor" => note.data["object"]["actor"],
814 "id" => note.data["object"]["id"],
815 "to" => [user.ap_id],
819 |> Object.update_and_set_cache()
821 {:ok, delete} = ActivityPub.delete(object)
823 assert user.ap_id in delete.data["to"]
826 test "decreases reply count" do
828 user2 = insert(:user)
830 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
831 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
832 ap_id = activity.data["id"]
834 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
835 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
836 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
837 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
839 _ = CommonAPI.delete(direct_reply.id, user2)
840 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
841 assert data["object"]["repliesCount"] == 2
842 assert object.data["repliesCount"] == 2
844 _ = CommonAPI.delete(private_reply.id, user2)
845 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
846 assert data["object"]["repliesCount"] == 2
847 assert object.data["repliesCount"] == 2
849 _ = CommonAPI.delete(public_reply.id, user2)
850 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
851 assert data["object"]["repliesCount"] == 1
852 assert object.data["repliesCount"] == 1
854 _ = CommonAPI.delete(unlisted_reply.id, user2)
855 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
856 assert data["object"]["repliesCount"] == 0
857 assert object.data["repliesCount"] == 0
861 describe "timeline post-processing" do
862 test "it filters broken threads" do
863 user1 = insert(:user)
864 user2 = insert(:user)
865 user3 = insert(:user)
867 {:ok, user1} = User.follow(user1, user3)
868 assert User.following?(user1, user3)
870 {:ok, user2} = User.follow(user2, user3)
871 assert User.following?(user2, user3)
873 {:ok, user3} = User.follow(user3, user2)
874 assert User.following?(user3, user2)
876 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
878 {:ok, private_activity_1} =
879 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
881 {:ok, private_activity_2} =
882 CommonAPI.post(user2, %{
884 "visibility" => "private",
885 "in_reply_to_status_id" => private_activity_1.id
888 {:ok, private_activity_3} =
889 CommonAPI.post(user3, %{
891 "visibility" => "private",
892 "in_reply_to_status_id" => private_activity_2.id
895 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
897 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
898 assert [public_activity, private_activity_1, private_activity_3] == activities
899 assert length(activities) == 3
901 activities = ActivityPub.contain_timeline(activities, user1)
903 assert [public_activity, private_activity_1] == activities
904 assert length(activities) == 2
908 test "it can fetch plume articles" do
910 ActivityPub.fetch_object_from_id(
911 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
918 test "it creates an update activity with the new user data" do
920 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
921 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
924 ActivityPub.update(%{
925 actor: user_data["id"],
926 to: [user.follower_address],
931 assert update.data["actor"] == user.ap_id
932 assert update.data["to"] == [user.follower_address]
933 assert update.data["object"]["id"] == user_data["id"]
934 assert update.data["object"]["type"] == user_data["type"]
938 test "it can fetch peertube videos" do
940 ActivityPub.fetch_object_from_id(
941 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
947 test "returned pinned statuses" do
948 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
951 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
952 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
953 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
955 CommonAPI.pin(activity_one.id, user)
956 user = refresh_record(user)
958 CommonAPI.pin(activity_two.id, user)
959 user = refresh_record(user)
961 CommonAPI.pin(activity_three.id, user)
962 user = refresh_record(user)
964 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
966 assert 3 = length(activities)
969 test "it can create a Flag activity" do
970 reporter = insert(:user)
971 target_account = insert(:user)
972 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
973 context = Utils.generate_context_id()
976 reporter_ap_id = reporter.ap_id
977 target_ap_id = target_account.ap_id
978 activity_ap_id = activity.data["id"]
980 assert {:ok, activity} =
984 account: target_account,
985 statuses: [activity],
990 actor: ^reporter_ap_id,
993 "content" => ^content,
994 "context" => ^context,
995 "object" => [^target_ap_id, ^activity_ap_id]
1000 describe "publish_one/1" do
1001 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
1005 actor = insert(:user)
1006 inbox = "http://200.site/users/nick1/inbox"
1008 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1010 assert called(Instances.set_reachable(inbox))
1013 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
1017 actor = insert(:user)
1018 inbox = "http://200.site/users/nick1/inbox"
1021 ActivityPub.publish_one(%{
1026 unreachable_since: NaiveDateTime.utc_now()
1029 assert called(Instances.set_reachable(inbox))
1032 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1036 actor = insert(:user)
1037 inbox = "http://200.site/users/nick1/inbox"
1040 ActivityPub.publish_one(%{
1045 unreachable_since: nil
1048 refute called(Instances.set_reachable(inbox))
1051 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1055 actor = insert(:user)
1056 inbox = "http://404.site/users/nick1/inbox"
1058 assert {:error, _} =
1059 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1061 assert called(Instances.set_unreachable(inbox))
1064 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1068 actor = insert(:user)
1069 inbox = "http://connrefused.site/users/nick1/inbox"
1071 assert {:error, _} =
1072 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1074 assert called(Instances.set_unreachable(inbox))
1077 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1081 actor = insert(:user)
1082 inbox = "http://200.site/users/nick1/inbox"
1084 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1086 refute called(Instances.set_unreachable(inbox))
1089 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1093 actor = insert(:user)
1094 inbox = "http://connrefused.site/users/nick1/inbox"
1096 assert {:error, _} =
1097 ActivityPub.publish_one(%{
1102 unreachable_since: NaiveDateTime.utc_now()
1105 refute called(Instances.set_unreachable(inbox))
1110 File.read!("test/fixtures/avatar_data_uri")