defmodule FrenzyWeb.FeedController do use FrenzyWeb, :controller alias Frenzy.{Repo, Group, Feed, Item, Pipeline, Paginator} alias FrenzyWeb.Endpoint import Ecto.Query plug :user_owns_feed defp user_owns_feed(%Plug.Conn{path_params: %{"id" => id}} = conn, _opts) do user = conn.assigns[:user] feed = Repo.get(Feed, id) if Enum.any?(user.groups, fn g -> g.id == feed.group_id end) do conn |> assign(:feed, feed) else conn |> put_flash(:error, "You do not have permission to access that resource.") |> redirect(to: Routes.group_path(Endpoint, :index)) |> halt() end end defp user_owns_feed(conn, _opts), do: conn def show(conn, %{"id" => id} = params) do feed = conn.assigns[:feed] items = Item |> where([i], not i.tombstone and i.feed_id == ^id) |> Paginator.paginate(params) |> limit(50) |> Repo.all() |> Enum.sort_by(fn item -> item.date end, fn a, b -> DateTime.compare(a, b) == :gt end) first_item = List.first(items) last_item = List.last(items) prev_page_path = if !is_nil(first_item) && Repo.exists?( from i in Item, where: not i.tombstone and i.date > ^first_item.date and i.feed_id == ^id ) do current_path(conn, %{after: first_item.id}) else nil end next_page_path = if !is_nil(last_item) && Repo.exists?( from i in Item, where: not i.tombstone and i.date < ^last_item.date and i.feed_id == ^id ) do current_path(conn, %{before: last_item.id}) else nil end render(conn, "show.html", %{ feed: feed, items: items, next_page_path: next_page_path, prev_page_path: prev_page_path }) end def create(conn, %{"feed" => %{"group_id" => group_id, "feed_url" => feed_url}}) do user = conn.assigns[:user] group = Repo.get(Group, group_id) feed_url = feed_url |> String.trim() cond do group != nil && group.user_id == user.id -> if String.length(feed_url) > 0 do changeset = Ecto.build_assoc(group, :feeds, %{ feed_url: feed_url }) {:ok, _feed} = Repo.insert(changeset) conn |> put_flash(:info, "Feed created") |> redirect(to: Routes.group_path(Endpoint, :show, group_id)) else conn |> put_flash(:error, "Feed URL must not be empty") |> redirect(to: Routes.group_path(Endpoint, :show, group_id)) end true -> conn |> put_flash(:error, "Invalid group") |> redirect(to: Routes.group_path(Endpoint, :index)) end end def edit(conn, _params) do user = conn.assigns[:user] feed = conn.assigns[:feed] edit_changeset = Feed.changeset(feed, %{}) pipelines = Repo.all(from p in Pipeline, where: p.user_id == ^user.id) |> Enum.map(fn pipeline -> {pipeline.name, pipeline.id} end) render(conn, "edit.html", %{ feed: feed, changeset: edit_changeset, pipelines: [{"No Pipeline", nil} | pipelines], refresh_frequencies: [ {"Every 30 minutes", 30 * 60}, {"Every hour", 60 * 60}, {"Every 6 hours", 6 * 60 * 60}, {"Every 24 hours", 24 * 60 * 60} ] }) end def update(conn, %{"feed" => feed_params}) do feed = conn.assigns[:feed] changeset = Feed.changeset(feed, feed_params) {:ok, feed} = Repo.update(changeset) redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id)) end def delete(conn, _params) do feed = conn.assigns[:feed] {:ok, _} = Repo.delete(feed) redirect(conn, to: Routes.group_path(Endpoint, :show, feed.group_id)) end def enable_filter(conn, _params) do feed = conn.assigns[:feed] changeset = Feed.changeset(feed, %{filter_enabled: true}) Repo.update(changeset) redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id)) end def disable_filter(conn, _params) do feed = conn.assigns[:feed] changeset = Feed.changeset(feed, %{filter_enabled: false}) Repo.update(changeset) redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id)) end def refresh(conn, _params) do feed = conn.assigns[:feed] Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed) conn |> put_flash(:info, "Refreshing feed. Wait a moment before reloading...") |> redirect(to: Routes.feed_path(Endpoint, :show, feed.id)) end end