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
11 alias Pleroma.Conversation.Participation
14 alias Pleroma.ScheduledActivity
15 alias Pleroma.Tests.ObanHelpers
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.CommonAPI
20 import Pleroma.Factory
22 setup do: clear_config([:instance, :federating])
23 setup do: clear_config([:instance, :allow_relay])
24 setup do: clear_config([:rich_media, :enabled])
25 setup do: clear_config([:mrf, :policies])
26 setup do: clear_config([:mrf_keyword, :reject])
28 describe "posting statuses" do
29 setup do: oauth_access(["write:statuses"])
31 test "posting a status does not increment reblog_count when relaying", %{conn: conn} do
32 Config.put([:instance, :federating], true)
33 Config.get([:instance, :allow_relay], true)
37 |> put_req_header("content-type", "application/json")
38 |> post("api/v1/statuses", %{
39 "content_type" => "text/plain",
40 "source" => "Pleroma FE",
41 "status" => "Hello world",
42 "visibility" => "public"
44 |> json_response_and_validate_schema(200)
46 assert response["reblogs_count"] == 0
47 ObanHelpers.perform_all()
51 |> get("api/v1/statuses/#{response["id"]}", %{})
52 |> json_response_and_validate_schema(200)
54 assert response["reblogs_count"] == 0
57 test "posting a status", %{conn: conn} do
58 idempotency_key = "Pikachu rocks!"
62 |> put_req_header("content-type", "application/json")
63 |> put_req_header("idempotency-key", idempotency_key)
64 |> post("/api/v1/statuses", %{
66 "spoiler_text" => "2hu",
70 assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
71 json_response_and_validate_schema(conn_one, 200)
73 assert Activity.get_by_id(id)
77 |> put_req_header("content-type", "application/json")
78 |> put_req_header("idempotency-key", idempotency_key)
79 |> post("/api/v1/statuses", %{
81 "spoiler_text" => "2hu",
85 assert %{"id" => second_id} = json_response(conn_two, 200)
86 assert id == second_id
90 |> put_req_header("content-type", "application/json")
91 |> post("/api/v1/statuses", %{
93 "spoiler_text" => "2hu",
94 "sensitive" => "False"
97 assert %{"id" => third_id} = json_response_and_validate_schema(conn_three, 200)
100 # An activity that will expire:
102 expires_in = 2 * 60 * 60
104 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
108 |> put_req_header("content-type", "application/json")
109 |> post("api/v1/statuses", %{
110 "status" => "oolong",
111 "expires_in" => expires_in
114 assert %{"id" => fourth_id} = json_response_and_validate_schema(conn_four, 200)
116 assert Activity.get_by_id(fourth_id)
119 worker: Pleroma.Workers.PurgeExpiredActivity,
120 args: %{activity_id: fourth_id},
121 scheduled_at: expires_at
125 test "it fails to create a status if `expires_in` is less or equal than an hour", %{
131 assert %{"error" => "Expiry date is too soon"} =
133 |> put_req_header("content-type", "application/json")
134 |> post("api/v1/statuses", %{
135 "status" => "oolong",
136 "expires_in" => expires_in
138 |> json_response_and_validate_schema(422)
143 assert %{"error" => "Expiry date is too soon"} =
145 |> put_req_header("content-type", "application/json")
146 |> post("api/v1/statuses", %{
147 "status" => "oolong",
148 "expires_in" => expires_in
150 |> json_response_and_validate_schema(422)
153 test "Get MRF reason when posting a status is rejected by one", %{conn: conn} do
154 Config.put([:mrf_keyword, :reject], ["GNO"])
155 Config.put([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
157 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} =
159 |> put_req_header("content-type", "application/json")
160 |> post("api/v1/statuses", %{"status" => "GNO/Linux"})
161 |> json_response_and_validate_schema(422)
164 test "posting an undefined status with an attachment", %{user: user, conn: conn} do
166 content_type: "image/jpeg",
167 path: Path.absname("test/fixtures/image.jpg"),
168 filename: "an_image.jpg"
171 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
175 |> put_req_header("content-type", "application/json")
176 |> post("/api/v1/statuses", %{
177 "media_ids" => [to_string(upload.id)]
180 assert json_response_and_validate_schema(conn, 200)
183 test "replying to a status", %{user: user, conn: conn} do
184 {:ok, replied_to} = CommonAPI.post(user, %{status: "cofe"})
188 |> put_req_header("content-type", "application/json")
189 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
191 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn, 200)
193 activity = Activity.get_by_id(id)
195 assert activity.data["context"] == replied_to.data["context"]
196 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
199 test "replying to a direct message with visibility other than direct", %{
203 {:ok, replied_to} = CommonAPI.post(user, %{status: "suya..", visibility: "direct"})
205 Enum.each(["public", "private", "unlisted"], fn visibility ->
208 |> put_req_header("content-type", "application/json")
209 |> post("/api/v1/statuses", %{
210 "status" => "@#{user.nickname} hey",
211 "in_reply_to_id" => replied_to.id,
212 "visibility" => visibility
215 assert json_response_and_validate_schema(conn, 422) == %{
216 "error" => "The message visibility must be direct"
221 test "posting a status with an invalid in_reply_to_id", %{conn: conn} do
224 |> put_req_header("content-type", "application/json")
225 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => ""})
227 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn, 200)
228 assert Activity.get_by_id(id)
231 test "posting a sensitive status", %{conn: conn} do
234 |> put_req_header("content-type", "application/json")
235 |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true})
237 assert %{"content" => "cofe", "id" => id, "sensitive" => true} =
238 json_response_and_validate_schema(conn, 200)
240 assert Activity.get_by_id(id)
243 test "posting a fake status", %{conn: conn} do
246 |> put_req_header("content-type", "application/json")
247 |> post("/api/v1/statuses", %{
249 "\"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"
252 real_status = json_response_and_validate_schema(real_conn, 200)
255 assert Object.get_by_ap_id(real_status["uri"])
259 |> Map.put("id", nil)
260 |> Map.put("url", nil)
261 |> Map.put("uri", nil)
262 |> Map.put("created_at", nil)
263 |> Kernel.put_in(["pleroma", "conversation_id"], nil)
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
959 describe "unreblogging" do
960 setup do: oauth_access(["write:statuses"])
962 test "unreblogs and returns the unreblogged status", %{user: user, conn: conn} do
963 activity = insert(:note_activity)
965 {:ok, _} = CommonAPI.repeat(activity.id, user)
969 |> put_req_header("content-type", "application/json")
970 |> post("/api/v1/statuses/#{activity.id}/unreblog")
972 assert %{"id" => id, "reblogged" => false, "reblogs_count" => 0} =
973 json_response_and_validate_schema(conn, 200)
975 assert to_string(activity.id) == id
978 test "returns 404 error when activity does not exist", %{conn: conn} do
981 |> put_req_header("content-type", "application/json")
982 |> post("/api/v1/statuses/foo/unreblog")
984 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
988 describe "favoriting" do
989 setup do: oauth_access(["write:favourites"])
991 test "favs a status and returns it", %{conn: conn} do
992 activity = insert(:note_activity)
996 |> put_req_header("content-type", "application/json")
997 |> post("/api/v1/statuses/#{activity.id}/favourite")
999 assert %{"id" => id, "favourites_count" => 1, "favourited" => true} =
1000 json_response_and_validate_schema(conn, 200)
1002 assert to_string(activity.id) == id
1005 test "favoriting twice will just return 200", %{conn: conn} do
1006 activity = insert(:note_activity)
1009 |> put_req_header("content-type", "application/json")
1010 |> post("/api/v1/statuses/#{activity.id}/favourite")
1013 |> put_req_header("content-type", "application/json")
1014 |> post("/api/v1/statuses/#{activity.id}/favourite")
1015 |> json_response_and_validate_schema(200)
1018 test "returns 404 error for a wrong id", %{conn: conn} do
1021 |> put_req_header("content-type", "application/json")
1022 |> post("/api/v1/statuses/1/favourite")
1024 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1028 describe "unfavoriting" do
1029 setup do: oauth_access(["write:favourites"])
1031 test "unfavorites a status and returns it", %{user: user, conn: conn} do
1032 activity = insert(:note_activity)
1034 {:ok, _} = CommonAPI.favorite(user, activity.id)
1038 |> put_req_header("content-type", "application/json")
1039 |> post("/api/v1/statuses/#{activity.id}/unfavourite")
1041 assert %{"id" => id, "favourites_count" => 0, "favourited" => false} =
1042 json_response_and_validate_schema(conn, 200)
1044 assert to_string(activity.id) == id
1047 test "returns 404 error for a wrong id", %{conn: conn} do
1050 |> put_req_header("content-type", "application/json")
1051 |> post("/api/v1/statuses/1/unfavourite")
1053 assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
1057 describe "pinned statuses" do
1058 setup do: oauth_access(["write:accounts"])
1060 setup %{user: user} do
1061 {:ok, activity} = CommonAPI.post(user, %{status: "HI!!!"})
1063 %{activity: activity}
1066 setup do: clear_config([:instance, :max_pinned_statuses], 1)
1068 test "pin status", %{conn: conn, user: user, activity: activity} do
1069 id_str = to_string(activity.id)
1071 assert %{"id" => ^id_str, "pinned" => true} =
1073 |> put_req_header("content-type", "application/json")
1074 |> post("/api/v1/statuses/#{activity.id}/pin")
1075 |> json_response_and_validate_schema(200)
1077 assert [%{"id" => ^id_str, "pinned" => true}] =
1079 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1080 |> json_response_and_validate_schema(200)
1083 test "/pin: returns 400 error when activity is not public", %{conn: conn, user: user} do
1084 {:ok, dm} = CommonAPI.post(user, %{status: "test", visibility: "direct"})
1088 |> put_req_header("content-type", "application/json")
1089 |> post("/api/v1/statuses/#{dm.id}/pin")
1091 assert json_response_and_validate_schema(conn, 400) == %{"error" => "Could not pin"}
1094 test "unpin status", %{conn: conn, user: user, activity: activity} do
1095 {:ok, _} = CommonAPI.pin(activity.id, user)
1096 user = refresh_record(user)
1098 id_str = to_string(activity.id)
1100 assert %{"id" => ^id_str, "pinned" => false} =
1102 |> assign(:user, user)
1103 |> post("/api/v1/statuses/#{activity.id}/unpin")
1104 |> json_response_and_validate_schema(200)
1108 |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true")
1109 |> json_response_and_validate_schema(200)
1112 test "/unpin: returns 400 error when activity is not exist", %{conn: conn} do
1115 |> put_req_header("content-type", "application/json")
1116 |> post("/api/v1/statuses/1/unpin")
1118 assert json_response_and_validate_schema(conn, 400) == %{"error" => "Could not unpin"}
1121 test "max pinned statuses", %{conn: conn, user: user, activity: activity_one} do
1122 {:ok, activity_two} = CommonAPI.post(user, %{status: "HI!!!"})
1124 id_str_one = to_string(activity_one.id)
1126 assert %{"id" => ^id_str_one, "pinned" => true} =
1128 |> put_req_header("content-type", "application/json")
1129 |> post("/api/v1/statuses/#{id_str_one}/pin")
1130 |> json_response_and_validate_schema(200)
1132 user = refresh_record(user)
1134 assert %{"error" => "You have already pinned the maximum number of statuses"} =
1136 |> assign(:user, user)
1137 |> post("/api/v1/statuses/#{activity_two.id}/pin")
1138 |> json_response_and_validate_schema(400)
1141 test "on pin removes deletion job, on unpin reschedule deletion" do
1142 %{conn: conn} = oauth_access(["write:accounts", "write:statuses"])
1143 expires_in = 2 * 60 * 60
1145 expires_at = DateTime.add(DateTime.utc_now(), expires_in)
1147 assert %{"id" => id} =
1149 |> put_req_header("content-type", "application/json")
1150 |> post("api/v1/statuses", %{
1151 "status" => "oolong",
1152 "expires_in" => expires_in
1154 |> json_response_and_validate_schema(200)
1157 worker: Pleroma.Workers.PurgeExpiredActivity,
1158 args: %{activity_id: id},
1159 scheduled_at: expires_at
1162 assert %{"id" => ^id, "pinned" => true} =
1164 |> put_req_header("content-type", "application/json")
1165 |> post("/api/v1/statuses/#{id}/pin")
1166 |> json_response_and_validate_schema(200)
1169 worker: Pleroma.Workers.PurgeExpiredActivity,
1170 args: %{activity_id: id},
1171 scheduled_at: expires_at
1174 assert %{"id" => ^id, "pinned" => false} =
1176 |> put_req_header("content-type", "application/json")
1177 |> post("/api/v1/statuses/#{id}/unpin")
1178 |> json_response_and_validate_schema(200)
1181 worker: Pleroma.Workers.PurgeExpiredActivity,
1182 args: %{activity_id: id},
1183 scheduled_at: expires_at
1190 Config.put([:rich_media, :enabled], true)
1192 oauth_access(["read:statuses"])
1195 test "returns rich-media card", %{conn: conn, user: user} do
1196 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
1198 {:ok, activity} = CommonAPI.post(user, %{status: "https://example.com/ogp"})
1201 "image" => "http://ia.media-imdb.com/images/rock.jpg",
1202 "provider_name" => "example.com",
1203 "provider_url" => "https://example.com",
1204 "title" => "The Rock",
1206 "url" => "https://example.com/ogp",
1208 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer.",
1211 "image" => "http://ia.media-imdb.com/images/rock.jpg",
1212 "title" => "The Rock",
1213 "type" => "video.movie",
1214 "url" => "https://example.com/ogp",
1216 "Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer."
1223 |> get("/api/v1/statuses/#{activity.id}/card")
1224 |> json_response_and_validate_schema(200)
1226 assert response == card_data
1228 # works with private posts
1230 CommonAPI.post(user, %{status: "https://example.com/ogp", visibility: "direct"})
1234 |> get("/api/v1/statuses/#{activity.id}/card")
1235 |> json_response_and_validate_schema(200)
1237 assert response_two == card_data
1240 test "replaces missing description with an empty string", %{conn: conn, user: user} do
1241 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
1243 {:ok, activity} = CommonAPI.post(user, %{status: "https://example.com/ogp-missing-data"})
1247 |> get("/api/v1/statuses/#{activity.id}/card")
1248 |> json_response_and_validate_schema(:ok)
1250 assert response == %{
1252 "title" => "Pleroma",
1253 "description" => "",
1255 "provider_name" => "example.com",
1256 "provider_url" => "https://example.com",
1257 "url" => "https://example.com/ogp-missing-data",
1260 "title" => "Pleroma",
1261 "type" => "website",
1262 "url" => "https://example.com/ogp-missing-data"
1270 bookmarks_uri = "/api/v1/bookmarks"
1272 %{conn: conn} = oauth_access(["write:bookmarks", "read:bookmarks"])
1273 author = insert(:user)
1275 {:ok, activity1} = CommonAPI.post(author, %{status: "heweoo?"})
1276 {:ok, activity2} = CommonAPI.post(author, %{status: "heweoo!"})
1280 |> put_req_header("content-type", "application/json")
1281 |> post("/api/v1/statuses/#{activity1.id}/bookmark")
1283 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == true
1287 |> put_req_header("content-type", "application/json")
1288 |> post("/api/v1/statuses/#{activity2.id}/bookmark")
1290 assert json_response_and_validate_schema(response2, 200)["bookmarked"] == true
1292 bookmarks = get(conn, bookmarks_uri)
1295 json_response_and_validate_schema(response2, 200),
1296 json_response_and_validate_schema(response1, 200)
1298 json_response_and_validate_schema(bookmarks, 200)
1302 |> put_req_header("content-type", "application/json")
1303 |> post("/api/v1/statuses/#{activity1.id}/unbookmark")
1305 assert json_response_and_validate_schema(response1, 200)["bookmarked"] == false
1307 bookmarks = get(conn, bookmarks_uri)
1309 assert [json_response_and_validate_schema(response2, 200)] ==
1310 json_response_and_validate_schema(bookmarks, 200)
1313 describe "conversation muting" do
1314 setup do: oauth_access(["write:mutes"])
1317 post_user = insert(:user)
1318 {:ok, activity} = CommonAPI.post(post_user, %{status: "HIE"})
1319 %{activity: activity}
1322 test "mute conversation", %{conn: conn, activity: activity} do
1323 id_str = to_string(activity.id)
1325 assert %{"id" => ^id_str, "muted" => true} =
1327 |> put_req_header("content-type", "application/json")
1328 |> post("/api/v1/statuses/#{activity.id}/mute")
1329 |> json_response_and_validate_schema(200)
1332 test "cannot mute already muted conversation", %{conn: conn, user: user, activity: activity} do
1333 {:ok, _} = CommonAPI.add_mute(user, activity)
1337 |> put_req_header("content-type", "application/json")
1338 |> post("/api/v1/statuses/#{activity.id}/mute")
1340 assert json_response_and_validate_schema(conn, 400) == %{
1341 "error" => "conversation is already muted"
1345 test "unmute conversation", %{conn: conn, user: user, activity: activity} do
1346 {:ok, _} = CommonAPI.add_mute(user, activity)
1348 id_str = to_string(activity.id)
1350 assert %{"id" => ^id_str, "muted" => false} =
1352 # |> assign(:user, user)
1353 |> post("/api/v1/statuses/#{activity.id}/unmute")
1354 |> json_response_and_validate_schema(200)
1358 test "Repeated posts that are replies incorrectly have in_reply_to_id null", %{conn: conn} do
1359 user1 = insert(:user)
1360 user2 = insert(:user)
1361 user3 = insert(:user)
1363 {:ok, replied_to} = CommonAPI.post(user1, %{status: "cofe"})
1365 # Reply to status from another user
1368 |> assign(:user, user2)
1369 |> assign(:token, insert(:oauth_token, user: user2, scopes: ["write:statuses"]))
1370 |> put_req_header("content-type", "application/json")
1371 |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id})
1373 assert %{"content" => "xD", "id" => id} = json_response_and_validate_schema(conn1, 200)
1375 activity = Activity.get_by_id_with_object(id)
1377 assert Object.normalize(activity, fetch: false).data["inReplyTo"] ==
1378 Object.normalize(replied_to, fetch: false).data["id"]
1380 assert Activity.get_in_reply_to_activity(activity).id == replied_to.id
1382 # Reblog from the third user
1385 |> assign(:user, user3)
1386 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["write:statuses"]))
1387 |> put_req_header("content-type", "application/json")
1388 |> post("/api/v1/statuses/#{activity.id}/reblog")
1390 assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} =
1391 json_response_and_validate_schema(conn2, 200)
1393 assert to_string(activity.id) == id
1395 # Getting third user status
1398 |> assign(:user, user3)
1399 |> assign(:token, insert(:oauth_token, user: user3, scopes: ["read:statuses"]))
1400 |> get("api/v1/timelines/home")
1402 [reblogged_activity] = json_response(conn3, 200)
1404 assert reblogged_activity["reblog"]["in_reply_to_id"] == replied_to.id
1406 replied_to_user = User.get_by_ap_id(replied_to.data["actor"])
1407 assert reblogged_activity["reblog"]["in_reply_to_account_id"] == replied_to_user.id
1410 describe "GET /api/v1/statuses/:id/favourited_by" do
1411 setup do: oauth_access(["read:accounts"])
1413 setup %{user: user} do
1414 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1416 %{activity: activity}
1419 test "returns users who have favorited the status", %{conn: conn, activity: activity} do
1420 other_user = insert(:user)
1421 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1425 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1426 |> json_response_and_validate_schema(:ok)
1428 [%{"id" => id}] = response
1430 assert id == other_user.id
1433 test "returns empty array when status has not been favorited yet", %{
1439 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1440 |> json_response_and_validate_schema(:ok)
1442 assert Enum.empty?(response)
1445 test "does not return users who have favorited the status but are blocked", %{
1446 conn: %{assigns: %{user: user}} = conn,
1449 other_user = insert(:user)
1450 {:ok, _user_relationship} = User.block(user, other_user)
1452 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
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 fail on an unauthenticated request", %{activity: activity} do
1463 other_user = insert(:user)
1464 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1468 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1469 |> json_response_and_validate_schema(:ok)
1471 [%{"id" => id}] = response
1472 assert id == other_user.id
1475 test "requires authentication for private posts", %{user: user} do
1476 other_user = insert(:user)
1479 CommonAPI.post(user, %{
1480 status: "@#{other_user.nickname} wanna get some #cofe together?",
1481 visibility: "direct"
1484 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1486 favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by"
1489 |> get(favourited_by_url)
1490 |> json_response_and_validate_schema(404)
1494 |> assign(:user, other_user)
1495 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1498 |> assign(:token, nil)
1499 |> get(favourited_by_url)
1500 |> json_response_and_validate_schema(404)
1504 |> get(favourited_by_url)
1505 |> json_response_and_validate_schema(200)
1507 [%{"id" => id}] = response
1508 assert id == other_user.id
1511 test "returns empty array when :show_reactions is disabled", %{conn: conn, activity: activity} do
1512 clear_config([:instance, :show_reactions], false)
1514 other_user = insert(:user)
1515 {:ok, _} = CommonAPI.favorite(other_user, activity.id)
1519 |> get("/api/v1/statuses/#{activity.id}/favourited_by")
1520 |> json_response_and_validate_schema(:ok)
1522 assert Enum.empty?(response)
1526 describe "GET /api/v1/statuses/:id/reblogged_by" do
1527 setup do: oauth_access(["read:accounts"])
1529 setup %{user: user} do
1530 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1532 %{activity: activity}
1535 test "returns users who have reblogged the status", %{conn: conn, activity: activity} do
1536 other_user = insert(:user)
1537 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1541 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1542 |> json_response_and_validate_schema(:ok)
1544 [%{"id" => id}] = response
1546 assert id == other_user.id
1549 test "returns empty array when status has not been reblogged yet", %{
1555 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1556 |> json_response_and_validate_schema(:ok)
1558 assert Enum.empty?(response)
1561 test "does not return users who have reblogged the status but are blocked", %{
1562 conn: %{assigns: %{user: user}} = conn,
1565 other_user = insert(:user)
1566 {:ok, _user_relationship} = User.block(user, other_user)
1568 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
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 privately", %{
1581 other_user = insert(:user)
1582 {:ok, activity} = CommonAPI.post(other_user, %{status: "my secret post"})
1584 {:ok, _} = CommonAPI.repeat(activity.id, other_user, %{visibility: "private"})
1588 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1589 |> json_response_and_validate_schema(:ok)
1591 assert Enum.empty?(response)
1594 test "does not fail on an unauthenticated request", %{activity: activity} do
1595 other_user = insert(:user)
1596 {:ok, _} = CommonAPI.repeat(activity.id, other_user)
1600 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1601 |> json_response_and_validate_schema(:ok)
1603 [%{"id" => id}] = response
1604 assert id == other_user.id
1607 test "requires authentication for private posts", %{user: user} do
1608 other_user = insert(:user)
1611 CommonAPI.post(user, %{
1612 status: "@#{other_user.nickname} wanna get some #cofe together?",
1613 visibility: "direct"
1617 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1618 |> json_response_and_validate_schema(404)
1622 |> assign(:user, other_user)
1623 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:accounts"]))
1624 |> get("/api/v1/statuses/#{activity.id}/reblogged_by")
1625 |> json_response_and_validate_schema(200)
1627 assert [] == response
1632 user = insert(:user)
1634 {:ok, %{id: id1}} = CommonAPI.post(user, %{status: "1"})
1635 {:ok, %{id: id2}} = CommonAPI.post(user, %{status: "2", in_reply_to_status_id: id1})
1636 {:ok, %{id: id3}} = CommonAPI.post(user, %{status: "3", in_reply_to_status_id: id2})
1637 {:ok, %{id: id4}} = CommonAPI.post(user, %{status: "4", in_reply_to_status_id: id3})
1638 {:ok, %{id: id5}} = CommonAPI.post(user, %{status: "5", in_reply_to_status_id: id4})
1642 |> get("/api/v1/statuses/#{id3}/context")
1643 |> json_response_and_validate_schema(:ok)
1646 "ancestors" => [%{"id" => ^id1}, %{"id" => ^id2}],
1647 "descendants" => [%{"id" => ^id4}, %{"id" => ^id5}]
1651 test "favorites paginate correctly" do
1652 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1653 other_user = insert(:user)
1654 {:ok, first_post} = CommonAPI.post(other_user, %{status: "bla"})
1655 {:ok, second_post} = CommonAPI.post(other_user, %{status: "bla"})
1656 {:ok, third_post} = CommonAPI.post(other_user, %{status: "bla"})
1658 {:ok, _first_favorite} = CommonAPI.favorite(user, third_post.id)
1659 {:ok, _second_favorite} = CommonAPI.favorite(user, first_post.id)
1660 {:ok, third_favorite} = CommonAPI.favorite(user, second_post.id)
1664 |> get("/api/v1/favourites?limit=1")
1666 assert [%{"id" => post_id}] = json_response_and_validate_schema(result, 200)
1667 assert post_id == second_post.id
1669 # Using the header for pagination works correctly
1670 [next, _] = get_resp_header(result, "link") |> hd() |> String.split(", ")
1671 [_, max_id] = Regex.run(~r/max_id=([^&]+)/, next)
1673 assert max_id == third_favorite.id
1677 |> get("/api/v1/favourites?max_id=#{max_id}")
1679 assert [%{"id" => first_post_id}, %{"id" => third_post_id}] =
1680 json_response_and_validate_schema(result, 200)
1682 assert first_post_id == first_post.id
1683 assert third_post_id == third_post.id
1686 test "returns the favorites of a user" do
1687 %{user: user, conn: conn} = oauth_access(["read:favourites"])
1688 other_user = insert(:user)
1690 {:ok, _} = CommonAPI.post(other_user, %{status: "bla"})
1691 {:ok, activity} = CommonAPI.post(other_user, %{status: "trees are happy"})
1693 {:ok, last_like} = CommonAPI.favorite(user, activity.id)
1695 first_conn = get(conn, "/api/v1/favourites")
1697 assert [status] = json_response_and_validate_schema(first_conn, 200)
1698 assert status["id"] == to_string(activity.id)
1700 assert [{"link", _link_header}] =
1701 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
1703 # Honours query params
1704 {:ok, second_activity} =
1705 CommonAPI.post(other_user, %{
1706 status: "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
1709 {:ok, _} = CommonAPI.favorite(user, second_activity.id)
1711 second_conn = get(conn, "/api/v1/favourites?since_id=#{last_like.id}")
1713 assert [second_status] = json_response_and_validate_schema(second_conn, 200)
1714 assert second_status["id"] == to_string(second_activity.id)
1716 third_conn = get(conn, "/api/v1/favourites?limit=0")
1718 assert [] = json_response_and_validate_schema(third_conn, 200)
1721 test "expires_at is nil for another user" do
1722 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1723 expires_at = DateTime.add(DateTime.utc_now(), 1_000_000)
1724 {:ok, activity} = CommonAPI.post(user, %{status: "foobar", expires_in: 1_000_000})
1726 assert %{"pleroma" => %{"expires_at" => a_expires_at}} =
1728 |> get("/api/v1/statuses/#{activity.id}")
1729 |> json_response_and_validate_schema(:ok)
1731 {:ok, a_expires_at, 0} = DateTime.from_iso8601(a_expires_at)
1732 assert DateTime.diff(expires_at, a_expires_at) == 0
1734 %{conn: conn} = oauth_access(["read:statuses"])
1736 assert %{"pleroma" => %{"expires_at" => nil}} =
1738 |> get("/api/v1/statuses/#{activity.id}")
1739 |> json_response_and_validate_schema(:ok)
1742 test "posting a local only status" do
1743 %{user: _user, conn: conn} = oauth_access(["write:statuses"])
1747 |> put_req_header("content-type", "application/json")
1748 |> post("/api/v1/statuses", %{
1750 "visibility" => "local"
1753 local = Pleroma.Constants.as_local_public()
1755 assert %{"content" => "cofe", "id" => id, "visibility" => "local"} =
1756 json_response(conn_one, 200)
1758 assert %Activity{id: ^id, data: %{"to" => [^local]}} = Activity.get_by_id(id)
1761 describe "muted reactions" do
1763 %{conn: conn, user: user} = oauth_access(["read:statuses"])
1765 other_user = insert(:user)
1766 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1768 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1769 User.mute(user, other_user)
1773 |> get("/api/v1/statuses/?ids[]=#{activity.id}")
1774 |> json_response_and_validate_schema(200)
1779 "emoji_reactions" => []
1786 |> get("/api/v1/statuses/?ids[]=#{activity.id}&with_muted=true")
1787 |> json_response_and_validate_schema(200)
1792 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]
1799 # %{conn: conn, user: user, token: token} = oauth_access(["read:statuses"])
1800 %{conn: conn, user: user, token: _token} = oauth_access(["read:statuses"])
1802 other_user = insert(:user)
1803 {:ok, activity} = CommonAPI.post(user, %{status: "test"})
1805 {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
1806 User.mute(user, other_user)
1810 |> get("/api/v1/statuses/#{activity.id}")
1811 |> json_response_and_validate_schema(200)
1815 "emoji_reactions" => []
1821 |> get("/api/v1/statuses/#{activity.id}?with_muted=true")
1822 |> json_response_and_validate_schema(200)
1826 "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}]