From aa1fff279e98c6d16095538382413d3b870cee2b Mon Sep 17 00:00:00 2001 From: Tusooa Zhu Date: Tue, 8 Mar 2022 16:19:35 -0500 Subject: [PATCH] Implement GET /api/v1/announcements/:id --- .../controllers/announcement_controller.ex | 17 +++++- .../announcement_controller_test.exs | 52 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/controllers/announcement_controller.ex b/lib/pleroma/web/mastodon_api/controllers/announcement_controller.ex index e5fcd3066..528d14adc 100644 --- a/lib/pleroma/web/mastodon_api/controllers/announcement_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/announcement_controller.ex @@ -55,7 +55,20 @@ defmodule Pleroma.Web.MastodonAPI.AnnouncementController do end @doc "POST /api/v1/announcements/:id" - def show(_conn, _params) do - {:error, :not_found} + def show(%{assigns: %{user: user}} = conn, %{id: id} = _params) do + render_announcement_by_id(conn, id, user) + end + + def show(conn, %{id: id} = _params) do + render_announcement_by_id(conn, id) + end + + def render_announcement_by_id(conn, id, user \\ nil) do + with announcement when not is_nil(announcement) <- Announcement.get_by_id(id) do + render(conn, "show.json", announcement: announcement, user: user) + else + _ -> + {:error, :not_found} + end end end diff --git a/test/pleroma/web/mastodon_api/controllers/announcement_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/announcement_controller_test.exs index 134db5fcc..090a2706b 100644 --- a/test/pleroma/web/mastodon_api/controllers/announcement_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/announcement_controller_test.exs @@ -51,4 +51,56 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert [%{"id" => ^id, "read" => true}] = response end end + + describe "GET /api/v1/announcements/:id" do + test "it shows one announcement" do + %{id: id} = insert(:announcement) + + response = + build_conn() + |> get("/api/v1/announcements/#{id}") + |> json_response_and_validate_schema(:ok) + + assert %{"id" => ^id} = response + refute Map.has_key?(response, "read") + end + + test "it gives 404 for non-existent announcements" do + %{id: id} = insert(:announcement) + + _response = + build_conn() + |> get("/api/v1/announcements/#{id}xxx") + |> json_response_and_validate_schema(:not_found) + end + + test "when authenticated, also expose read property" do + %{id: id} = insert(:announcement) + + %{conn: conn} = oauth_access(["read:accounts"]) + + response = + conn + |> get("/api/v1/announcements/#{id}") + |> json_response_and_validate_schema(:ok) + + assert %{"id" => ^id, "read" => false} = response + end + + test "when authenticated and announcement is read by user" do + %{id: id} = announcement = insert(:announcement) + user = insert(:user) + + AnnouncementReadRelationship.mark_read(user, announcement) + + %{conn: conn} = oauth_access(["read:accounts"], user: user) + + response = + conn + |> get("/api/v1/announcements/#{id}") + |> json_response_and_validate_schema(:ok) + + assert %{"id" => ^id, "read" => true} = response + end + end end