Add Fever API for groups

This commit is contained in:
Shadowfacts 2019-03-21 19:43:13 -04:00
parent eb0ebeba91
commit 29411a9d08
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 54 additions and 24 deletions

View File

@ -2,6 +2,20 @@ defmodule Frenzy.Group do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
def to_fever_group(group) do
%{
id: group.id,
title: group.title
}
end
def to_fever_feeds_group(group) do
%{
group_id: group.id,
feed_ids: group.feeds |> Enum.map(fn feed -> feed.id end) |> Enum.join(",")
}
end
schema "groups" do schema "groups" do
field :title, :string field :title, :string

View File

@ -1,6 +1,6 @@
defmodule FrenzyWeb.FeverController do defmodule FrenzyWeb.FeverController do
use FrenzyWeb, :controller use FrenzyWeb, :controller
alias Frenzy.{Repo, Feed, Item} alias Frenzy.{Repo, Group, Feed, Item}
import Ecto.Query import Ecto.Query
plug :api_check plug :api_check
@ -13,7 +13,7 @@ defmodule FrenzyWeb.FeverController do
end end
end end
def get(conn, params) do def get(conn, _params) do
json(conn, %{api_version: 2, auth: 0}) json(conn, %{api_version: 2, auth: 0})
end end
@ -40,20 +40,20 @@ defmodule FrenzyWeb.FeverController do
end end
defp fever_response(params) do defp fever_response(params) do
res = %{api_version: 2, auth: 1}
%{api_version: 2, auth: 1} |> mark(params)
|> mark(params) |> unread_recently_read(params)
|> unread_recently_read(params) |> feeds(params)
|> groups(params) |> groups(params)
|> feeds(params) |> feeds_groups(params)
|> favicons(params) |> favicons(params)
|> links(params) |> links(params)
|> unread(params) |> unread(params)
|> saved(params) |> saved(params)
|> items(params) |> items(params)
end end
defp mark(res, %{"mark" => "item", "id" => id, "as" => as} = params) do defp mark(res, %{"mark" => "item", "id" => id, "as" => as}) do
item = Repo.get(Item, id) |> Repo.preload(:feed) item = Repo.get(Item, id) |> Repo.preload(:feed)
diff = diff =
@ -85,14 +85,6 @@ defmodule FrenzyWeb.FeverController do
defp unread_recently_read(res, _), do: res 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 defp feeds(res, %{"feeds" => _}) do
feeds = feeds =
Repo.all(Feed) Repo.all(Feed)
@ -100,11 +92,34 @@ defmodule FrenzyWeb.FeverController do
res res
|> Map.put(:feeds, feeds) |> Map.put(:feeds, feeds)
|> Map.put(:feeds_groups, [])
end end
defp feeds(res, _), do: res defp feeds(res, _), do: res
defp groups(res, %{"groups" => _}) do
groups =
Repo.all(Group)
|> Enum.map(&Group.to_fever_group/1)
res
|> Map.put(:groups, groups)
end
defp groups(res, _), do: res
defp feeds_groups(res, params) do
if Map.has_key?(params, "feeds") or Map.has_key?(params, "groups") do
feeds_groups =
Repo.all(from Group, preload: [:feeds])
|> Enum.map(&Group.to_fever_feeds_group/1)
res
|> Map.put(:feeds_groups, feeds_groups)
else
res
end
end
defp favicons(res, %{"favicons" => _}) do defp favicons(res, %{"favicons" => _}) do
res res
|> Map.put(:favicons, []) |> Map.put(:favicons, [])
@ -178,6 +193,7 @@ defmodule FrenzyWeb.FeverController do
items = items =
items items
|> Enum.reject(&is_nil/1) |> Enum.reject(&is_nil/1)
|> Enum.reject(fn item -> item.tombstone end)
|> Enum.map(&Item.to_fever/1) |> Enum.map(&Item.to_fever/1)
res res

View File

@ -11,7 +11,7 @@
<%= text_input form, :feed_url %> <%= text_input form, :feed_url %>
</div> </div>
<div class="form-group"> <div class="form-group">
<%= submit "Create Feed" %> <%= submit "Add Feed" %>
</div> </div>
<% end %> <% end %>