1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.CommonAPITest do
6 use Oban.Testing, repo: Pleroma.Repo
11 alias Pleroma.Conversation.Participation
12 alias Pleroma.Notification
16 alias Pleroma.Web.ActivityPub.ActivityPub
17 alias Pleroma.Web.ActivityPub.Transmogrifier
18 alias Pleroma.Web.ActivityPub.Visibility
19 alias Pleroma.Web.AdminAPI.AccountView
20 alias Pleroma.Web.CommonAPI
22 import Pleroma.Factory
24 import Ecto.Query, only: [from: 2]
26 require Pleroma.Constants
29 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
33 setup do: clear_config([:instance, :safe_dm_mentions])
34 setup do: clear_config([:instance, :limit])
35 setup do: clear_config([:instance, :max_pinned_statuses])
37 describe "posting polls" do
38 test "it posts a poll" do
42 CommonAPI.post(user, %{
43 status: "who is the best",
44 poll: %{expires_in: 600, options: ["reimu", "marisa"]}
47 object = Object.normalize(activity, fetch: false)
49 assert object.data["type"] == "Question"
50 assert object.data["oneOf"] |> length() == 2
54 describe "blocking" do
56 blocker = insert(:user)
57 blocked = insert(:user)
58 User.follow(blocker, blocked)
59 User.follow(blocked, blocker)
60 %{blocker: blocker, blocked: blocked}
63 test "it blocks and federates", %{blocker: blocker, blocked: blocked} do
64 clear_config([:instance, :federating], true)
66 with_mock Pleroma.Web.Federator,
67 publish: fn _ -> nil end do
68 assert {:ok, block} = CommonAPI.block(blocker, blocked)
71 assert User.blocks?(blocker, blocked)
72 refute User.following?(blocker, blocked)
73 refute User.following?(blocked, blocker)
75 assert called(Pleroma.Web.Federator.publish(block))
79 test "it blocks and does not federate if outgoing blocks are disabled", %{
83 clear_config([:instance, :federating], true)
84 clear_config([:activitypub, :outgoing_blocks], false)
86 with_mock Pleroma.Web.Federator,
87 publish: fn _ -> nil end do
88 assert {:ok, block} = CommonAPI.block(blocker, blocked)
91 assert User.blocks?(blocker, blocked)
92 refute User.following?(blocker, blocked)
93 refute User.following?(blocked, blocker)
95 refute called(Pleroma.Web.Federator.publish(block))
100 describe "posting chat messages" do
101 setup do: clear_config([:instance, :chat_limit])
103 test "it posts a self-chat" do
104 author = insert(:user)
108 CommonAPI.post_chat_message(
111 "remember to buy milk when milk truk arive"
114 assert activity.data["type"] == "Create"
117 test "it posts a chat message without content but with an attachment" do
118 author = insert(:user)
119 recipient = insert(:user)
122 content_type: "image/jpeg",
123 path: Path.absname("test/fixtures/image.jpg"),
124 filename: "an_image.jpg"
127 {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id)
131 Pleroma.Web.Streamer,
143 send: fn _ -> nil end
148 CommonAPI.post_chat_message(
156 Notification.for_user_and_activity(recipient, activity)
157 |> Repo.preload(:activity)
159 assert called(Pleroma.Web.Push.send(notification))
160 assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
161 assert called(Pleroma.Web.Streamer.stream(["user", "user:pleroma_chat"], :_))
167 test "it adds html newlines" do
168 author = insert(:user)
169 recipient = insert(:user)
171 other_user = insert(:user)
174 CommonAPI.post_chat_message(
180 assert other_user.ap_id not in activity.recipients
182 object = Object.normalize(activity, fetch: false)
184 assert object.data["content"] == "uguu<br/>uguuu"
187 test "it linkifies" do
188 author = insert(:user)
189 recipient = insert(:user)
191 other_user = insert(:user)
194 CommonAPI.post_chat_message(
197 "https://example.org is the site of @#{other_user.nickname} #2hu"
200 assert other_user.ap_id not in activity.recipients
202 object = Object.normalize(activity, fetch: false)
204 assert object.data["content"] ==
205 "<a href=\"https://example.org\" rel=\"ugc\">https://example.org</a> is the site of <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{
207 }\" href=\"#{other_user.ap_id}\" rel=\"ugc\">@<span>#{other_user.nickname}</span></a></span> <a class=\"hashtag\" data-tag=\"2hu\" href=\"http://localhost:4001/tag/2hu\">#2hu</a>"
210 test "it posts a chat message" do
211 author = insert(:user)
212 recipient = insert(:user)
215 CommonAPI.post_chat_message(
218 "a test message <script>alert('uuu')</script> :firefox:"
221 assert activity.data["type"] == "Create"
222 assert activity.local
223 object = Object.normalize(activity, fetch: false)
225 assert object.data["type"] == "ChatMessage"
226 assert object.data["to"] == [recipient.ap_id]
228 assert object.data["content"] ==
229 "a test message <script>alert('uuu')</script> :firefox:"
231 assert object.data["emoji"] == %{
232 "firefox" => "http://localhost:4001/emoji/Firefox.gif"
235 assert Chat.get(author.id, recipient.ap_id)
236 assert Chat.get(recipient.id, author.ap_id)
238 assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
241 test "it reject messages over the local limit" do
242 clear_config([:instance, :chat_limit], 2)
244 author = insert(:user)
245 recipient = insert(:user)
248 CommonAPI.post_chat_message(
254 assert message == :content_too_long
257 test "it reject messages via MRF" do
258 clear_config([:mrf_keyword, :reject], ["GNO"])
259 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
261 author = insert(:user)
262 recipient = insert(:user)
264 assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
265 CommonAPI.post_chat_message(author, recipient, "GNO/Linux")
269 describe "unblocking" do
270 test "it works even without an existing block activity" do
271 blocked = insert(:user)
272 blocker = insert(:user)
273 User.block(blocker, blocked)
275 assert User.blocks?(blocker, blocked)
276 assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
277 refute User.blocks?(blocker, blocked)
281 describe "deletion" do
282 test "it works with pruned objects" do
285 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
287 clear_config([:instance, :federating], true)
289 Object.normalize(post, fetch: false)
292 with_mock Pleroma.Web.Federator,
293 publish: fn _ -> nil end do
294 assert {:ok, delete} = CommonAPI.delete(post.id, user)
296 assert called(Pleroma.Web.Federator.publish(delete))
299 refute Activity.get_by_id(post.id)
302 test "it allows users to delete their posts" do
305 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
307 clear_config([:instance, :federating], true)
309 with_mock Pleroma.Web.Federator,
310 publish: fn _ -> nil end do
311 assert {:ok, delete} = CommonAPI.delete(post.id, user)
313 assert called(Pleroma.Web.Federator.publish(delete))
316 refute Activity.get_by_id(post.id)
319 test "it does not allow a user to delete their posts" do
321 other_user = insert(:user)
323 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
325 assert {:error, "Could not delete"} = CommonAPI.delete(post.id, other_user)
326 assert Activity.get_by_id(post.id)
329 test "it allows moderators to delete other user's posts" do
331 moderator = insert(:user, is_moderator: true)
333 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
335 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
338 refute Activity.get_by_id(post.id)
341 test "it allows admins to delete other user's posts" do
343 moderator = insert(:user, is_admin: true)
345 {:ok, post} = CommonAPI.post(user, %{status: "namu amida butsu"})
347 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
350 refute Activity.get_by_id(post.id)
353 test "superusers deleting non-local posts won't federate the delete" do
354 # This is the user of the ingested activity
358 ap_id: "http://mastodon.example.org/users/admin",
359 last_refreshed_at: NaiveDateTime.utc_now()
362 moderator = insert(:user, is_admin: true)
365 File.read!("test/fixtures/mastodon-post-activity.json")
368 {:ok, post} = Transmogrifier.handle_incoming(data)
370 with_mock Pleroma.Web.Federator,
371 publish: fn _ -> nil end do
372 assert {:ok, delete} = CommonAPI.delete(post.id, moderator)
374 refute called(Pleroma.Web.Federator.publish(:_))
377 refute Activity.get_by_id(post.id)
381 test "favoriting race condition" do
383 users_serial = insert_list(10, :user)
384 users = insert_list(10, :user)
386 {:ok, activity} = CommonAPI.post(user, %{status: "."})
389 |> Enum.map(fn user ->
390 CommonAPI.favorite(user, activity.id)
393 object = Object.get_by_ap_id(activity.data["object"])
394 assert object.data["like_count"] == 10
397 |> Enum.map(fn user ->
399 CommonAPI.favorite(user, activity.id)
402 |> Enum.map(&Task.await/1)
404 object = Object.get_by_ap_id(activity.data["object"])
405 assert object.data["like_count"] == 20
408 test "repeating race condition" do
410 users_serial = insert_list(10, :user)
411 users = insert_list(10, :user)
413 {:ok, activity} = CommonAPI.post(user, %{status: "."})
416 |> Enum.map(fn user ->
417 CommonAPI.repeat(activity.id, user)
420 object = Object.get_by_ap_id(activity.data["object"])
421 assert object.data["announcement_count"] == 10
424 |> Enum.map(fn user ->
426 CommonAPI.repeat(activity.id, user)
429 |> Enum.map(&Task.await/1)
431 object = Object.get_by_ap_id(activity.data["object"])
432 assert object.data["announcement_count"] == 20
435 test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
437 {:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "direct"})
439 [participation] = Participation.for_user(user)
442 CommonAPI.post(user, %{status: ".", in_reply_to_conversation_id: participation.id})
444 assert Visibility.is_direct?(convo_reply)
446 assert activity.data["context"] == convo_reply.data["context"]
449 test "when replying to a conversation / participation, it only mentions the recipients explicitly declared in the participation" do
451 jafnhar = insert(:user)
452 tridi = insert(:user)
455 CommonAPI.post(har, %{
456 status: "@#{jafnhar.nickname} hey",
460 assert har.ap_id in activity.recipients
461 assert jafnhar.ap_id in activity.recipients
463 [participation] = Participation.for_user(har)
466 CommonAPI.post(har, %{
467 status: "I don't really like @#{tridi.nickname}",
468 visibility: "direct",
469 in_reply_to_status_id: activity.id,
470 in_reply_to_conversation_id: participation.id
473 assert har.ap_id in activity.recipients
474 assert jafnhar.ap_id in activity.recipients
475 refute tridi.ap_id in activity.recipients
478 test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do
480 jafnhar = insert(:user)
481 tridi = insert(:user)
483 clear_config([:instance, :safe_dm_mentions], true)
486 CommonAPI.post(har, %{
487 status: "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again",
491 refute tridi.ap_id in activity.recipients
492 assert jafnhar.ap_id in activity.recipients
495 test "it de-duplicates tags" do
497 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU"})
499 object = Object.normalize(activity, fetch: false)
501 assert Object.tags(object) == ["2hu"]
504 test "it adds emoji in the object" do
506 {:ok, activity} = CommonAPI.post(user, %{status: ":firefox:"})
508 assert Object.normalize(activity, fetch: false).data["emoji"]["firefox"]
511 describe "posting" do
512 test "it adds an emoji on an external site" do
514 {:ok, activity} = CommonAPI.post(user, %{status: "hey :external_emoji:"})
516 assert %{"external_emoji" => url} = Object.normalize(activity).data["emoji"]
517 assert url == "https://example.com/emoji.png"
519 {:ok, activity} = CommonAPI.post(user, %{status: "hey :blank:"})
521 assert %{"blank" => url} = Object.normalize(activity).data["emoji"]
522 assert url == "#{Pleroma.Web.base_url()}/emoji/blank.png"
525 test "it copies emoji from the subject of the parent post" do
528 Object.normalize("https://patch.cx/objects/a399c28e-c821-4820-bc3e-4afeb044c16f",
532 activity = Activity.get_create_by_object_ap_id(object.data["id"])
535 {:ok, reply_activity} =
536 CommonAPI.post(user, %{
537 in_reply_to_id: activity.id,
538 status: ":joker_disapprove:",
539 spoiler_text: ":joker_smile:"
542 assert Object.normalize(reply_activity).data["emoji"][":joker_smile:"]
543 refute Object.normalize(reply_activity).data["emoji"][":joker_disapprove:"]
546 test "deactivated users can't post" do
547 user = insert(:user, is_active: false)
548 assert {:error, _} = CommonAPI.post(user, %{status: "ye"})
551 test "it supports explicit addressing" do
553 user_two = insert(:user)
554 user_three = insert(:user)
555 user_four = insert(:user)
558 CommonAPI.post(user, %{
560 "Hey, I think @#{user_three.nickname} is ugly. @#{user_four.nickname} is alright though.",
561 to: [user_two.nickname, user_four.nickname, "nonexistent"]
564 assert user.ap_id in activity.recipients
565 assert user_two.ap_id in activity.recipients
566 assert user_four.ap_id in activity.recipients
567 refute user_three.ap_id in activity.recipients
570 test "it filters out obviously bad tags when accepting a post as HTML" do
573 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
576 CommonAPI.post(user, %{
578 content_type: "text/html"
581 object = Object.normalize(activity, fetch: false)
583 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
584 assert object.data["source"] == post
587 test "it filters out obviously bad tags when accepting a post as Markdown" do
590 post = "<p><b>2hu</b></p><script>alert('xss')</script>"
593 CommonAPI.post(user, %{
595 content_type: "text/markdown"
598 object = Object.normalize(activity, fetch: false)
600 assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"
601 assert object.data["source"] == post
604 test "it does not allow replies to direct messages that are not direct messages themselves" do
607 {:ok, activity} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
610 CommonAPI.post(user, %{
612 visibility: "direct",
613 in_reply_to_status_id: activity.id
616 Enum.each(["public", "private", "unlisted"], fn visibility ->
617 assert {:error, "The message visibility must be direct"} =
618 CommonAPI.post(user, %{
620 visibility: visibility,
621 in_reply_to_status_id: activity.id
626 test "replying with a direct message will NOT auto-add the author of the reply to the recipient list" do
628 other_user = insert(:user)
629 third_user = insert(:user)
631 {:ok, post} = CommonAPI.post(user, %{status: "I'm stupid"})
634 CommonAPI.post(other_user, %{status: "No ur smart", in_reply_to_status_id: post.id})
636 # The OP is implicitly added
637 assert user.ap_id in open_answer.recipients
639 {:ok, secret_answer} =
640 CommonAPI.post(other_user, %{
641 status: "lol, that guy really is stupid, right, @#{third_user.nickname}?",
642 in_reply_to_status_id: post.id,
646 assert third_user.ap_id in secret_answer.recipients
648 # The OP is not added
649 refute user.ap_id in secret_answer.recipients
652 test "it allows to address a list" do
654 {:ok, list} = Pleroma.List.create("foo", user)
656 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", visibility: "list:#{list.id}"})
658 assert activity.data["bcc"] == [list.ap_id]
659 assert activity.recipients == [list.ap_id, user.ap_id]
660 assert activity.data["listMessage"] == list.ap_id
663 test "it returns error when status is empty and no attachments" do
666 assert {:error, "Cannot post an empty status without attachments"} =
667 CommonAPI.post(user, %{status: ""})
670 test "it validates character limits are correctly enforced" do
671 clear_config([:instance, :limit], 5)
675 assert {:error, "The status is over the character limit"} =
676 CommonAPI.post(user, %{status: "foobar"})
678 assert {:ok, _activity} = CommonAPI.post(user, %{status: "12345"})
681 test "it can handle activities that expire" do
684 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
686 assert {:ok, activity} = CommonAPI.post(user, %{status: "chai", expires_in: 1_000_000})
689 worker: Pleroma.Workers.PurgeExpiredActivity,
690 args: %{activity_id: activity.id},
691 scheduled_at: expires_at
696 describe "reactions" do
697 test "reacting to a status with an emoji" do
699 other_user = insert(:user)
701 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
703 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
705 assert reaction.data["actor"] == user.ap_id
706 assert reaction.data["content"] == "👍"
708 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
710 {:error, _} = CommonAPI.react_with_emoji(activity.id, user, ".")
713 test "unreacting to a status with an emoji" do
715 other_user = insert(:user)
717 clear_config([:instance, :federating], true)
719 with_mock Pleroma.Web.Federator,
720 publish: fn _ -> nil end do
721 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
722 {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
724 {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
726 assert unreaction.data["type"] == "Undo"
727 assert unreaction.data["object"] == reaction.data["id"]
728 assert unreaction.local
730 # On federation, it contains the undone (and deleted) object
731 unreaction_with_object = %{
733 | data: Map.put(unreaction.data, "object", reaction.data)
736 assert called(Pleroma.Web.Federator.publish(unreaction_with_object))
740 test "repeating a status" do
742 other_user = insert(:user)
744 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
746 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, user)
747 assert Visibility.is_public?(announce_activity)
750 test "can't repeat a repeat" do
752 other_user = insert(:user)
753 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
755 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, other_user)
757 refute match?({:ok, %Activity{}}, CommonAPI.repeat(announce.id, user))
760 test "repeating a status privately" do
762 other_user = insert(:user)
764 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
766 {:ok, %Activity{} = announce_activity} =
767 CommonAPI.repeat(activity.id, user, %{visibility: "private"})
769 assert Visibility.is_private?(announce_activity)
770 refute Visibility.visible_for_user?(announce_activity, nil)
773 test "author can repeat own private statuses" do
774 author = insert(:user)
775 follower = insert(:user)
776 CommonAPI.follow(follower, author)
778 {:ok, activity} = CommonAPI.post(author, %{status: "cofe", visibility: "private"})
780 {:ok, %Activity{} = announce_activity} = CommonAPI.repeat(activity.id, author)
782 assert Visibility.is_private?(announce_activity)
783 refute Visibility.visible_for_user?(announce_activity, nil)
785 assert Visibility.visible_for_user?(activity, follower)
786 assert {:error, :not_found} = CommonAPI.repeat(activity.id, follower)
789 test "favoriting a status" do
791 other_user = insert(:user)
793 {:ok, post_activity} = CommonAPI.post(other_user, %{status: "cofe"})
795 {:ok, %Activity{data: data}} = CommonAPI.favorite(user, post_activity.id)
796 assert data["type"] == "Like"
797 assert data["actor"] == user.ap_id
798 assert data["object"] == post_activity.data["object"]
801 test "retweeting a status twice returns the status" do
803 other_user = insert(:user)
805 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
806 {:ok, %Activity{} = announce} = CommonAPI.repeat(activity.id, user)
807 {:ok, ^announce} = CommonAPI.repeat(activity.id, user)
810 test "favoriting a status twice returns ok, but without the like activity" do
812 other_user = insert(:user)
814 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"})
815 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
816 assert {:ok, :already_liked} = CommonAPI.favorite(user, activity.id)
820 describe "pinned statuses" do
822 clear_config([:instance, :max_pinned_statuses], 1)
825 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
827 [user: user, activity: activity]
830 test "pin status", %{user: user, activity: activity} do
831 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
834 user = refresh_record(user)
836 assert %User{pinned_activities: [^id]} = user
839 test "pin poll", %{user: user} do
841 CommonAPI.post(user, %{
842 status: "How is fediverse today?",
843 poll: %{options: ["Absolutely outstanding", "Not good"], expires_in: 20}
846 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
849 user = refresh_record(user)
851 assert %User{pinned_activities: [^id]} = user
854 test "unlisted statuses can be pinned", %{user: user} do
855 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!", visibility: "unlisted"})
856 assert {:ok, ^activity} = CommonAPI.pin(activity.id, user)
859 test "only self-authored can be pinned", %{activity: activity} do
862 assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user)
865 test "max pinned statuses", %{user: user, activity: activity_one} do
866 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
868 assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user)
870 user = refresh_record(user)
872 assert {:error, "You have already pinned the maximum number of statuses"} =
873 CommonAPI.pin(activity_two.id, user)
876 test "unpin status", %{user: user, activity: activity} do
877 {:ok, activity} = CommonAPI.pin(activity.id, user)
879 user = refresh_record(user)
883 assert match?({:ok, %{id: ^id}}, CommonAPI.unpin(activity.id, user))
885 user = refresh_record(user)
887 assert %User{pinned_activities: []} = user
890 test "should unpin when deleting a status", %{user: user, activity: activity} do
891 {:ok, activity} = CommonAPI.pin(activity.id, user)
893 user = refresh_record(user)
895 assert {:ok, _} = CommonAPI.delete(activity.id, user)
897 user = refresh_record(user)
899 assert %User{pinned_activities: []} = user
903 describe "mute tests" do
907 activity = insert(:note_activity)
909 [user: user, activity: activity]
912 test "marks notifications as read after mute" do
913 author = insert(:user)
914 activity = insert(:note_activity, user: author)
916 friend1 = insert(:user)
917 friend2 = insert(:user)
919 {:ok, reply_activity} =
923 status: "@#{author.nickname} @#{friend1.nickname} test reply",
924 in_reply_to_status_id: activity.id
928 {:ok, favorite_activity} = CommonAPI.favorite(friend2, activity.id)
929 {:ok, repeat_activity} = CommonAPI.repeat(activity.id, friend1)
931 assert Repo.aggregate(
932 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
936 unread_notifications =
937 Repo.all(from(n in Notification, where: n.seen == false, where: n.user_id == ^author.id))
939 assert Enum.any?(unread_notifications, fn n ->
940 n.type == "favourite" && n.activity_id == favorite_activity.id
943 assert Enum.any?(unread_notifications, fn n ->
944 n.type == "reblog" && n.activity_id == repeat_activity.id
947 assert Enum.any?(unread_notifications, fn n ->
948 n.type == "mention" && n.activity_id == reply_activity.id
951 {:ok, _} = CommonAPI.add_mute(author, activity)
952 assert CommonAPI.thread_muted?(author, activity)
954 assert Repo.aggregate(
955 from(n in Notification, where: n.seen == false and n.user_id == ^friend1.id),
960 Repo.all(from(n in Notification, where: n.seen == true, where: n.user_id == ^author.id))
962 assert Enum.any?(read_notifications, fn n ->
963 n.type == "favourite" && n.activity_id == favorite_activity.id
966 assert Enum.any?(read_notifications, fn n ->
967 n.type == "reblog" && n.activity_id == repeat_activity.id
970 assert Enum.any?(read_notifications, fn n ->
971 n.type == "mention" && n.activity_id == reply_activity.id
975 test "add mute", %{user: user, activity: activity} do
976 {:ok, _} = CommonAPI.add_mute(user, activity)
977 assert CommonAPI.thread_muted?(user, activity)
980 test "add expiring mute", %{user: user, activity: activity} do
981 {:ok, _} = CommonAPI.add_mute(user, activity, %{expires_in: 60})
982 assert CommonAPI.thread_muted?(user, activity)
984 worker = Pleroma.Workers.MuteExpireWorker
985 args = %{"op" => "unmute_conversation", "user_id" => user.id, "activity_id" => activity.id}
992 assert :ok = perform_job(worker, args)
993 refute CommonAPI.thread_muted?(user, activity)
996 test "remove mute", %{user: user, activity: activity} do
997 CommonAPI.add_mute(user, activity)
998 {:ok, _} = CommonAPI.remove_mute(user, activity)
999 refute CommonAPI.thread_muted?(user, activity)
1002 test "remove mute by ids", %{user: user, activity: activity} do
1003 CommonAPI.add_mute(user, activity)
1004 {:ok, _} = CommonAPI.remove_mute(user.id, activity.id)
1005 refute CommonAPI.thread_muted?(user, activity)
1008 test "check that mutes can't be duplicate", %{user: user, activity: activity} do
1009 CommonAPI.add_mute(user, activity)
1010 {:error, _} = CommonAPI.add_mute(user, activity)
1014 describe "reports" do
1015 test "creates a report" do
1016 reporter = insert(:user)
1017 target_user = insert(:user)
1019 {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
1021 reporter_ap_id = reporter.ap_id
1022 target_ap_id = target_user.ap_id
1023 activity_ap_id = activity.data["id"]
1027 account_id: target_user.id,
1029 status_ids: [activity.id]
1034 "id" => activity_ap_id,
1035 "content" => "foobar",
1036 "published" => activity.object.data["published"],
1037 "actor" => AccountView.render("show.json", %{user: target_user})
1040 assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data)
1043 actor: ^reporter_ap_id,
1046 "content" => ^comment,
1047 "object" => [^target_ap_id, ^note_obj],
1053 test "updates report state" do
1054 [reporter, target_user] = insert_pair(:user)
1055 activity = insert(:note_activity, user: target_user)
1057 {:ok, %Activity{id: report_id}} =
1058 CommonAPI.report(reporter, %{
1059 account_id: target_user.id,
1060 comment: "I feel offended",
1061 status_ids: [activity.id]
1064 {:ok, report} = CommonAPI.update_report_state(report_id, "resolved")
1066 assert report.data["state"] == "resolved"
1068 [reported_user, activity_id] = report.data["object"]
1070 assert reported_user == target_user.ap_id
1071 assert activity_id == activity.data["id"]
1074 test "does not update report state when state is unsupported" do
1075 [reporter, target_user] = insert_pair(:user)
1076 activity = insert(:note_activity, user: target_user)
1078 {:ok, %Activity{id: report_id}} =
1079 CommonAPI.report(reporter, %{
1080 account_id: target_user.id,
1081 comment: "I feel offended",
1082 status_ids: [activity.id]
1085 assert CommonAPI.update_report_state(report_id, "test") == {:error, "Unsupported state"}
1088 test "updates state of multiple reports" do
1089 [reporter, target_user] = insert_pair(:user)
1090 activity = insert(:note_activity, user: target_user)
1092 {:ok, %Activity{id: first_report_id}} =
1093 CommonAPI.report(reporter, %{
1094 account_id: target_user.id,
1095 comment: "I feel offended",
1096 status_ids: [activity.id]
1099 {:ok, %Activity{id: second_report_id}} =
1100 CommonAPI.report(reporter, %{
1101 account_id: target_user.id,
1102 comment: "I feel very offended!",
1103 status_ids: [activity.id]
1107 CommonAPI.update_report_state([first_report_id, second_report_id], "resolved")
1109 first_report = Activity.get_by_id(first_report_id)
1110 second_report = Activity.get_by_id(second_report_id)
1112 assert report_ids -- [first_report_id, second_report_id] == []
1113 assert first_report.data["state"] == "resolved"
1114 assert second_report.data["state"] == "resolved"
1118 describe "reblog muting" do
1120 muter = insert(:user)
1122 muted = insert(:user)
1124 [muter: muter, muted: muted]
1127 test "add a reblog mute", %{muter: muter, muted: muted} do
1128 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1130 assert User.showing_reblogs?(muter, muted) == false
1133 test "remove a reblog mute", %{muter: muter, muted: muted} do
1134 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(muter, muted)
1135 {:ok, _reblog_mute} = CommonAPI.show_reblogs(muter, muted)
1137 assert User.showing_reblogs?(muter, muted) == true
1141 describe "follow/2" do
1142 test "directly follows a non-locked local user" do
1143 [follower, followed] = insert_pair(:user)
1144 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1146 assert User.following?(follower, followed)
1150 describe "unfollow/2" do
1151 test "also unsubscribes a user" do
1152 [follower, followed] = insert_pair(:user)
1153 {:ok, follower, followed, _} = CommonAPI.follow(follower, followed)
1154 {:ok, _subscription} = User.subscribe(follower, followed)
1156 assert User.subscribed_to?(follower, followed)
1158 {:ok, follower} = CommonAPI.unfollow(follower, followed)
1160 refute User.subscribed_to?(follower, followed)
1163 test "cancels a pending follow for a local user" do
1164 follower = insert(:user)
1165 followed = insert(:user, is_locked: true)
1167 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1168 CommonAPI.follow(follower, followed)
1170 assert User.get_follow_state(follower, followed) == :follow_pending
1171 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1172 assert User.get_follow_state(follower, followed) == nil
1174 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1175 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1180 "object" => %{"type" => "Follow", "state" => "cancelled"}
1182 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1185 test "cancels a pending follow for a remote user" do
1186 follower = insert(:user)
1187 followed = insert(:user, is_locked: true, local: false, ap_enabled: true)
1189 assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
1190 CommonAPI.follow(follower, followed)
1192 assert User.get_follow_state(follower, followed) == :follow_pending
1193 assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
1194 assert User.get_follow_state(follower, followed) == nil
1196 assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
1197 Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
1202 "object" => %{"type" => "Follow", "state" => "cancelled"}
1204 } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
1208 describe "accept_follow_request/2" do
1209 test "after acceptance, it sets all existing pending follow request states to 'accept'" do
1210 user = insert(:user, is_locked: true)
1211 follower = insert(:user)
1212 follower_two = insert(:user)
1214 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1215 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1216 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1218 assert follow_activity.data["state"] == "pending"
1219 assert follow_activity_two.data["state"] == "pending"
1220 assert follow_activity_three.data["state"] == "pending"
1222 {:ok, _follower} = CommonAPI.accept_follow_request(follower, user)
1224 assert Repo.get(Activity, follow_activity.id).data["state"] == "accept"
1225 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "accept"
1226 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1229 test "after rejection, it sets all existing pending follow request states to 'reject'" do
1230 user = insert(:user, is_locked: true)
1231 follower = insert(:user)
1232 follower_two = insert(:user)
1234 {:ok, _, _, follow_activity} = CommonAPI.follow(follower, user)
1235 {:ok, _, _, follow_activity_two} = CommonAPI.follow(follower, user)
1236 {:ok, _, _, follow_activity_three} = CommonAPI.follow(follower_two, user)
1238 assert follow_activity.data["state"] == "pending"
1239 assert follow_activity_two.data["state"] == "pending"
1240 assert follow_activity_three.data["state"] == "pending"
1242 {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
1244 assert Repo.get(Activity, follow_activity.id).data["state"] == "reject"
1245 assert Repo.get(Activity, follow_activity_two.id).data["state"] == "reject"
1246 assert Repo.get(Activity, follow_activity_three.id).data["state"] == "pending"
1249 test "doesn't create a following relationship if the corresponding follow request doesn't exist" do
1250 user = insert(:user, is_locked: true)
1251 not_follower = insert(:user)
1252 CommonAPI.accept_follow_request(not_follower, user)
1254 assert Pleroma.FollowingRelationship.following?(not_follower, user) == false
1258 describe "vote/3" do
1259 test "does not allow to vote twice" do
1260 user = insert(:user)
1261 other_user = insert(:user)
1264 CommonAPI.post(user, %{
1265 status: "Am I cute?",
1266 poll: %{options: ["Yes", "No"], expires_in: 20}
1269 object = Object.normalize(activity, fetch: false)
1271 {:ok, _, object} = CommonAPI.vote(other_user, object, [0])
1273 assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
1277 describe "listen/2" do
1278 test "returns a valid activity" do
1279 user = insert(:user)
1282 CommonAPI.listen(user, %{
1283 title: "lain radio episode 1",
1284 album: "lain radio",
1289 object = Object.normalize(activity, fetch: false)
1291 assert object.data["title"] == "lain radio episode 1"
1293 assert Visibility.get_visibility(activity) == "public"
1296 test "respects visibility=private" do
1297 user = insert(:user)
1300 CommonAPI.listen(user, %{
1301 title: "lain radio episode 1",
1302 album: "lain radio",
1305 visibility: "private"
1308 object = Object.normalize(activity, fetch: false)
1310 assert object.data["title"] == "lain radio episode 1"
1312 assert Visibility.get_visibility(activity) == "private"
1316 describe "get_user/1" do
1317 test "gets user by ap_id" do
1318 user = insert(:user)
1319 assert CommonAPI.get_user(user.ap_id) == user
1322 test "gets user by guessed nickname" do
1323 user = insert(:user, ap_id: "", nickname: "mario@mushroom.kingdom")
1324 assert CommonAPI.get_user("https://mushroom.kingdom/users/mario") == user
1331 nickname: "erroruser@example.com"
1332 } = CommonAPI.get_user("")
1336 describe "with `local` visibility" do
1337 setup do: clear_config([:instance, :federating], true)
1340 user = insert(:user)
1342 with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
1343 {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU", visibility: "local"})
1345 assert Visibility.is_local_public?(activity)
1346 assert_not_called(Pleroma.Web.Federator.publish(activity))
1351 user = insert(:user)
1353 {:ok, %Activity{id: activity_id}} =
1354 CommonAPI.post(user, %{status: "#2hu #2HU", visibility: "local"})
1356 with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
1357 assert {:ok, %Activity{data: %{"deleted_activity_id" => ^activity_id}} = activity} =
1358 CommonAPI.delete(activity_id, user)
1360 assert Visibility.is_local_public?(activity)
1361 assert_not_called(Pleroma.Web.Federator.publish(activity))
1366 user = insert(:user)
1367 other_user = insert(:user)
1369 {:ok, %Activity{id: activity_id}} =
1370 CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
1372 with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
1373 assert {:ok, %Activity{data: %{"type" => "Announce"}} = activity} =
1374 CommonAPI.repeat(activity_id, user)
1376 assert Visibility.is_local_public?(activity)
1377 refute called(Pleroma.Web.Federator.publish(activity))
1382 user = insert(:user)
1383 other_user = insert(:user)
1385 {:ok, %Activity{id: activity_id}} =
1386 CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
1388 assert {:ok, _} = CommonAPI.repeat(activity_id, user)
1390 with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
1391 assert {:ok, %Activity{data: %{"type" => "Undo"}} = activity} =
1392 CommonAPI.unrepeat(activity_id, user)
1394 assert Visibility.is_local_public?(activity)
1395 refute called(Pleroma.Web.Federator.publish(activity))
1400 user = insert(:user)
1401 other_user = insert(:user)
1403 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
1405 with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
1406 assert {:ok, %Activity{data: %{"type" => "Like"}} = activity} =
1407 CommonAPI.favorite(user, activity.id)
1409 assert Visibility.is_local_public?(activity)
1410 refute called(Pleroma.Web.Federator.publish(activity))
1414 test "unfavorite" do
1415 user = insert(:user)
1416 other_user = insert(:user)
1418 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
1420 {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
1422 with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
1423 assert {:ok, activity} = CommonAPI.unfavorite(activity.id, user)
1424 assert Visibility.is_local_public?(activity)
1425 refute called(Pleroma.Web.Federator.publish(activity))
1429 test "react_with_emoji" do
1430 user = insert(:user)
1431 other_user = insert(:user)
1432 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
1434 with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
1435 assert {:ok, %Activity{data: %{"type" => "EmojiReact"}} = activity} =
1436 CommonAPI.react_with_emoji(activity.id, user, "👍")
1438 assert Visibility.is_local_public?(activity)
1439 refute called(Pleroma.Web.Federator.publish(activity))
1443 test "unreact_with_emoji" do
1444 user = insert(:user)
1445 other_user = insert(:user)
1446 {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", visibility: "local"})
1448 {:ok, _reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
1450 with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
1451 assert {:ok, %Activity{data: %{"type" => "Undo"}} = activity} =
1452 CommonAPI.unreact_with_emoji(activity.id, user, "👍")
1454 assert Visibility.is_local_public?(activity)
1455 refute called(Pleroma.Web.Federator.publish(activity))