Compare commits

..

2 Commits

4 changed files with 14 additions and 6 deletions

View File

@ -83,5 +83,10 @@ defmodule Frenzy.Item do
item item
|> cast(attrs, [:guid, :title, :url, :creator, :date, :content, :read, :read_date, :tombstone]) |> cast(attrs, [:guid, :title, :url, :creator, :date, :content, :read, :read_date, :tombstone])
|> validate_required([:guid, :url, :date, :content, :feed]) |> validate_required([:guid, :url, :date, :content, :feed])
|> unique_constraint(:items_feed_guid_index)
end
def exists?(feed_id, guid) do
Frenzy.Repo.exists?(__MODULE__, feed_id: feed_id, guid: guid)
end end
end end

View File

@ -62,7 +62,7 @@ defmodule Frenzy.Task.CreateItem do
end end
case result do case result do
{:err, error} -> {:error, error} ->
Logger.error(error) Logger.error(error)
{:ok, item_params} -> {:ok, item_params} ->

View File

@ -53,8 +53,6 @@ defmodule Frenzy.UpdateFeeds do
Logger.info("Updating #{count} feeds") Logger.info("Updating #{count} feeds")
Enum.each(feeds, &update_feed/1)
Enum.each(feeds, fn feed -> Enum.each(feeds, fn feed ->
try do try do
update_feed(feed) update_feed(feed)
@ -160,11 +158,9 @@ defmodule Frenzy.UpdateFeeds do
FetchFavicon.run(feed) FetchFavicon.run(feed)
end end
feed = Repo.preload(feed, [:items])
Enum.each(rss.items, fn entry -> Enum.each(rss.items, fn entry ->
# todo: use Repo.exists for this # todo: use Repo.exists for this
if !Enum.any?(feed.items, fn item -> item.guid == entry.guid end) do unless Item.exists?(feed.id, entry.guid) do
CreateItem.start_link(feed, entry) CreateItem.start_link(feed, entry)
end end
end) end)

View File

@ -0,0 +1,7 @@
defmodule Frenzy.Repo.Migrations.CreatItemUniqueIndex do
use Ecto.Migration
def change do
create unique_index(:items, [:feed_id, :guid], name: :items_feed_guid_index)
end
end