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
struct
|> validate_params()
|> cast(params, [:data])
|> cast(validate_params(params), [:data, :starts_at, :ends_at])
|> validate_required([:data])
end
@ -39,11 +38,8 @@ defmodule Pleroma.Announcement 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 @@ defmodule Pleroma.Announcement 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: [],

View File

@ -32,12 +32,15 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementController 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)

View File

@ -95,7 +95,10 @@ defmodule Pleroma.Web.ApiSpec.Admin.AnnouncementOperation 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

View File

@ -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"},

View File

@ -80,15 +80,29 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementControllerTest 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

View File

@ -630,7 +630,7 @@ defmodule Pleroma.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