Implement POST /api/v1/announcements/:id/dismiss

This commit is contained in:
Tusooa Zhu 2022-03-08 16:59:20 -05:00
parent aa1fff279e
commit 2b39b36e49
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
3 changed files with 62 additions and 8 deletions

View file

@ -61,7 +61,7 @@ def mark_read_operation do
) )
], ],
responses: %{ responses: %{
200 => Operation.response("Response", "application/json", Announcement), 200 => Operation.response("Response", "application/json", %Schema{type: :object}),
403 => Operation.response("Forbidden", "application/json", ApiError), 403 => Operation.response("Forbidden", "application/json", ApiError),
404 => Operation.response("Not Found", "application/json", ApiError) 404 => Operation.response("Not Found", "application/json", ApiError)
} }

View file

@ -5,10 +5,10 @@
defmodule Pleroma.Web.MastodonAPI.AnnouncementController do defmodule Pleroma.Web.MastodonAPI.AnnouncementController do
use Pleroma.Web, :controller use Pleroma.Web, :controller
# import Pleroma.Web.ControllerHelper, import Pleroma.Web.ControllerHelper,
# only: [ only: [
# json_response: 3 json_response: 3
# ] ]
alias Pleroma.Announcement alias Pleroma.Announcement
alias Pleroma.Web.Plugs.OAuthScopesPlug alias Pleroma.Web.Plugs.OAuthScopesPlug
@ -50,9 +50,15 @@ defp all_visible do
end end
@doc "POST /api/v1/announcements/:id/dismiss" @doc "POST /api/v1/announcements/:id/dismiss"
def mark_read(_conn, _params) do def mark_read(%{assigns: %{user: user}} = conn, %{id: id} = _params) do
with announcement when not is_nil(announcement) <- Announcement.get_by_id(id),
{:ok, _} <- Announcement.mark_read_by(announcement, user) do
json_response(conn, :ok, %{})
else
_ ->
{:error, :not_found} {:error, :not_found}
end end
end
@doc "POST /api/v1/announcements/:id" @doc "POST /api/v1/announcements/:id"
def show(%{assigns: %{user: user}} = conn, %{id: id} = _params) do def show(%{assigns: %{user: user}} = conn, %{id: id} = _params) do

View file

@ -2,11 +2,12 @@
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do defmodule Pleroma.Web.MastodonAPI.AnnouncementControllerTest do
use Pleroma.Web.ConnCase use Pleroma.Web.ConnCase
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Announcement
alias Pleroma.AnnouncementReadRelationship alias Pleroma.AnnouncementReadRelationship
describe "GET /api/v1/announcements" do describe "GET /api/v1/announcements" do
@ -103,4 +104,51 @@ test "when authenticated and announcement is read by user" do
assert %{"id" => ^id, "read" => true} = response assert %{"id" => ^id, "read" => true} = response
end end
end end
describe "POST /api/v1/announcements/:id/dismiss" do
setup do: oauth_access(["write:accounts"])
test "it requires auth", %{conn: conn} do
%{id: id} = insert(:announcement)
_response =
conn
|> assign(:token, nil)
|> post("/api/v1/announcements/#{id}/dismiss")
|> json_response_and_validate_schema(:forbidden)
end
test "it requires write:accounts oauth scope" do
%{id: id} = insert(:announcement)
%{conn: conn} = oauth_access(["read:accounts"])
_response =
conn
|> post("/api/v1/announcements/#{id}/dismiss")
|> json_response_and_validate_schema(:forbidden)
end
test "it gives 404 for non-existent announcements", %{conn: conn} do
%{id: id} = insert(:announcement)
_response =
conn
|> post("/api/v1/announcements/#{id}xxx/dismiss")
|> json_response_and_validate_schema(:not_found)
end
test "it marks announcement as read", %{user: user, conn: conn} do
%{id: id} = announcement = insert(:announcement)
refute Announcement.read_by?(announcement, user)
_response =
conn
|> post("/api/v1/announcements/#{id}/dismiss")
|> json_response_and_validate_schema(:ok)
assert Announcement.read_by?(announcement, user)
end
end
end end