Skip to content

Commit

Permalink
fix(server) - add publish date (#58)
Browse files Browse the repository at this point in the history
implements #57
  • Loading branch information
dnkbln authored Dec 29, 2024
1 parent 1608f2e commit 2880fd0
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 7 deletions.
1 change: 1 addition & 0 deletions client/src/sagas/import.saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function* fetchEpisodeDetails({ payload }: Action<string>) {
explicit: get(episode, 'explicit', null),
duration: get(episode, 'duration', null),
cover: get(episode, 'cover', null),
pub_date: get(episode, 'pub_date', null),
transcript: {
language: get(episode, ['transcript', 'language'], null),
rel: get(episode, ['transcript', 'rel'], null),
Expand Down
1 change: 1 addition & 0 deletions client/src/types/episode.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface EpisodeDetailsPayload {
explicit: boolean;
duration: string;
cover: string;
pub_date: string;
transcript: {
language: string;
rel: string;
Expand Down
3 changes: 2 additions & 1 deletion server/lib/publisher/feed_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ defmodule Publisher.FeedParser do
cover: episode.image_url,
chapters: episode.chapters,
transcript: transcript(episode),
contributors: episode.contributors
contributors: episode.contributors,
pub_date: episode.pub_date
}
}}
end
Expand Down
51 changes: 48 additions & 3 deletions server/lib/publisher/wordpress/episode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,57 @@ defmodule Publisher.WordPress.Episode do
Enum.reject(map, fn {_, v} -> is_nil(v) end)
end

defp upload_content(req, post_id, %{"content" => content} = _params)
when not is_nil(content) do
defp wordpress_date(date) do
date
|> parse_date()
|> DateTime.to_iso8601()
end

defp parse_date(date) do
formats = [
"{RFC822}",
"{RFC822z}",
"{RFC1123}",
"{RFC1123z}",
"{RFC3339}",
"{RFC3339z}",
"{ISO:Extended}",
"{ISO:Extended:Z}"
]

Enum.find_value(formats, fn format ->
case Timex.parse(date, format) do
{:ok, date} -> date
{:error, _} -> false
end
end)
end

defp upload_content(req, post_id, %{"content" => content, "pub_date" => pub_date} = _params)
when not is_nil(content) and not is_nil(pub_date) do
Logger.info("Episode post #{post_id} content is #{String.length(content)}")
Logger.info("Episode post #{post_id} release date is #{pub_date}")

payload = %{
content: content,
date: wordpress_date(pub_date)
}

Req.post(req,
url: "wp/v2/episodes/#{post_id}",
json: payload
)

:ok
end

defp upload_content(req, post_id, %{"content" => content, "pub_date" => pub_date} = _params)
when is_nil(content) and not is_nil(pub_date) do
Logger.info("Episode post has no post content")
Logger.info("Episode post #{post_id} release date is #{pub_date}")

payload = %{
content: content
date: wordpress_date(pub_date)
}

Req.post(req,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule PublisherWeb.Controllers.Validator.SaveChapters do
if normalplaytime_valid?(value) do
[]
else
[{field, "is not conform with NormalPlayTime"}]
[{field, " is not conform with NormalPlayTime"}]
end
end)
end
Expand Down
34 changes: 33 additions & 1 deletion server/lib/publisher_web/controllers/validator/save_episode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,46 @@ defmodule PublisherWeb.Controllers.Validator.SaveEpisode do
field(:slug, :string)
field(:content, :string)
field(:cover, :string)
field(:pub_date, :string)
end

@allowed_attrs [:guid, :title, :subtitle, :summary, :slug, :content, :cover]
@allowed_attrs [:guid, :title, :subtitle, :summary, :slug, :content, :cover, :pub_date]
@required_attrs [:guid, :title, :slug]

def changeset(attrs) do
%__MODULE__{}
|> cast(attrs, @allowed_attrs)
|> validate_required(@required_attrs)
|> validate_date(:pub_date)
end

defp validate_date(changeset, field) do
validate_change(changeset, field, fn _, value ->
if publication_date_valid?(value) do
[]
else
[{field, " is not a conformed date"}]
end
end)
end

defp publication_date_valid?(field) do
formats = [
"{RFC822}",
"{RFC822z}",
"{RFC1123}",
"{RFC1123z}",
"{RFC3339}",
"{RFC3339z}",
"{ISO:Extended}",
"{ISO:Extended:Z}"
]

Enum.find_value(formats, fn format ->
case Timex.parse(field, format) do
{:ok, field} -> field
{:error, _} -> false
end
end)
end
end
3 changes: 2 additions & 1 deletion server/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ defmodule Publisher.MixProject do
{:slugify, "~> 1.3"},
{:nimble_options, "~> 1.1"},
{:metalove, git: "https://github.com/podlove/metalove", branch: "master"},
{:honeybadger, "~> 0.22"}
{:honeybadger, "~> 0.22"},
{:timex, "~> 3.7"}
]
end
end

0 comments on commit 2880fd0

Please sign in to comment.