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.MastodonAPI.StatusControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Conversation.Participation
13 alias Pleroma.ScheduledActivity
14 alias Pleroma.Tests.ObanHelpers
16 alias Pleroma.Web.ActivityPub.ActivityPub
17 alias Pleroma.Web.CommonAPI
19 import Pleroma.Factory
21 setup do: clear_config([:instance, :federating])
22 setup do: clear_config([:instance, :allow_relay])
23 setup do: clear_config([:rich_media, :enabled])
24 setup do: clear_config([:mrf, :policies])
25 setup do: clear_config([:mrf_keyword, :reject])
27 describe "posting statuses" do
28 setup do: oauth_access(["write:statuses"])
30 test "posting a status does not increment reblog_count when relaying", %{conn: conn} do
31 clear_config([:instance, :federating], true)
32 Config.get([:instance, :allow_relay], true)
36 |> put_req_header("content-type", "application/json")
37 |> post("api/v1/statuses", %{
38 "content_type" => "text/plain",
39 "source" => "Pleroma FE",
40 "status" => "Hello world",
41 "visibility" => "public"
43 |> json_response_and_validate_schema(200)
45 assert response["reblogs_count"] == 0
46 ObanHelpers.perform_all()
50 |> get("api/v1/statuses/#{response["id"]}", %{})
51 |> json_response_and_validate_schema(200)
53 assert response["reblogs_count"] == 0
56 test "posting a status", %{conn: conn} do
57 idempotency_key = "Pikachu rocks!"
61 |> put_req_header("content-type", "application/json")
62 |> put_req_header("idempotency-key", idempotency_key)
63 |> post("/api/v1/statuses", %{
65 "spoiler_text" => "2hu",
69 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
70 json_response_and_validate_schema(conn_one, 200)
72 assert Activity.get_by_id(id)
76 |> put_req_header("content-type", "application/json")
77 |> put_req_header("idempotency-key", idempotency_key)
78 |> post("/api/v1/statuses", %{
80 "spoiler_text" => "2hu",
84 assert %{"id" => second_id} = json_response(conn_two, 200)
85 assert id == second_id
89 |> put_req_header("content-type", "application/json")
90 |> post("/api/v1/statuses", %{
92 "spoiler_text" => "2hu",
93 "sensitive" => "False"
96 assert %{"id" => third_id} = json_response_and_validate_schema(conn_three, 200)
99 # An activity that will expire:
101 expires_in = 2 * 60 * 60
103 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
107 |> put_req_header("content-type", "application/json")
108 |> post("api/v1/statuses", %{
109 "status" => "oolong",
110 "expires_in" => expires_in
113 assert %{"id" => fourth_id} = json_response_and_validate_schema(conn_four, 200)
115 assert Activity.get_by_id(fourth_id)
118 worker: Pleroma.Workers.PurgeExpiredActivity,
119 args: %{activity_id: fourth_id},
120 scheduled_at: expires_at
124 test "it fails to create a status if `expires_in` is less or equal than an hour", %{
130 assert %{"error" => "Expiry date is too soon"} =
132 |> put_req_header("content-type", "application/json")
133 |> post("api/v1/statuses", %{
134 "status" => "oolong",
135 "expires_in" => expires_in
137 |> json_response_and_validate_schema(422)
142 assert %{"error" => "Expiry date is too soon"} =
144 |> put_req_header("content-type", "application/json")
145 |> post("api/v1/statuses", %{
146 "status" => "oolong",
147 "expires_in" => expires_in
149 |> json_response_and_validate_schema(422)
152 test "Get MRF reason when posting a status is rejected by one", %{conn: conn} do
153 clear_config([:mrf_keyword, :reject], ["GNO"])
154 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
156 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} =
158 |> put_req_header("content-type", "application/json")
159 |> post("api/v1/statuses", %{"status" => "GNO/Linux"})
160 |> json_response_and_validate_schema(422)
163 test "posting an undefined status with an attachment", %{user: user, conn: conn} do
165 content_type: "image/jpeg",
166 path: Path.absname("test/fixtures/image.jpg"),
167 filename: "an_image.jpg"
170 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
174 |> put_req_header("content-type", "application/json")
175 |> post("/api/v1/statuses", %{
176 "media_ids" => [to_string(upload.id)]
179 assert json_response_and_validate_schema(conn, 200)
182 test "replying to a status", %{user: user, conn: conn} do
183 {:ok, replied_to} = CommonAPI.post(user, %{status: "cofe"})
187 |> put_req_header("content-type", "application/json")
188 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
190 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn, 200)
192 activity = Activity.get_by_id(id)
194 assert activity.data["context"] == replied_to.data["context"]
195 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
198 test "replying to a direct message with visibility other than direct", %{
202 {:ok, replied_to} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
204 Enum.each(["public", "private", "unlisted"], fn visibility ->
207 |> put_req_header("content-type", "application/json")
208 |> post("/api/v1/statuses", %{
209 "status" => "@#{user.nickname} hey",
210 "in_reply_to_id" => replied_to.id,
211 "visibility" => visibility
214 assert json_response_and_validate_schema(conn, 422) == %{
215 "error" => "The message visibility must be direct"
220 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
223 |> put_req_header("content-type", "application/json")
224 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
226 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn, 200)
227 assert Activity.get_by_id(id)
230 test "posting a sensitive status", %{conn: conn} do
233 |> put_req_header("content-type", "application/json")
234 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
236 assert %{"content" => "cofe", "id" => id, "sensitive" => true} =
237 json_response_and_validate_schema(conn, 200)
239 assert Activity.get_by_id(id)
242 test "posting a fake status", %{conn: conn} do
245 |> put_req_header("content-type", "application/json")
246 |> post("/api/v1/statuses", %{
248 "\"Tenshi Eating a Corndog\" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it"
251 real_status = json_response_and_validate_schema(real_conn, 200)
254 assert Object.get_by_ap_id(real_status["uri"])
258 |> Map.put("id", nil)
259 |> Map.put("url", nil)
260 |> Map.put("uri", nil)
261 |> Map.put("created_at", nil)
262 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
266 |> assign(:user, refresh_record(conn.assigns.user))
267 |> put_req_header("content-type", "application/json")
268 |> post("/api/v1/statuses", %{
270 "\"Tenshi Eating a Corndog\" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it",
274 fake_status = json_response_and_validate_schema(fake_conn, 200)
277 refute Object.get_by_ap_id(fake_status["uri"])
281 |> Map.put("id", nil)
282 |> Map.put("url", nil)
283 |> Map.put("uri", nil)
284 |> Map.put("created_at", nil)
285 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
287 assert real_status == fake_status
290 test "fake statuses' preview card is not cached", %{conn: conn} do
291 clear_config([:rich_media, :enabled], true)
296 url: "https://example.com/twitter-card"
298 %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/twitter_card.html")}
301 apply(HttpRequestMock, :request, [env])
306 |> put_req_header("content-type", "application/json")
307 |> post("/api/v1/statuses", %{
308 "status" => "https://example.com/ogp",
314 |> put_req_header("content-type", "application/json")
315 |> post("/api/v1/statuses", %{
316 "status" => "https://example.com/twitter-card",
320 assert %{"card" => %{"title" => "The Rock"}} = json_response_and_validate_schema(conn1, 200)
322 assert %{"card" => %{"title" => "Small Island Developing States Photo Submission"}} =
323 json_response_and_validate_schema(conn2, 200)
326 test "posting a status with OGP link preview", %{conn: conn} do
327 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
328 clear_config([:rich_media, :enabled], true)
332 |> put_req_header("content-type", "application/json")
333 |> post("/api/v1/statuses", %{
334 "status" => "https://example.com/ogp"
337 assert %{"id" => id, "card" => %{"title" => "The Rock"}} =
338 json_response_and_validate_schema(conn, 200)
340 assert Activity.get_by_id(id)
343 test "posting a direct status", %{conn: conn} do
344 user2 = insert(:user)
345 content = "direct cofe @#{user2.nickname}"
349 |> put_req_header("content-type", "application/json")
350 |> post("api/v1/statuses", %{"status" => content, "visibility" => "direct"})
352 assert %{"id" => id} = response = json_response_and_validate_schema(conn, 200)
353 assert response["visibility"] == "direct"
354 assert response["pleroma"]["direct_conversation_id"]
355 assert activity = Activity.get_by_id(id)
356 assert activity.recipients == [user2.ap_id, conn.assigns[:user].ap_id]
357 assert activity.data["to"] == [user2.ap_id]
358 assert activity.data["cc"] == []
361 test "discloses application metadata when enabled" do
362 user = insert(:user, disclose_client: true)
363 %{user: _user, token: token, conn: conn} = oauth_access(["write:statuses"], user: user)
365 %Pleroma.Web.OAuth.Token{
366 app: %Pleroma.Web.OAuth.App{
367 client_name: app_name,
374 |> put_req_header("content-type", "application/json")
375 |> post("/api/v1/statuses", %{
376 "status" => "cofe is my copilot"
380 "content" => "cofe is my copilot"
381 } = json_response_and_validate_schema(result, 200)
383 activity = result.assigns.activity.id
387 |> put_req_header("content-type", "application/json")
388 |> get("api/v1/statuses/#{activity}")
391 "content" => "cofe is my copilot",
394 "website" => ^app_website
396 } = json_response_and_validate_schema(result, 200)
399 test "hides application metadata when disabled" do
400 user = insert(:user, disclose_client: false)
401 %{user: _user, token: _token, conn: conn} = oauth_access(["write:statuses"], user: user)
405 |> put_req_header("content-type", "application/json")
406 |> post("/api/v1/statuses", %{
407 "status" => "club mate is my wingman"
410 assert %{"content" => "club mate is my wingman"} =
411 json_response_and_validate_schema(result, 200)
413 activity = result.assigns.activity.id
417 |> put_req_header("content-type", "application/json")
418 |> get("api/v1/statuses/#{activity}")
421 "content" => "club mate is my wingman",
423 } = json_response_and_validate_schema(result, 200)
427 describe "posting scheduled statuses" do
428 setup do: oauth_access(["write:statuses"])
430 test "creates a scheduled activity", %{conn: conn} do
432 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
433 |> NaiveDateTime.to_iso8601()
438 |> put_req_header("content-type", "application/json")
439 |> post("/api/v1/statuses", %{
440 "status" => "scheduled",
441 "scheduled_at" => scheduled_at
444 assert %{"scheduled_at" => expected_scheduled_at} =
445 json_response_and_validate_schema(conn, 200)
447 assert expected_scheduled_at == CommonAPI.Utils.to_masto_date(scheduled_at)
448 assert [] == Repo.all(Activity)
451 test "with expiration" do
452 %{conn: conn} = oauth_access(["write:statuses", "read:statuses"])
455 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(6), :millisecond)
456 |> NaiveDateTime.to_iso8601()
459 assert %{"id" => status_id, "params" => %{"expires_in" => 300}} =
461 |> put_req_header("content-type", "application/json")
462 |> post("/api/v1/statuses", %{
463 "status" => "scheduled",
464 "scheduled_at" => scheduled_at,
467 |> json_response_and_validate_schema(200)
469 assert %{"id" => ^status_id, "params" => %{"expires_in" => 300}} =
471 |> put_req_header("content-type", "application/json")
472 |> get("/api/v1/scheduled_statuses/#{status_id}")
473 |> json_response_and_validate_schema(200)
476 test "ignores nil values", %{conn: conn} do
479 |> put_req_header("content-type", "application/json")
480 |> post("/api/v1/statuses", %{
481 "status" => "not scheduled",
482 "scheduled_at" => nil
485 assert result = json_response_and_validate_schema(conn, 200)
486 assert Activity.get_by_id(result["id"])
489 test "creates a scheduled activity with a media attachment", %{user: user, conn: conn} do
491 NaiveDateTime.utc_now()
492 |> NaiveDateTime.add(:timer.minutes(120), :millisecond)
493 |> NaiveDateTime.to_iso8601()
497 content_type: "image/jpeg",
498 path: Path.absname("test/fixtures/image.jpg"),
499 filename: "an_image.jpg"
502 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
506 |> put_req_header("content-type", "application/json")
507 |> post("/api/v1/statuses", %{
508 "media_ids" => [to_string(upload.id)],
509 "status" => "scheduled",
510 "scheduled_at" => scheduled_at
513 assert %{"media_attachments" => [media_attachment]} =
514 json_response_and_validate_schema(conn, 200)
516 assert %{"type" => "image"} = media_attachment
519 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
522 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
523 |> NaiveDateTime.to_iso8601()
528 |> put_req_header("content-type", "application/json")
529 |> post("/api/v1/statuses", %{
530 "status" => "not scheduled",
531 "scheduled_at" => scheduled_at
534 assert %{"content" => "not scheduled"} = json_response_and_validate_schema(conn, 200)
535 assert [] == Repo.all(ScheduledActivity)
538 test "returns error when daily user limit is exceeded", %{user: user, conn: conn} do
540 NaiveDateTime.utc_now()
541 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
542 |> NaiveDateTime.to_iso8601()
546 attrs = %{params: %{}, scheduled_at: today}
547 {:ok, _} = ScheduledActivity.create(user, attrs)
548 {:ok, _} = ScheduledActivity.create(user, attrs)
552 |> put_req_header("content-type", "application/json")
553 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
555 assert %{"error" => "daily limit exceeded"} == json_response_and_validate_schema(conn, 422)
558 test "returns error when total user limit is exceeded", %{user: user, conn: conn} do
560 NaiveDateTime.utc_now()
561 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
562 |> NaiveDateTime.to_iso8601()
566 NaiveDateTime.utc_now()
567 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
568 |> NaiveDateTime.to_iso8601()
571 attrs = %{params: %{}, scheduled_at: today}
572 {:ok, _} = ScheduledActivity.create(user, attrs)
573 {:ok, _} = ScheduledActivity.create(user, attrs)
574 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
578 |> put_req_header("content-type", "application/json")
579 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
581 assert %{"error" => "total limit exceeded"} == json_response_and_validate_schema(conn, 422)
585 describe "posting polls" do
586 setup do: oauth_access(["write:statuses"])
588 test "posting a poll", %{conn: conn} do
589 time = NaiveDateTime.utc_now()
593 |> put_req_header("content-type", "application/json")
594 |> post("/api/v1/statuses", %{
595 "status" => "Who is the #bestgrill?",
597 "options" => ["Rei", "Asuka", "Misato"],
602 response = json_response_and_validate_schema(conn, 200)
604 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
605 title in ["Rei", "Asuka", "Misato"]
608 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
609 assert response["poll"]["expired"] == false
611 question = Object.get_by_id(response["poll"]["id"])
613 # closed contains utc timezone
614 assert question.data["closed"] =~ "Z"
617 test "option limit is enforced", %{conn: conn} do
618 limit = Config.get([:instance, :poll_limits, :max_options])
622 |> put_req_header("content-type", "application/json")
623 |> post("/api/v1/statuses", %{
625 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
628 %{"error" => error} = json_response_and_validate_schema(conn, 422)
629 assert error == "Poll can't contain more than #{limit} options"
632 test "option character limit is enforced", %{conn: conn} do
633 limit = Config.get([:instance, :poll_limits, :max_option_chars])
637 |> put_req_header("content-type", "application/json")
638 |> post("/api/v1/statuses", %{
641 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
646 %{"error" => error} = json_response_and_validate_schema(conn, 422)
647 assert error == "Poll options cannot be longer than #{limit} characters each"
650 test "minimal date limit is enforced", %{conn: conn} do
651 limit = Config.get([:instance, :poll_limits, :min_expiration])
655 |> put_req_header("content-type", "application/json")
656 |> post("/api/v1/statuses", %{
657 "status" => "imagine arbitrary limits",
659 "options" => ["this post was made by pleroma gang"],
660 "expires_in" => limit - 1
664 %{"error" => error} = json_response_and_validate_schema(conn, 422)
665 assert error == "Expiration date is too soon"
668 test "maximum date limit is enforced", %{conn: conn} do
669 limit = Config.get([:instance, :poll_limits, :max_expiration])
673 |> put_req_header("content-type", "application/json")
674 |> post("/api/v1/statuses", %{
675 "status" => "imagine arbitrary limits",
677 "options" => ["this post was made by pleroma gang"],
678 "expires_in" => limit + 1
682 %{"error" => error} = json_response_and_validate_schema(conn, 422)
683 assert error == "Expiration date is too far in the future"
686 test "scheduled poll", %{conn: conn} do
687 clear_config([ScheduledActivity, :enabled], true)
690 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(6), :millisecond)
691 |> NaiveDateTime.to_iso8601()
694 %{"id" => scheduled_id} =
696 |> put_req_header("content-type", "application/json")
697 |> post("/api/v1/statuses", %{
698 "status" => "very cool poll",
700 "options" => ~w(a b c),
703 "scheduled_at" => scheduled_at
705 |> json_response_and_validate_schema(200)
707 assert {:ok, %{id: activity_id}} =
708 perform_job(Pleroma.Workers.ScheduledActivityWorker, %{
709 activity_id: scheduled_id
712 assert Repo.all(Oban.Job) == []
716 |> Repo.get(activity_id)
717 |> Object.normalize()
719 assert object.data["content"] == "very cool poll"
720 assert object.data["type"] == "Question"
721 assert length(object.data["oneOf"]) == 3
725 test "get a status" do
726 %{conn: conn} = oauth_access(["read:statuses"])
727 activity = insert(:note_activity)
729 conn = get(conn, "/api/v1/statuses/#{activity.id}")
731 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
732 assert id == to_string(activity.id)
735 defp local_and_remote_activities do
736 local = insert(:note_activity)
737 remote = insert(:note_activity, local: false)
738 {:ok, local: local, remote: remote}
741 describe "status with restrict unauthenticated activities for local and remote" do
742 setup do: local_and_remote_activities()
744 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
746 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
748 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
749 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
751 assert json_response_and_validate_schema(res_conn, :not_found) == %{
752 "error" => "Record not found"
755 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
757 assert json_response_and_validate_schema(res_conn, :not_found) == %{
758 "error" => "Record not found"
762 test "if user is authenticated", %{local: local, remote: remote} do
763 %{conn: conn} = oauth_access(["read"])
764 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
765 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
767 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
768 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
772 describe "status with restrict unauthenticated activities for local" do
773 setup do: local_and_remote_activities()
775 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
777 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
778 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
780 assert json_response_and_validate_schema(res_conn, :not_found) == %{
781 "error" => "Record not found"
784 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
785 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
788 test "if user is authenticated", %{local: local, remote: remote} do
789 %{conn: conn} = oauth_access(["read"])
790 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
791 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
793 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
794 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
798 describe "status with restrict unauthenticated activities for remote" do
799 setup do: local_and_remote_activities()
801 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
803 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
804 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
805 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
807 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
809 assert json_response_and_validate_schema(res_conn, :not_found) == %{
810 "error" => "Record not found"
814 test "if user is authenticated", %{local: local, remote: remote} do
815 %{conn: conn} = oauth_access(["read"])
816 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
817 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
819 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
820 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
824 test "getting a status that doesn't exist returns 404" do
825 %{conn: conn} = oauth_access(["read:statuses"])
826 activity = insert(:note_activity)
828 conn = get(conn, "/api/v1/statuses/#{String.downcase(activity.id)}")
830 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
833 test "get a direct status" do
834 %{user: user, conn: conn} = oauth_access(["read:statuses"])
835 other_user = insert(:user)
838 CommonAPI.post(user, %{status: "@#{other_user.nickname}", visibility: "direct"})
842 |> assign(:user, user)
843 |> get("/api/v1/statuses/#{activity.id}")
845 [participation] = Participation.for_user(user)
847 res = json_response_and_validate_schema(conn, 200)
848 assert res["pleroma"]["direct_conversation_id"] == participation.id
851 test "get statuses by IDs" do
852 %{conn: conn} = oauth_access(["read:statuses"])
853 %{id: id1} = insert(:note_activity)
854 %{id: id2} = insert(:note_activity)
856 query_string = "ids[]=#{id1}&ids[]=#{id2}"
857 conn = get(conn, "/api/v1/statuses/?#{query_string}")
859 assert [%{"id" => ^id1}, %{"id" => ^id2}] =
860 Enum.sort_by(json_response_and_validate_schema(conn, :ok), & &1["id"])
863 describe "getting statuses by ids with restricted unauthenticated for local and remote" do
864 setup do: local_and_remote_activities()
866 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
868 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
870 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
871 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
873 assert json_response_and_validate_schema(res_conn, 200) == []
876 test "if user is authenticated", %{local: local, remote: remote} do
877 %{conn: conn} = oauth_access(["read"])
879 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
881 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
885 describe "getting statuses by ids with restricted unauthenticated for local" do
886 setup do: local_and_remote_activities()
888 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
890 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
891 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
893 remote_id = remote.id
894 assert [%{"id" => ^remote_id}] = json_response_and_validate_schema(res_conn, 200)
897 test "if user is authenticated", %{local: local, remote: remote} do
898 %{conn: conn} = oauth_access(["read"])
900 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
902 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
906 describe "getting statuses by ids with restricted unauthenticated for remote" do
907 setup do: local_and_remote_activities()
909 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
911 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
912 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
915 assert [%{"id" => ^local_id}] = json_response_and_validate_schema(res_conn, 200)
918 test "if user is authenticated", %{local: local, remote: remote} do
919 %{conn: conn} = oauth_access(["read"])
921 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
923 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
927 describe "deleting a status" do
928 test "when you created it" do
929 %{user: author, conn: conn} = oauth_access(["write:statuses"])
930 activity = insert(:note_activity, user: author)
931 object = Object.normalize(activity, fetch: false)
933 content = object.data["content"]
934 source = object.data["source"]
938 |> assign(:user, author)
939 |> delete("/api/v1/statuses/#{activity.id}")
940 |> json_response_and_validate_schema(200)
942 assert match?(%{"content" => ^content, "text" => ^source}, result)
944 refute Activity.get_by_id(activity.id)
947 test "when it doesn't exist" do
948 %{user: author, conn: conn} = oauth_access(["write:statuses"])
949 activity = insert(:note_activity, user: author)
953 |> assign(:user, author)
954 |> delete("/api/v1/statuses/#{String.downcase(activity.id)}")
956 assert %{"error" => "Record not found"} == json_response_and_validate_schema(conn, 404)
959 test "when you didn't create it" do
960 %{conn: conn} = oauth_access(["write:statuses"])
961 activity = insert(:note_activity)
963 conn = delete(conn, "/api/v1/statuses/#{activity.id}")
965 assert %{"error" => "Record not found"} == json_response_and_validate_schema(conn, 404)
967 assert Activity.get_by_id(activity.id) == activity
970 test "when you're an admin or moderator", %{conn: conn} do
971 activity1 = insert(:note_activity)
972 activity2 = insert(:note_activity)
973 admin = insert(:user, is_admin: true)
974 moderator = insert(:user, is_moderator: true)
978 |> assign(:user, admin)
979 |> assign(:token, insert(:oauth_token, user: admin, scopes: ["write:statuses"]))
980 |> delete("/api/v1/statuses/#{activity1.id}")
982 assert %{} = json_response_and_validate_schema(res_conn, 200)
986 |> assign(:user, moderator)
987 |> assign(:token, insert(:oauth_token, user: moderator, scopes: ["write:statuses"]))
988 |> delete("/api/v1/statuses/#{activity2.id}")
990 assert %{} = json_response_and_validate_schema(res_conn, 200)
992 refute Activity.get_by_id(activity1.id)
993 refute Activity.get_by_id(activity2.id)
997 describe "reblogging" do
998 setup do: oauth_access(["write:statuses"])
1000 test "reblogs and returns the reblogged status", %{conn: conn} do
1001 activity = insert(:note_activity)
1005 |> put_req_header("content-type", "application/json")
1006 |> post("/api/v1/statuses/#{activity.id}/reblog")
1009 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1011 } = json_response_and_validate_schema(conn, 200)
1013 assert to_string(activity.id) == id
1016 test "returns 404 if the reblogged status doesn't exist", %{conn: conn} do
1017 activity = insert(:note_activity)
1021 |> put_req_header("content-type", "application/json")
1022 |> post("/api/v1/statuses/#{String.downcase(activity.id)}/reblog")
1024 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
1027 test "reblogs privately and returns the reblogged status", %{conn: conn} do
1028 activity = insert(:note_activity)
1032 |> put_req_header("content-type", "application/json")
1034 "/api/v1/statuses/#{activity.id}/reblog",
1035 %{"visibility" => "private"}
1039 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1040 "reblogged" => true,
1041 "visibility" => "private"
1042 } = json_response_and_validate_schema(conn, 200)
1044 assert to_string(activity.id) == id
1047 test "reblogged status for another user" do
1048 activity = insert(:note_activity)
1049 user1 = insert(:user)
1050 user2 = insert(:user)
1051 user3 = insert(:user)
1052 {:ok, _} = CommonAPI.favorite(user2, activity.id)
1053 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
1054 {:ok, reblog_activity1} = CommonAPI.repeat(activity.id, user1)
1055 {:ok, _} = CommonAPI.repeat(activity.id, user2)
1059 |> assign(:user, user3)
1060 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1061 |> get("/api/v1/statuses/#{reblog_activity1.id}")
1064 "reblog" => %{"id" => _id, "reblogged" => false, "reblogs_count" => 2},
1065 "reblogged" => false,
1066 "favourited" => false,
1067 "bookmarked" => false
1068 } = json_response_and_validate_schema(conn_res, 200)
1072 |> assign(:user, user2)
1073 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["read:statuses"]))
1074 |> get("/api/v1/statuses/#{reblog_activity1.id}")
1077 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
1078 "reblogged" => true,
1079 "favourited" => true,
1080 "bookmarked" => true
1081 } = json_response_and_validate_schema(conn_res, 200)
1083 assert to_string(activity.id) == id
1086 test "author can reblog own private status", %{conn: conn, user: user} do
1087 {:ok, activity} = CommonAPI.post(user, %{status: "cofe", visibility: "private"})
1091 |> put_req_header("content-type", "application/json")
1092 |> post("/api/v1/statuses/#{activity.id}/reblog")
1095 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
1096 "reblogged" => true,
1097 "visibility" => "private"
1098 } = json_response_and_validate_schema(conn, 200)
1100 assert to_string(activity.id) == id
1104 describe "unreblogging" do
1105 setup do: oauth_access(["write:statuses"])
1107 test "unreblogs and returns the unreblogged status", %{user: user, conn: conn} do
1108 activity = insert(:note_activity)
1110 {:ok, _} = CommonAPI.repeat(activity.id, user)
1114 |> put_req_header("content-type", "application/json")
1115 |> post("/api/v1/statuses/#{activity.id}/unreblog")
1117 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} =
1118 json_response_and_validate_schema(conn, 200)
1120 assert to_string(activity.id) == id
1123 test "returns 404 error when activity does not exist", %{conn: conn} do
1126 |> put_req_header("content-type", "application/json")
1127 |> post("/api/v1/statuses/foo/unreblog")
1129 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1133 describe "favoriting" do
1134 setup do: oauth_access(["write:favourites"])
1136 test "favs a status and returns it", %{conn: conn} do
1137 activity = insert(:note_activity)
1141 |> put_req_header("content-type", "application/json")
1142 |> post("/api/v1/statuses/#{activity.id}/favourite")
1144 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
1145 json_response_and_validate_schema(conn, 200)
1147 assert to_string(activity.id) == id
1150 test "favoriting twice will just return 200", %{conn: conn} do
1151 activity = insert(:note_activity)
1154 |> put_req_header("content-type", "application/json")
1155 |> post("/api/v1/statuses/#{activity.id}/favourite")
1158 |> put_req_header("content-type", "application/json")
1159 |> post("/api/v1/statuses/#{activity.id}/favourite")
1160 |> json_response_and_validate_schema(200)
1163 test "returns 404 error for a wrong id", %{conn: conn} do
1166 |> put_req_header("content-type", "application/json")
1167 |> post("/api/v1/statuses/1/favourite")
1169 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1173 describe "unfavoriting" do
1174 setup do: oauth_access(["write:favourites"])
1176 test "unfavorites a status and returns it", %{user: user, conn: conn} do
1177 activity = insert(:note_activity)
1179 {:ok, _} = CommonAPI.favorite(user, activity.id)
1183 |> put_req_header("content-type", "application/json")
1184 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
1186 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
1187 json_response_and_validate_schema(conn, 200)
1189 assert to_string(activity.id) == id
1192 test "returns 404 error for a wrong id", %{conn: conn} do
1195 |> put_req_header("content-type", "application/json")
1196 |> post("/api/v1/statuses/1/unfavourite")
1198 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1202 describe "pinned statuses" do
1203 setup do: oauth_access(["write:accounts"])
1205 setup %{user: user} do
1206 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1208 %{activity: activity}
1211 setup do: clear_config([:instance, :max_pinned_statuses], 1)
1213 test "pin status", %{conn: conn, user: user, activity: activity} do
1214 id_str = to_string(activity.id)
1216 assert %{"id" => ^id_str, "pinned" => true} =
1218 |> put_req_header("content-type", "application/json")
1219 |> post("/api/v1/statuses/#{activity.id}/pin")
1220 |> json_response_and_validate_schema(200)
1222 assert [%{"id" => ^id_str, "pinned" => true}] =
1224 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1225 |> json_response_and_validate_schema(200)
1228 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
1229 {:ok, dm} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
1233 |> put_req_header("content-type", "application/json")
1234 |> post("/api/v1/statuses/#{dm.id}/pin")
1236 assert json_response_and_validate_schema(conn, 400) == %{"error" => "Could not pin"}
1239 test "unpin status", %{conn: conn, user: user, activity: activity} do
1240 {:ok, _} = CommonAPI.pin(activity.id, user)
1241 user = refresh_record(user)
1243 id_str = to_string(activity.id)
1245 assert %{"id" => ^id_str, "pinned" => false} =
1247 |> assign(:user, user)
1248 |> post("/api/v1/statuses/#{activity.id}/unpin")
1249 |> json_response_and_validate_schema(200)
1253 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1254 |> json_response_and_validate_schema(200)
1257 test "/unpin: returns 400 error when activity is not exist", %{conn: conn} do
1260 |> put_req_header("content-type", "application/json")
1261 |> post("/api/v1/statuses/1/unpin")
1263 assert json_response_and_validate_schema(conn, 400) == %{"error" => "Could not unpin"}
1266 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
1267 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
1269 id_str_one = to_string(activity_one.id)
1271 assert %{"id" => ^id_str_one, "pinned" => true} =
1273 |> put_req_header("content-type", "application/json")
1274 |> post("/api/v1/statuses/#{id_str_one}/pin")
1275 |> json_response_and_validate_schema(200)
1277 user = refresh_record(user)
1279 assert %{"error" => "You have already pinned the maximum number of statuses"} =
1281 |> assign(:user, user)
1282 |> post("/api/v1/statuses/#{activity_two.id}/pin")
1283 |> json_response_and_validate_schema(400)
1286 test "on pin removes deletion job, on unpin reschedule deletion" do
1287 %{conn: conn} = oauth_access(["write:accounts", "write:statuses"])
1288 expires_in = 2 * 60 * 60
1290 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
1292 assert %{"id" => id} =
1294 |> put_req_header("content-type", "application/json")
1295 |> post("api/v1/statuses", %{
1296 "status" => "oolong",
1297 "expires_in" => expires_in
1299 |> json_response_and_validate_schema(200)
1302 worker: Pleroma.Workers.PurgeExpiredActivity,
1303 args: %{activity_id: id},
1304 scheduled_at: expires_at
1307 assert %{"id" => ^id, "pinned" => true} =
1309 |> put_req_header("content-type", "application/json")
1310 |> post("/api/v1/statuses/#{id}/pin")
1311 |> json_response_and_validate_schema(200)
1314 worker: Pleroma.Workers.PurgeExpiredActivity,
1315 args: %{activity_id: id},
1316 scheduled_at: expires_at
1319 assert %{"id" => ^id, "pinned" => false} =
1321 |> put_req_header("content-type", "application/json")
1322 |> post("/api/v1/statuses/#{id}/unpin")
1323 |> json_response_and_validate_schema(200)
1326 worker: Pleroma.Workers.PurgeExpiredActivity,
1327 args: %{activity_id: id},
1328 scheduled_at: expires_at
1335 clear_config([:rich_media, :enabled], true)
1337 oauth_access(["read:statuses"])
1340 test "returns rich-media card", %{conn: conn, user: user} do
1341 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
1343 {:ok, activity} = CommonAPI.post(user, %{status: "https://example.com/ogp"})
1346 "image" => "http://ia.media-imdb.com/images/rock.jpg",
1347 "provider_name" => "example.com",
1348 "provider_url" => "https://example.com",
1349 "title" => "The Rock",
1351 "url" => "https://example.com/ogp",
1353 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
1356 "image" => "http://ia.media-imdb.com/images/rock.jpg",
1357 "title" => "The Rock",
1358 "type" => "video.movie",
1359 "url" => "https://example.com/ogp",
1361 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
1368 |> get("/api/v1/statuses/#{activity.id}/card")
1369 |> json_response_and_validate_schema(200)
1371 assert response == card_data
1373 # works with private posts
1375 CommonAPI.post(user, %{status: "https://example.com/ogp", visibility: "direct"})
1379 |> get("/api/v1/statuses/#{activity.id}/card")
1380 |> json_response_and_validate_schema(200)
1382 assert response_two == card_data
1385 test "replaces missing description with an empty string", %{conn: conn, user: user} do
1386 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
1388 {:ok, activity} = CommonAPI.post(user, %{status: "https://example.com/ogp-missing-data"})
1392 |> get("/api/v1/statuses/#{activity.id}/card")
1393 |> json_response_and_validate_schema(:ok)
1395 assert response == %{
1397 "title" => "Pleroma",
1398 "description" => "",
1400 "provider_name" => "example.com",
1401 "provider_url" => "https://example.com",
1402 "url" => "https://example.com/ogp-missing-data",
1405 "title" => "Pleroma",
1406 "type" => "website",
1407 "url" => "https://example.com/ogp-missing-data"
1415 bookmarks_uri = "/api/v1/bookmarks"
1417 %{conn: conn} = oauth_access(["write:bookmarks", "read:bookmarks"])
1418 author = insert(:user)
1420 {:ok, activity1} = CommonAPI.post(author, %{status: "heweoo?"})
1421 {:ok, activity2} = CommonAPI.post(author, %{status: "heweoo!"})
1425 |> put_req_header("content-type", "application/json")
1426 |> post("/api/v1/statuses/#{activity1.id}/bookmark")
1428 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == true
1432 |> put_req_header("content-type", "application/json")
1433 |> post("/api/v1/statuses/#{activity2.id}/bookmark")
1435 assert json_response_and_validate_schema(response2, 200)["bookmarked"] == true
1437 bookmarks = get(conn, bookmarks_uri)
1440 json_response_and_validate_schema(response2, 200),
1441 json_response_and_validate_schema(response1, 200)
1443 json_response_and_validate_schema(bookmarks, 200)
1447 |> put_req_header("content-type", "application/json")
1448 |> post("/api/v1/statuses/#{activity1.id}/unbookmark")
1450 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == false
1452 bookmarks = get(conn, bookmarks_uri)
1454 assert [json_response_and_validate_schema(response2, 200)] ==
1455 json_response_and_validate_schema(bookmarks, 200)
1458 describe "conversation muting" do
1459 setup do: oauth_access(["write:mutes"])
1462 post_user = insert(:user)
1463 {:ok, activity} = CommonAPI.post(post_user, %{status: "HIE"})
1464 %{activity: activity}
1467 test "mute conversation", %{conn: conn, activity: activity} do
1468 id_str = to_string(activity.id)
1470 assert %{"id" => ^id_str, "muted" => true} =
1472 |> put_req_header("content-type", "application/json")
1473 |> post("/api/v1/statuses/#{activity.id}/mute")
1474 |> json_response_and_validate_schema(200)
1477 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
1478 {:ok, _} = CommonAPI.add_mute(user, activity)
1482 |> put_req_header("content-type", "application/json")
1483 |> post("/api/v1/statuses/#{activity.id}/mute")
1485 assert json_response_and_validate_schema(conn, 400) == %{
1486 "error" => "conversation is already muted"
1490 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
1491 {:ok, _} = CommonAPI.add_mute(user, activity)
1493 id_str = to_string(activity.id)
1495 assert %{"id" => ^id_str, "muted" => false} =
1497 # |> assign(:user, user)
1498 |> post("/api/v1/statuses/#{activity.id}/unmute")
1499 |> json_response_and_validate_schema(200)
1503 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
1504 user1 = insert(:user)
1505 user2 = insert(:user)
1506 user3 = insert(:user)
1508 {:ok, replied_to} = CommonAPI.post(user1, %{status: "cofe"})
1510 # Reply to status from another user
1513 |> assign(:user, user2)
1514 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["write:statuses"]))
1515 |> put_req_header("content-type", "application/json")
1516 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
1518 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn1, 200)
1520 activity = Activity.get_by_id_with_object(id)
1522 assert Object.normalize(activity, fetch: false).data["inReplyTo"] ==
1523 Object.normalize(replied_to, fetch: false).data["id"]
1525 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
1527 # Reblog from the third user
1530 |> assign(:user, user3)
1531 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["write:statuses"]))
1532 |> put_req_header("content-type", "application/json")
1533 |> post("/api/v1/statuses/#{activity.id}/reblog")
1535 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
1536 json_response_and_validate_schema(conn2, 200)
1538 assert to_string(activity.id) == id
1540 # Getting third user status
1543 |> assign(:user, user3)
1544 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1545 |> get("api/v1/timelines/home")
1547 [reblogged_activity] = json_response(conn3, 200)
1549 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
1551 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
1552 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
1555 describe "GET /api/v1/statuses/:id/favourited_by" do
1556 setup do: oauth_access(["read:accounts"])
1558 setup %{user: user} do
1559 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1561 %{activity: activity}
1564 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
1565 other_user = insert(:user)
1566 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1570 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1571 |> json_response_and_validate_schema(:ok)
1573 [%{"id" => id}] = response
1575 assert id == other_user.id
1578 test "returns empty array when status has not been favorited yet", %{
1584 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1585 |> json_response_and_validate_schema(:ok)
1587 assert Enum.empty?(response)
1590 test "does not return users who have favorited the status but are blocked", %{
1591 conn: %{assigns: %{user: user}} = conn,
1594 other_user = insert(:user)
1595 {:ok, _user_relationship} = User.block(user, other_user)
1597 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1601 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1602 |> json_response_and_validate_schema(:ok)
1604 assert Enum.empty?(response)
1607 test "does not fail on an unauthenticated request", %{activity: activity} do
1608 other_user = insert(:user)
1609 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1613 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1614 |> json_response_and_validate_schema(:ok)
1616 [%{"id" => id}] = response
1617 assert id == other_user.id
1620 test "requires authentication for private posts", %{user: user} do
1621 other_user = insert(:user)
1624 CommonAPI.post(user, %{
1625 status: "@#{other_user.nickname} wanna get some #cofe together?",
1626 visibility: "direct"
1629 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1631 favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by"
1634 |> get(favourited_by_url)
1635 |> json_response_and_validate_schema(404)
1639 |> assign(:user, other_user)
1640 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1643 |> assign(:token, nil)
1644 |> get(favourited_by_url)
1645 |> json_response_and_validate_schema(404)
1649 |> get(favourited_by_url)
1650 |> json_response_and_validate_schema(200)
1652 [%{"id" => id}] = response
1653 assert id == other_user.id
1656 test "returns empty array when :show_reactions is disabled", %{conn: conn, activity: activity} do
1657 clear_config([:instance, :show_reactions], false)
1659 other_user = insert(:user)
1660 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1664 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1665 |> json_response_and_validate_schema(:ok)
1667 assert Enum.empty?(response)
1671 describe "GET /api/v1/statuses/:id/reblogged_by" do
1672 setup do: oauth_access(["read:accounts"])
1674 setup %{user: user} do
1675 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1677 %{activity: activity}
1680 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1681 other_user = insert(:user)
1682 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1686 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1687 |> json_response_and_validate_schema(:ok)
1689 [%{"id" => id}] = response
1691 assert id == other_user.id
1694 test "returns empty array when status has not been reblogged yet", %{
1700 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1701 |> json_response_and_validate_schema(:ok)
1703 assert Enum.empty?(response)
1706 test "does not return users who have reblogged the status but are blocked", %{
1707 conn: %{assigns: %{user: user}} = conn,
1710 other_user = insert(:user)
1711 {:ok, _user_relationship} = User.block(user, other_user)
1713 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1717 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1718 |> json_response_and_validate_schema(:ok)
1720 assert Enum.empty?(response)
1723 test "does not return users who have reblogged the status privately", %{
1726 other_user = insert(:user)
1727 {:ok, activity} = CommonAPI.post(other_user, %{status: "my secret post"})
1729 {:ok, _} = CommonAPI.repeat(activity.id, other_user, %{visibility: "private"})
1733 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1734 |> json_response_and_validate_schema(:ok)
1736 assert Enum.empty?(response)
1739 test "does not fail on an unauthenticated request", %{activity: activity} do
1740 other_user = insert(:user)
1741 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1745 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1746 |> json_response_and_validate_schema(:ok)
1748 [%{"id" => id}] = response
1749 assert id == other_user.id
1752 test "requires authentication for private posts", %{user: user} do
1753 other_user = insert(:user)
1756 CommonAPI.post(user, %{
1757 status: "@#{other_user.nickname} wanna get some #cofe together?",
1758 visibility: "direct"
1762 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1763 |> json_response_and_validate_schema(404)
1767 |> assign(:user, other_user)
1768 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1769 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1770 |> json_response_and_validate_schema(200)
1772 assert [] == response
1777 user = insert(:user)
1779 {:ok, %{id: id1}} = CommonAPI.post(user, %{status: "1"})
1780 {:ok, %{id: id2}} = CommonAPI.post(user, %{status: "2", in_reply_to_status_id: id1})
1781 {:ok, %{id: id3}} = CommonAPI.post(user, %{status: "3", in_reply_to_status_id: id2})
1782 {:ok, %{id: id4}} = CommonAPI.post(user, %{status: "4", in_reply_to_status_id: id3})
1783 {:ok, %{id: id5}} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
1787 |> get("/api/v1/statuses/#{id3}/context")
1788 |> json_response_and_validate_schema(:ok)
1791 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1792 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1796 test "favorites paginate correctly" do
1797 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1798 other_user = insert(:user)
1799 {:ok, first_post} = CommonAPI.post(other_user, %{status: "bla"})
1800 {:ok, second_post} = CommonAPI.post(other_user, %{status: "bla"})
1801 {:ok, third_post} = CommonAPI.post(other_user, %{status: "bla"})
1803 {:ok, _first_favorite} = CommonAPI.favorite(user, third_post.id)
1804 {:ok, _second_favorite} = CommonAPI.favorite(user, first_post.id)
1805 {:ok, third_favorite} = CommonAPI.favorite(user, second_post.id)
1809 |> get("/api/v1/favourites?limit=1")
1811 assert [%{"id" => post_id}] = json_response_and_validate_schema(result, 200)
1812 assert post_id == second_post.id
1814 # Using the header for pagination works correctly
1815 [next, _] = get_resp_header(result, "link") |> hd() |> String.split(", ")
1816 [_, max_id] = Regex.run(~r/max_id=([^&]+)/, next)
1818 assert max_id == third_favorite.id
1822 |> get("/api/v1/favourites?max_id=#{max_id}")
1824 assert [%{"id" => first_post_id}, %{"id" => third_post_id}] =
1825 json_response_and_validate_schema(result, 200)
1827 assert first_post_id == first_post.id
1828 assert third_post_id == third_post.id
1831 test "returns the favorites of a user" do
1832 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1833 other_user = insert(:user)
1835 {:ok, _} = CommonAPI.post(other_user, %{status: "bla"})
1836 {:ok, activity} = CommonAPI.post(other_user, %{status: "trees are happy"})
1838 {:ok, last_like} = CommonAPI.favorite(user, activity.id)
1840 first_conn = get(conn, "/api/v1/favourites")
1842 assert [status] = json_response_and_validate_schema(first_conn, 200)
1843 assert status["id"] == to_string(activity.id)
1845 assert [{"link", _link_header}] =
1846 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1848 # Honours query params
1849 {:ok, second_activity} =
1850 CommonAPI.post(other_user, %{
1851 status: "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1854 {:ok, _} = CommonAPI.favorite(user, second_activity.id)
1856 second_conn = get(conn, "/api/v1/favourites?since_id=#{last_like.id}")
1858 assert [second_status] = json_response_and_validate_schema(second_conn, 200)
1859 assert second_status["id"] == to_string(second_activity.id)
1861 third_conn = get(conn, "/api/v1/favourites?limit=0")
1863 assert [] = json_response_and_validate_schema(third_conn, 200)
1866 test "expires_at is nil for another user" do
1867 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1868 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
1869 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", expires_in: 1_000_000})
1871 assert %{"pleroma" => %{"expires_at" => a_expires_at}} =
1873 |> get("/api/v1/statuses/#{activity.id}")
1874 |> json_response_and_validate_schema(:ok)
1876 {:ok, a_expires_at, 0} = DateTime.from_iso8601(a_expires_at)
1877 assert DateTime.diff(expires_at, a_expires_at) == 0
1879 %{conn: conn} = oauth_access(["read:statuses"])
1881 assert %{"pleroma" => %{"expires_at" => nil}} =
1883 |> get("/api/v1/statuses/#{activity.id}")
1884 |> json_response_and_validate_schema(:ok)
1887 test "posting a local only status" do
1888 %{user: _user, conn: conn} = oauth_access(["write:statuses"])
1892 |> put_req_header("content-type", "application/json")
1893 |> post("/api/v1/statuses", %{
1895 "visibility" => "local"
1898 local = Pleroma.Constants.as_local_public()
1900 assert %{"content" => "cofe", "id" => id, "visibility" => "local"} =
1901 json_response(conn_one, 200)
1903 assert %Activity{id: ^id, data: %{"to" => [^local]}} = Activity.get_by_id(id)
1906 describe "muted reactions" do
1908 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1910 other_user = insert(:user)
1911 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1913 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1914 User.mute(user, other_user)
1918 |> get("/api/v1/statuses/?ids[]=#{activity.id}")
1919 |> json_response_and_validate_schema(200)
1924 "emoji_reactions" => []
1931 |> get("/api/v1/statuses/?ids[]=#{activity.id}&with_muted=true")
1932 |> json_response_and_validate_schema(200)
1937 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
1944 # %{conn: conn, user: user, token: token} = oauth_access(["read:statuses"])
1945 %{conn: conn, user: user, token: _token} = oauth_access(["read:statuses"])
1947 other_user = insert(:user)
1948 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1950 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1951 User.mute(user, other_user)
1955 |> get("/api/v1/statuses/#{activity.id}")
1956 |> json_response_and_validate_schema(200)
1960 "emoji_reactions" => []
1966 |> get("/api/v1/statuses/#{activity.id}?with_muted=true")
1967 |> json_response_and_validate_schema(200)
1971 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]