diff --git a/lib/frenzy/feed.ex b/lib/frenzy/feed.ex index facd90c..82bb9f2 100644 --- a/lib/frenzy/feed.ex +++ b/lib/frenzy/feed.ex @@ -32,6 +32,6 @@ defmodule Frenzy.Feed do feed |> cast(attrs, [:title, :feed_url, :site_url, :last_updated, :filter_enabled]) |> cast_assoc(:filter, required: true) - |> validate_required([:feed_url]) + |> validate_required([:feed_url, :filter]) end end diff --git a/lib/frenzy/item.ex b/lib/frenzy/item.ex index 5b6a83f..4e98856 100644 --- a/lib/frenzy/item.ex +++ b/lib/frenzy/item.ex @@ -11,7 +11,7 @@ defmodule Frenzy.Item do html: item.content, url: item.url, is_saved: 0, - is_read: (if item.read, do: 1, else: 0), + is_read: if(item.read, do: 1, else: 0), created_on_time: Timex.to_unix(item.date) } end diff --git a/lib/frenzy/update_feeds.ex b/lib/frenzy/update_feeds.ex index a26bde6..c691f7e 100644 --- a/lib/frenzy/update_feeds.ex +++ b/lib/frenzy/update_feeds.ex @@ -156,7 +156,7 @@ defmodule Frenzy.UpdateFeeds do {:ok, %HTTPoison.Response{status_code: 301, headers: headers}} -> {"Location", new_url} = - Enum.find(headers, fn {name, value} -> + Enum.find(headers, fn {name, _value} -> name == "Location" end) diff --git a/lib/frenzy_web/controllers/feed_controller.ex b/lib/frenzy_web/controllers/feed_controller.ex index 6772ee5..64142f8 100644 --- a/lib/frenzy_web/controllers/feed_controller.ex +++ b/lib/frenzy_web/controllers/feed_controller.ex @@ -25,17 +25,17 @@ defmodule FrenzyWeb.FeedController do render(conn, "new.html", changeset: changeset) end - def create(conn, %{"feed" => feed}) do + def create(conn, %{"feed" => %{"feed_url" => feed_url}}) do changeset = Feed.changeset( %Feed{ + feed_url: feed_url, filter: %Filter{ - mode: "accept", - score: 0, - rules: [] + mode: "reject", + score: 0 } }, - feed + %{} ) {:ok, feed} = Repo.insert(changeset) @@ -50,7 +50,7 @@ defmodule FrenzyWeb.FeedController do end def disable_filter(conn, %{"id" => id}) do - feed = Repo.get(Feed, id) + feed = Repo.get(Feed, id) |> Repo.preload(:filter) changeset = Feed.changeset(feed, %{filter_enabled: false}) Repo.update(changeset) redirect(conn, to: Routes.feed_path(Endpoint, :show, id)) @@ -63,7 +63,7 @@ defmodule FrenzyWeb.FeedController do end def refresh(conn, %{"id" => id}) do - feed = Repo.get(Feed, id) + feed = Repo.get(Feed, id) |> Repo.preload(:filter) feed = Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed) redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id)) end diff --git a/lib/frenzy_web/controllers/fever_controller.ex b/lib/frenzy_web/controllers/fever_controller.ex index daab9fc..33b2d4a 100644 --- a/lib/frenzy_web/controllers/fever_controller.ex +++ b/lib/frenzy_web/controllers/fever_controller.ex @@ -21,6 +21,7 @@ defmodule FrenzyWeb.FeverController do case validate_key(api_key) do :invalid -> resp(conn, 401, "Invalid API key") + :ok -> json(conn, fever_response(params)) end @@ -31,14 +32,16 @@ defmodule FrenzyWeb.FeverController do username = auth[:username] password = auth[:password] expected = :crypto.hash(:md5, "#{username}:#{password}") |> Base.encode16(case: :lower) - case api_key |> String.downcase do + + case api_key |> String.downcase() do ^expected -> :ok _ -> :invalid end end defp fever_response(params) do - res = %{api_version: 2, auth: 1} + res = + %{api_version: 2, auth: 1} |> mark(params) |> unread_recently_read(params) |> groups(params) @@ -52,98 +55,135 @@ defmodule FrenzyWeb.FeverController do defp mark(res, %{"mark" => "item", "id" => id, "as" => as} = params) do item = Repo.get(Item, id) |> Repo.preload(:feed) - diff = case as do - "read" -> - %{read: true, read_date: Timex.now} - "unread" -> - %{read: false, read_date: nil} - _ -> - %{} - end + + diff = + case as do + "read" -> + %{read: true, read_date: Timex.now()} + + "unread" -> + %{read: false, read_date: nil} + + _ -> + %{} + end + changeset = Item.changeset(item, diff) Repo.update(changeset) res end + defp mark(res, _), do: res defp unread_recently_read(res, %{"unread_recently_read" => 1}) do Repo.all(from i in Item, where: i.read, where: i.read_date >= from_now(-1, "hour")) - |> Enum.map(fn i -> Item.changeset(i, %{read: false, read_date: nil}) end) - |> Enum.map(&Repo.update/1) + |> Enum.map(fn i -> Item.changeset(i, %{read: false, read_date: nil}) end) + |> Enum.map(&Repo.update/1) + res end + defp unread_recently_read(res, _), do: res defp groups(res, %{"groups" => _}) do res - |> Map.put(:groups, []) - |> Map.put(:feeds_groups, []) + |> Map.put(:groups, []) + |> Map.put(:feeds_groups, []) end + defp groups(res, _), do: res defp feeds(res, %{"feeds" => _}) do - feeds = Repo.all(Feed) + feeds = + Repo.all(Feed) |> Enum.map(&Feed.to_fever/1) + res - |> Map.put(:feeds, feeds) - |> Map.put(:feeds_groups, []) + |> Map.put(:feeds, feeds) + |> Map.put(:feeds_groups, []) end + defp feeds(res, _), do: res defp favicons(res, %{"favicons" => _}) do res - |> Map.put(:favicons, []) + |> Map.put(:favicons, []) end + defp favicons(res, _), do: res defp links(res, %{"links" => _}) do res - |> Map.put(:links, []) + |> Map.put(:links, []) end + defp links(res, _), do: res defp unread(res, %{"unread_item_ids" => _}) do - unread = Repo.all(from Item, where: [read: false]) + unread = + Repo.all(from Item, where: [read: false]) |> Enum.map(fn item -> item.id end) |> Enum.join(",") + res - |> Map.put(:unread_item_ids, unread) + |> Map.put(:unread_item_ids, unread) end + defp unread(res, _), do: res defp saved(res, %{"saved_item_ids" => _}) do res - |> Map.put(:saved_item_ids, "") + |> Map.put(:saved_item_ids, "") end + defp saved(res, _), do: res defp items(res, %{"items" => _} = params) do - items = cond do - Map.has_key?(params, "with_ids") -> - params["with_ids"] + items = + cond do + Map.has_key?(params, "with_ids") -> + params["with_ids"] |> String.split(",") |> Enum.map(fn id -> - {id, _} = id |> String.trim |> Integer.parse + {id, _} = id |> String.trim() |> Integer.parse() Repo.get(Item, id) end) - Map.has_key?(params, "since_id") -> - since = Repo.get(Item, params["since_id"]) - {since, _} = Integer.parse(since) - Repo.all(from i in Item, where: i.inserted_at > ^since.inserted_at, order_by: [asc: :id], limit: 50) - Map.has_key?(params, "max_id") -> - max = Repo.get(Item, params["max_id"]) - {max, _} = Integer.parse(max) - Repo.all(from i in Item, where: i.inserted_at < ^max.inserted_at, order_by: [desc: :id], limit: 50) - true -> - [] - end - items = items + + Map.has_key?(params, "since_id") -> + since = Repo.get(Item, params["since_id"]) + {since, _} = Integer.parse(since) + + Repo.all( + from i in Item, + where: i.inserted_at > ^since.inserted_at, + order_by: [asc: :id], + limit: 50 + ) + + Map.has_key?(params, "max_id") -> + max = Repo.get(Item, params["max_id"]) + {max, _} = Integer.parse(max) + + Repo.all( + from i in Item, + where: i.inserted_at < ^max.inserted_at, + order_by: [desc: :id], + limit: 50 + ) + + true -> + [] + end + + items = + items |> Enum.reject(&is_nil/1) |> Enum.map(&Item.to_fever/1) - res - |> Map.put(:items, items) - |> Map.put(:total_items, Enum.count(items)) - end - defp items(res, _), do: res -end \ No newline at end of file + res + |> Map.put(:items, items) + |> Map.put(:total_items, Enum.count(items)) + end + + defp items(res, _), do: res +end diff --git a/priv/repo/migrations/20181121001110_create_items.exs b/priv/repo/migrations/20181121001110_create_items.exs index 1bb355f..2104897 100644 --- a/priv/repo/migrations/20181121001110_create_items.exs +++ b/priv/repo/migrations/20181121001110_create_items.exs @@ -11,7 +11,7 @@ defmodule Frenzy.Repo.Migrations.CreateItems do add :content, :text add :read, :boolean, default: false, null: false add :read_date, :utc_datetime - add :feed_id, references(:feeds) + add :feed_id, references(:feeds, on_delete: :delete_all) timestamps() end diff --git a/priv/repo/migrations/20190309150407_create_filters.exs b/priv/repo/migrations/20190309150407_create_filters.exs index 513b535..d181b09 100644 --- a/priv/repo/migrations/20190309150407_create_filters.exs +++ b/priv/repo/migrations/20190309150407_create_filters.exs @@ -1,4 +1,4 @@ -defmodule Frenzy.Repo.Migrations.CreateRules do +defmodule Frenzy.Repo.Migrations.CreateFilters do use Ecto.Migration def change do @@ -6,7 +6,7 @@ defmodule Frenzy.Repo.Migrations.CreateRules do add :mode, :string add :score, :integer add :rules, :map - add :feed_id, references(:feeds) + add :feed_id, references(:feeds, on_delete: :delete_all) timestamps() end diff --git a/priv/repo/migrations/20190309185450_create_rules.exs b/priv/repo/migrations/20190309185450_create_rules.exs index d74821e..241d3d4 100644 --- a/priv/repo/migrations/20190309185450_create_rules.exs +++ b/priv/repo/migrations/20190309185450_create_rules.exs @@ -7,7 +7,7 @@ defmodule Frenzy.Repo.Migrations.CreateRules do add :property, :string add :param, :string add :weight, :integer - add :filter_id, references(:filters) + add :filter_id, references(:filters, on_delete: :delete_all) timestamps() end