From 27cf787d52589094adfe0a33bb9e350080928210 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 14 Mar 2019 19:48:46 -0400 Subject: [PATCH] Add tombstone items --- lib/frenzy/item.ex | 5 +++-- lib/frenzy/update_feeds.ex | 21 ++++++++++++++----- lib/frenzy_web/controllers/feed_controller.ex | 11 +++++++++- .../20190310234846_items_add_tombstone.exs | 9 ++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 priv/repo/migrations/20190310234846_items_add_tombstone.exs diff --git a/lib/frenzy/item.ex b/lib/frenzy/item.ex index 4e98856..b1fb25e 100644 --- a/lib/frenzy/item.ex +++ b/lib/frenzy/item.ex @@ -25,7 +25,8 @@ defmodule Frenzy.Item do field :read, :boolean, default: false field :read_date, :utc_datetime field :title, :string - + field :tombstone, :boolean, default: false + belongs_to :feed, Frenzy.Feed timestamps() @@ -34,7 +35,7 @@ defmodule Frenzy.Item do @doc false def changeset(item, attrs) do item - |> cast(attrs, [:guid, :title, :url, :creator, :date, :content, :read, :read_date]) + |> cast(attrs, [:guid, :title, :url, :creator, :date, :content, :read, :read_date, :tombstone]) |> validate_required([:guid, :title, :url, :date, :content, :feed]) end end diff --git a/lib/frenzy/update_feeds.ex b/lib/frenzy/update_feeds.ex index c691f7e..8c00473 100644 --- a/lib/frenzy/update_feeds.ex +++ b/lib/frenzy/update_feeds.ex @@ -32,22 +32,33 @@ defmodule Frenzy.UpdateFeeds do defp schedule_update() do # 15 minutes - Process.send_after(self(), :update_feeds, 15 * 60 * 1000) - # Process.send_after(self(), :update_feeds, 60 * 1000) # 1 minutes + # Process.send_after(self(), :update_feeds, 15 * 60 * 1000) + # 1 minutes + Process.send_after(self(), :update_feeds, 60 * 1000) end defp update_feeds() do Logger.info("Updating all feeds") - Enum.map(Repo.all(Feed), &update_feed/1) + Repo.all(from Feed, preload: [:filter]) + |> Enum.map(&update_feed/1) + prune_old_items() end defp prune_old_items() do {count, _} = - Repo.delete_all(from i in Item, where: i.read, where: i.read_date <= from_now(-1, "week")) + from(i in Item, + where: i.read and not i.tombstone, + # where: i.read_date <= from_now(-1, "week"), + where: i.read_date <= from_now(-1, "minute"), + update: [ + set: [tombstone: true, content: nil, creator: nil, date: nil, url: nil, title: nil] + ] + ) + |> Repo.update_all([]) - Logger.info("Removed #{count} read items") + Logger.info("Converted #{count} read items to tombstones") end defp update_feed(feed) do diff --git a/lib/frenzy_web/controllers/feed_controller.ex b/lib/frenzy_web/controllers/feed_controller.ex index 64142f8..c1a9120 100644 --- a/lib/frenzy_web/controllers/feed_controller.ex +++ b/lib/frenzy_web/controllers/feed_controller.ex @@ -11,7 +11,7 @@ defmodule FrenzyWeb.FeedController do def show(conn, %{"id" => id}) do feed = Repo.get(Feed, id) |> Repo.preload(:filter) - items = Repo.all(from Item, where: [feed_id: ^id], order_by: [desc: :date]) + items = Repo.all(from Item, where: [feed_id: ^id, tombstone: false], order_by: [desc: :date]) render(conn, "show.html", %{ feed: feed, @@ -39,6 +39,15 @@ defmodule FrenzyWeb.FeedController do ) {:ok, feed} = Repo.insert(changeset) + + # changeset = + # Ecto.build_assoc(feed, :filter, %{ + # mode: "reject", + # score: 0 + # }) + + # {:ok, _} = Repo.insert(changeset) + redirect(conn, to: Routes.feed_path(Endpoint, :index)) end diff --git a/priv/repo/migrations/20190310234846_items_add_tombstone.exs b/priv/repo/migrations/20190310234846_items_add_tombstone.exs new file mode 100644 index 0000000..319bfbf --- /dev/null +++ b/priv/repo/migrations/20190310234846_items_add_tombstone.exs @@ -0,0 +1,9 @@ +defmodule Frenzy.Repo.Migrations.ItemsAddTombstone do + use Ecto.Migration + + def change do + alter table(:items) do + add :tombstone, :boolean + end + end +end