diff --git a/lib/pleroma/announcement.ex b/lib/pleroma/announcement.ex index b3c88b441..85500751e 100644 --- a/lib/pleroma/announcement.ex +++ b/lib/pleroma/announcement.ex @@ -24,8 +24,7 @@ defmodule Pleroma.Announcement do def change(struct, params \\ %{}) do struct - |> validate_params() - |> cast(params, [:data]) + |> cast(validate_params(params), [:data, :starts_at, :ends_at]) |> validate_required([:data]) end @@ -39,11 +38,8 @@ defp validate_params(params) do Map.merge(base_struct, params.data) |> Map.take(["content", "all_day"]) - %{ - data: merged_data, - starts_at: Map.get(params, "starts_at"), - ends_at: Map.get(params, "ends_at") - } + params + |> Map.merge(%{data: merged_data}) end def add(params) do @@ -92,9 +88,9 @@ def render_json(announcement, opts \\ []) do base = %{ id: announcement.id, content: announcement.data["content"], - starts_at: :null, - ends_at: :null, - all_day: false, + starts_at: announcement.starts_at, + ends_at: announcement.ends_at, + all_day: announcement.data["all_day"], published_at: announcement.inserted_at, updated_at: announcement.updated_at, mentions: [], diff --git a/lib/pleroma/web/admin_api/controllers/announcement_controller.ex b/lib/pleroma/web/admin_api/controllers/announcement_controller.ex index 803408057..ad94e2642 100644 --- a/lib/pleroma/web/admin_api/controllers/announcement_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/announcement_controller.ex @@ -32,12 +32,15 @@ def show(conn, %{id: id} = _params) do end end - def create(%{body_params: %{content: content}} = conn, _params) do - add_params = %{ - data: %{ - "content" => content - } - } + 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 render(conn, "show.json", announcement: announcement) 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 e4212dd06..8179a0e7b 100644 --- a/lib/pleroma/web/api_spec/operations/admin/announcement_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/announcement_operation.ex @@ -95,7 +95,10 @@ def create_request do type: :object, required: [:content], properties: %{ - content: %Schema{type: :string} + 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 diff --git a/lib/pleroma/web/api_spec/schemas/announcement.ex b/lib/pleroma/web/api_spec/schemas/announcement.ex index 433437aac..094fd7c68 100644 --- a/lib/pleroma/web/api_spec/schemas/announcement.ex +++ b/lib/pleroma/web/api_spec/schemas/announcement.ex @@ -16,10 +16,14 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Announcement do id: FlakeID, content: %Schema{type: :string}, starts_at: %Schema{ - oneOf: [%Schema{type: :null}, %Schema{type: :string, format: "date-time"}] + type: :string, + format: "date-time", + nullable: true }, ends_at: %Schema{ - oneOf: [%Schema{type: :null}, %Schema{type: :string, format: "date-time"}] + type: :string, + format: "date-time", + nullable: true }, all_day: %Schema{type: :boolean}, published_at: %Schema{type: :string, format: "date-time"}, diff --git a/test/pleroma/web/admin_api/controllers/announcement_controller_test.exs b/test/pleroma/web/admin_api/controllers/announcement_controller_test.exs index d667526c3..5c9d50120 100644 --- a/test/pleroma/web/admin_api/controllers/announcement_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/announcement_controller_test.exs @@ -80,15 +80,29 @@ test "it returns not found for non-existent id", %{conn: conn} do test "it creates an announcement", %{conn: conn} do content = "test post announcement api" + now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) + starts_at = NaiveDateTime.add(now, -10, :second) + ends_at = NaiveDateTime.add(now, 10, :second) + response = conn |> put_req_header("content-type", "application/json") |> post("/api/v1/pleroma/admin/announcements", %{ - "content" => content + "content" => content, + "starts_at" => NaiveDateTime.to_iso8601(starts_at), + "ends_at" => NaiveDateTime.to_iso8601(ends_at), + "all_day" => true }) |> json_response_and_validate_schema(:ok) - assert %{"content" => ^content} = response + assert %{"content" => ^content, "all_day" => true} = response + + announcement = Pleroma.Announcement.get_by_id(response["id"]) + + assert not is_nil(announcement) + + assert NaiveDateTime.compare(announcement.starts_at, starts_at) == :eq + assert NaiveDateTime.compare(announcement.ends_at, ends_at) == :eq end end end diff --git a/test/support/factory.ex b/test/support/factory.ex index 620102bbb..64b0049ac 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -630,7 +630,7 @@ def filter_factory do def announcement_factory(params \\ %{}, data \\ %{}) do %Pleroma.Announcement{ - data: Map.merge(%{"content" => "test announcement"}, data) + data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data) } |> Map.merge(params) end