diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/announcement.ex | 22 | ||||
-rw-r--r-- | lib/pleroma/web/admin_api/controllers/announcement_controller.ex | 29 | ||||
-rw-r--r-- | lib/pleroma/web/api_spec/operations/admin/announcement_operation.ex | 49 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 1 |
4 files changed, 81 insertions, 20 deletions
diff --git a/lib/pleroma/announcement.ex b/lib/pleroma/announcement.ex index 85500751e..8c15d5bdf 100644 --- a/lib/pleroma/announcement.ex +++ b/lib/pleroma/announcement.ex @@ -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() diff --git a/lib/pleroma/web/admin_api/controllers/announcement_controller.ex b/lib/pleroma/web/admin_api/controllers/announcement_controller.ex index ad94e2642..6195e582a 100644 --- a/lib/pleroma/web/admin_api/controllers/announcement_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/announcement_controller.ex @@ -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,18 +33,33 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController do end def create(%{body_params: params} = conn, _params) do + with {:ok, announcement} <- Announcement.add(change_params(params)) do + render(conn, "show.json", announcement: announcement) + else + _ -> + {:error, 400} + end + end + + def change_params(orig_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)) + |> 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)) - add_params = - params - |> Map.merge(%{data: data}) + orig_params + |> Map.merge(%{data: data}) + end - with {:ok, announcement} <- Announcement.add(add_params) do + 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 diff --git a/lib/pleroma/web/api_spec/operations/admin/announcement_operation.ex b/lib/pleroma/web/api_spec/operations/admin/announcement_operation.ex index 8179a0e7b..cdf04d357 100644 --- a/lib/pleroma/web/api_spec/operations/admin/announcement_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/announcement_operation.ex @@ -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 diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 51a9dec6b..af56494a2 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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 |