149 lines
3.9 KiB
Elixir
149 lines
3.9 KiB
Elixir
defmodule FrenzyWeb.FeverController do
|
|
use FrenzyWeb, :controller
|
|
alias Frenzy.{Repo, Feed, Item}
|
|
import Ecto.Query
|
|
|
|
plug :api_check
|
|
|
|
def api_check(conn, _) do
|
|
if Map.has_key?(conn.params, "api") do
|
|
conn
|
|
else
|
|
conn |> resp(400, "Invalid request") |> halt()
|
|
end
|
|
end
|
|
|
|
def get(conn, params) do
|
|
json(conn, %{api_version: 2, auth: 0})
|
|
end
|
|
|
|
def post(conn, %{"api_key" => api_key} = params) do
|
|
case validate_key(api_key) do
|
|
:invalid ->
|
|
resp(conn, 401, "Invalid API key")
|
|
:ok ->
|
|
json(conn, fever_response(params))
|
|
end
|
|
end
|
|
|
|
defp validate_key(api_key) do
|
|
auth = Application.get_env(:frenzy, :auth)
|
|
username = auth[:username]
|
|
password = auth[:password]
|
|
expected = :crypto.hash(:md5, "#{username}:#{password}") |> Base.encode16(case: :lower)
|
|
case api_key |> String.downcase do
|
|
^expected -> :ok
|
|
_ -> :invalid
|
|
end
|
|
end
|
|
|
|
defp fever_response(params) do
|
|
res = %{api_version: 2, auth: 1}
|
|
|> mark(params)
|
|
|> unread_recently_read(params)
|
|
|> groups(params)
|
|
|> feeds(params)
|
|
|> favicons(params)
|
|
|> links(params)
|
|
|> unread(params)
|
|
|> saved(params)
|
|
|> items(params)
|
|
end
|
|
|
|
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
|
|
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
|
|
res
|
|
|> Map.put(:groups, [])
|
|
|> Map.put(:feeds_groups, [])
|
|
end
|
|
defp groups(res, _), do: res
|
|
|
|
defp feeds(res, %{"feeds" => _}) do
|
|
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])
|
|
|> 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
|
|
Map.has_key?(params, "with_ids") ->
|
|
params["with_ids"]
|
|
|> String.split(",")
|
|
|> Enum.map(fn id ->
|
|
{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
|
|
|> 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 |