Compare commits

...

3 Commits

Author SHA1 Message Date
Shadowfacts f70e358a80 Add items sync endpoint 2022-01-12 18:01:43 -05:00
Shadowfacts 7b9956a1aa Fix fervor item schema 2022-01-12 18:01:35 -05:00
Shadowfacts 306dea226c Fix oauth redirect not working 2021-12-24 15:50:42 -05:00
4 changed files with 43 additions and 4 deletions

View File

@ -38,7 +38,7 @@ defmodule Frenzy.Item do
}
if item.date do
Map.put(res, :created_at, DateTime.to_iso8601(item.date))
Map.put(res, :published, DateTime.to_iso8601(item.date))
else
res
end

View File

@ -1,12 +1,12 @@
defmodule FrenzyWeb.Fervor.ItemsController do
use FrenzyWeb, :controller
alias Frenzy.{Repo, Item}
alias Frenzy.{Repo, Item, Group, Feed}
import Ecto.Query
alias FrenzyWeb.Fervor.Paginator
plug :get_specific_item
def get_specific_item(%Plug.Conn{path_params: %{"id" => id}} = conn, _opts) do
def get_specific_item(%Plug.Conn{path_params: %{"id" => id}} = conn, _opts) when id != "sync" do
user = conn.assigns[:user] |> Repo.preload(:feeds)
item = Repo.get(Item, id)
@ -119,4 +119,42 @@ defmodule FrenzyWeb.Fervor.ItemsController do
def unread_multiple(conn, params) do
mark_multiple_items(conn, params, %{read: false})
end
def sync(conn, params) do
sync_timestamp = Timex.now()
feed_ids =
Group
|> where([g], g.user_id == ^conn.assigns.user.id)
|> join(:inner, [g], f in Feed, on: f.group_id == g.id)
|> select([g, f], f.id)
|> Repo.all()
last_sync =
Map.get(params, "last_sync")
|> case do
s when is_binary(s) -> Timex.parse!(s, "{ISO:Extended:Z}")
_ -> nil
end
{deleted_ids, upserted} =
case last_sync do
nil ->
items = Repo.all(from i in Item, where: not i.tombstone and i.feed_id in ^feed_ids)
{[], items}
_ ->
all_items =
Repo.all(from i in Item, where: i.feed_id in ^feed_ids and i.updated_at >= ^last_sync)
{tombstones, rest} = Enum.split_with(all_items, & &1.tombstone)
{Enum.map(tombstones, & &1.id), rest}
end
json(conn, %{
sync_timestamp: Timex.format!(sync_timestamp, "{ISO:Extended:Z}"),
delete: Enum.map(deleted_ids, &to_string/1),
upsert: Enum.map(upserted, &Item.to_fervor/1)
})
end
end

View File

@ -110,7 +110,7 @@ defmodule FrenzyWeb.Fervor.OauthController do
)
uri = %URI{parsed | query: query}
redirect(conn, to: uri)
redirect(conn, external: URI.to_string(uri))
end
end

View File

@ -99,6 +99,7 @@ defmodule FrenzyWeb.Router do
post "/api/v1/feeds/:id/delete", FeedsController, :delete
get "/api/v1/items", ItemsController, :items_list
get "/api/v1/items/sync", ItemsController, :sync
get "/api/v1/items/:id", ItemsController, :specific_item
post "/api/v1/items/:id/read", ItemsController, :read_specific_item
post "/api/v1/items/:id/unread", ItemsController, :unread_specific_item