Add tombstone items

This commit is contained in:
Shadowfacts 2019-03-14 19:48:46 -04:00
parent 3c4210d9a6
commit 27cf787d52
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 38 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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