Skip to content

Commit 2880fd0

Browse files
authored
fix(server) - add publish date (#58)
implements #57
1 parent 1608f2e commit 2880fd0

File tree

7 files changed

+88
-7
lines changed

7 files changed

+88
-7
lines changed

client/src/sagas/import.saga.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ function* fetchEpisodeDetails({ payload }: Action<string>) {
176176
explicit: get(episode, 'explicit', null),
177177
duration: get(episode, 'duration', null),
178178
cover: get(episode, 'cover', null),
179+
pub_date: get(episode, 'pub_date', null),
179180
transcript: {
180181
language: get(episode, ['transcript', 'language'], null),
181182
rel: get(episode, ['transcript', 'rel'], null),

client/src/types/episode.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface EpisodeDetailsPayload {
4646
explicit: boolean;
4747
duration: string;
4848
cover: string;
49+
pub_date: string;
4950
transcript: {
5051
language: string;
5152
rel: string;

server/lib/publisher/feed_parser.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ defmodule Publisher.FeedParser do
8686
cover: episode.image_url,
8787
chapters: episode.chapters,
8888
transcript: transcript(episode),
89-
contributors: episode.contributors
89+
contributors: episode.contributors,
90+
pub_date: episode.pub_date
9091
}
9192
}}
9293
end

server/lib/publisher/wordpress/episode.ex

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,57 @@ defmodule Publisher.WordPress.Episode do
100100
Enum.reject(map, fn {_, v} -> is_nil(v) end)
101101
end
102102

103-
defp upload_content(req, post_id, %{"content" => content} = _params)
104-
when not is_nil(content) do
103+
defp wordpress_date(date) do
104+
date
105+
|> parse_date()
106+
|> DateTime.to_iso8601()
107+
end
108+
109+
defp parse_date(date) do
110+
formats = [
111+
"{RFC822}",
112+
"{RFC822z}",
113+
"{RFC1123}",
114+
"{RFC1123z}",
115+
"{RFC3339}",
116+
"{RFC3339z}",
117+
"{ISO:Extended}",
118+
"{ISO:Extended:Z}"
119+
]
120+
121+
Enum.find_value(formats, fn format ->
122+
case Timex.parse(date, format) do
123+
{:ok, date} -> date
124+
{:error, _} -> false
125+
end
126+
end)
127+
end
128+
129+
defp upload_content(req, post_id, %{"content" => content, "pub_date" => pub_date} = _params)
130+
when not is_nil(content) and not is_nil(pub_date) do
105131
Logger.info("Episode post #{post_id} content is #{String.length(content)}")
132+
Logger.info("Episode post #{post_id} release date is #{pub_date}")
133+
134+
payload = %{
135+
content: content,
136+
date: wordpress_date(pub_date)
137+
}
138+
139+
Req.post(req,
140+
url: "wp/v2/episodes/#{post_id}",
141+
json: payload
142+
)
143+
144+
:ok
145+
end
146+
147+
defp upload_content(req, post_id, %{"content" => content, "pub_date" => pub_date} = _params)
148+
when is_nil(content) and not is_nil(pub_date) do
149+
Logger.info("Episode post has no post content")
150+
Logger.info("Episode post #{post_id} release date is #{pub_date}")
106151

107152
payload = %{
108-
content: content
153+
date: wordpress_date(pub_date)
109154
}
110155

111156
Req.post(req,

server/lib/publisher_web/controllers/validator/save_chapters.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule PublisherWeb.Controllers.Validator.SaveChapters do
2020
if normalplaytime_valid?(value) do
2121
[]
2222
else
23-
[{field, "is not conform with NormalPlayTime"}]
23+
[{field, " is not conform with NormalPlayTime"}]
2424
end
2525
end)
2626
end

server/lib/publisher_web/controllers/validator/save_episode.ex

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,46 @@ defmodule PublisherWeb.Controllers.Validator.SaveEpisode do
99
field(:slug, :string)
1010
field(:content, :string)
1111
field(:cover, :string)
12+
field(:pub_date, :string)
1213
end
1314

14-
@allowed_attrs [:guid, :title, :subtitle, :summary, :slug, :content, :cover]
15+
@allowed_attrs [:guid, :title, :subtitle, :summary, :slug, :content, :cover, :pub_date]
1516
@required_attrs [:guid, :title, :slug]
1617

1718
def changeset(attrs) do
1819
%__MODULE__{}
1920
|> cast(attrs, @allowed_attrs)
2021
|> validate_required(@required_attrs)
22+
|> validate_date(:pub_date)
23+
end
24+
25+
defp validate_date(changeset, field) do
26+
validate_change(changeset, field, fn _, value ->
27+
if publication_date_valid?(value) do
28+
[]
29+
else
30+
[{field, " is not a conformed date"}]
31+
end
32+
end)
33+
end
34+
35+
defp publication_date_valid?(field) do
36+
formats = [
37+
"{RFC822}",
38+
"{RFC822z}",
39+
"{RFC1123}",
40+
"{RFC1123z}",
41+
"{RFC3339}",
42+
"{RFC3339z}",
43+
"{ISO:Extended}",
44+
"{ISO:Extended:Z}"
45+
]
46+
47+
Enum.find_value(formats, fn format ->
48+
case Timex.parse(field, format) do
49+
{:ok, field} -> field
50+
{:error, _} -> false
51+
end
52+
end)
2153
end
2254
end

server/mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ defmodule Publisher.MixProject do
2929
{:slugify, "~> 1.3"},
3030
{:nimble_options, "~> 1.1"},
3131
{:metalove, git: "https://github.com/podlove/metalove", branch: "master"},
32-
{:honeybadger, "~> 0.22"}
32+
{:honeybadger, "~> 0.22"},
33+
{:timex, "~> 3.7"}
3334
]
3435
end
3536
end

0 commit comments

Comments
 (0)