From 24c942c5d214c86ed42a17d6700f6f4593551b72 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 5 Jul 2019 22:00:18 -0400 Subject: [PATCH] Add option to disable remote scraping for feeds --- lib/frenzy/feed.ex | 3 ++- lib/frenzy/update_feeds.ex | 16 +++++++----- lib/frenzy_web/controllers/feed_controller.ex | 13 ++++++++++ lib/frenzy_web/router.ex | 6 ++--- lib/frenzy_web/templates/feed/edit.html.eex | 25 +++++++++++++++++++ lib/frenzy_web/templates/feed/show.html.eex | 17 ++----------- .../templates/filter/rules.html.eex | 0 ...224558_feeds_add_scrape_remote_content.exs | 9 +++++++ 8 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 lib/frenzy_web/templates/feed/edit.html.eex create mode 100644 lib/frenzy_web/templates/filter/rules.html.eex create mode 100644 priv/repo/migrations/20190705224558_feeds_add_scrape_remote_content.exs diff --git a/lib/frenzy/feed.ex b/lib/frenzy/feed.ex index 3bc812c..e899adb 100644 --- a/lib/frenzy/feed.ex +++ b/lib/frenzy/feed.ex @@ -35,6 +35,7 @@ defmodule Frenzy.Feed do field :site_url, :string field :title, :string field :filter_enabled, :boolean + field :scrape_remote_content, :boolean belongs_to :group, Frenzy.Group @@ -47,7 +48,7 @@ defmodule Frenzy.Feed do @doc false def changeset(feed, attrs) do feed - |> cast(attrs, [:title, :feed_url, :site_url, :last_updated, :filter_enabled]) + |> cast(attrs, [:title, :feed_url, :site_url, :last_updated, :filter_enabled, :scrape_remote_content]) |> cast_assoc(:filter, required: true) |> validate_required([:feed_url, :filter]) end diff --git a/lib/frenzy/update_feeds.ex b/lib/frenzy/update_feeds.ex index bc3e612..d519442 100644 --- a/lib/frenzy/update_feeds.ex +++ b/lib/frenzy/update_feeds.ex @@ -124,13 +124,17 @@ defmodule Frenzy.UpdateFeeds do Logger.debug("Creating item for #{url}") content = - case get_article_content(url) do - {:ok, content} -> - content + if feed.scrape_remote_content do + case get_article_content(url) do + {:ok, content} -> + content - {:err, reason} -> - Logger.warn("Unable to fetch article for #{url}: #{inspect(reason)}") - entry.description + {:err, reason} -> + Logger.warn("Unable to fetch article for #{url}: #{inspect(reason)}") + entry.description + end + else + entry.description end item_params = %{ diff --git a/lib/frenzy_web/controllers/feed_controller.ex b/lib/frenzy_web/controllers/feed_controller.ex index fb518f3..70528c2 100644 --- a/lib/frenzy_web/controllers/feed_controller.ex +++ b/lib/frenzy_web/controllers/feed_controller.ex @@ -52,6 +52,19 @@ defmodule FrenzyWeb.FeedController do redirect(conn, to: Routes.group_path(Endpoint, :show, group_id)) end + def edit(conn, _params) do + feed = conn.assigns[:feed] |> Repo.preload(:filter) + changeset = Feed.changeset(feed, %{}) + render(conn, "edit.html", changeset: changeset) + end + + def update(conn, %{"feed" => feed_params}) do + feed = conn.assigns[:feed] |> Repo.preload(:filter) + changeset = Feed.changeset(feed, feed_params) + {:ok, feed} = Repo.update(changeset) + redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id)) + end + def delete(conn, _params) do feed = conn.assigns[:feed] {:ok, _} = Repo.delete(feed) diff --git a/lib/frenzy_web/router.ex b/lib/frenzy_web/router.ex index 7bbf759..4f7e673 100644 --- a/lib/frenzy_web/router.ex +++ b/lib/frenzy_web/router.ex @@ -47,10 +47,10 @@ defmodule FrenzyWeb.Router do get "/", GroupController, :index resources "/groups", GroupController, except: [:edit, :update] - resources "/feeds", FeedController, only: [:show, :create, :delete] + resources "/feeds", FeedController, except: [:index, :new] post "/feeds/:id/refresh", FeedController, :refresh - post "/feeds/:id/enable_filter", FeedController, :enable_filter - post "/feeds/:id/disable_filter", FeedController, :disable_filter + #post "/feeds/:id/enable_filter", FeedController, :enable_filter + #post "/feeds/:id/disable_filter", FeedController, :disable_filter resources "/filters", FilterController, only: [:edit, :update] post "/filters/:id/add_rule", FilterController, :add_rule diff --git a/lib/frenzy_web/templates/feed/edit.html.eex b/lib/frenzy_web/templates/feed/edit.html.eex new file mode 100644 index 0000000..6acb57a --- /dev/null +++ b/lib/frenzy_web/templates/feed/edit.html.eex @@ -0,0 +1,25 @@ +

Feed

+<%= form_tag Routes.filter_path(@conn, :edit, @changeset.data.filter.id), method: :get, class: "d-inline" do %> + <%= submit "Edit Filter", class: "btn btn-secondary" %> +<% end %> + +<%= form_for @changeset, Routes.feed_path(@conn, :update, @changeset.data.id), fn form -> %> +
+ +
+ <%= checkbox form, :filter_enabled %> +
+
+
+ +
+ <%= checkbox form, :scrape_remote_content %> +
+
+ +
+
+ <%= submit "Update Feed", class: "btn btn-primary" %> +
+
+<% end %> diff --git a/lib/frenzy_web/templates/feed/show.html.eex b/lib/frenzy_web/templates/feed/show.html.eex index da11943..61450d4 100644 --- a/lib/frenzy_web/templates/feed/show.html.eex +++ b/lib/frenzy_web/templates/feed/show.html.eex @@ -4,25 +4,12 @@ <%= submit "Refresh Feed", class: "btn btn-primary" %> <% end %> -<%= if @feed.filter_enabled do %> - <%= form_tag Routes.feed_path(@conn, :disable_filter, @feed.id), method: :post, class: "d-inline" do %> - <%= submit "Disable Filter", class: "btn btn-secondary" %> - <% end %> - - <%= form_tag Routes.filter_path(@conn, :edit, @feed.filter.id), method: :get, class: "d-inline" do %> - <%= submit "Edit Filter", class: "btn btn-secondary" %> - <% end %> -<% else %> - <%= form_tag Routes.feed_path(@conn, :enable_filter, @feed.id), method: :post, class: "d-inline" do %> - <%= submit "Enable Filter", class: "btn btn-secondary" %> - <% end %> -<% end %> +Edit Feed <%= form_tag Routes.feed_path(@conn, :delete, @feed.id), method: :delete, class: "d-inline" do %> <%= submit "Delete Feed", class: "btn btn-danger" %> <% end %> -

Items

@@ -39,4 +26,4 @@ <% end %> -
\ No newline at end of file + diff --git a/lib/frenzy_web/templates/filter/rules.html.eex b/lib/frenzy_web/templates/filter/rules.html.eex new file mode 100644 index 0000000..e69de29 diff --git a/priv/repo/migrations/20190705224558_feeds_add_scrape_remote_content.exs b/priv/repo/migrations/20190705224558_feeds_add_scrape_remote_content.exs new file mode 100644 index 0000000..92111e2 --- /dev/null +++ b/priv/repo/migrations/20190705224558_feeds_add_scrape_remote_content.exs @@ -0,0 +1,9 @@ +defmodule Frenzy.Repo.Migrations.FeedsAddScrapeRemoteContent do + use Ecto.Migration + + def change do + alter table(:feeds) do + add :scrape_remote_content, :boolean, default: true + end + end +end