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 @@ defmodule Pleroma.Web.ApiSpec.AnnouncementOperation do
)
],
responses: %{
200 => Operation.response("Response", "application/json", Announcement),
200 => Operation.response("Response", "application/json", %Schema{type: :object}),
403 => Operation.response("Forbidden", "application/json", ApiError),
404 => Operation.response("Not Found", "application/json", ApiError)
}

View file

@ -5,10 +5,10 @@
defmodule Pleroma.Web.MastodonAPI.AnnouncementController do
use Pleroma.Web, :controller
# import Pleroma.Web.ControllerHelper,
# only: [
# json_response: 3
# ]
import Pleroma.Web.ControllerHelper,
only: [
json_response: 3
]
alias Pleroma.Announcement
alias Pleroma.Web.Plugs.OAuthScopesPlug
@ -50,9 +50,15 @@ defmodule Pleroma.Web.MastodonAPI.AnnouncementController do
end
@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}
end
end
@doc "POST /api/v1/announcements/:id"
def show(%{assigns: %{user: user}} = conn, %{id: id} = _params) do

View file

@ -2,11 +2,12 @@
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
defmodule Pleroma.Web.MastodonAPI.AnnouncementControllerTest do
use Pleroma.Web.ConnCase
import Pleroma.Factory
alias Pleroma.Announcement
alias Pleroma.AnnouncementReadRelationship
describe "GET /api/v1/announcements" do
@ -103,4 +104,51 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
assert %{"id" => ^id, "read" => true} = response
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