Add starts_at, ends_at and all_day parameters

This commit is contained in:
Tusooa Zhu 2022-03-08 19:12:01 -05:00
parent fcf3c9057e
commit cf8334dbc1
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
6 changed files with 42 additions and 22 deletions

View File

@ -24,8 +24,7 @@ defmodule Pleroma.Announcement do
def change(struct, params \\ %{}) do def change(struct, params \\ %{}) do
struct struct
|> validate_params() |> cast(validate_params(params), [:data, :starts_at, :ends_at])
|> cast(params, [:data])
|> validate_required([:data]) |> validate_required([:data])
end end
@ -39,11 +38,8 @@ defp validate_params(params) do
Map.merge(base_struct, params.data) Map.merge(base_struct, params.data)
|> Map.take(["content", "all_day"]) |> Map.take(["content", "all_day"])
%{ params
data: merged_data, |> Map.merge(%{data: merged_data})
starts_at: Map.get(params, "starts_at"),
ends_at: Map.get(params, "ends_at")
}
end end
def add(params) do def add(params) do
@ -92,9 +88,9 @@ def render_json(announcement, opts \\ []) do
base = %{ base = %{
id: announcement.id, id: announcement.id,
content: announcement.data["content"], content: announcement.data["content"],
starts_at: :null, starts_at: announcement.starts_at,
ends_at: :null, ends_at: announcement.ends_at,
all_day: false, all_day: announcement.data["all_day"],
published_at: announcement.inserted_at, published_at: announcement.inserted_at,
updated_at: announcement.updated_at, updated_at: announcement.updated_at,
mentions: [], mentions: [],

View File

@ -32,12 +32,15 @@ def show(conn, %{id: id} = _params) do
end end
end end
def create(%{body_params: %{content: content}} = conn, _params) do def create(%{body_params: params} = conn, _params) do
add_params = %{ data =
data: %{ %{}
"content" => content |> 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(add_params) do
render(conn, "show.json", announcement: announcement) render(conn, "show.json", announcement: announcement)

View File

@ -95,7 +95,10 @@ def create_request do
type: :object, type: :object,
required: [:content], required: [:content],
properties: %{ 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 end

View File

@ -16,10 +16,14 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Announcement do
id: FlakeID, id: FlakeID,
content: %Schema{type: :string}, content: %Schema{type: :string},
starts_at: %Schema{ starts_at: %Schema{
oneOf: [%Schema{type: :null}, %Schema{type: :string, format: "date-time"}] type: :string,
format: "date-time",
nullable: true
}, },
ends_at: %Schema{ 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}, all_day: %Schema{type: :boolean},
published_at: %Schema{type: :string, format: "date-time"}, published_at: %Schema{type: :string, format: "date-time"},

View File

@ -80,15 +80,29 @@ test "it returns not found for non-existent id", %{conn: conn} 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"
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
starts_at = NaiveDateTime.add(now, -10, :second)
ends_at = NaiveDateTime.add(now, 10, :second)
response = response =
conn conn
|> put_req_header("content-type", "application/json") |> put_req_header("content-type", "application/json")
|> post("/api/v1/pleroma/admin/announcements", %{ |> 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) |> 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 end
end end

View File

@ -630,7 +630,7 @@ def filter_factory do
def announcement_factory(params \\ %{}, data \\ %{}) do def announcement_factory(params \\ %{}, data \\ %{}) do
%Pleroma.Announcement{ %Pleroma.Announcement{
data: Map.merge(%{"content" => "test announcement"}, data) data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
} }
|> Map.merge(params) |> Map.merge(params)
end end