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(User.get_by_id(user.id), %{"status" => "1", "visibility" => "public"})
224 CommonAPI.post(User.get_by_id(user.id), %{"status" => "2", "visibility" => "unlisted"})
227 CommonAPI.post(User.get_by_id(user.id), %{"status" => "2", "visibility" => "private"})
230 CommonAPI.post(User.get_by_id(user.id), %{"status" => "3", "visibility" => "direct"})
232 user = User.get_by_id(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 = Activity.get_by_id(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 = Activity.get_by_id(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 Activity.get_by_id(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 Activity.get_by_id(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 "fetch the latest Follow" do
639 test "fetches the latest Follow activity" do
640 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
641 follower = Repo.get_by(User, ap_id: activity.data["actor"])
642 followed = Repo.get_by(User, ap_id: activity.data["object"])
644 assert activity == Utils.fetch_latest_follow(follower, followed)
648 describe "fetching an object" do
649 test "it fetches an object" do
651 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
653 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
654 assert activity.data["id"]
656 {:ok, object_again} =
657 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
659 assert [attachment] = object.data["attachment"]
660 assert is_list(attachment["url"])
662 assert object == object_again
665 test "it works with objects only available via Ostatus" do
666 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
667 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
668 assert activity.data["id"]
670 {:ok, object_again} =
671 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
673 assert object == object_again
676 test "it correctly stitches up conversations between ostatus and ap" do
677 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
678 {:ok, object} = ActivityPub.fetch_object_from_id(last)
680 object = Object.get_by_ap_id(object.data["inReplyTo"])
685 describe "following / unfollowing" do
686 test "creates a follow activity" do
687 follower = insert(:user)
688 followed = insert(:user)
690 {:ok, activity} = ActivityPub.follow(follower, followed)
691 assert activity.data["type"] == "Follow"
692 assert activity.data["actor"] == follower.ap_id
693 assert activity.data["object"] == followed.ap_id
696 test "creates an undo activity for the last follow" do
697 follower = insert(:user)
698 followed = insert(:user)
700 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
701 {:ok, activity} = ActivityPub.unfollow(follower, followed)
703 assert activity.data["type"] == "Undo"
704 assert activity.data["actor"] == follower.ap_id
706 assert is_map(activity.data["object"])
707 assert activity.data["object"]["type"] == "Follow"
708 assert activity.data["object"]["object"] == followed.ap_id
709 assert activity.data["object"]["id"] == follow_activity.data["id"]
713 describe "blocking / unblocking" do
714 test "creates a block activity" do
715 blocker = insert(:user)
716 blocked = insert(:user)
718 {:ok, activity} = ActivityPub.block(blocker, blocked)
720 assert activity.data["type"] == "Block"
721 assert activity.data["actor"] == blocker.ap_id
722 assert activity.data["object"] == blocked.ap_id
725 test "creates an undo activity for the last block" do
726 blocker = insert(:user)
727 blocked = insert(:user)
729 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
730 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
732 assert activity.data["type"] == "Undo"
733 assert activity.data["actor"] == blocker.ap_id
735 assert is_map(activity.data["object"])
736 assert activity.data["object"]["type"] == "Block"
737 assert activity.data["object"]["object"] == blocked.ap_id
738 assert activity.data["object"]["id"] == block_activity.data["id"]
742 describe "deletion" do
743 test "it creates a delete activity and deletes the original object" do
744 note = insert(:note_activity)
745 object = Object.get_by_ap_id(note.data["object"]["id"])
746 {:ok, delete} = ActivityPub.delete(object)
748 assert delete.data["type"] == "Delete"
749 assert delete.data["actor"] == note.data["actor"]
750 assert delete.data["object"] == note.data["object"]["id"]
752 assert Activity.get_by_id(delete.id) != nil
754 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
757 test "decrements user note count only for public activities" do
758 user = insert(:user, info: %{note_count: 10})
761 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "public"})
764 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "unlisted"})
767 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "private"})
770 CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "direct"})
772 {:ok, _} = a1.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
773 {:ok, _} = a2.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
774 {:ok, _} = a3.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
775 {:ok, _} = a4.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
777 user = User.get_by_id(user.id)
778 assert user.info.note_count == 10
781 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
783 note = insert(:note_activity)
786 Object.get_by_ap_id(note.data["object"]["id"])
789 "actor" => note.data["object"]["actor"],
790 "id" => note.data["object"]["id"],
791 "to" => [user.ap_id],
795 |> Object.update_and_set_cache()
797 {:ok, delete} = ActivityPub.delete(object)
799 assert user.ap_id in delete.data["to"]
802 test "decreases reply count" do
804 user2 = insert(:user)
806 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
807 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
808 ap_id = activity.data["id"]
810 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
811 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
812 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
813 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
815 _ = CommonAPI.delete(direct_reply.id, user2)
816 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
817 assert data["object"]["repliesCount"] == 2
818 assert object.data["repliesCount"] == 2
820 _ = CommonAPI.delete(private_reply.id, user2)
821 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
822 assert data["object"]["repliesCount"] == 2
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 data["object"]["repliesCount"] == 1
828 assert object.data["repliesCount"] == 1
830 _ = CommonAPI.delete(unlisted_reply.id, user2)
831 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
832 assert data["object"]["repliesCount"] == 0
833 assert object.data["repliesCount"] == 0
837 describe "timeline post-processing" do
838 test "it filters broken threads" do
839 user1 = insert(:user)
840 user2 = insert(:user)
841 user3 = insert(:user)
843 {:ok, user1} = User.follow(user1, user3)
844 assert User.following?(user1, user3)
846 {:ok, user2} = User.follow(user2, user3)
847 assert User.following?(user2, user3)
849 {:ok, user3} = User.follow(user3, user2)
850 assert User.following?(user3, user2)
852 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
854 {:ok, private_activity_1} =
855 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
857 {:ok, private_activity_2} =
858 CommonAPI.post(user2, %{
860 "visibility" => "private",
861 "in_reply_to_status_id" => private_activity_1.id
864 {:ok, private_activity_3} =
865 CommonAPI.post(user3, %{
867 "visibility" => "private",
868 "in_reply_to_status_id" => private_activity_2.id
871 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
873 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
874 assert [public_activity, private_activity_1, private_activity_3] == activities
875 assert length(activities) == 3
877 activities = ActivityPub.contain_timeline(activities, user1)
879 assert [public_activity, private_activity_1] == activities
880 assert length(activities) == 2
884 test "it can fetch plume articles" do
886 ActivityPub.fetch_object_from_id(
887 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
894 test "it creates an update activity with the new user data" do
896 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
897 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
900 ActivityPub.update(%{
901 actor: user_data["id"],
902 to: [user.follower_address],
907 assert update.data["actor"] == user.ap_id
908 assert update.data["to"] == [user.follower_address]
909 assert update.data["object"]["id"] == user_data["id"]
910 assert update.data["object"]["type"] == user_data["type"]
914 test "it can fetch peertube videos" do
916 ActivityPub.fetch_object_from_id(
917 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
923 test "returned pinned statuses" do
924 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
927 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
928 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
929 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
931 CommonAPI.pin(activity_one.id, user)
932 user = refresh_record(user)
934 CommonAPI.pin(activity_two.id, user)
935 user = refresh_record(user)
937 CommonAPI.pin(activity_three.id, user)
938 user = refresh_record(user)
940 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
942 assert 3 = length(activities)
945 test "it can create a Flag activity" do
946 reporter = insert(:user)
947 target_account = insert(:user)
948 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
949 context = Utils.generate_context_id()
952 reporter_ap_id = reporter.ap_id
953 target_ap_id = target_account.ap_id
954 activity_ap_id = activity.data["id"]
956 assert {:ok, activity} =
960 account: target_account,
961 statuses: [activity],
966 actor: ^reporter_ap_id,
969 "content" => ^content,
970 "context" => ^context,
971 "object" => [^target_ap_id, ^activity_ap_id]
976 describe "publish_one/1" do
977 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
981 actor = insert(:user)
982 inbox = "http://200.site/users/nick1/inbox"
984 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
986 assert called(Instances.set_reachable(inbox))
989 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
993 actor = insert(:user)
994 inbox = "http://200.site/users/nick1/inbox"
997 ActivityPub.publish_one(%{
1002 unreachable_since: NaiveDateTime.utc_now()
1005 assert called(Instances.set_reachable(inbox))
1008 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
1012 actor = insert(:user)
1013 inbox = "http://200.site/users/nick1/inbox"
1016 ActivityPub.publish_one(%{
1021 unreachable_since: nil
1024 refute called(Instances.set_reachable(inbox))
1027 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1031 actor = insert(:user)
1032 inbox = "http://404.site/users/nick1/inbox"
1034 assert {:error, _} =
1035 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1037 assert called(Instances.set_unreachable(inbox))
1040 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1044 actor = insert(:user)
1045 inbox = "http://connrefused.site/users/nick1/inbox"
1047 assert {:error, _} =
1048 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1050 assert called(Instances.set_unreachable(inbox))
1053 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1057 actor = insert(:user)
1058 inbox = "http://200.site/users/nick1/inbox"
1060 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1062 refute called(Instances.set_unreachable(inbox))
1065 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1069 actor = insert(:user)
1070 inbox = "http://connrefused.site/users/nick1/inbox"
1072 assert {:error, _} =
1073 ActivityPub.publish_one(%{
1078 unreachable_since: NaiveDateTime.utc_now()
1081 refute called(Instances.set_unreachable(inbox))
1086 File.read!("test/fixtures/avatar_data_uri")