Skip to content

Commit f7cf54e

Browse files
committed
iterate
1 parent f0ea6e9 commit f7cf54e

4 files changed

Lines changed: 38 additions & 25 deletions

File tree

apps/cf/lib/comments/comments.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ defmodule CF.Comments do
9393
full_comment = comment
9494

9595
# If new source, fetch metadata
96-
if source && is_nil(Map.get(source, :id)),
97-
do: fetch_source_metadata_and_update_comment(comment, source_fetch_callback)
96+
if source && is_nil(Map.get(source, :id)) do
97+
callback = source_fetch_callback || fn _comment -> :ok end
98+
fetch_source_metadata_and_update_comment(comment, callback)
99+
end
98100

99101
# Return comment
100102
full_comment
@@ -266,11 +268,14 @@ defmodule CF.Comments do
266268
defp reverse_vote_type(:vote_down), do: :revert_vote_down
267269
defp reverse_vote_type(:self_vote), do: :revert_self_vote
268270

269-
defp fetch_source_metadata_and_update_comment(%Comment{source: nil}, _), do: nil
271+
defp fetch_source_metadata_and_update_comment(%Comment{source: nil}, _callback), do: nil
270272

271273
defp fetch_source_metadata_and_update_comment(comment = %Comment{source: base_source}, callback) do
272274
Sources.update_source_metadata(base_source, fn updated_source ->
273-
callback.(Map.merge(comment, %{source: updated_source, source_id: updated_source.id}))
275+
updated_comment =
276+
Map.merge(comment, %{source: updated_source, source_id: updated_source.id})
277+
278+
callback.(updated_comment)
274279
end)
275280
end
276281
end

apps/cf/lib/sources/fetcher.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule CF.Sources.Fetcher do
1616
start: {__MODULE__, :start_link, [opts]},
1717
type: :supervisor,
1818
restart: :permanent,
19-
shutdown: 500
19+
shutdown: 2000
2020
}
2121
end
2222

@@ -60,15 +60,15 @@ defmodule CF.Sources.Fetcher do
6060

6161
def get_queue, do: Fetcher.LinkChecker.get_queue()
6262

63-
@url_regex ~r/^https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/
64-
6563
defp fetch(url, callback) do
66-
without_domain = Regex.replace(@url_regex, url, "\\1")
67-
path = Regex.replace(~r/\?.+$/, without_domain, "")
64+
uri = URI.parse(url)
65+
66+
case do_fetch_source_metadata(url, MIME.from_path(uri.path)) do
67+
{:error, err} ->
68+
:error
6869

69-
case do_fetch_source_metadata(url, MIME.from_path(path)) do
70-
{:error, _} -> :error
71-
{:ok, result} -> callback.(result)
70+
{:ok, result} ->
71+
callback.(result)
7272
end
7373
end
7474

@@ -77,13 +77,13 @@ defmodule CF.Sources.Fetcher do
7777
defp do_fetch_source_metadata(url, mime_types) when mime_types in @fetchable_mime_types do
7878
case HTTPoison.get(
7979
url,
80-
[],
80+
[{"User-Agent", "CaptainFact/#{CF.Application.version()}"}],
8181
follow_redirect: true,
8282
max_redirect: 5,
8383
hackney: [pool: pool_name()]
8484
) do
8585
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
86-
{:ok, source_params_from_tree(Floki.parse_document(body))}
86+
{:ok, source_params_from_tree(Floki.parse_document!(body))}
8787

8888
{:ok, %HTTPoison.Response{status_code: 404}} ->
8989
{:error, :not_found}

apps/cf_graphql/lib/resolvers/comments.ex

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,25 @@ defmodule CF.Graphql.Resolvers.Comments do
4141

4242
source_url = Map.get(args, :source)
4343

44-
# Comments.add_comment returns the comment directly or {:error, reason}
45-
case Comments.add_comment(user, video_id, params, source_url) do
46-
{:error, reason} ->
47-
{:error, reason}
48-
49-
comment ->
50-
# Preload associations for GraphQL response
51-
comment = Repo.preload(comment, [:source, :user, :statement])
44+
# Callback to broadcast comment_updated when source metadata is fetched
45+
update_callback = fn updated_comment ->
46+
updated_comment = Repo.preload(updated_comment, [:source, :user, :statement])
47+
Subscriptions.publish_comment_updated(updated_comment, video_id)
48+
end
5249

53-
Subscriptions.publish_comment_added(comment, video_id)
54-
{:ok, comment}
50+
try do
51+
case Comments.add_comment(user, video_id, params, source_url, update_callback) do
52+
{:error, reason} ->
53+
{:error, reason}
54+
55+
comment ->
56+
comment = Repo.preload(comment, [:source, :user, :statement])
57+
Subscriptions.publish_comment_added(comment, video_id)
58+
{:ok, comment}
59+
end
60+
rescue
61+
exception ->
62+
{:error, exception}
5563
end
5664
end
5765

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule CF.Umbrella.Mixfile do
33

44
def project do
55
[
6-
version: "1.2.0",
6+
version: "2.0.0",
77
apps_path: "apps",
88
deps_path: "deps",
99
build_embedded: Mix.env() == :prod,

0 commit comments

Comments
 (0)