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
|
||||
struct
|
||||
|> cast(validate_params(params), [:data, :starts_at, :ends_at])
|
||||
|> cast(validate_params(struct, params), [:data, :starts_at, :ends_at])
|
||||
|> validate_required([:data])
|
||||
end
|
||||
|
||||
defp validate_params(params) do
|
||||
base_struct = %{
|
||||
"content" => "",
|
||||
"all_day" => false
|
||||
}
|
||||
defp validate_params(struct, params) do
|
||||
base_data =
|
||||
%{
|
||||
"content" => "",
|
||||
"all_day" => false
|
||||
}
|
||||
|> Map.merge((struct && struct.data) || %{})
|
||||
|
||||
merged_data =
|
||||
Map.merge(base_struct, params.data)
|
||||
Map.merge(base_data, params.data)
|
||||
|> Map.take(["content", "all_day"])
|
||||
|
||||
params
|
||||
|
|
@ -48,6 +50,12 @@ defmodule Pleroma.Announcement do
|
|||
Repo.insert(changeset)
|
||||
end
|
||||
|
||||
def update(announcement, params) do
|
||||
changeset = change(announcement, params)
|
||||
|
||||
Repo.update(changeset)
|
||||
end
|
||||
|
||||
def list_all do
|
||||
__MODULE__
|
||||
|> Repo.all()
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do
|
|||
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
||||
|
||||
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])
|
||||
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
||||
|
||||
|
|
@ -33,16 +33,7 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do
|
|||
end
|
||||
|
||||
def create(%{body_params: params} = conn, _params) do
|
||||
data =
|
||||
%{}
|
||||
|> 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
|
||||
with {:ok, announcement} <- Announcement.add(change_params(params)) do
|
||||
render(conn, "show.json", announcement: announcement)
|
||||
else
|
||||
_ ->
|
||||
|
|
@ -50,6 +41,30 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do
|
|||
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
|
||||
case Announcement.delete_by_id(id) do
|
||||
:ok ->
|
||||
|
|
|
|||
|
|
@ -89,17 +89,54 @@ defmodule Pleroma.Web.ApiSpec.Admin.AnnouncementOperation do
|
|||
}
|
||||
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
|
||||
%Schema{
|
||||
title: "AnnouncementCreateRequest",
|
||||
type: :object,
|
||||
required: [:content],
|
||||
properties: %{
|
||||
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}
|
||||
}
|
||||
properties: create_or_change_props()
|
||||
}
|
||||
end
|
||||
|
||||
def change_request do
|
||||
%Schema{
|
||||
title: "AnnouncementChangeRequest",
|
||||
type: :object,
|
||||
properties: create_or_change_props()
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -233,6 +233,7 @@ defmodule Pleroma.Web.Router do
|
|||
get("/announcements", AnnouncementController, :index)
|
||||
post("/announcements", AnnouncementController, :create)
|
||||
get("/announcements/:id", AnnouncementController, :show)
|
||||
patch("/announcements/:id", AnnouncementController, :change)
|
||||
delete("/announcements/:id", AnnouncementController, :delete)
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue