frenzy/lib/frenzy_web/controllers/fever_controller.ex

263 lines
5.9 KiB
Elixir
Raw Normal View History

2019-02-11 22:22:35 +00:00
defmodule FrenzyWeb.FeverController do
use FrenzyWeb, :controller
2019-03-24 15:00:46 +00:00
alias Frenzy.{Repo, User, Group, Feed, Item}
2019-02-11 22:22:35 +00:00
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
2019-03-21 23:43:13 +00:00
def get(conn, _params) do
2019-02-11 22:22:35 +00:00
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")
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
{:ok, user} ->
user = Repo.preload(user, groups: [:feeds])
res = fever_response(user, params)
json(conn, res)
2019-02-11 22:22:35 +00:00
end
end
defp validate_key(api_key) do
2019-03-24 15:00:46 +00:00
case Repo.get_by(User, fever_auth_token: api_key) do
nil ->
:invalid
user ->
{:ok, user}
2019-02-11 22:22:35 +00:00
end
2019-03-24 15:00:46 +00:00
# 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
2019-02-11 22:22:35 +00:00
end
2019-03-24 15:00:46 +00:00
defp fever_response(user, params) do
2019-03-21 23:43:13 +00:00
%{api_version: 2, auth: 1}
2019-03-24 15:00:46 +00:00
|> mark(user, params)
|> unread_recently_read(user, params)
|> feeds(user, params)
|> groups(user, params)
|> feeds_groups(user, params)
|> favicons(user, params)
|> links(user, params)
|> unread(user, params)
|> saved(user, params)
|> items(user, params)
end
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
defp get_user_item(user, id) do
item = Repo.get(Item, id) |> Repo.preload(:feed)
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
if not is_nil(item) do
feeds = Enum.flat_map(user.groups, fn g -> g.feeds end)
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
if Enum.any?(feeds, fn f -> f.id == item.feed_id end) do
{:ok, item}
else
{:error, "item does not belong to given user"}
2019-03-11 02:23:28 +00:00
end
2019-03-24 15:00:46 +00:00
else
{:error, "item does not exist"}
end
end
defp mark(res, user, %{"mark" => "item", "id" => id, "as" => as}) do
with {:ok, item} <- get_user_item(user, id) do
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)
end
2019-03-11 02:23:28 +00:00
2019-02-11 22:22:35 +00:00
res
end
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
defp mark(res, _, _), do: res
2019-02-11 22:22:35 +00:00
2019-03-24 15:00:46 +00:00
defp unread_recently_read(res, user, %{"unread_recently_read" => 1}) do
feed_ids =
user.groups
|> Enum.flat_map(fn g -> g.feeds end)
|> Enum.map(fn f -> f.id end)
Repo.all(
from i in Item,
where: i.feed_id in ^feed_ids,
where: i.read,
where: i.read_date >= from_now(-1, "hour")
)
2019-03-11 02:23:28 +00:00
|> Enum.map(fn i -> Item.changeset(i, %{read: false, read_date: nil}) end)
|> Enum.map(&Repo.update/1)
2019-02-11 22:22:35 +00:00
res
end
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
defp unread_recently_read(res, _, _), do: res
2019-02-11 22:22:35 +00:00
2019-03-24 15:00:46 +00:00
defp feeds(res, user, %{"feeds" => _}) do
2019-03-11 02:23:28 +00:00
feeds =
2019-03-24 15:00:46 +00:00
user.groups
|> Enum.flat_map(fn g -> g.feeds end)
2019-02-11 22:22:35 +00:00
|> Enum.map(&Feed.to_fever/1)
2019-03-11 02:23:28 +00:00
2019-02-11 22:22:35 +00:00
res
2019-03-11 02:23:28 +00:00
|> Map.put(:feeds, feeds)
2019-02-11 22:22:35 +00:00
end
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
defp feeds(res, _, _), do: res
2019-02-11 22:22:35 +00:00
2019-03-24 15:00:46 +00:00
defp groups(res, user, %{"groups" => _}) do
2019-03-21 23:43:13 +00:00
groups =
2019-03-24 15:00:46 +00:00
user.groups
2019-03-21 23:43:13 +00:00
|> Enum.map(&Group.to_fever_group/1)
res
|> Map.put(:groups, groups)
end
2019-03-24 15:00:46 +00:00
defp groups(res, _, _), do: res
2019-03-21 23:43:13 +00:00
2019-03-24 15:00:46 +00:00
defp feeds_groups(res, user, params) do
2019-03-21 23:43:13 +00:00
if Map.has_key?(params, "feeds") or Map.has_key?(params, "groups") do
feeds_groups =
2019-03-24 15:00:46 +00:00
user.groups
2019-03-21 23:43:13 +00:00
|> Enum.map(&Group.to_fever_feeds_group/1)
res
|> Map.put(:feeds_groups, feeds_groups)
else
res
end
end
2019-03-24 15:00:46 +00:00
defp favicons(res, _user, %{"favicons" => _}) do
2019-02-11 22:22:35 +00:00
res
2019-03-11 02:23:28 +00:00
|> Map.put(:favicons, [])
2019-02-11 22:22:35 +00:00
end
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
defp favicons(res, _, _), do: res
2019-02-11 22:22:35 +00:00
2019-03-24 15:00:46 +00:00
defp links(res, _user, %{"links" => _}) do
2019-02-11 22:22:35 +00:00
res
2019-03-11 02:23:28 +00:00
|> Map.put(:links, [])
2019-02-11 22:22:35 +00:00
end
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
defp links(res, _, _), do: res
defp unread(res, user, %{"unread_item_ids" => _}) do
feed_ids =
user.groups
|> Enum.flat_map(fn g -> g.feeds end)
|> Enum.map(fn f -> f.id end)
2019-02-11 22:22:35 +00:00
2019-03-11 02:23:28 +00:00
unread =
2019-03-24 15:00:46 +00:00
Repo.all(from i in Item, where: i.feed_id in ^feed_ids, where: [read: false])
2019-02-11 22:22:35 +00:00
|> Enum.map(fn item -> item.id end)
|> Enum.join(",")
2019-03-11 02:23:28 +00:00
2019-02-11 22:22:35 +00:00
res
2019-03-11 02:23:28 +00:00
|> Map.put(:unread_item_ids, unread)
2019-02-11 22:22:35 +00:00
end
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
defp unread(res, _, _), do: res
2019-02-11 22:22:35 +00:00
2019-03-24 15:00:46 +00:00
defp saved(res, _user, %{"saved_item_ids" => _}) do
2019-02-11 22:22:35 +00:00
res
2019-03-11 02:23:28 +00:00
|> Map.put(:saved_item_ids, "")
2019-02-11 22:22:35 +00:00
end
2019-03-11 02:23:28 +00:00
2019-03-24 15:00:46 +00:00
defp saved(res, _, _), do: res
defp items(res, user, %{"items" => _} = params) do
feed_ids =
user.groups
|> Enum.flat_map(fn g -> g.feeds end)
|> Enum.map(fn f -> f.id end)
2019-02-11 22:22:35 +00:00
2019-03-11 02:23:28 +00:00
items =
cond do
Map.has_key?(params, "with_ids") ->
params["with_ids"]
2019-02-11 22:22:35 +00:00
|> String.split(",")
|> Enum.map(fn id ->
2019-03-11 02:23:28 +00:00
{id, _} = id |> String.trim() |> Integer.parse()
2019-03-24 15:00:46 +00:00
item = Repo.get(Item, id)
if item.feed_id in feed_ids do
item
else
nil
end
2019-02-11 22:22:35 +00:00
end)
2019-03-24 15:00:46 +00:00
|> Enum.reject(&is_nil/1)
2019-03-11 02:23:28 +00:00
Map.has_key?(params, "since_id") ->
since = Repo.get(Item, params["since_id"])
{since, _} = Integer.parse(since)
Repo.all(
from i in Item,
2019-03-24 15:00:46 +00:00
where: i.feed_id in ^feed_ids,
where: i.inserted_at > ^since,
2019-03-11 02:23:28 +00:00
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,
2019-03-24 15:00:46 +00:00
where: i.feed_id in ^feed_ids,
where: i.inserted_at < ^max,
2019-03-11 02:23:28 +00:00
order_by: [desc: :id],
limit: 50
)
true ->
[]
end
items =
items
2019-02-11 22:22:35 +00:00
|> Enum.reject(&is_nil/1)
2019-03-21 23:43:13 +00:00
|> Enum.reject(fn item -> item.tombstone end)
2019-02-11 22:22:35 +00:00
|> Enum.map(&Item.to_fever/1)
2019-03-11 02:23:28 +00:00
2019-02-11 22:22:35 +00:00
res
2019-03-11 02:23:28 +00:00
|> Map.put(:items, items)
|> Map.put(:total_items, Enum.count(items))
2019-02-11 22:22:35 +00:00
end
2019-03-24 15:00:46 +00:00
defp items(res, _, _), do: res
2019-03-11 02:23:28 +00:00
end