Skip to content

Commit

Permalink
Merge 161b6ed into 02da7b0
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Sep 3, 2018
2 parents 02da7b0 + 161b6ed commit 7cb1474
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 65 deletions.
82 changes: 23 additions & 59 deletions apps/cf_atom_feed/lib/statements.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ defmodule CF.AtomFeed.Statements do

alias Atomex.{Feed, Entry}
alias DB.Schema.Statement
alias CF.Utils.FrontendRouter

@nb_items_max 50

@url "https://captainfact.io"

@doc """
Get an RSS feed containing all site's statements in reverse chronological
order (newest first)
Expand All @@ -24,56 +23,53 @@ defmodule CF.AtomFeed.Statements do
|> order_by([s, v, sp], desc: [s.id])
|> select([s, v, sp], %{
id: s.id,
time_code: s.time,
content: s.text,
creation_time: s.inserted_at,
speaker_id: sp.id,
speaker: sp.full_name,
video_id: v.id,
video_title: v.title
time: s.time,
text: s.text,
inserted_at: s.inserted_at,
speaker: %{
id: sp.id,
slug: sp.slug,
full_name: sp.full_name,
},
video: %{
hash_id: v.hash_id,
title: v.title
}
})
|> limit(@nb_items_max)
|> DB.Repo.all()
end

defp last_update(_statements = [statement | _]),
do: DateTime.from_naive!(statement.creation_time, "Etc/UTC")
do: DateTime.from_naive!(statement.inserted_at, "Etc/UTC")

defp last_update(_),
do: DateTime.utc_now()

defp generate_feed(statements, last_update) do
Feed.new("https://captainfact.io/", last_update, "[CaptainFact] All Statements")
|> Feed.author("Captain Fact", email: "atom-feed@captainfact.io")
|> Feed.author("CaptainFact", email: "atom-feed@captainfact.io")
|> Feed.link("https://feed.captainfact.io/statements/", rel: "self")
|> Feed.entries(Enum.map(statements, &get_entry/1))
|> Feed.build()
|> Atomex.generate_document()
end

defp get_entry(statement) do
title = "New statement on ##{statement.video_id}"
insert_datetime = DateTime.from_naive!(statement.creation_time, "Etc/UTC")
link_statement = statement_url(statement)
link_video = video_url(statement)
title = "New statement for video #{statement.video.title}"
insert_datetime = DateTime.from_naive!(statement.inserted_at, "Etc/UTC")
link_statement = FrontendRouter.statement_url(statement.video.hash_id, statement.id)

Entry.new(link_statement, insert_datetime, title)
|> Entry.link(link_statement)
|> Entry.published(insert_datetime)
|> Entry.content(
"""
<div>ID: #{statement.id}</div>
<div>Time code: #{timecode_to_time(statement.time_code)}</div>
<div>Text: #{statement.content}</div>
<div>Posted at: #{insert_datetime}</div>
<div>Video title: #{statement.video_title}</div>
""" <>
speaker_info(statement) <>
"""
<div>#{link(link_video, "Video link")}</div>
<div>#{link(link_statement, "Statement link")}</div>
""",
type: "html"
At #{timecode_to_time(statement.time)}:
```
#{statement.text}
```
"""
)
|> Entry.build()
end
Expand All @@ -96,36 +92,4 @@ defmodule CF.AtomFeed.Statements do

"#{hours_time_code}:#{minutes_time_code}:#{seconds_time_code}"
end

defp speaker_info(%{speaker_id: nil}), do: ""

defp speaker_info(statement) do
link_speaker = speaker_url(statement)

"""
<div>Speaker : #{statement.speaker}</div>
<div>#{link(link_speaker, "Speaker profile")}</div>
"""
end

defp link(url, text),
do: "<a href='#{url}'>#{text}</a>"

defp statement_url(nil) do
"#{@url}/"
end

defp statement_url(statement) do
video_hash_id = DB.Type.VideoHashId.encode(statement.video_id)
"#{@url}/videos/#{video_hash_id}?statement=#{statement.id}"
end

defp speaker_url(statement) do
"#{@url}/s/#{statement.speaker_id}"
end

defp video_url(statement) do
video_hash_id = DB.Type.VideoHashId.encode(statement.video_id)
"#{@url}/videos/#{video_hash_id}"
end
end
9 changes: 4 additions & 5 deletions apps/cf_atom_feed/test/statements_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule CF.AtomFeed.StatementsTest do
<feed xmlns="http://www.w3.org/2005/Atom">
<link href="https://feed.captainfact.io/statements/" rel="self"/>
<author>
<name>Captain Fact</name>
<name>CaptainFact</name>
<email>atom-feed@captainfact.io</email>
</author>
<id>https://captainfact.io/</id>
Expand All @@ -29,10 +29,9 @@ defmodule CF.AtomFeed.StatementsTest do

# Check comment entries
for statement <- statements do
video_id = DB.Type.VideoHashId.encode(statement.video_id)
statement_url = "https://captainfact\.io/videos/#{video_id}?statement=#{statement.id}"
assert feed =~ "<link href=\"#{statement_url}\"/>"
assert feed =~ "<title>New statement on ##{statement.video.id}</title>"
statement_url = "https://captainfact\.io/videos/#{statement.video.hash_id}?statement=#{statement.id}"
assert feed =~ statement_url
assert feed =~ "<title>New statement for video #{statement.video.title}</title>"
end
end
end
15 changes: 14 additions & 1 deletion apps/cf_utils/lib/frontend_router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule CF.Utils.FrontendRouter do
"""

alias DB.Schema.User
alias DB.Schema.Speaker
alias DB.Schema.Comment

@doc """
Expand All @@ -23,9 +24,21 @@ defmodule CF.Utils.FrontendRouter do
"""
def video_url(video_hash_id), do: base_url() <> "videos/#{video_hash_id}"

@doc """
Comment's URL
"""
def statement_url(video_hash_id, statement_id),
do: video_url(video_hash_id) <> "?statement=#{statement_id}"

@doc """
Comment's URL
"""
def comment_url(video_hash_id, %Comment{statement: statement}),
do: video_url(video_hash_id) <> "?statement=#{statement.id}"
do: statement_url(video_hash_id, statement.id)

@doc """
Speaker URL
"""
def speaker_url(%Speaker{slug: slug, id: id}),
do: base_url() <> "s/#{slug || id}"
end

0 comments on commit 7cb1474

Please sign in to comment.