Implement update announcement admin api
This commit is contained in:
parent
881179ec72
commit
11a1996bf5
6 changed files with 169 additions and 26 deletions
|
@ -24,18 +24,20 @@ defmodule Pleroma.Announcement do
|
||||||
|
|
||||||
def change(struct, params \\ %{}) do
|
def change(struct, params \\ %{}) do
|
||||||
struct
|
struct
|
||||||
|> cast(validate_params(params), [:data, :starts_at, :ends_at])
|
|> cast(validate_params(struct, params), [:data, :starts_at, :ends_at])
|
||||||
|> validate_required([:data])
|
|> validate_required([:data])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_params(params) do
|
defp validate_params(struct, params) do
|
||||||
base_struct = %{
|
base_data =
|
||||||
|
%{
|
||||||
"content" => "",
|
"content" => "",
|
||||||
"all_day" => false
|
"all_day" => false
|
||||||
}
|
}
|
||||||
|
|> Map.merge((struct && struct.data) || %{})
|
||||||
|
|
||||||
merged_data =
|
merged_data =
|
||||||
Map.merge(base_struct, params.data)
|
Map.merge(base_data, params.data)
|
||||||
|> Map.take(["content", "all_day"])
|
|> Map.take(["content", "all_day"])
|
||||||
|
|
||||||
params
|
params
|
||||||
|
@ -48,6 +50,12 @@ defmodule Pleroma.Announcement do
|
||||||
Repo.insert(changeset)
|
Repo.insert(changeset)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update(announcement, params) do
|
||||||
|
changeset = change(announcement, params)
|
||||||
|
|
||||||
|
Repo.update(changeset)
|
||||||
|
end
|
||||||
|
|
||||||
def list_all do
|
def list_all do
|
||||||
__MODULE__
|
__MODULE__
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|
|
|
@ -10,7 +10,7 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do
|
||||||
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
||||||
|
|
||||||
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
||||||
plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action in [:create, :delete])
|
plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action in [:create, :delete, :change])
|
||||||
plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action in [:index, :show])
|
plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action in [:index, :show])
|
||||||
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
||||||
|
|
||||||
|
@ -33,16 +33,7 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(%{body_params: params} = conn, _params) do
|
def create(%{body_params: params} = conn, _params) do
|
||||||
data =
|
with {:ok, announcement} <- Announcement.add(change_params(params)) do
|
||||||
%{}
|
|
||||||
|> Pleroma.Maps.put_if_present("content", params, &Map.fetch(&1, :content))
|
|
||||||
|> Pleroma.Maps.put_if_present("all_day", params, &Map.fetch(&1, :all_day))
|
|
||||||
|
|
||||||
add_params =
|
|
||||||
params
|
|
||||||
|> Map.merge(%{data: data})
|
|
||||||
|
|
||||||
with {:ok, announcement} <- Announcement.add(add_params) do
|
|
||||||
render(conn, "show.json", announcement: announcement)
|
render(conn, "show.json", announcement: announcement)
|
||||||
else
|
else
|
||||||
_ ->
|
_ ->
|
||||||
|
@ -50,6 +41,30 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def change_params(orig_params) do
|
||||||
|
data =
|
||||||
|
%{}
|
||||||
|
|> Pleroma.Maps.put_if_present("content", orig_params, &Map.fetch(&1, :content))
|
||||||
|
|> Pleroma.Maps.put_if_present("all_day", orig_params, &Map.fetch(&1, :all_day))
|
||||||
|
|
||||||
|
orig_params
|
||||||
|
|> Map.merge(%{data: data})
|
||||||
|
end
|
||||||
|
|
||||||
|
def change(%{body_params: params} = conn, %{id: id} = _params) do
|
||||||
|
with announcement <- Announcement.get_by_id(id),
|
||||||
|
{:exists, true} <- {:exists, not is_nil(announcement)},
|
||||||
|
{:ok, announcement} <- Announcement.update(announcement, change_params(params)) do
|
||||||
|
render(conn, "show.json", announcement: announcement)
|
||||||
|
else
|
||||||
|
{:exists, false} ->
|
||||||
|
{:error, :not_found}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
{:error, 400}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def delete(conn, %{id: id} = _params) do
|
def delete(conn, %{id: id} = _params) do
|
||||||
case Announcement.delete_by_id(id) do
|
case Announcement.delete_by_id(id) do
|
||||||
:ok ->
|
:ok ->
|
||||||
|
|
|
@ -89,17 +89,54 @@ defmodule Pleroma.Web.ApiSpec.Admin.AnnouncementOperation do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def change_operation do
|
||||||
|
%Operation{
|
||||||
|
tags: ["Announcement managment"],
|
||||||
|
summary: "Change one announcement",
|
||||||
|
operationId: "AdminAPI.AnnouncementController.change",
|
||||||
|
security: [%{"oAuth" => ["admin:write"]}],
|
||||||
|
parameters: [
|
||||||
|
Operation.parameter(
|
||||||
|
:id,
|
||||||
|
:path,
|
||||||
|
:string,
|
||||||
|
"announcement id"
|
||||||
|
)
|
||||||
|
| admin_api_params()
|
||||||
|
],
|
||||||
|
requestBody: request_body("Parameters", change_request(), required: true),
|
||||||
|
responses: %{
|
||||||
|
200 => Operation.response("Response", "application/json", Announcement),
|
||||||
|
400 => Operation.response("Bad Request", "application/json", ApiError),
|
||||||
|
403 => Operation.response("Forbidden", "application/json", ApiError),
|
||||||
|
404 => Operation.response("Not Found", "application/json", ApiError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp create_or_change_props do
|
||||||
|
%{
|
||||||
|
content: %Schema{type: :string},
|
||||||
|
starts_at: %Schema{type: :string, format: "date-time", nullable: true},
|
||||||
|
ends_at: %Schema{type: :string, format: "date-time", nullable: true},
|
||||||
|
all_day: %Schema{type: :boolean}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def create_request do
|
def create_request do
|
||||||
%Schema{
|
%Schema{
|
||||||
title: "AnnouncementCreateRequest",
|
title: "AnnouncementCreateRequest",
|
||||||
type: :object,
|
type: :object,
|
||||||
required: [:content],
|
required: [:content],
|
||||||
properties: %{
|
properties: create_or_change_props()
|
||||||
content: %Schema{type: :string},
|
|
||||||
starts_at: %Schema{type: :string, format: "date-time"},
|
|
||||||
ends_at: %Schema{type: :string, format: "date-time"},
|
|
||||||
all_day: %Schema{type: :boolean}
|
|
||||||
}
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def change_request do
|
||||||
|
%Schema{
|
||||||
|
title: "AnnouncementChangeRequest",
|
||||||
|
type: :object,
|
||||||
|
properties: create_or_change_props()
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -233,6 +233,7 @@ defmodule Pleroma.Web.Router do
|
||||||
get("/announcements", AnnouncementController, :index)
|
get("/announcements", AnnouncementController, :index)
|
||||||
post("/announcements", AnnouncementController, :create)
|
post("/announcements", AnnouncementController, :create)
|
||||||
get("/announcements/:id", AnnouncementController, :show)
|
get("/announcements/:id", AnnouncementController, :show)
|
||||||
|
patch("/announcements/:id", AnnouncementController, :change)
|
||||||
delete("/announcements/:id", AnnouncementController, :delete)
|
delete("/announcements/:id", AnnouncementController, :delete)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -69,13 +69,91 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementControllerTest do
|
||||||
|
|
||||||
_response =
|
_response =
|
||||||
conn
|
conn
|
||||||
|> get("/api/v1/pleroma/admin/announcements/#{id}xxx")
|
|> delete("/api/v1/pleroma/admin/announcements/#{id}xxx")
|
||||||
|> json_response_and_validate_schema(:not_found)
|
|> json_response_and_validate_schema(:not_found)
|
||||||
|
|
||||||
assert %{id: ^id} = Pleroma.Announcement.get_by_id(id)
|
assert %{id: ^id} = Pleroma.Announcement.get_by_id(id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "PATCH /api/v1/pleroma/admin/announcements/:id" do
|
||||||
|
test "it returns not found for non-existent id", %{conn: conn} do
|
||||||
|
%{id: id} = insert(:announcement)
|
||||||
|
|
||||||
|
_response =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> patch("/api/v1/pleroma/admin/announcements/#{id}xxx", %{})
|
||||||
|
|> json_response_and_validate_schema(:not_found)
|
||||||
|
|
||||||
|
assert %{id: ^id} = Pleroma.Announcement.get_by_id(id)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it updates a field", %{conn: conn} do
|
||||||
|
%{id: id} = insert(:announcement)
|
||||||
|
|
||||||
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||||
|
starts_at = NaiveDateTime.add(now, -10, :second)
|
||||||
|
|
||||||
|
_response =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
|
||||||
|
starts_at: NaiveDateTime.to_iso8601(starts_at)
|
||||||
|
})
|
||||||
|
|> json_response_and_validate_schema(:ok)
|
||||||
|
|
||||||
|
new = Pleroma.Announcement.get_by_id(id)
|
||||||
|
|
||||||
|
assert NaiveDateTime.compare(new.starts_at, starts_at) == :eq
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it updates a data field", %{conn: conn} do
|
||||||
|
%{id: id} = announcement = insert(:announcement, data: %{"all_day" => true})
|
||||||
|
|
||||||
|
assert announcement.data["all_day"] == true
|
||||||
|
|
||||||
|
new_content = "new content"
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
|
||||||
|
content: new_content
|
||||||
|
})
|
||||||
|
|> json_response_and_validate_schema(:ok)
|
||||||
|
|
||||||
|
assert response["content"] == new_content
|
||||||
|
assert response["all_day"] == true
|
||||||
|
|
||||||
|
new = Pleroma.Announcement.get_by_id(id)
|
||||||
|
|
||||||
|
assert new.data["content"] == new_content
|
||||||
|
assert new.data["all_day"] == true
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it nullifies a nullable field", %{conn: conn} do
|
||||||
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||||
|
starts_at = NaiveDateTime.add(now, -10, :second)
|
||||||
|
|
||||||
|
%{id: id} = insert(:announcement, starts_at: starts_at)
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
|
||||||
|
starts_at: nil
|
||||||
|
})
|
||||||
|
|> json_response_and_validate_schema(:ok)
|
||||||
|
|
||||||
|
assert response["starts_at"] == nil
|
||||||
|
|
||||||
|
new = Pleroma.Announcement.get_by_id(id)
|
||||||
|
|
||||||
|
assert new.starts_at == nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "POST /api/v1/pleroma/admin/announcements" do
|
describe "POST /api/v1/pleroma/admin/announcements" do
|
||||||
test "it creates an announcement", %{conn: conn} do
|
test "it creates an announcement", %{conn: conn} do
|
||||||
content = "test post announcement api"
|
content = "test post announcement api"
|
||||||
|
|
|
@ -628,7 +628,11 @@ defmodule Pleroma.Factory do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def announcement_factory(params \\ %{}, data \\ %{}) do
|
def announcement_factory(params \\ %{}) do
|
||||||
|
data = Map.get(params, :data, %{})
|
||||||
|
|
||||||
|
{_, params} = Map.pop(params, :data)
|
||||||
|
|
||||||
%Pleroma.Announcement{
|
%Pleroma.Announcement{
|
||||||
data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
|
data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue