More random filter crap

This commit is contained in:
Shadowfacts 2019-03-10 22:23:28 -04:00
parent c90f2d48e2
commit 3c4210d9a6
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
8 changed files with 98 additions and 58 deletions

View File

@ -32,6 +32,6 @@ defmodule Frenzy.Feed do
feed feed
|> cast(attrs, [:title, :feed_url, :site_url, :last_updated, :filter_enabled]) |> cast(attrs, [:title, :feed_url, :site_url, :last_updated, :filter_enabled])
|> cast_assoc(:filter, required: true) |> cast_assoc(:filter, required: true)
|> validate_required([:feed_url]) |> validate_required([:feed_url, :filter])
end end
end end

View File

@ -11,7 +11,7 @@ defmodule Frenzy.Item do
html: item.content, html: item.content,
url: item.url, url: item.url,
is_saved: 0, 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) created_on_time: Timex.to_unix(item.date)
} }
end end

View File

@ -156,7 +156,7 @@ defmodule Frenzy.UpdateFeeds do
{:ok, %HTTPoison.Response{status_code: 301, headers: headers}} -> {:ok, %HTTPoison.Response{status_code: 301, headers: headers}} ->
{"Location", new_url} = {"Location", new_url} =
Enum.find(headers, fn {name, value} -> Enum.find(headers, fn {name, _value} ->
name == "Location" name == "Location"
end) end)

View File

@ -25,17 +25,17 @@ defmodule FrenzyWeb.FeedController do
render(conn, "new.html", changeset: changeset) render(conn, "new.html", changeset: changeset)
end end
def create(conn, %{"feed" => feed}) do def create(conn, %{"feed" => %{"feed_url" => feed_url}}) do
changeset = changeset =
Feed.changeset( Feed.changeset(
%Feed{ %Feed{
feed_url: feed_url,
filter: %Filter{ filter: %Filter{
mode: "accept", mode: "reject",
score: 0, score: 0
rules: []
} }
}, },
feed %{}
) )
{:ok, feed} = Repo.insert(changeset) {:ok, feed} = Repo.insert(changeset)
@ -50,7 +50,7 @@ defmodule FrenzyWeb.FeedController do
end end
def disable_filter(conn, %{"id" => id}) do 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}) changeset = Feed.changeset(feed, %{filter_enabled: false})
Repo.update(changeset) Repo.update(changeset)
redirect(conn, to: Routes.feed_path(Endpoint, :show, id)) redirect(conn, to: Routes.feed_path(Endpoint, :show, id))
@ -63,7 +63,7 @@ defmodule FrenzyWeb.FeedController do
end end
def refresh(conn, %{"id" => id}) do 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) feed = Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed)
redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id)) redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id))
end end

View File

@ -21,6 +21,7 @@ defmodule FrenzyWeb.FeverController do
case validate_key(api_key) do case validate_key(api_key) do
:invalid -> :invalid ->
resp(conn, 401, "Invalid API key") resp(conn, 401, "Invalid API key")
:ok -> :ok ->
json(conn, fever_response(params)) json(conn, fever_response(params))
end end
@ -31,14 +32,16 @@ defmodule FrenzyWeb.FeverController do
username = auth[:username] username = auth[:username]
password = auth[:password] password = auth[:password]
expected = :crypto.hash(:md5, "#{username}:#{password}") |> Base.encode16(case: :lower) expected = :crypto.hash(:md5, "#{username}:#{password}") |> Base.encode16(case: :lower)
case api_key |> String.downcase do
case api_key |> String.downcase() do
^expected -> :ok ^expected -> :ok
_ -> :invalid _ -> :invalid
end end
end end
defp fever_response(params) do defp fever_response(params) do
res = %{api_version: 2, auth: 1} res =
%{api_version: 2, auth: 1}
|> mark(params) |> mark(params)
|> unread_recently_read(params) |> unread_recently_read(params)
|> groups(params) |> groups(params)
@ -52,98 +55,135 @@ defmodule FrenzyWeb.FeverController do
defp mark(res, %{"mark" => "item", "id" => id, "as" => as} = params) do defp mark(res, %{"mark" => "item", "id" => id, "as" => as} = params) do
item = Repo.get(Item, id) |> Repo.preload(:feed) item = Repo.get(Item, id) |> Repo.preload(:feed)
diff = case as do
"read" -> diff =
%{read: true, read_date: Timex.now} case as do
"unread" -> "read" ->
%{read: false, read_date: nil} %{read: true, read_date: Timex.now()}
_ ->
%{} "unread" ->
end %{read: false, read_date: nil}
_ ->
%{}
end
changeset = Item.changeset(item, diff) changeset = Item.changeset(item, diff)
Repo.update(changeset) Repo.update(changeset)
res res
end end
defp mark(res, _), do: res defp mark(res, _), do: res
defp unread_recently_read(res, %{"unread_recently_read" => 1}) do 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")) 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(fn i -> Item.changeset(i, %{read: false, read_date: nil}) end)
|> Enum.map(&Repo.update/1) |> Enum.map(&Repo.update/1)
res res
end end
defp unread_recently_read(res, _), do: res defp unread_recently_read(res, _), do: res
defp groups(res, %{"groups" => _}) do defp groups(res, %{"groups" => _}) do
res res
|> Map.put(:groups, []) |> Map.put(:groups, [])
|> Map.put(:feeds_groups, []) |> Map.put(:feeds_groups, [])
end end
defp groups(res, _), do: res defp groups(res, _), do: res
defp feeds(res, %{"feeds" => _}) do defp feeds(res, %{"feeds" => _}) do
feeds = Repo.all(Feed) feeds =
Repo.all(Feed)
|> Enum.map(&Feed.to_fever/1) |> Enum.map(&Feed.to_fever/1)
res res
|> Map.put(:feeds, feeds) |> Map.put(:feeds, feeds)
|> Map.put(:feeds_groups, []) |> Map.put(:feeds_groups, [])
end end
defp feeds(res, _), do: res defp feeds(res, _), do: res
defp favicons(res, %{"favicons" => _}) do defp favicons(res, %{"favicons" => _}) do
res res
|> Map.put(:favicons, []) |> Map.put(:favicons, [])
end end
defp favicons(res, _), do: res defp favicons(res, _), do: res
defp links(res, %{"links" => _}) do defp links(res, %{"links" => _}) do
res res
|> Map.put(:links, []) |> Map.put(:links, [])
end end
defp links(res, _), do: res defp links(res, _), do: res
defp unread(res, %{"unread_item_ids" => _}) do 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.map(fn item -> item.id end)
|> Enum.join(",") |> Enum.join(",")
res res
|> Map.put(:unread_item_ids, unread) |> Map.put(:unread_item_ids, unread)
end end
defp unread(res, _), do: res defp unread(res, _), do: res
defp saved(res, %{"saved_item_ids" => _}) do defp saved(res, %{"saved_item_ids" => _}) do
res res
|> Map.put(:saved_item_ids, "") |> Map.put(:saved_item_ids, "")
end end
defp saved(res, _), do: res defp saved(res, _), do: res
defp items(res, %{"items" => _} = params) do defp items(res, %{"items" => _} = params) do
items = cond do items =
Map.has_key?(params, "with_ids") -> cond do
params["with_ids"] Map.has_key?(params, "with_ids") ->
params["with_ids"]
|> String.split(",") |> String.split(",")
|> Enum.map(fn id -> |> Enum.map(fn id ->
{id, _} = id |> String.trim |> Integer.parse {id, _} = id |> String.trim() |> Integer.parse()
Repo.get(Item, id) Repo.get(Item, id)
end) end)
Map.has_key?(params, "since_id") ->
since = Repo.get(Item, params["since_id"]) Map.has_key?(params, "since_id") ->
{since, _} = Integer.parse(since) since = Repo.get(Item, params["since_id"])
Repo.all(from i in Item, where: i.inserted_at > ^since.inserted_at, order_by: [asc: :id], limit: 50) {since, _} = Integer.parse(since)
Map.has_key?(params, "max_id") ->
max = Repo.get(Item, params["max_id"]) Repo.all(
{max, _} = Integer.parse(max) from i in Item,
Repo.all(from i in Item, where: i.inserted_at < ^max.inserted_at, order_by: [desc: :id], limit: 50) where: i.inserted_at > ^since.inserted_at,
true -> order_by: [asc: :id],
[] limit: 50
end )
items = items
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.reject(&is_nil/1)
|> Enum.map(&Item.to_fever/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 res
|> Map.put(:items, items)
|> Map.put(:total_items, Enum.count(items))
end
defp items(res, _), do: res
end

View File

@ -11,7 +11,7 @@ defmodule Frenzy.Repo.Migrations.CreateItems do
add :content, :text add :content, :text
add :read, :boolean, default: false, null: false add :read, :boolean, default: false, null: false
add :read_date, :utc_datetime add :read_date, :utc_datetime
add :feed_id, references(:feeds) add :feed_id, references(:feeds, on_delete: :delete_all)
timestamps() timestamps()
end end

View File

@ -1,4 +1,4 @@
defmodule Frenzy.Repo.Migrations.CreateRules do defmodule Frenzy.Repo.Migrations.CreateFilters do
use Ecto.Migration use Ecto.Migration
def change do def change do
@ -6,7 +6,7 @@ defmodule Frenzy.Repo.Migrations.CreateRules do
add :mode, :string add :mode, :string
add :score, :integer add :score, :integer
add :rules, :map add :rules, :map
add :feed_id, references(:feeds) add :feed_id, references(:feeds, on_delete: :delete_all)
timestamps() timestamps()
end end

View File

@ -7,7 +7,7 @@ defmodule Frenzy.Repo.Migrations.CreateRules do
add :property, :string add :property, :string
add :param, :string add :param, :string
add :weight, :integer add :weight, :integer
add :filter_id, references(:filters) add :filter_id, references(:filters, on_delete: :delete_all)
timestamps() timestamps()
end end