Validate event datetime

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-08-21 23:23:35 +02:00
parent b8e44cb231
commit ff5ddd040e

View file

@ -102,6 +102,7 @@ def event(user, params) do
|> to_and_cc()
|> context()
|> event_object()
|> with_valid(&event_date/1)
|> with_valid(&changes/1)
|> validate()
end
@ -117,8 +118,8 @@ defp event_object(draft) do
|> Map.put("cc", draft.cc)
|> Map.put("content", draft.content_html)
|> Map.put("actor", draft.user.ap_id)
|> Map.put("startTime", draft.params[:start_time])
|> Map.put("endTime", draft.params[:end_time])
# |> Map.put("startTime", draft.params[:start_time])
# |> Map.put("endTime", draft.params[:end_time])
|> Map.put("joinMode", draft.params[:join_mode])
|> Map.put("tag", Keyword.values(draft.tags) |> Enum.uniq())
|> Map.put("emoji", emoji)
@ -326,6 +327,35 @@ defp changes(draft) do
%__MODULE__{draft | changes: changes}
end
defp event_date(draft) do
case draft.params[:start_time] do
%DateTime{} = start_time ->
case draft.params[:end_time] do
%DateTime{} = end_time ->
if DateTime.compare(end_time, start_time) == :lt do
add_error(draft, dgettext("errors", "Event can't end before its start"))
else
object =
draft.object
|> Map.put("startTime", start_time)
|> Map.put("endTime", end_time)
%__MODULE__{draft | object: object}
end
_ ->
object =
draft.object
|> Map.put("startTime", start_time)
%__MODULE__{draft | object: object}
end
_ ->
add_error(draft, dgettext("errors", "Start date is required"))
end
end
defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
defp with_valid(draft, _func), do: draft