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
136 assert {:ok, _} = ActivityPub.insert(data)
139 test "returns the activity if one with the same id is already in" do
140 activity = insert(:note_activity)
141 {:ok, new_activity} = ActivityPub.insert(activity.data)
143 assert activity.id == new_activity.id
146 test "inserts a given map into the activity database, giving it an id if it has none." do
151 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
152 assert activity.data["ok"] == data["ok"]
153 assert is_binary(activity.data["id"])
160 "context" => "blabla"
163 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
164 assert activity.data["ok"] == data["ok"]
165 assert activity.data["id"] == given_id
166 assert activity.data["context"] == "blabla"
167 assert activity.data["context_id"]
170 test "adds a context when none is there" do
178 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
180 assert is_binary(activity.data["context"])
181 assert is_binary(activity.data["object"]["context"])
182 assert activity.data["context_id"]
183 assert activity.data["object"]["context_id"]
186 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
194 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
195 object = Object.normalize(activity.data["object"])
197 assert is_binary(object.data["id"])
198 assert %Object{} = Object.get_by_ap_id(activity.data["object"])
202 describe "create activities" do
203 test "removes doubled 'to' recipients" do
207 ActivityPub.create(%{
208 to: ["user1", "user1", "user2"],
212 "to" => ["user1", "user1", "user2"],
214 "content" => "testing"
218 assert activity.data["to"] == ["user1", "user2"]
219 assert activity.actor == user.ap_id
220 assert activity.recipients == ["user1", "user2", user.ap_id]
223 test "increases user note count only for public activities" do
227 CommonAPI.post(User.get_by_id(user.id), %{"status" => "1", "visibility" => "public"})
230 CommonAPI.post(User.get_by_id(user.id), %{"status" => "2", "visibility" => "unlisted"})
233 CommonAPI.post(User.get_by_id(user.id), %{"status" => "2", "visibility" => "private"})
236 CommonAPI.post(User.get_by_id(user.id), %{"status" => "3", "visibility" => "direct"})
238 user = User.get_by_id(user.id)
239 assert user.info.note_count == 2
242 test "increases replies count" do
244 user2 = insert(:user)
246 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
247 ap_id = activity.data["id"]
248 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
251 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
252 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
253 assert data["object"]["repliesCount"] == 1
254 assert object.data["repliesCount"] == 1
257 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
258 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
259 assert data["object"]["repliesCount"] == 2
260 assert object.data["repliesCount"] == 2
263 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
264 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
265 assert data["object"]["repliesCount"] == 2
266 assert object.data["repliesCount"] == 2
269 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
270 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
271 assert data["object"]["repliesCount"] == 2
272 assert object.data["repliesCount"] == 2
276 describe "fetch activities for recipients" do
277 test "retrieve the activities for certain recipients" do
278 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
279 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
280 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
282 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
283 assert length(activities) == 2
284 assert activities == [activity_one, activity_two]
288 describe "fetch activities in context" do
289 test "retrieves activities that have a given context" do
290 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
291 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
292 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
293 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
294 activity_five = insert(:note_activity)
297 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
299 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
300 assert activities == [activity_two, activity]
304 test "doesn't return blocked activities" do
305 activity_one = insert(:note_activity)
306 activity_two = insert(:note_activity)
307 activity_three = insert(:note_activity)
309 booster = insert(:user)
310 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
313 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
315 assert Enum.member?(activities, activity_two)
316 assert Enum.member?(activities, activity_three)
317 refute Enum.member?(activities, activity_one)
319 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
322 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
324 assert Enum.member?(activities, activity_two)
325 assert Enum.member?(activities, activity_three)
326 assert Enum.member?(activities, activity_one)
328 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
329 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
330 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
331 activity_three = Activity.get_by_id(activity_three.id)
334 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
336 assert Enum.member?(activities, activity_two)
337 refute Enum.member?(activities, activity_three)
338 refute Enum.member?(activities, boost_activity)
339 assert Enum.member?(activities, activity_one)
342 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
344 assert Enum.member?(activities, activity_two)
345 assert Enum.member?(activities, activity_three)
346 assert Enum.member?(activities, boost_activity)
347 assert Enum.member?(activities, activity_one)
350 test "doesn't return muted activities" do
351 activity_one = insert(:note_activity)
352 activity_two = insert(:note_activity)
353 activity_three = insert(:note_activity)
355 booster = insert(:user)
356 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
359 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
361 assert Enum.member?(activities, activity_two)
362 assert Enum.member?(activities, activity_three)
363 refute Enum.member?(activities, activity_one)
365 # Calling with 'with_muted' will deliver muted activities, too.
367 ActivityPub.fetch_activities([], %{
368 "muting_user" => user,
369 "with_muted" => true,
370 "skip_preload" => true
373 assert Enum.member?(activities, activity_two)
374 assert Enum.member?(activities, activity_three)
375 assert Enum.member?(activities, activity_one)
377 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
380 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
382 assert Enum.member?(activities, activity_two)
383 assert Enum.member?(activities, activity_three)
384 assert Enum.member?(activities, activity_one)
386 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
387 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
388 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
389 activity_three = Activity.get_by_id(activity_three.id)
392 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
394 assert Enum.member?(activities, activity_two)
395 refute Enum.member?(activities, activity_three)
396 refute Enum.member?(activities, boost_activity)
397 assert Enum.member?(activities, activity_one)
399 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
401 assert Enum.member?(activities, activity_two)
402 assert Enum.member?(activities, activity_three)
403 assert Enum.member?(activities, boost_activity)
404 assert Enum.member?(activities, activity_one)
407 test "does include announces on request" do
408 activity_three = insert(:note_activity)
410 booster = insert(:user)
412 {:ok, user} = User.follow(user, booster)
414 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
416 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
418 assert announce_activity.id == announce.id
421 test "excludes reblogs on request" do
423 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
424 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
426 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
428 assert activity == expected_activity
431 describe "public fetch activities" do
432 test "doesn't retrieve unlisted activities" do
435 {:ok, _unlisted_activity} =
436 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
438 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
440 [activity] = ActivityPub.fetch_public_activities()
442 assert activity == listed_activity
445 test "retrieves public activities" do
446 _activities = ActivityPub.fetch_public_activities()
448 %{public: public} = ActivityBuilder.public_and_non_public()
450 activities = ActivityPub.fetch_public_activities()
451 assert length(activities) == 1
452 assert Enum.at(activities, 0) == public
455 test "retrieves a maximum of 20 activities" do
456 activities = ActivityBuilder.insert_list(30)
457 last_expected = List.last(activities)
459 activities = ActivityPub.fetch_public_activities()
460 last = List.last(activities)
462 assert length(activities) == 20
463 assert last == last_expected
466 test "retrieves ids starting from a since_id" do
467 activities = ActivityBuilder.insert_list(30)
468 later_activities = ActivityBuilder.insert_list(10)
469 since_id = List.last(activities).id
470 last_expected = List.last(later_activities)
472 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
473 last = List.last(activities)
475 assert length(activities) == 10
476 assert last == last_expected
479 test "retrieves ids up to max_id" do
480 _first_activities = ActivityBuilder.insert_list(10)
481 activities = ActivityBuilder.insert_list(20)
482 later_activities = ActivityBuilder.insert_list(10)
483 max_id = List.first(later_activities).id
484 last_expected = List.last(activities)
486 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
487 last = List.last(activities)
489 assert length(activities) == 20
490 assert last == last_expected
493 test "doesn't return reblogs for users for whom reblogs have been muted" do
494 activity = insert(:note_activity)
496 booster = insert(:user)
497 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
499 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
501 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
503 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
506 test "returns reblogs for users for whom reblogs have not been muted" do
507 activity = insert(:note_activity)
509 booster = insert(:user)
510 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
511 {:ok, user} = CommonAPI.show_reblogs(user, booster)
513 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
515 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
517 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
521 describe "like an object" do
522 test "adds a like activity to the db" do
523 note_activity = insert(:note_activity)
524 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
526 user_two = insert(:user)
528 {:ok, like_activity, object} = ActivityPub.like(user, object)
530 assert like_activity.data["actor"] == user.ap_id
531 assert like_activity.data["type"] == "Like"
532 assert like_activity.data["object"] == object.data["id"]
533 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
534 assert like_activity.data["context"] == object.data["context"]
535 assert object.data["like_count"] == 1
536 assert object.data["likes"] == [user.ap_id]
538 # Just return the original activity if the user already liked it.
539 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
541 assert like_activity == same_like_activity
542 assert object.data["likes"] == [user.ap_id]
544 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
545 assert note_activity.data["object"]["like_count"] == 1
547 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
548 assert object.data["like_count"] == 2
552 describe "unliking" do
553 test "unliking a previously liked object" do
554 note_activity = insert(:note_activity)
555 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
558 # Unliking something that hasn't been liked does nothing
559 {:ok, object} = ActivityPub.unlike(user, object)
560 assert object.data["like_count"] == 0
562 {:ok, like_activity, object} = ActivityPub.like(user, object)
563 assert object.data["like_count"] == 1
565 {:ok, _, _, object} = ActivityPub.unlike(user, object)
566 assert object.data["like_count"] == 0
568 assert Activity.get_by_id(like_activity.id) == nil
572 describe "announcing an object" do
573 test "adds an announce activity to the db" do
574 note_activity = insert(:note_activity)
575 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
578 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
579 assert object.data["announcement_count"] == 1
580 assert object.data["announcements"] == [user.ap_id]
582 assert announce_activity.data["to"] == [
583 User.ap_followers(user),
584 note_activity.data["actor"]
587 assert announce_activity.data["object"] == object.data["id"]
588 assert announce_activity.data["actor"] == user.ap_id
589 assert announce_activity.data["context"] == object.data["context"]
593 describe "unannouncing an object" do
594 test "unannouncing a previously announced object" do
595 note_activity = insert(:note_activity)
596 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
599 # Unannouncing an object that is not announced does nothing
600 # {:ok, object} = ActivityPub.unannounce(user, object)
601 # assert object.data["announcement_count"] == 0
603 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
604 assert object.data["announcement_count"] == 1
606 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
607 assert object.data["announcement_count"] == 0
609 assert unannounce_activity.data["to"] == [
610 User.ap_followers(user),
611 announce_activity.data["actor"]
614 assert unannounce_activity.data["type"] == "Undo"
615 assert unannounce_activity.data["object"] == announce_activity.data
616 assert unannounce_activity.data["actor"] == user.ap_id
617 assert unannounce_activity.data["context"] == announce_activity.data["context"]
619 assert Activity.get_by_id(announce_activity.id) == nil
623 describe "uploading files" do
624 test "copies the file to the configured folder" do
626 content_type: "image/jpg",
627 path: Path.absname("test/fixtures/image.jpg"),
628 filename: "an_image.jpg"
631 {:ok, %Object{} = object} = ActivityPub.upload(file)
632 assert object.data["name"] == "an_image.jpg"
635 test "works with base64 encoded images" do
640 {:ok, %Object{}} = ActivityPub.upload(file)
644 describe "fetch the latest Follow" do
645 test "fetches the latest Follow activity" do
646 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
647 follower = Repo.get_by(User, ap_id: activity.data["actor"])
648 followed = Repo.get_by(User, ap_id: activity.data["object"])
650 assert activity == Utils.fetch_latest_follow(follower, followed)
654 describe "fetching an object" do
655 test "it fetches an object" do
657 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
659 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
660 assert activity.data["id"]
662 {:ok, object_again} =
663 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
665 assert [attachment] = object.data["attachment"]
666 assert is_list(attachment["url"])
668 assert object == object_again
671 test "it works with objects only available via Ostatus" do
672 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
673 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
674 assert activity.data["id"]
676 {:ok, object_again} =
677 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
679 assert object == object_again
682 test "it correctly stitches up conversations between ostatus and ap" do
683 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
684 {:ok, object} = ActivityPub.fetch_object_from_id(last)
686 object = Object.get_by_ap_id(object.data["inReplyTo"])
691 describe "following / unfollowing" do
692 test "creates a follow activity" do
693 follower = insert(:user)
694 followed = insert(:user)
696 {:ok, activity} = ActivityPub.follow(follower, followed)
697 assert activity.data["type"] == "Follow"
698 assert activity.data["actor"] == follower.ap_id
699 assert activity.data["object"] == followed.ap_id
702 test "creates an undo activity for the last follow" do
703 follower = insert(:user)
704 followed = insert(:user)
706 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
707 {:ok, activity} = ActivityPub.unfollow(follower, followed)
709 assert activity.data["type"] == "Undo"
710 assert activity.data["actor"] == follower.ap_id
712 assert is_map(activity.data["object"])
713 assert activity.data["object"]["type"] == "Follow"
714 assert activity.data["object"]["object"] == followed.ap_id
715 assert activity.data["object"]["id"] == follow_activity.data["id"]
719 describe "blocking / unblocking" do
720 test "creates a block activity" do
721 blocker = insert(:user)
722 blocked = insert(:user)
724 {:ok, activity} = ActivityPub.block(blocker, blocked)
726 assert activity.data["type"] == "Block"
727 assert activity.data["actor"] == blocker.ap_id
728 assert activity.data["object"] == blocked.ap_id
731 test "creates an undo activity for the last block" do
732 blocker = insert(:user)
733 blocked = insert(:user)
735 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
736 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
738 assert activity.data["type"] == "Undo"
739 assert activity.data["actor"] == blocker.ap_id
741 assert is_map(activity.data["object"])
742 assert activity.data["object"]["type"] == "Block"
743 assert activity.data["object"]["object"] == blocked.ap_id
744 assert activity.data["object"]["id"] == block_activity.data["id"]
748 describe "deletion" do
749 test "it creates a delete activity and deletes the original object" do
750 note = insert(:note_activity)
751 object = Object.get_by_ap_id(note.data["object"]["id"])
752 {:ok, delete} = ActivityPub.delete(object)
754 assert delete.data["type"] == "Delete"
755 assert delete.data["actor"] == note.data["actor"]
756 assert delete.data["object"] == note.data["object"]["id"]
758 assert Activity.get_by_id(delete.id) != nil
760 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
763 test "decrements user note count only for public activities" do
764 user = insert(:user, info: %{note_count: 10})
767 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "public"})
770 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "unlisted"})
773 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "private"})
776 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "direct"})
778 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
779 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
780 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
781 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
783 user = User.get_by_id(user.id)
784 assert user.info.note_count == 10
787 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
789 note = insert(:note_activity)
792 Object.get_by_ap_id(note.data["object"]["id"])
795 "actor" => note.data["object"]["actor"],
796 "id" => note.data["object"]["id"],
797 "to" => [user.ap_id],
801 |> Object.update_and_set_cache()
803 {:ok, delete} = ActivityPub.delete(object)
805 assert user.ap_id in delete.data["to"]
808 test "decreases reply count" do
810 user2 = insert(:user)
812 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
813 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
814 ap_id = activity.data["id"]
816 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
817 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
818 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
819 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
821 _ = CommonAPI.delete(direct_reply.id, user2)
822 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
823 assert data["object"]["repliesCount"] == 2
824 assert object.data["repliesCount"] == 2
826 _ = CommonAPI.delete(private_reply.id, user2)
827 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
828 assert data["object"]["repliesCount"] == 2
829 assert object.data["repliesCount"] == 2
831 _ = CommonAPI.delete(public_reply.id, user2)
832 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
833 assert data["object"]["repliesCount"] == 1
834 assert object.data["repliesCount"] == 1
836 _ = CommonAPI.delete(unlisted_reply.id, user2)
837 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
838 assert data["object"]["repliesCount"] == 0
839 assert object.data["repliesCount"] == 0
843 describe "timeline post-processing" do
844 test "it filters broken threads" do
845 user1 = insert(:user)
846 user2 = insert(:user)
847 user3 = insert(:user)
849 {:ok, user1} = User.follow(user1, user3)
850 assert User.following?(user1, user3)
852 {:ok, user2} = User.follow(user2, user3)
853 assert User.following?(user2, user3)
855 {:ok, user3} = User.follow(user3, user2)
856 assert User.following?(user3, user2)
858 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
860 {:ok, private_activity_1} =
861 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
863 {:ok, private_activity_2} =
864 CommonAPI.post(user2, %{
866 "visibility" => "private",
867 "in_reply_to_status_id" => private_activity_1.id
870 {:ok, private_activity_3} =
871 CommonAPI.post(user3, %{
873 "visibility" => "private",
874 "in_reply_to_status_id" => private_activity_2.id
877 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
879 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
880 assert [public_activity, private_activity_1, private_activity_3] == activities
881 assert length(activities) == 3
883 activities = ActivityPub.contain_timeline(activities, user1)
885 assert [public_activity, private_activity_1] == activities
886 assert length(activities) == 2
891 test "it creates an update activity with the new user data" do
893 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
894 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
897 ActivityPub.update(%{
898 actor: user_data["id"],
899 to: [user.follower_address],
904 assert update.data["actor"] == user.ap_id
905 assert update.data["to"] == [user.follower_address]
906 assert update.data["object"]["id"] == user_data["id"]
907 assert update.data["object"]["type"] == user_data["type"]
911 test "it can fetch peertube videos" do
913 ActivityPub.fetch_object_from_id(
914 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
920 test "returned pinned statuses" do
921 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
924 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
925 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
926 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
928 CommonAPI.pin(activity_one.id, user)
929 user = refresh_record(user)
931 CommonAPI.pin(activity_two.id, user)
932 user = refresh_record(user)
934 CommonAPI.pin(activity_three.id, user)
935 user = refresh_record(user)
937 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
939 assert 3 = length(activities)
942 test "it can create a Flag activity" do
943 reporter = insert(:user)
944 target_account = insert(:user)
945 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
946 context = Utils.generate_context_id()
949 reporter_ap_id = reporter.ap_id
950 target_ap_id = target_account.ap_id
951 activity_ap_id = activity.data["id"]
953 assert {:ok, activity} =
957 account: target_account,
958 statuses: [activity],
963 actor: ^reporter_ap_id,
966 "content" => ^content,
967 "context" => ^context,
968 "object" => [^target_ap_id, ^activity_ap_id]
973 describe "publish_one/1" do
974 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
978 actor = insert(:user)
979 inbox = "http://200.site/users/nick1/inbox"
981 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
983 assert called(Instances.set_reachable(inbox))
986 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
990 actor = insert(:user)
991 inbox = "http://200.site/users/nick1/inbox"
994 ActivityPub.publish_one(%{
999 unreachable_since: NaiveDateTime.utc_now()
1002 assert called(Instances.set_reachable(inbox))
1005 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1009 actor = insert(:user)
1010 inbox = "http://200.site/users/nick1/inbox"
1013 ActivityPub.publish_one(%{
1018 unreachable_since: nil
1021 refute called(Instances.set_reachable(inbox))
1024 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1028 actor = insert(:user)
1029 inbox = "http://404.site/users/nick1/inbox"
1031 assert {:error, _} =
1032 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1034 assert called(Instances.set_unreachable(inbox))
1037 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1041 actor = insert(:user)
1042 inbox = "http://connrefused.site/users/nick1/inbox"
1044 assert {:error, _} =
1045 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1047 assert called(Instances.set_unreachable(inbox))
1050 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1054 actor = insert(:user)
1055 inbox = "http://200.site/users/nick1/inbox"
1057 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1059 refute called(Instances.set_unreachable(inbox))
1062 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1066 actor = insert(:user)
1067 inbox = "http://connrefused.site/users/nick1/inbox"
1069 assert {:error, _} =
1070 ActivityPub.publish_one(%{
1075 unreachable_since: NaiveDateTime.utc_now()
1078 refute called(Instances.set_unreachable(inbox))
1083 File.read!("test/fixtures/avatar_data_uri")