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

View File

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

View File

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

View File

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

View File

@ -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,26 +55,34 @@ 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
diff =
case as do
"read" ->
%{read: true, read_date: Timex.now}
%{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)
res
end
defp unread_recently_read(res, _), do: res
defp groups(res, %{"groups" => _}) do
@ -79,71 +90,100 @@ defmodule FrenzyWeb.FeverController do
|> 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, [])
end
defp feeds(res, _), do: res
defp favicons(res, %{"favicons" => _}) do
res
|> Map.put(:favicons, [])
end
defp favicons(res, _), do: res
defp links(res, %{"links" => _}) do
res
|> 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)
end
defp unread(res, _), do: res
defp saved(res, %{"saved_item_ids" => _}) do
res
|> Map.put(:saved_item_ids, "")
end
defp saved(res, _), do: res
defp items(res, %{"items" => _} = params) do
items = cond do
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)
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)
Repo.all(
from i in Item,
where: i.inserted_at < ^max.inserted_at,
order_by: [desc: :id],
limit: 50
)
true ->
[]
end
items = items
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
defp items(res, _), do: res
end

View File

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

View File

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

View File

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