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 assert is_binary(activity.data["object"]["id"])
196 assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"])
200 describe "create activities" do
201 test "removes doubled 'to' recipients" do
205 ActivityPub.create(%{
206 to: ["user1", "user1", "user2"],
212 assert activity.data["to"] == ["user1", "user2"]
213 assert activity.actor == user.ap_id
214 assert activity.recipients == ["user1", "user2", user.ap_id]
217 test "increases user note count only for public activities" do
221 CommonAPI.post(Repo.get(User, user.id), %{"status" => "1", "visibility" => "public"})
224 CommonAPI.post(Repo.get(User, user.id), %{"status" => "2", "visibility" => "unlisted"})
227 CommonAPI.post(Repo.get(User, user.id), %{"status" => "2", "visibility" => "private"})
230 CommonAPI.post(Repo.get(User, user.id), %{"status" => "3", "visibility" => "direct"})
232 user = Repo.get(User, user.id)
233 assert user.info.note_count == 2
236 test "increases replies count" do
238 user2 = insert(:user)
240 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
241 ap_id = activity.data["id"]
242 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
245 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
246 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
247 assert data["object"]["repliesCount"] == 1
248 assert object.data["repliesCount"] == 1
251 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
252 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
253 assert data["object"]["repliesCount"] == 2
254 assert object.data["repliesCount"] == 2
257 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
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", "direct"))
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
270 describe "fetch activities for recipients" do
271 test "retrieve the activities for certain recipients" do
272 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
273 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
274 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
276 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
277 assert length(activities) == 2
278 assert activities == [activity_one, activity_two]
282 describe "fetch activities in context" do
283 test "retrieves activities that have a given context" do
284 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
285 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
286 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
287 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
288 activity_five = insert(:note_activity)
291 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
293 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
294 assert activities == [activity_two, activity]
298 test "doesn't return blocked activities" do
299 activity_one = insert(:note_activity)
300 activity_two = insert(:note_activity)
301 activity_three = insert(:note_activity)
303 booster = insert(:user)
304 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
307 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
309 assert Enum.member?(activities, activity_two)
310 assert Enum.member?(activities, activity_three)
311 refute Enum.member?(activities, activity_one)
313 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
316 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
318 assert Enum.member?(activities, activity_two)
319 assert Enum.member?(activities, activity_three)
320 assert Enum.member?(activities, activity_one)
322 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
323 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
324 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
325 activity_three = Repo.get(Activity, activity_three.id)
328 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
330 assert Enum.member?(activities, activity_two)
331 refute Enum.member?(activities, activity_three)
332 refute Enum.member?(activities, boost_activity)
333 assert Enum.member?(activities, activity_one)
336 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
338 assert Enum.member?(activities, activity_two)
339 assert Enum.member?(activities, activity_three)
340 assert Enum.member?(activities, boost_activity)
341 assert Enum.member?(activities, activity_one)
344 test "doesn't return muted activities" do
345 activity_one = insert(:note_activity)
346 activity_two = insert(:note_activity)
347 activity_three = insert(:note_activity)
349 booster = insert(:user)
350 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
353 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
355 assert Enum.member?(activities, activity_two)
356 assert Enum.member?(activities, activity_three)
357 refute Enum.member?(activities, activity_one)
359 # Calling with 'with_muted' will deliver muted activities, too.
361 ActivityPub.fetch_activities([], %{
362 "muting_user" => user,
363 "with_muted" => true,
364 "skip_preload" => true
367 assert Enum.member?(activities, activity_two)
368 assert Enum.member?(activities, activity_three)
369 assert Enum.member?(activities, activity_one)
371 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
374 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
376 assert Enum.member?(activities, activity_two)
377 assert Enum.member?(activities, activity_three)
378 assert Enum.member?(activities, activity_one)
380 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
381 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
382 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
383 activity_three = Repo.get(Activity, activity_three.id)
386 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
388 assert Enum.member?(activities, activity_two)
389 refute Enum.member?(activities, activity_three)
390 refute Enum.member?(activities, boost_activity)
391 assert Enum.member?(activities, activity_one)
393 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
395 assert Enum.member?(activities, activity_two)
396 assert Enum.member?(activities, activity_three)
397 assert Enum.member?(activities, boost_activity)
398 assert Enum.member?(activities, activity_one)
401 test "does include announces on request" do
402 activity_three = insert(:note_activity)
404 booster = insert(:user)
406 {:ok, user} = User.follow(user, booster)
408 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
410 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
412 assert announce_activity.id == announce.id
415 test "excludes reblogs on request" do
417 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
418 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
420 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
422 assert activity == expected_activity
425 describe "public fetch activities" do
426 test "doesn't retrieve unlisted activities" do
429 {:ok, _unlisted_activity} =
430 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
432 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
434 [activity] = ActivityPub.fetch_public_activities()
436 assert activity == listed_activity
439 test "retrieves public activities" do
440 _activities = ActivityPub.fetch_public_activities()
442 %{public: public} = ActivityBuilder.public_and_non_public()
444 activities = ActivityPub.fetch_public_activities()
445 assert length(activities) == 1
446 assert Enum.at(activities, 0) == public
449 test "retrieves a maximum of 20 activities" do
450 activities = ActivityBuilder.insert_list(30)
451 last_expected = List.last(activities)
453 activities = ActivityPub.fetch_public_activities()
454 last = List.last(activities)
456 assert length(activities) == 20
457 assert last == last_expected
460 test "retrieves ids starting from a since_id" do
461 activities = ActivityBuilder.insert_list(30)
462 later_activities = ActivityBuilder.insert_list(10)
463 since_id = List.last(activities).id
464 last_expected = List.last(later_activities)
466 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
467 last = List.last(activities)
469 assert length(activities) == 10
470 assert last == last_expected
473 test "retrieves ids up to max_id" do
474 _first_activities = ActivityBuilder.insert_list(10)
475 activities = ActivityBuilder.insert_list(20)
476 later_activities = ActivityBuilder.insert_list(10)
477 max_id = List.first(later_activities).id
478 last_expected = List.last(activities)
480 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
481 last = List.last(activities)
483 assert length(activities) == 20
484 assert last == last_expected
487 test "doesn't return reblogs for users for whom reblogs have been muted" do
488 activity = insert(:note_activity)
490 booster = insert(:user)
491 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
493 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
495 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
497 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
500 test "returns reblogs for users for whom reblogs have not been muted" do
501 activity = insert(:note_activity)
503 booster = insert(:user)
504 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
505 {:ok, user} = CommonAPI.show_reblogs(user, booster)
507 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
509 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
511 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
515 describe "like an object" do
516 test "adds a like activity to the db" do
517 note_activity = insert(:note_activity)
518 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
520 user_two = insert(:user)
522 {:ok, like_activity, object} = ActivityPub.like(user, object)
524 assert like_activity.data["actor"] == user.ap_id
525 assert like_activity.data["type"] == "Like"
526 assert like_activity.data["object"] == object.data["id"]
527 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
528 assert like_activity.data["context"] == object.data["context"]
529 assert object.data["like_count"] == 1
530 assert object.data["likes"] == [user.ap_id]
532 # Just return the original activity if the user already liked it.
533 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
535 assert like_activity == same_like_activity
536 assert object.data["likes"] == [user.ap_id]
538 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
539 assert note_activity.data["object"]["like_count"] == 1
541 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
542 assert object.data["like_count"] == 2
546 describe "unliking" do
547 test "unliking a previously liked object" do
548 note_activity = insert(:note_activity)
549 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
552 # Unliking something that hasn't been liked does nothing
553 {:ok, object} = ActivityPub.unlike(user, object)
554 assert object.data["like_count"] == 0
556 {:ok, like_activity, object} = ActivityPub.like(user, object)
557 assert object.data["like_count"] == 1
559 {:ok, _, _, object} = ActivityPub.unlike(user, object)
560 assert object.data["like_count"] == 0
562 assert Repo.get(Activity, like_activity.id) == nil
566 describe "announcing an object" do
567 test "adds an announce activity to the db" do
568 note_activity = insert(:note_activity)
569 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
572 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
573 assert object.data["announcement_count"] == 1
574 assert object.data["announcements"] == [user.ap_id]
576 assert announce_activity.data["to"] == [
577 User.ap_followers(user),
578 note_activity.data["actor"]
581 assert announce_activity.data["object"] == object.data["id"]
582 assert announce_activity.data["actor"] == user.ap_id
583 assert announce_activity.data["context"] == object.data["context"]
587 describe "unannouncing an object" do
588 test "unannouncing a previously announced object" do
589 note_activity = insert(:note_activity)
590 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
593 # Unannouncing an object that is not announced does nothing
594 # {:ok, object} = ActivityPub.unannounce(user, object)
595 # assert object.data["announcement_count"] == 0
597 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
598 assert object.data["announcement_count"] == 1
600 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
601 assert object.data["announcement_count"] == 0
603 assert unannounce_activity.data["to"] == [
604 User.ap_followers(user),
605 announce_activity.data["actor"]
608 assert unannounce_activity.data["type"] == "Undo"
609 assert unannounce_activity.data["object"] == announce_activity.data
610 assert unannounce_activity.data["actor"] == user.ap_id
611 assert unannounce_activity.data["context"] == announce_activity.data["context"]
613 assert Repo.get(Activity, announce_activity.id) == nil
617 describe "uploading files" do
618 test "copies the file to the configured folder" do
620 content_type: "image/jpg",
621 path: Path.absname("test/fixtures/image.jpg"),
622 filename: "an_image.jpg"
625 {:ok, %Object{} = object} = ActivityPub.upload(file)
626 assert object.data["name"] == "an_image.jpg"
629 test "works with base64 encoded images" do
634 {:ok, %Object{}} = ActivityPub.upload(file)
638 describe "fetching an object" do
639 test "it fetches an object" do
641 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
643 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
644 assert activity.data["id"]
646 {:ok, object_again} =
647 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
649 assert [attachment] = object.data["attachment"]
650 assert is_list(attachment["url"])
652 assert object == object_again
655 test "it works with objects only available via Ostatus" do
656 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
657 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
658 assert activity.data["id"]
660 {:ok, object_again} =
661 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
663 assert object == object_again
666 test "it correctly stitches up conversations between ostatus and ap" do
667 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
668 {:ok, object} = ActivityPub.fetch_object_from_id(last)
670 object = Object.get_by_ap_id(object.data["inReplyTo"])
675 describe "following / unfollowing" do
676 test "creates a follow activity" do
677 follower = insert(:user)
678 followed = insert(:user)
680 {:ok, activity} = ActivityPub.follow(follower, followed)
681 assert activity.data["type"] == "Follow"
682 assert activity.data["actor"] == follower.ap_id
683 assert activity.data["object"] == followed.ap_id
686 test "creates an undo activity for the last follow" do
687 follower = insert(:user)
688 followed = insert(:user)
690 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
691 {:ok, activity} = ActivityPub.unfollow(follower, followed)
693 assert activity.data["type"] == "Undo"
694 assert activity.data["actor"] == follower.ap_id
696 assert is_map(activity.data["object"])
697 assert activity.data["object"]["type"] == "Follow"
698 assert activity.data["object"]["object"] == followed.ap_id
699 assert activity.data["object"]["id"] == follow_activity.data["id"]
703 describe "blocking / unblocking" do
704 test "creates a block activity" do
705 blocker = insert(:user)
706 blocked = insert(:user)
708 {:ok, activity} = ActivityPub.block(blocker, blocked)
710 assert activity.data["type"] == "Block"
711 assert activity.data["actor"] == blocker.ap_id
712 assert activity.data["object"] == blocked.ap_id
715 test "creates an undo activity for the last block" do
716 blocker = insert(:user)
717 blocked = insert(:user)
719 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
720 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
722 assert activity.data["type"] == "Undo"
723 assert activity.data["actor"] == blocker.ap_id
725 assert is_map(activity.data["object"])
726 assert activity.data["object"]["type"] == "Block"
727 assert activity.data["object"]["object"] == blocked.ap_id
728 assert activity.data["object"]["id"] == block_activity.data["id"]
732 describe "deletion" do
733 test "it creates a delete activity and deletes the original object" do
734 note = insert(:note_activity)
735 object = Object.get_by_ap_id(note.data["object"]["id"])
736 {:ok, delete} = ActivityPub.delete(object)
738 assert delete.data["type"] == "Delete"
739 assert delete.data["actor"] == note.data["actor"]
740 assert delete.data["object"] == note.data["object"]["id"]
742 assert Repo.get(Activity, delete.id) != nil
744 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
747 test "decrements user note count only for public activities" do
748 user = insert(:user, info: %{note_count: 10})
751 CommonAPI.post(Repo.get(User, user.id), %{"status" => "yeah", "visibility" => "public"})
754 CommonAPI.post(Repo.get(User, user.id), %{"status" => "yeah", "visibility" => "unlisted"})
757 CommonAPI.post(Repo.get(User, user.id), %{"status" => "yeah", "visibility" => "private"})
760 CommonAPI.post(Repo.get(User, user.id), %{"status" => "yeah", "visibility" => "direct"})
762 {:ok, _} = a1.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
763 {:ok, _} = a2.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
764 {:ok, _} = a3.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
765 {:ok, _} = a4.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
767 user = Repo.get(User, user.id)
768 assert user.info.note_count == 10
771 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
773 note = insert(:note_activity)
776 Object.get_by_ap_id(note.data["object"]["id"])
779 "actor" => note.data["object"]["actor"],
780 "id" => note.data["object"]["id"],
781 "to" => [user.ap_id],
785 |> Object.update_and_set_cache()
787 {:ok, delete} = ActivityPub.delete(object)
789 assert user.ap_id in delete.data["to"]
792 test "decreases reply count" do
794 user2 = insert(:user)
796 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
797 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
798 ap_id = activity.data["id"]
800 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
801 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
802 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
803 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
805 _ = CommonAPI.delete(direct_reply.id, user2)
806 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
807 assert data["object"]["repliesCount"] == 2
808 assert object.data["repliesCount"] == 2
810 _ = CommonAPI.delete(private_reply.id, user2)
811 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
812 assert data["object"]["repliesCount"] == 2
813 assert object.data["repliesCount"] == 2
815 _ = CommonAPI.delete(public_reply.id, user2)
816 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
817 assert data["object"]["repliesCount"] == 1
818 assert object.data["repliesCount"] == 1
820 _ = CommonAPI.delete(unlisted_reply.id, user2)
821 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
822 assert data["object"]["repliesCount"] == 0
823 assert object.data["repliesCount"] == 0
827 describe "timeline post-processing" do
828 test "it filters broken threads" do
829 user1 = insert(:user)
830 user2 = insert(:user)
831 user3 = insert(:user)
833 {:ok, user1} = User.follow(user1, user3)
834 assert User.following?(user1, user3)
836 {:ok, user2} = User.follow(user2, user3)
837 assert User.following?(user2, user3)
839 {:ok, user3} = User.follow(user3, user2)
840 assert User.following?(user3, user2)
842 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
844 {:ok, private_activity_1} =
845 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
847 {:ok, private_activity_2} =
848 CommonAPI.post(user2, %{
850 "visibility" => "private",
851 "in_reply_to_status_id" => private_activity_1.id
854 {:ok, private_activity_3} =
855 CommonAPI.post(user3, %{
857 "visibility" => "private",
858 "in_reply_to_status_id" => private_activity_2.id
861 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
863 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
864 assert [public_activity, private_activity_1, private_activity_3] == activities
865 assert length(activities) == 3
867 activities = ActivityPub.contain_timeline(activities, user1)
869 assert [public_activity, private_activity_1] == activities
870 assert length(activities) == 2
874 test "it can fetch plume articles" do
876 ActivityPub.fetch_object_from_id(
877 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
884 test "it creates an update activity with the new user data" do
886 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
887 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
890 ActivityPub.update(%{
891 actor: user_data["id"],
892 to: [user.follower_address],
897 assert update.data["actor"] == user.ap_id
898 assert update.data["to"] == [user.follower_address]
899 assert update.data["object"]["id"] == user_data["id"]
900 assert update.data["object"]["type"] == user_data["type"]
904 test "it can fetch peertube videos" do
906 ActivityPub.fetch_object_from_id(
907 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
913 test "returned pinned statuses" do
914 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
917 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
918 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
919 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
921 CommonAPI.pin(activity_one.id, user)
922 user = refresh_record(user)
924 CommonAPI.pin(activity_two.id, user)
925 user = refresh_record(user)
927 CommonAPI.pin(activity_three.id, user)
928 user = refresh_record(user)
930 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
932 assert 3 = length(activities)
935 test "it can create a Flag activity" do
936 reporter = insert(:user)
937 target_account = insert(:user)
938 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
939 context = Utils.generate_context_id()
942 reporter_ap_id = reporter.ap_id
943 target_ap_id = target_account.ap_id
944 activity_ap_id = activity.data["id"]
946 assert {:ok, activity} =
950 account: target_account,
951 statuses: [activity],
956 actor: ^reporter_ap_id,
959 "content" => ^content,
960 "context" => ^context,
961 "object" => [^target_ap_id, ^activity_ap_id]
966 describe "publish_one/1" do
967 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
971 actor = insert(:user)
972 inbox = "http://200.site/users/nick1/inbox"
974 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
976 assert called(Instances.set_reachable(inbox))
979 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
983 actor = insert(:user)
984 inbox = "http://200.site/users/nick1/inbox"
987 ActivityPub.publish_one(%{
992 unreachable_since: NaiveDateTime.utc_now()
995 assert called(Instances.set_reachable(inbox))
998 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1002 actor = insert(:user)
1003 inbox = "http://200.site/users/nick1/inbox"
1006 ActivityPub.publish_one(%{
1011 unreachable_since: nil
1014 refute called(Instances.set_reachable(inbox))
1017 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1021 actor = insert(:user)
1022 inbox = "http://404.site/users/nick1/inbox"
1024 assert {:error, _} =
1025 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1027 assert called(Instances.set_unreachable(inbox))
1030 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1034 actor = insert(:user)
1035 inbox = "http://connrefused.site/users/nick1/inbox"
1037 assert {:error, _} =
1038 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1040 assert called(Instances.set_unreachable(inbox))
1043 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1047 actor = insert(:user)
1048 inbox = "http://200.site/users/nick1/inbox"
1050 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1052 refute called(Instances.set_unreachable(inbox))
1055 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1059 actor = insert(:user)
1060 inbox = "http://connrefused.site/users/nick1/inbox"
1062 assert {:error, _} =
1063 ActivityPub.publish_one(%{
1068 unreachable_since: NaiveDateTime.utc_now()
1071 refute called(Instances.set_unreachable(inbox))
1076 File.read!("test/fixtures/avatar_data_uri")