2019-02-11 22:22:35 +00:00
|
|
|
defmodule FrenzyWeb.FeedController do
|
2019-03-10 23:47:01 +00:00
|
|
|
use FrenzyWeb, :controller
|
2019-03-21 23:21:56 +00:00
|
|
|
alias Frenzy.{Repo, Group, Feed, Filter, Item}
|
2019-02-11 22:22:35 +00:00
|
|
|
alias FrenzyWeb.Router.Helpers, as: Routes
|
|
|
|
alias FrenzyWeb.Endpoint
|
|
|
|
import Ecto.Query
|
|
|
|
|
2019-03-23 23:42:38 +00:00
|
|
|
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
|
|
|
|
|
2019-02-11 22:22:35 +00:00
|
|
|
def show(conn, %{"id" => id}) do
|
2019-04-01 15:29:25 +00:00
|
|
|
feed = conn.assigns[:feed] |> Repo.preload(:filter)
|
2019-03-14 23:48:46 +00:00
|
|
|
items = Repo.all(from Item, where: [feed_id: ^id, tombstone: false], order_by: [desc: :date])
|
2019-03-10 23:47:01 +00:00
|
|
|
|
2019-02-11 22:22:35 +00:00
|
|
|
render(conn, "show.html", %{
|
|
|
|
feed: feed,
|
|
|
|
items: items
|
2019-03-10 23:47:01 +00:00
|
|
|
})
|
2019-02-11 22:22:35 +00:00
|
|
|
end
|
|
|
|
|
2019-03-21 23:21:56 +00:00
|
|
|
def create(conn, %{"feed" => %{"group_id" => group_id, "feed_url" => feed_url}}) do
|
2019-08-31 19:09:40 +00:00
|
|
|
user = conn.assigns[:user]
|
2019-03-21 23:21:56 +00:00
|
|
|
group = Repo.get(Group, group_id)
|
2019-08-31 19:09:40 +00:00
|
|
|
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,
|
|
|
|
filter: %Filter{
|
|
|
|
mode: "reject",
|
|
|
|
score: 0
|
|
|
|
}
|
|
|
|
})
|
2019-02-11 22:22:35 +00:00
|
|
|
|
2019-08-31 19:09:40 +00:00
|
|
|
{:ok, feed} = Repo.insert(changeset)
|
2019-03-10 23:47:01 +00:00
|
|
|
|
2019-08-31 19:09:40 +00:00
|
|
|
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
|
2019-03-14 23:48:46 +00:00
|
|
|
|
2019-08-31 19:09:40 +00:00
|
|
|
true ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Invalid group")
|
|
|
|
|> redirect(to: Routes.group_path(Endpoint, :index))
|
|
|
|
end
|
2019-03-21 23:21:56 +00:00
|
|
|
end
|
2019-03-14 23:48:46 +00:00
|
|
|
|
2019-07-06 02:00:18 +00:00
|
|
|
def edit(conn, _params) do
|
2019-07-20 23:08:56 +00:00
|
|
|
feed = conn.assigns[:feed] |> Repo.preload([:filter, :pipeline_stages])
|
|
|
|
stages = Enum.sort_by(feed.pipeline_stages, fn stage -> stage.index end)
|
|
|
|
|
|
|
|
render(conn, "edit.html", %{
|
|
|
|
feed: feed,
|
|
|
|
stages: stages
|
|
|
|
})
|
2019-07-06 02:00:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"feed" => feed_params}) do
|
|
|
|
feed = conn.assigns[:feed] |> Repo.preload(:filter)
|
|
|
|
changeset = Feed.changeset(feed, feed_params)
|
|
|
|
{:ok, feed} = Repo.update(changeset)
|
|
|
|
redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id))
|
|
|
|
end
|
|
|
|
|
2019-03-23 23:42:38 +00:00
|
|
|
def delete(conn, _params) do
|
|
|
|
feed = conn.assigns[:feed]
|
2019-03-21 23:21:56 +00:00
|
|
|
{:ok, _} = Repo.delete(feed)
|
|
|
|
redirect(conn, to: Routes.group_path(Endpoint, :show, feed.group_id))
|
2019-02-11 22:22:35 +00:00
|
|
|
end
|
|
|
|
|
2019-03-23 23:42:38 +00:00
|
|
|
def enable_filter(conn, _params) do
|
|
|
|
feed = conn.assigns[:feed] |> Repo.preload(:filter)
|
2019-03-10 23:47:01 +00:00
|
|
|
changeset = Feed.changeset(feed, %{filter_enabled: true})
|
|
|
|
Repo.update(changeset)
|
2019-03-23 23:42:38 +00:00
|
|
|
redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id))
|
2019-03-10 23:47:01 +00:00
|
|
|
end
|
|
|
|
|
2019-03-23 23:42:38 +00:00
|
|
|
def disable_filter(conn, _params) do
|
|
|
|
feed = conn.assigns[:feed] |> Repo.preload(:filter)
|
2019-03-10 23:47:01 +00:00
|
|
|
changeset = Feed.changeset(feed, %{filter_enabled: false})
|
|
|
|
Repo.update(changeset)
|
2019-03-23 23:42:38 +00:00
|
|
|
redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id))
|
2019-03-10 23:47:01 +00:00
|
|
|
end
|
|
|
|
|
2019-03-23 23:42:38 +00:00
|
|
|
def refresh(conn, _params) do
|
2019-03-24 15:00:46 +00:00
|
|
|
feed = conn.assigns[:feed] |> Repo.preload(:filter)
|
2019-02-11 22:22:35 +00:00
|
|
|
feed = Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed)
|
|
|
|
redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id))
|
|
|
|
end
|
2019-03-10 23:47:01 +00:00
|
|
|
end
|