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"] == []
362 describe "posting scheduled statuses" do
363 setup do: oauth_access(["write:statuses"])
365 test "creates a scheduled activity", %{conn: conn} do
367 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond)
368 |> NaiveDateTime.to_iso8601()
373 |> put_req_header("content-type", "application/json")
374 |> post("/api/v1/statuses", %{
375 "status" => "scheduled",
376 "scheduled_at" => scheduled_at
379 assert %{"scheduled_at" => expected_scheduled_at} =
380 json_response_and_validate_schema(conn, 200)
382 assert expected_scheduled_at == CommonAPI.Utils.to_masto_date(scheduled_at)
383 assert [] == Repo.all(Activity)
386 test "ignores nil values", %{conn: conn} do
389 |> put_req_header("content-type", "application/json")
390 |> post("/api/v1/statuses", %{
391 "status" => "not scheduled",
392 "scheduled_at" => nil
395 assert result = json_response_and_validate_schema(conn, 200)
396 assert Activity.get_by_id(result["id"])
399 test "creates a scheduled activity with a media attachment", %{user: user, conn: conn} do
401 NaiveDateTime.utc_now()
402 |> NaiveDateTime.add(:timer.minutes(120), :millisecond)
403 |> NaiveDateTime.to_iso8601()
407 content_type: "image/jpeg",
408 path: Path.absname("test/fixtures/image.jpg"),
409 filename: "an_image.jpg"
412 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
416 |> put_req_header("content-type", "application/json")
417 |> post("/api/v1/statuses", %{
418 "media_ids" => [to_string(upload.id)],
419 "status" => "scheduled",
420 "scheduled_at" => scheduled_at
423 assert %{"media_attachments" => [media_attachment]} =
424 json_response_and_validate_schema(conn, 200)
426 assert %{"type" => "image"} = media_attachment
429 test "skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now",
432 NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(5) - 1, :millisecond)
433 |> NaiveDateTime.to_iso8601()
438 |> put_req_header("content-type", "application/json")
439 |> post("/api/v1/statuses", %{
440 "status" => "not scheduled",
441 "scheduled_at" => scheduled_at
444 assert %{"content" => "not scheduled"} = json_response_and_validate_schema(conn, 200)
445 assert [] == Repo.all(ScheduledActivity)
448 test "returns error when daily user limit is exceeded", %{user: user, conn: conn} do
450 NaiveDateTime.utc_now()
451 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
452 |> NaiveDateTime.to_iso8601()
456 attrs = %{params: %{}, scheduled_at: today}
457 {:ok, _} = ScheduledActivity.create(user, attrs)
458 {:ok, _} = ScheduledActivity.create(user, attrs)
462 |> put_req_header("content-type", "application/json")
463 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => today})
465 assert %{"error" => "daily limit exceeded"} == json_response_and_validate_schema(conn, 422)
468 test "returns error when total user limit is exceeded", %{user: user, conn: conn} do
470 NaiveDateTime.utc_now()
471 |> NaiveDateTime.add(:timer.minutes(6), :millisecond)
472 |> NaiveDateTime.to_iso8601()
476 NaiveDateTime.utc_now()
477 |> NaiveDateTime.add(:timer.hours(36), :millisecond)
478 |> NaiveDateTime.to_iso8601()
481 attrs = %{params: %{}, scheduled_at: today}
482 {:ok, _} = ScheduledActivity.create(user, attrs)
483 {:ok, _} = ScheduledActivity.create(user, attrs)
484 {:ok, _} = ScheduledActivity.create(user, %{params: %{}, scheduled_at: tomorrow})
488 |> put_req_header("content-type", "application/json")
489 |> post("/api/v1/statuses", %{"status" => "scheduled", "scheduled_at" => tomorrow})
491 assert %{"error" => "total limit exceeded"} == json_response_and_validate_schema(conn, 422)
495 describe "posting polls" do
496 setup do: oauth_access(["write:statuses"])
498 test "posting a poll", %{conn: conn} do
499 time = NaiveDateTime.utc_now()
503 |> put_req_header("content-type", "application/json")
504 |> post("/api/v1/statuses", %{
505 "status" => "Who is the #bestgrill?",
507 "options" => ["Rei", "Asuka", "Misato"],
512 response = json_response_and_validate_schema(conn, 200)
514 assert Enum.all?(response["poll"]["options"], fn %{"title" => title} ->
515 title in ["Rei", "Asuka", "Misato"]
518 assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430
519 refute response["poll"]["expred"]
521 question = Object.get_by_id(response["poll"]["id"])
523 # closed contains utc timezone
524 assert question.data["closed"] =~ "Z"
527 test "option limit is enforced", %{conn: conn} do
528 limit = Config.get([:instance, :poll_limits, :max_options])
532 |> put_req_header("content-type", "application/json")
533 |> post("/api/v1/statuses", %{
535 "poll" => %{"options" => Enum.map(0..limit, fn _ -> "desu" end), "expires_in" => 1}
538 %{"error" => error} = json_response_and_validate_schema(conn, 422)
539 assert error == "Poll can't contain more than #{limit} options"
542 test "option character limit is enforced", %{conn: conn} do
543 limit = Config.get([:instance, :poll_limits, :max_option_chars])
547 |> put_req_header("content-type", "application/json")
548 |> post("/api/v1/statuses", %{
551 "options" => [Enum.reduce(0..limit, "", fn _, acc -> acc <> "." end)],
556 %{"error" => error} = json_response_and_validate_schema(conn, 422)
557 assert error == "Poll options cannot be longer than #{limit} characters each"
560 test "minimal date limit is enforced", %{conn: conn} do
561 limit = Config.get([:instance, :poll_limits, :min_expiration])
565 |> put_req_header("content-type", "application/json")
566 |> post("/api/v1/statuses", %{
567 "status" => "imagine arbitrary limits",
569 "options" => ["this post was made by pleroma gang"],
570 "expires_in" => limit - 1
574 %{"error" => error} = json_response_and_validate_schema(conn, 422)
575 assert error == "Expiration date is too soon"
578 test "maximum date limit is enforced", %{conn: conn} do
579 limit = Config.get([:instance, :poll_limits, :max_expiration])
583 |> put_req_header("content-type", "application/json")
584 |> post("/api/v1/statuses", %{
585 "status" => "imagine arbitrary limits",
587 "options" => ["this post was made by pleroma gang"],
588 "expires_in" => limit + 1
592 %{"error" => error} = json_response_and_validate_schema(conn, 422)
593 assert error == "Expiration date is too far in the future"
597 test "get a status" do
598 %{conn: conn} = oauth_access(["read:statuses"])
599 activity = insert(:note_activity)
601 conn = get(conn, "/api/v1/statuses/#{activity.id}")
603 assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
604 assert id == to_string(activity.id)
607 defp local_and_remote_activities do
608 local = insert(:note_activity)
609 remote = insert(:note_activity, local: false)
610 {:ok, local: local, remote: remote}
613 describe "status with restrict unauthenticated activities for local and remote" do
614 setup do: local_and_remote_activities()
616 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
618 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
620 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
621 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
623 assert json_response_and_validate_schema(res_conn, :not_found) == %{
624 "error" => "Record not found"
627 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
629 assert json_response_and_validate_schema(res_conn, :not_found) == %{
630 "error" => "Record not found"
634 test "if user is authenticated", %{local: local, remote: remote} do
635 %{conn: conn} = oauth_access(["read"])
636 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
637 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
639 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
640 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
644 describe "status with restrict unauthenticated activities for local" do
645 setup do: local_and_remote_activities()
647 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
649 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
650 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
652 assert json_response_and_validate_schema(res_conn, :not_found) == %{
653 "error" => "Record not found"
656 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
657 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
660 test "if user is authenticated", %{local: local, remote: remote} do
661 %{conn: conn} = oauth_access(["read"])
662 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
663 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
665 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
666 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
670 describe "status with restrict unauthenticated activities for remote" do
671 setup do: local_and_remote_activities()
673 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
675 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
676 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
677 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
679 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
681 assert json_response_and_validate_schema(res_conn, :not_found) == %{
682 "error" => "Record not found"
686 test "if user is authenticated", %{local: local, remote: remote} do
687 %{conn: conn} = oauth_access(["read"])
688 res_conn = get(conn, "/api/v1/statuses/#{local.id}")
689 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
691 res_conn = get(conn, "/api/v1/statuses/#{remote.id}")
692 assert %{"id" => _} = json_response_and_validate_schema(res_conn, 200)
696 test "getting a status that doesn't exist returns 404" do
697 %{conn: conn} = oauth_access(["read:statuses"])
698 activity = insert(:note_activity)
700 conn = get(conn, "/api/v1/statuses/#{String.downcase(activity.id)}")
702 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
705 test "get a direct status" do
706 %{user: user, conn: conn} = oauth_access(["read:statuses"])
707 other_user = insert(:user)
710 CommonAPI.post(user, %{status: "@#{other_user.nickname}", visibility: "direct"})
714 |> assign(:user, user)
715 |> get("/api/v1/statuses/#{activity.id}")
717 [participation] = Participation.for_user(user)
719 res = json_response_and_validate_schema(conn, 200)
720 assert res["pleroma"]["direct_conversation_id"] == participation.id
723 test "get statuses by IDs" do
724 %{conn: conn} = oauth_access(["read:statuses"])
725 %{id: id1} = insert(:note_activity)
726 %{id: id2} = insert(:note_activity)
728 query_string = "ids[]=#{id1}&ids[]=#{id2}"
729 conn = get(conn, "/api/v1/statuses/?#{query_string}")
731 assert [%{"id" => ^id1}, %{"id" => ^id2}] =
732 Enum.sort_by(json_response_and_validate_schema(conn, :ok), & &1["id"])
735 describe "getting statuses by ids with restricted unauthenticated for local and remote" do
736 setup do: local_and_remote_activities()
738 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
740 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
742 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
743 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
745 assert json_response_and_validate_schema(res_conn, 200) == []
748 test "if user is authenticated", %{local: local, remote: remote} do
749 %{conn: conn} = oauth_access(["read"])
751 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
753 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
757 describe "getting statuses by ids with restricted unauthenticated for local" do
758 setup do: local_and_remote_activities()
760 setup do: clear_config([:restrict_unauthenticated, :activities, :local], true)
762 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
763 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
765 remote_id = remote.id
766 assert [%{"id" => ^remote_id}] = json_response_and_validate_schema(res_conn, 200)
769 test "if user is authenticated", %{local: local, remote: remote} do
770 %{conn: conn} = oauth_access(["read"])
772 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
774 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
778 describe "getting statuses by ids with restricted unauthenticated for remote" do
779 setup do: local_and_remote_activities()
781 setup do: clear_config([:restrict_unauthenticated, :activities, :remote], true)
783 test "if user is unauthenticated", %{conn: conn, local: local, remote: remote} do
784 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
787 assert [%{"id" => ^local_id}] = json_response_and_validate_schema(res_conn, 200)
790 test "if user is authenticated", %{local: local, remote: remote} do
791 %{conn: conn} = oauth_access(["read"])
793 res_conn = get(conn, "/api/v1/statuses?ids[]=#{local.id}&ids[]=#{remote.id}")
795 assert length(json_response_and_validate_schema(res_conn, 200)) == 2
799 describe "deleting a status" do
800 test "when you created it" do
801 %{user: author, conn: conn} = oauth_access(["write:statuses"])
802 activity = insert(:note_activity, user: author)
803 object = Object.normalize(activity, fetch: false)
805 content = object.data["content"]
806 source = object.data["source"]
810 |> assign(:user, author)
811 |> delete("/api/v1/statuses/#{activity.id}")
812 |> json_response_and_validate_schema(200)
814 assert match?(%{"content" => ^content, "text" => ^source}, result)
816 refute Activity.get_by_id(activity.id)
819 test "when it doesn't exist" do
820 %{user: author, conn: conn} = oauth_access(["write:statuses"])
821 activity = insert(:note_activity, user: author)
825 |> assign(:user, author)
826 |> delete("/api/v1/statuses/#{String.downcase(activity.id)}")
828 assert %{"error" => "Record not found"} == json_response_and_validate_schema(conn, 404)
831 test "when you didn't create it" do
832 %{conn: conn} = oauth_access(["write:statuses"])
833 activity = insert(:note_activity)
835 conn = delete(conn, "/api/v1/statuses/#{activity.id}")
837 assert %{"error" => "Record not found"} == json_response_and_validate_schema(conn, 404)
839 assert Activity.get_by_id(activity.id) == activity
842 test "when you're an admin or moderator", %{conn: conn} do
843 activity1 = insert(:note_activity)
844 activity2 = insert(:note_activity)
845 admin = insert(:user, is_admin: true)
846 moderator = insert(:user, is_moderator: true)
850 |> assign(:user, admin)
851 |> assign(:token, insert(:oauth_token, user: admin, scopes: ["write:statuses"]))
852 |> delete("/api/v1/statuses/#{activity1.id}")
854 assert %{} = json_response_and_validate_schema(res_conn, 200)
858 |> assign(:user, moderator)
859 |> assign(:token, insert(:oauth_token, user: moderator, scopes: ["write:statuses"]))
860 |> delete("/api/v1/statuses/#{activity2.id}")
862 assert %{} = json_response_and_validate_schema(res_conn, 200)
864 refute Activity.get_by_id(activity1.id)
865 refute Activity.get_by_id(activity2.id)
869 describe "reblogging" do
870 setup do: oauth_access(["write:statuses"])
872 test "reblogs and returns the reblogged status", %{conn: conn} do
873 activity = insert(:note_activity)
877 |> put_req_header("content-type", "application/json")
878 |> post("/api/v1/statuses/#{activity.id}/reblog")
881 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
883 } = json_response_and_validate_schema(conn, 200)
885 assert to_string(activity.id) == id
888 test "returns 404 if the reblogged status doesn't exist", %{conn: conn} do
889 activity = insert(:note_activity)
893 |> put_req_header("content-type", "application/json")
894 |> post("/api/v1/statuses/#{String.downcase(activity.id)}/reblog")
896 assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
899 test "reblogs privately and returns the reblogged status", %{conn: conn} do
900 activity = insert(:note_activity)
904 |> put_req_header("content-type", "application/json")
906 "/api/v1/statuses/#{activity.id}/reblog",
907 %{"visibility" => "private"}
911 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
913 "visibility" => "private"
914 } = json_response_and_validate_schema(conn, 200)
916 assert to_string(activity.id) == id
919 test "reblogged status for another user" do
920 activity = insert(:note_activity)
921 user1 = insert(:user)
922 user2 = insert(:user)
923 user3 = insert(:user)
924 {:ok, _} = CommonAPI.favorite(user2, activity.id)
925 {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)
926 {:ok, reblog_activity1} = CommonAPI.repeat(activity.id, user1)
927 {:ok, _} = CommonAPI.repeat(activity.id, user2)
931 |> assign(:user, user3)
932 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
933 |> get("/api/v1/statuses/#{reblog_activity1.id}")
936 "reblog" => %{"id" => _id, "reblogged" => false, "reblogs_count" => 2},
937 "reblogged" => false,
938 "favourited" => false,
939 "bookmarked" => false
940 } = json_response_and_validate_schema(conn_res, 200)
944 |> assign(:user, user2)
945 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["read:statuses"]))
946 |> get("/api/v1/statuses/#{reblog_activity1.id}")
949 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 2},
951 "favourited" => true,
953 } = json_response_and_validate_schema(conn_res, 200)
955 assert to_string(activity.id) == id
958 test "author can reblog own private status", %{conn: conn, user: user} do
959 {:ok, activity} = CommonAPI.post(user, %{status: "cofe", visibility: "private"})
963 |> put_req_header("content-type", "application/json")
964 |> post("/api/v1/statuses/#{activity.id}/reblog")
967 "reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1},
969 "visibility" => "private"
970 } = json_response_and_validate_schema(conn, 200)
972 assert to_string(activity.id) == id
976 describe "unreblogging" do
977 setup do: oauth_access(["write:statuses"])
979 test "unreblogs and returns the unreblogged status", %{user: user, conn: conn} do
980 activity = insert(:note_activity)
982 {:ok, _} = CommonAPI.repeat(activity.id, user)
986 |> put_req_header("content-type", "application/json")
987 |> post("/api/v1/statuses/#{activity.id}/unreblog")
989 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} =
990 json_response_and_validate_schema(conn, 200)
992 assert to_string(activity.id) == id
995 test "returns 404 error when activity does not exist", %{conn: conn} do
998 |> put_req_header("content-type", "application/json")
999 |> post("/api/v1/statuses/foo/unreblog")
1001 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1005 describe "favoriting" do
1006 setup do: oauth_access(["write:favourites"])
1008 test "favs a status and returns it", %{conn: conn} do
1009 activity = insert(:note_activity)
1013 |> put_req_header("content-type", "application/json")
1014 |> post("/api/v1/statuses/#{activity.id}/favourite")
1016 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
1017 json_response_and_validate_schema(conn, 200)
1019 assert to_string(activity.id) == id
1022 test "favoriting twice will just return 200", %{conn: conn} do
1023 activity = insert(:note_activity)
1026 |> put_req_header("content-type", "application/json")
1027 |> post("/api/v1/statuses/#{activity.id}/favourite")
1030 |> put_req_header("content-type", "application/json")
1031 |> post("/api/v1/statuses/#{activity.id}/favourite")
1032 |> json_response_and_validate_schema(200)
1035 test "returns 404 error for a wrong id", %{conn: conn} do
1038 |> put_req_header("content-type", "application/json")
1039 |> post("/api/v1/statuses/1/favourite")
1041 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1045 describe "unfavoriting" do
1046 setup do: oauth_access(["write:favourites"])
1048 test "unfavorites a status and returns it", %{user: user, conn: conn} do
1049 activity = insert(:note_activity)
1051 {:ok, _} = CommonAPI.favorite(user, activity.id)
1055 |> put_req_header("content-type", "application/json")
1056 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
1058 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
1059 json_response_and_validate_schema(conn, 200)
1061 assert to_string(activity.id) == id
1064 test "returns 404 error for a wrong id", %{conn: conn} do
1067 |> put_req_header("content-type", "application/json")
1068 |> post("/api/v1/statuses/1/unfavourite")
1070 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1074 describe "pinned statuses" do
1075 setup do: oauth_access(["write:accounts"])
1077 setup %{user: user} do
1078 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1080 %{activity: activity}
1083 setup do: clear_config([:instance, :max_pinned_statuses], 1)
1085 test "pin status", %{conn: conn, user: user, activity: activity} do
1086 id_str = to_string(activity.id)
1088 assert %{"id" => ^id_str, "pinned" => true} =
1090 |> put_req_header("content-type", "application/json")
1091 |> post("/api/v1/statuses/#{activity.id}/pin")
1092 |> json_response_and_validate_schema(200)
1094 assert [%{"id" => ^id_str, "pinned" => true}] =
1096 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1097 |> json_response_and_validate_schema(200)
1100 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
1101 {:ok, dm} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
1105 |> put_req_header("content-type", "application/json")
1106 |> post("/api/v1/statuses/#{dm.id}/pin")
1108 assert json_response_and_validate_schema(conn, 400) == %{"error" => "Could not pin"}
1111 test "unpin status", %{conn: conn, user: user, activity: activity} do
1112 {:ok, _} = CommonAPI.pin(activity.id, user)
1113 user = refresh_record(user)
1115 id_str = to_string(activity.id)
1117 assert %{"id" => ^id_str, "pinned" => false} =
1119 |> assign(:user, user)
1120 |> post("/api/v1/statuses/#{activity.id}/unpin")
1121 |> json_response_and_validate_schema(200)
1125 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1126 |> json_response_and_validate_schema(200)
1129 test "/unpin: returns 400 error when activity is not exist", %{conn: conn} do
1132 |> put_req_header("content-type", "application/json")
1133 |> post("/api/v1/statuses/1/unpin")
1135 assert json_response_and_validate_schema(conn, 400) == %{"error" => "Could not unpin"}
1138 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
1139 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
1141 id_str_one = to_string(activity_one.id)
1143 assert %{"id" => ^id_str_one, "pinned" => true} =
1145 |> put_req_header("content-type", "application/json")
1146 |> post("/api/v1/statuses/#{id_str_one}/pin")
1147 |> json_response_and_validate_schema(200)
1149 user = refresh_record(user)
1151 assert %{"error" => "You have already pinned the maximum number of statuses"} =
1153 |> assign(:user, user)
1154 |> post("/api/v1/statuses/#{activity_two.id}/pin")
1155 |> json_response_and_validate_schema(400)
1158 test "on pin removes deletion job, on unpin reschedule deletion" do
1159 %{conn: conn} = oauth_access(["write:accounts", "write:statuses"])
1160 expires_in = 2 * 60 * 60
1162 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
1164 assert %{"id" => id} =
1166 |> put_req_header("content-type", "application/json")
1167 |> post("api/v1/statuses", %{
1168 "status" => "oolong",
1169 "expires_in" => expires_in
1171 |> json_response_and_validate_schema(200)
1174 worker: Pleroma.Workers.PurgeExpiredActivity,
1175 args: %{activity_id: id},
1176 scheduled_at: expires_at
1179 assert %{"id" => ^id, "pinned" => true} =
1181 |> put_req_header("content-type", "application/json")
1182 |> post("/api/v1/statuses/#{id}/pin")
1183 |> json_response_and_validate_schema(200)
1186 worker: Pleroma.Workers.PurgeExpiredActivity,
1187 args: %{activity_id: id},
1188 scheduled_at: expires_at
1191 assert %{"id" => ^id, "pinned" => false} =
1193 |> put_req_header("content-type", "application/json")
1194 |> post("/api/v1/statuses/#{id}/unpin")
1195 |> json_response_and_validate_schema(200)
1198 worker: Pleroma.Workers.PurgeExpiredActivity,
1199 args: %{activity_id: id},
1200 scheduled_at: expires_at
1207 clear_config([:rich_media, :enabled], true)
1209 oauth_access(["read:statuses"])
1212 test "returns rich-media card", %{conn: conn, user: user} do
1213 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
1215 {:ok, activity} = CommonAPI.post(user, %{status: "https://example.com/ogp"})
1218 "image" => "http://ia.media-imdb.com/images/rock.jpg",
1219 "provider_name" => "example.com",
1220 "provider_url" => "https://example.com",
1221 "title" => "The Rock",
1223 "url" => "https://example.com/ogp",
1225 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
1228 "image" => "http://ia.media-imdb.com/images/rock.jpg",
1229 "title" => "The Rock",
1230 "type" => "video.movie",
1231 "url" => "https://example.com/ogp",
1233 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
1240 |> get("/api/v1/statuses/#{activity.id}/card")
1241 |> json_response_and_validate_schema(200)
1243 assert response == card_data
1245 # works with private posts
1247 CommonAPI.post(user, %{status: "https://example.com/ogp", visibility: "direct"})
1251 |> get("/api/v1/statuses/#{activity.id}/card")
1252 |> json_response_and_validate_schema(200)
1254 assert response_two == card_data
1257 test "replaces missing description with an empty string", %{conn: conn, user: user} do
1258 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
1260 {:ok, activity} = CommonAPI.post(user, %{status: "https://example.com/ogp-missing-data"})
1264 |> get("/api/v1/statuses/#{activity.id}/card")
1265 |> json_response_and_validate_schema(:ok)
1267 assert response == %{
1269 "title" => "Pleroma",
1270 "description" => "",
1272 "provider_name" => "example.com",
1273 "provider_url" => "https://example.com",
1274 "url" => "https://example.com/ogp-missing-data",
1277 "title" => "Pleroma",
1278 "type" => "website",
1279 "url" => "https://example.com/ogp-missing-data"
1287 bookmarks_uri = "/api/v1/bookmarks"
1289 %{conn: conn} = oauth_access(["write:bookmarks", "read:bookmarks"])
1290 author = insert(:user)
1292 {:ok, activity1} = CommonAPI.post(author, %{status: "heweoo?"})
1293 {:ok, activity2} = CommonAPI.post(author, %{status: "heweoo!"})
1297 |> put_req_header("content-type", "application/json")
1298 |> post("/api/v1/statuses/#{activity1.id}/bookmark")
1300 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == true
1304 |> put_req_header("content-type", "application/json")
1305 |> post("/api/v1/statuses/#{activity2.id}/bookmark")
1307 assert json_response_and_validate_schema(response2, 200)["bookmarked"] == true
1309 bookmarks = get(conn, bookmarks_uri)
1312 json_response_and_validate_schema(response2, 200),
1313 json_response_and_validate_schema(response1, 200)
1315 json_response_and_validate_schema(bookmarks, 200)
1319 |> put_req_header("content-type", "application/json")
1320 |> post("/api/v1/statuses/#{activity1.id}/unbookmark")
1322 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == false
1324 bookmarks = get(conn, bookmarks_uri)
1326 assert [json_response_and_validate_schema(response2, 200)] ==
1327 json_response_and_validate_schema(bookmarks, 200)
1330 describe "conversation muting" do
1331 setup do: oauth_access(["write:mutes"])
1334 post_user = insert(:user)
1335 {:ok, activity} = CommonAPI.post(post_user, %{status: "HIE"})
1336 %{activity: activity}
1339 test "mute conversation", %{conn: conn, activity: activity} do
1340 id_str = to_string(activity.id)
1342 assert %{"id" => ^id_str, "muted" => true} =
1344 |> put_req_header("content-type", "application/json")
1345 |> post("/api/v1/statuses/#{activity.id}/mute")
1346 |> json_response_and_validate_schema(200)
1349 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
1350 {:ok, _} = CommonAPI.add_mute(user, activity)
1354 |> put_req_header("content-type", "application/json")
1355 |> post("/api/v1/statuses/#{activity.id}/mute")
1357 assert json_response_and_validate_schema(conn, 400) == %{
1358 "error" => "conversation is already muted"
1362 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
1363 {:ok, _} = CommonAPI.add_mute(user, activity)
1365 id_str = to_string(activity.id)
1367 assert %{"id" => ^id_str, "muted" => false} =
1369 # |> assign(:user, user)
1370 |> post("/api/v1/statuses/#{activity.id}/unmute")
1371 |> json_response_and_validate_schema(200)
1375 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
1376 user1 = insert(:user)
1377 user2 = insert(:user)
1378 user3 = insert(:user)
1380 {:ok, replied_to} = CommonAPI.post(user1, %{status: "cofe"})
1382 # Reply to status from another user
1385 |> assign(:user, user2)
1386 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["write:statuses"]))
1387 |> put_req_header("content-type", "application/json")
1388 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
1390 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn1, 200)
1392 activity = Activity.get_by_id_with_object(id)
1394 assert Object.normalize(activity, fetch: false).data["inReplyTo"] ==
1395 Object.normalize(replied_to, fetch: false).data["id"]
1397 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
1399 # Reblog from the third user
1402 |> assign(:user, user3)
1403 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["write:statuses"]))
1404 |> put_req_header("content-type", "application/json")
1405 |> post("/api/v1/statuses/#{activity.id}/reblog")
1407 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
1408 json_response_and_validate_schema(conn2, 200)
1410 assert to_string(activity.id) == id
1412 # Getting third user status
1415 |> assign(:user, user3)
1416 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1417 |> get("api/v1/timelines/home")
1419 [reblogged_activity] = json_response(conn3, 200)
1421 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
1423 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
1424 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
1427 describe "GET /api/v1/statuses/:id/favourited_by" do
1428 setup do: oauth_access(["read:accounts"])
1430 setup %{user: user} do
1431 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1433 %{activity: activity}
1436 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
1437 other_user = insert(:user)
1438 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1442 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1443 |> json_response_and_validate_schema(:ok)
1445 [%{"id" => id}] = response
1447 assert id == other_user.id
1450 test "returns empty array when status has not been favorited yet", %{
1456 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1457 |> json_response_and_validate_schema(:ok)
1459 assert Enum.empty?(response)
1462 test "does not return users who have favorited the status but are blocked", %{
1463 conn: %{assigns: %{user: user}} = conn,
1466 other_user = insert(:user)
1467 {:ok, _user_relationship} = User.block(user, other_user)
1469 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1473 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1474 |> json_response_and_validate_schema(:ok)
1476 assert Enum.empty?(response)
1479 test "does not fail on an unauthenticated request", %{activity: activity} do
1480 other_user = insert(:user)
1481 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1485 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1486 |> json_response_and_validate_schema(:ok)
1488 [%{"id" => id}] = response
1489 assert id == other_user.id
1492 test "requires authentication for private posts", %{user: user} do
1493 other_user = insert(:user)
1496 CommonAPI.post(user, %{
1497 status: "@#{other_user.nickname} wanna get some #cofe together?",
1498 visibility: "direct"
1501 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1503 favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by"
1506 |> get(favourited_by_url)
1507 |> json_response_and_validate_schema(404)
1511 |> assign(:user, other_user)
1512 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1515 |> assign(:token, nil)
1516 |> get(favourited_by_url)
1517 |> json_response_and_validate_schema(404)
1521 |> get(favourited_by_url)
1522 |> json_response_and_validate_schema(200)
1524 [%{"id" => id}] = response
1525 assert id == other_user.id
1528 test "returns empty array when :show_reactions is disabled", %{conn: conn, activity: activity} do
1529 clear_config([:instance, :show_reactions], false)
1531 other_user = insert(:user)
1532 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1536 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1537 |> json_response_and_validate_schema(:ok)
1539 assert Enum.empty?(response)
1543 describe "GET /api/v1/statuses/:id/reblogged_by" do
1544 setup do: oauth_access(["read:accounts"])
1546 setup %{user: user} do
1547 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1549 %{activity: activity}
1552 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1553 other_user = insert(:user)
1554 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1558 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1559 |> json_response_and_validate_schema(:ok)
1561 [%{"id" => id}] = response
1563 assert id == other_user.id
1566 test "returns empty array when status has not been reblogged yet", %{
1572 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1573 |> json_response_and_validate_schema(:ok)
1575 assert Enum.empty?(response)
1578 test "does not return users who have reblogged the status but are blocked", %{
1579 conn: %{assigns: %{user: user}} = conn,
1582 other_user = insert(:user)
1583 {:ok, _user_relationship} = User.block(user, other_user)
1585 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1589 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1590 |> json_response_and_validate_schema(:ok)
1592 assert Enum.empty?(response)
1595 test "does not return users who have reblogged the status privately", %{
1598 other_user = insert(:user)
1599 {:ok, activity} = CommonAPI.post(other_user, %{status: "my secret post"})
1601 {:ok, _} = CommonAPI.repeat(activity.id, other_user, %{visibility: "private"})
1605 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1606 |> json_response_and_validate_schema(:ok)
1608 assert Enum.empty?(response)
1611 test "does not fail on an unauthenticated request", %{activity: activity} do
1612 other_user = insert(:user)
1613 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1617 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1618 |> json_response_and_validate_schema(:ok)
1620 [%{"id" => id}] = response
1621 assert id == other_user.id
1624 test "requires authentication for private posts", %{user: user} do
1625 other_user = insert(:user)
1628 CommonAPI.post(user, %{
1629 status: "@#{other_user.nickname} wanna get some #cofe together?",
1630 visibility: "direct"
1634 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1635 |> json_response_and_validate_schema(404)
1639 |> assign(:user, other_user)
1640 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1641 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1642 |> json_response_and_validate_schema(200)
1644 assert [] == response
1649 user = insert(:user)
1651 {:ok, %{id: id1}} = CommonAPI.post(user, %{status: "1"})
1652 {:ok, %{id: id2}} = CommonAPI.post(user, %{status: "2", in_reply_to_status_id: id1})
1653 {:ok, %{id: id3}} = CommonAPI.post(user, %{status: "3", in_reply_to_status_id: id2})
1654 {:ok, %{id: id4}} = CommonAPI.post(user, %{status: "4", in_reply_to_status_id: id3})
1655 {:ok, %{id: id5}} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
1659 |> get("/api/v1/statuses/#{id3}/context")
1660 |> json_response_and_validate_schema(:ok)
1663 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1664 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1668 test "favorites paginate correctly" do
1669 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1670 other_user = insert(:user)
1671 {:ok, first_post} = CommonAPI.post(other_user, %{status: "bla"})
1672 {:ok, second_post} = CommonAPI.post(other_user, %{status: "bla"})
1673 {:ok, third_post} = CommonAPI.post(other_user, %{status: "bla"})
1675 {:ok, _first_favorite} = CommonAPI.favorite(user, third_post.id)
1676 {:ok, _second_favorite} = CommonAPI.favorite(user, first_post.id)
1677 {:ok, third_favorite} = CommonAPI.favorite(user, second_post.id)
1681 |> get("/api/v1/favourites?limit=1")
1683 assert [%{"id" => post_id}] = json_response_and_validate_schema(result, 200)
1684 assert post_id == second_post.id
1686 # Using the header for pagination works correctly
1687 [next, _] = get_resp_header(result, "link") |> hd() |> String.split(", ")
1688 [_, max_id] = Regex.run(~r/max_id=([^&]+)/, next)
1690 assert max_id == third_favorite.id
1694 |> get("/api/v1/favourites?max_id=#{max_id}")
1696 assert [%{"id" => first_post_id}, %{"id" => third_post_id}] =
1697 json_response_and_validate_schema(result, 200)
1699 assert first_post_id == first_post.id
1700 assert third_post_id == third_post.id
1703 test "returns the favorites of a user" do
1704 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1705 other_user = insert(:user)
1707 {:ok, _} = CommonAPI.post(other_user, %{status: "bla"})
1708 {:ok, activity} = CommonAPI.post(other_user, %{status: "trees are happy"})
1710 {:ok, last_like} = CommonAPI.favorite(user, activity.id)
1712 first_conn = get(conn, "/api/v1/favourites")
1714 assert [status] = json_response_and_validate_schema(first_conn, 200)
1715 assert status["id"] == to_string(activity.id)
1717 assert [{"link", _link_header}] =
1718 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1720 # Honours query params
1721 {:ok, second_activity} =
1722 CommonAPI.post(other_user, %{
1723 status: "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1726 {:ok, _} = CommonAPI.favorite(user, second_activity.id)
1728 second_conn = get(conn, "/api/v1/favourites?since_id=#{last_like.id}")
1730 assert [second_status] = json_response_and_validate_schema(second_conn, 200)
1731 assert second_status["id"] == to_string(second_activity.id)
1733 third_conn = get(conn, "/api/v1/favourites?limit=0")
1735 assert [] = json_response_and_validate_schema(third_conn, 200)
1738 test "expires_at is nil for another user" do
1739 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1740 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
1741 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", expires_in: 1_000_000})
1743 assert %{"pleroma" => %{"expires_at" => a_expires_at}} =
1745 |> get("/api/v1/statuses/#{activity.id}")
1746 |> json_response_and_validate_schema(:ok)
1748 {:ok, a_expires_at, 0} = DateTime.from_iso8601(a_expires_at)
1749 assert DateTime.diff(expires_at, a_expires_at) == 0
1751 %{conn: conn} = oauth_access(["read:statuses"])
1753 assert %{"pleroma" => %{"expires_at" => nil}} =
1755 |> get("/api/v1/statuses/#{activity.id}")
1756 |> json_response_and_validate_schema(:ok)
1759 test "posting a local only status" do
1760 %{user: _user, conn: conn} = oauth_access(["write:statuses"])
1764 |> put_req_header("content-type", "application/json")
1765 |> post("/api/v1/statuses", %{
1767 "visibility" => "local"
1770 local = Pleroma.Constants.as_local_public()
1772 assert %{"content" => "cofe", "id" => id, "visibility" => "local"} =
1773 json_response(conn_one, 200)
1775 assert %Activity{id: ^id, data: %{"to" => [^local]}} = Activity.get_by_id(id)
1778 describe "muted reactions" do
1780 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1782 other_user = insert(:user)
1783 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1785 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1786 User.mute(user, other_user)
1790 |> get("/api/v1/statuses/?ids[]=#{activity.id}")
1791 |> json_response_and_validate_schema(200)
1796 "emoji_reactions" => []
1803 |> get("/api/v1/statuses/?ids[]=#{activity.id}&with_muted=true")
1804 |> json_response_and_validate_schema(200)
1809 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
1816 # %{conn: conn, user: user, token: token} = oauth_access(["read:statuses"])
1817 %{conn: conn, user: user, token: _token} = oauth_access(["read:statuses"])
1819 other_user = insert(:user)
1820 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1822 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1823 User.mute(user, other_user)
1827 |> get("/api/v1/statuses/#{activity.id}")
1828 |> json_response_and_validate_schema(200)
1832 "emoji_reactions" => []
1838 |> get("/api/v1/statuses/#{activity.id}?with_muted=true")
1839 |> json_response_and_validate_schema(200)
1843 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]