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 object.data["repliesCount"] == 1
256 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
257 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
258 assert object.data["repliesCount"] == 2
261 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
262 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
263 assert object.data["repliesCount"] == 2
266 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
267 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
268 assert object.data["repliesCount"] == 2
272 describe "fetch activities for recipients" do
273 test "retrieve the activities for certain recipients" do
274 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
275 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
276 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
278 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
279 assert length(activities) == 2
280 assert activities == [activity_one, activity_two]
284 describe "fetch activities in context" do
285 test "retrieves activities that have a given context" do
286 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
287 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
288 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
289 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
290 activity_five = insert(:note_activity)
293 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
295 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
296 assert activities == [activity_two, activity]
300 test "doesn't return blocked activities" do
301 activity_one = insert(:note_activity)
302 activity_two = insert(:note_activity)
303 activity_three = insert(:note_activity)
305 booster = insert(:user)
306 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
309 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
311 assert Enum.member?(activities, activity_two)
312 assert Enum.member?(activities, activity_three)
313 refute Enum.member?(activities, activity_one)
315 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
318 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
320 assert Enum.member?(activities, activity_two)
321 assert Enum.member?(activities, activity_three)
322 assert Enum.member?(activities, activity_one)
324 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
325 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
326 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
327 activity_three = Activity.get_by_id(activity_three.id)
330 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
332 assert Enum.member?(activities, activity_two)
333 refute Enum.member?(activities, activity_three)
334 refute Enum.member?(activities, boost_activity)
335 assert Enum.member?(activities, activity_one)
338 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
340 assert Enum.member?(activities, activity_two)
341 assert Enum.member?(activities, activity_three)
342 assert Enum.member?(activities, boost_activity)
343 assert Enum.member?(activities, activity_one)
346 test "doesn't return muted activities" do
347 activity_one = insert(:note_activity)
348 activity_two = insert(:note_activity)
349 activity_three = insert(:note_activity)
351 booster = insert(:user)
352 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
355 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
357 assert Enum.member?(activities, activity_two)
358 assert Enum.member?(activities, activity_three)
359 refute Enum.member?(activities, activity_one)
361 # Calling with 'with_muted' will deliver muted activities, too.
363 ActivityPub.fetch_activities([], %{
364 "muting_user" => user,
365 "with_muted" => true,
366 "skip_preload" => true
369 assert Enum.member?(activities, activity_two)
370 assert Enum.member?(activities, activity_three)
371 assert Enum.member?(activities, activity_one)
373 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
376 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
378 assert Enum.member?(activities, activity_two)
379 assert Enum.member?(activities, activity_three)
380 assert Enum.member?(activities, activity_one)
382 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
383 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
384 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
385 activity_three = Activity.get_by_id(activity_three.id)
388 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
390 assert Enum.member?(activities, activity_two)
391 refute Enum.member?(activities, activity_three)
392 refute Enum.member?(activities, boost_activity)
393 assert Enum.member?(activities, activity_one)
395 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
397 assert Enum.member?(activities, activity_two)
398 assert Enum.member?(activities, activity_three)
399 assert Enum.member?(activities, boost_activity)
400 assert Enum.member?(activities, activity_one)
403 test "does include announces on request" do
404 activity_three = insert(:note_activity)
406 booster = insert(:user)
408 {:ok, user} = User.follow(user, booster)
410 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
412 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
414 assert announce_activity.id == announce.id
417 test "excludes reblogs on request" do
419 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
420 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
422 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
424 assert activity == expected_activity
427 describe "public fetch activities" do
428 test "doesn't retrieve unlisted activities" do
431 {:ok, _unlisted_activity} =
432 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
434 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
436 [activity] = ActivityPub.fetch_public_activities()
438 assert activity == listed_activity
441 test "retrieves public activities" do
442 _activities = ActivityPub.fetch_public_activities()
444 %{public: public} = ActivityBuilder.public_and_non_public()
446 activities = ActivityPub.fetch_public_activities()
447 assert length(activities) == 1
448 assert Enum.at(activities, 0) == public
451 test "retrieves a maximum of 20 activities" do
452 activities = ActivityBuilder.insert_list(30)
453 last_expected = List.last(activities)
455 activities = ActivityPub.fetch_public_activities()
456 last = List.last(activities)
458 assert length(activities) == 20
459 assert last == last_expected
462 test "retrieves ids starting from a since_id" do
463 activities = ActivityBuilder.insert_list(30)
464 later_activities = ActivityBuilder.insert_list(10)
465 since_id = List.last(activities).id
466 last_expected = List.last(later_activities)
468 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
469 last = List.last(activities)
471 assert length(activities) == 10
472 assert last == last_expected
475 test "retrieves ids up to max_id" do
476 _first_activities = ActivityBuilder.insert_list(10)
477 activities = ActivityBuilder.insert_list(20)
478 later_activities = ActivityBuilder.insert_list(10)
479 max_id = List.first(later_activities).id
480 last_expected = List.last(activities)
482 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
483 last = List.last(activities)
485 assert length(activities) == 20
486 assert last == last_expected
489 test "doesn't return reblogs for users for whom reblogs have been muted" do
490 activity = insert(:note_activity)
492 booster = insert(:user)
493 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
495 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
497 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
499 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
502 test "returns reblogs for users for whom reblogs have not been muted" do
503 activity = insert(:note_activity)
505 booster = insert(:user)
506 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
507 {:ok, user} = CommonAPI.show_reblogs(user, booster)
509 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
511 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
513 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
517 describe "like an object" do
518 test "adds a like activity to the db" do
519 note_activity = insert(:note_activity)
520 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
522 user_two = insert(:user)
524 {:ok, like_activity, object} = ActivityPub.like(user, object)
526 assert like_activity.data["actor"] == user.ap_id
527 assert like_activity.data["type"] == "Like"
528 assert like_activity.data["object"] == object.data["id"]
529 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
530 assert like_activity.data["context"] == object.data["context"]
531 assert object.data["like_count"] == 1
532 assert object.data["likes"] == [user.ap_id]
534 # Just return the original activity if the user already liked it.
535 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
537 assert like_activity == same_like_activity
538 assert object.data["likes"] == [user.ap_id]
540 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
541 assert note_activity.data["object"]["like_count"] == 1
543 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
544 assert object.data["like_count"] == 2
548 describe "unliking" do
549 test "unliking a previously liked object" do
550 note_activity = insert(:note_activity)
551 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
554 # Unliking something that hasn't been liked does nothing
555 {:ok, object} = ActivityPub.unlike(user, object)
556 assert object.data["like_count"] == 0
558 {:ok, like_activity, object} = ActivityPub.like(user, object)
559 assert object.data["like_count"] == 1
561 {:ok, _, _, object} = ActivityPub.unlike(user, object)
562 assert object.data["like_count"] == 0
564 assert Activity.get_by_id(like_activity.id) == nil
568 describe "announcing an object" do
569 test "adds an announce activity to the db" do
570 note_activity = insert(:note_activity)
571 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
574 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
575 assert object.data["announcement_count"] == 1
576 assert object.data["announcements"] == [user.ap_id]
578 assert announce_activity.data["to"] == [
579 User.ap_followers(user),
580 note_activity.data["actor"]
583 assert announce_activity.data["object"] == object.data["id"]
584 assert announce_activity.data["actor"] == user.ap_id
585 assert announce_activity.data["context"] == object.data["context"]
589 describe "unannouncing an object" do
590 test "unannouncing a previously announced object" do
591 note_activity = insert(:note_activity)
592 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
595 # Unannouncing an object that is not announced does nothing
596 # {:ok, object} = ActivityPub.unannounce(user, object)
597 # assert object.data["announcement_count"] == 0
599 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
600 assert object.data["announcement_count"] == 1
602 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
603 assert object.data["announcement_count"] == 0
605 assert unannounce_activity.data["to"] == [
606 User.ap_followers(user),
607 announce_activity.data["actor"]
610 assert unannounce_activity.data["type"] == "Undo"
611 assert unannounce_activity.data["object"] == announce_activity.data
612 assert unannounce_activity.data["actor"] == user.ap_id
613 assert unannounce_activity.data["context"] == announce_activity.data["context"]
615 assert Activity.get_by_id(announce_activity.id) == nil
619 describe "uploading files" do
620 test "copies the file to the configured folder" do
622 content_type: "image/jpg",
623 path: Path.absname("test/fixtures/image.jpg"),
624 filename: "an_image.jpg"
627 {:ok, %Object{} = object} = ActivityPub.upload(file)
628 assert object.data["name"] == "an_image.jpg"
631 test "works with base64 encoded images" do
636 {:ok, %Object{}} = ActivityPub.upload(file)
640 describe "fetch the latest Follow" do
641 test "fetches the latest Follow activity" do
642 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
643 follower = Repo.get_by(User, ap_id: activity.data["actor"])
644 followed = Repo.get_by(User, ap_id: activity.data["object"])
646 assert activity == Utils.fetch_latest_follow(follower, followed)
650 describe "fetching an object" do
651 test "it fetches an object" do
653 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
655 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
656 assert activity.data["id"]
658 {:ok, object_again} =
659 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
661 assert [attachment] = object.data["attachment"]
662 assert is_list(attachment["url"])
664 assert object == object_again
667 test "it works with objects only available via Ostatus" do
668 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
669 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
670 assert activity.data["id"]
672 {:ok, object_again} =
673 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
675 assert object == object_again
678 test "it correctly stitches up conversations between ostatus and ap" do
679 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
680 {:ok, object} = ActivityPub.fetch_object_from_id(last)
682 object = Object.get_by_ap_id(object.data["inReplyTo"])
687 describe "following / unfollowing" do
688 test "creates a follow activity" do
689 follower = insert(:user)
690 followed = insert(:user)
692 {:ok, activity} = ActivityPub.follow(follower, followed)
693 assert activity.data["type"] == "Follow"
694 assert activity.data["actor"] == follower.ap_id
695 assert activity.data["object"] == followed.ap_id
698 test "creates an undo activity for the last follow" do
699 follower = insert(:user)
700 followed = insert(:user)
702 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
703 {:ok, activity} = ActivityPub.unfollow(follower, followed)
705 assert activity.data["type"] == "Undo"
706 assert activity.data["actor"] == follower.ap_id
708 assert is_map(activity.data["object"])
709 assert activity.data["object"]["type"] == "Follow"
710 assert activity.data["object"]["object"] == followed.ap_id
711 assert activity.data["object"]["id"] == follow_activity.data["id"]
715 describe "blocking / unblocking" do
716 test "creates a block activity" do
717 blocker = insert(:user)
718 blocked = insert(:user)
720 {:ok, activity} = ActivityPub.block(blocker, blocked)
722 assert activity.data["type"] == "Block"
723 assert activity.data["actor"] == blocker.ap_id
724 assert activity.data["object"] == blocked.ap_id
727 test "creates an undo activity for the last block" do
728 blocker = insert(:user)
729 blocked = insert(:user)
731 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
732 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
734 assert activity.data["type"] == "Undo"
735 assert activity.data["actor"] == blocker.ap_id
737 assert is_map(activity.data["object"])
738 assert activity.data["object"]["type"] == "Block"
739 assert activity.data["object"]["object"] == blocked.ap_id
740 assert activity.data["object"]["id"] == block_activity.data["id"]
744 describe "deletion" do
745 test "it creates a delete activity and deletes the original object" do
746 note = insert(:note_activity)
747 object = Object.get_by_ap_id(note.data["object"]["id"])
748 {:ok, delete} = ActivityPub.delete(object)
750 assert delete.data["type"] == "Delete"
751 assert delete.data["actor"] == note.data["actor"]
752 assert delete.data["object"] == note.data["object"]["id"]
754 assert Activity.get_by_id(delete.id) != nil
756 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
759 test "decrements user note count only for public activities" do
760 user = insert(:user, info: %{note_count: 10})
763 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "public"})
766 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "unlisted"})
769 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "private"})
772 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "direct"})
774 {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
775 {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
776 {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
777 {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
779 user = User.get_by_id(user.id)
780 assert user.info.note_count == 10
783 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
785 note = insert(:note_activity)
788 Object.get_by_ap_id(note.data["object"]["id"])
791 "actor" => note.data["object"]["actor"],
792 "id" => note.data["object"]["id"],
793 "to" => [user.ap_id],
797 |> Object.update_and_set_cache()
799 {:ok, delete} = ActivityPub.delete(object)
801 assert user.ap_id in delete.data["to"]
804 test "decreases reply count" do
806 user2 = insert(:user)
808 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
809 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
810 ap_id = activity.data["id"]
812 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
813 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
814 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
815 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
817 _ = CommonAPI.delete(direct_reply.id, user2)
818 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
819 assert object.data["repliesCount"] == 2
821 _ = CommonAPI.delete(private_reply.id, user2)
822 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
823 assert object.data["repliesCount"] == 2
825 _ = CommonAPI.delete(public_reply.id, user2)
826 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
827 assert object.data["repliesCount"] == 1
829 _ = CommonAPI.delete(unlisted_reply.id, user2)
830 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
831 assert object.data["repliesCount"] == 0
835 describe "timeline post-processing" do
836 test "it filters broken threads" do
837 user1 = insert(:user)
838 user2 = insert(:user)
839 user3 = insert(:user)
841 {:ok, user1} = User.follow(user1, user3)
842 assert User.following?(user1, user3)
844 {:ok, user2} = User.follow(user2, user3)
845 assert User.following?(user2, user3)
847 {:ok, user3} = User.follow(user3, user2)
848 assert User.following?(user3, user2)
850 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
852 {:ok, private_activity_1} =
853 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
855 {:ok, private_activity_2} =
856 CommonAPI.post(user2, %{
858 "visibility" => "private",
859 "in_reply_to_status_id" => private_activity_1.id
862 {:ok, private_activity_3} =
863 CommonAPI.post(user3, %{
865 "visibility" => "private",
866 "in_reply_to_status_id" => private_activity_2.id
869 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
871 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
872 assert [public_activity, private_activity_1, private_activity_3] == activities
873 assert length(activities) == 3
875 activities = ActivityPub.contain_timeline(activities, user1)
877 assert [public_activity, private_activity_1] == activities
878 assert length(activities) == 2
883 test "it creates an update activity with the new user data" do
885 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
886 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
889 ActivityPub.update(%{
890 actor: user_data["id"],
891 to: [user.follower_address],
896 assert update.data["actor"] == user.ap_id
897 assert update.data["to"] == [user.follower_address]
898 assert update.data["object"]["id"] == user_data["id"]
899 assert update.data["object"]["type"] == user_data["type"]
903 test "it can fetch peertube videos" do
905 ActivityPub.fetch_object_from_id(
906 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
912 test "returned pinned statuses" do
913 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
916 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
917 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
918 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
920 CommonAPI.pin(activity_one.id, user)
921 user = refresh_record(user)
923 CommonAPI.pin(activity_two.id, user)
924 user = refresh_record(user)
926 CommonAPI.pin(activity_three.id, user)
927 user = refresh_record(user)
929 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
931 assert 3 = length(activities)
934 test "it can create a Flag activity" do
935 reporter = insert(:user)
936 target_account = insert(:user)
937 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
938 context = Utils.generate_context_id()
941 reporter_ap_id = reporter.ap_id
942 target_ap_id = target_account.ap_id
943 activity_ap_id = activity.data["id"]
945 assert {:ok, activity} =
949 account: target_account,
950 statuses: [activity],
955 actor: ^reporter_ap_id,
958 "content" => ^content,
959 "context" => ^context,
960 "object" => [^target_ap_id, ^activity_ap_id]
965 describe "publish_one/1" do
966 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
970 actor = insert(:user)
971 inbox = "http://200.site/users/nick1/inbox"
973 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
975 assert called(Instances.set_reachable(inbox))
978 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
982 actor = insert(:user)
983 inbox = "http://200.site/users/nick1/inbox"
986 ActivityPub.publish_one(%{
991 unreachable_since: NaiveDateTime.utc_now()
994 assert called(Instances.set_reachable(inbox))
997 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1001 actor = insert(:user)
1002 inbox = "http://200.site/users/nick1/inbox"
1005 ActivityPub.publish_one(%{
1010 unreachable_since: nil
1013 refute called(Instances.set_reachable(inbox))
1016 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1020 actor = insert(:user)
1021 inbox = "http://404.site/users/nick1/inbox"
1023 assert {:error, _} =
1024 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1026 assert called(Instances.set_unreachable(inbox))
1029 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1033 actor = insert(:user)
1034 inbox = "http://connrefused.site/users/nick1/inbox"
1036 assert {:error, _} =
1037 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1039 assert called(Instances.set_unreachable(inbox))
1042 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1046 actor = insert(:user)
1047 inbox = "http://200.site/users/nick1/inbox"
1049 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1051 refute called(Instances.set_unreachable(inbox))
1054 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1058 actor = insert(:user)
1059 inbox = "http://connrefused.site/users/nick1/inbox"
1061 assert {:error, _} =
1062 ActivityPub.publish_one(%{
1067 unreachable_since: NaiveDateTime.utc_now()
1070 refute called(Instances.set_unreachable(inbox))
1075 File.read!("test/fixtures/avatar_data_uri")