philomena/lib/philomena_web/controllers/channel_controller.ex

106 lines
3.1 KiB
Elixir
Raw Normal View History

2019-11-30 07:30:45 +01:00
defmodule PhilomenaWeb.ChannelController do
use PhilomenaWeb, :controller
alias Philomena.Channels
alias Philomena.Channels.Channel
alias Philomena.Repo
import Ecto.Query
2019-12-18 18:01:33 +01:00
plug :load_and_authorize_resource, model: Channel, only: [:show, :new, :create, :edit, :update]
2019-11-30 07:30:45 +01:00
2020-05-02 04:33:40 +02:00
def index(conn, params) do
2019-11-30 23:40:53 +01:00
show_nsfw? = conn.cookies["chan_nsfw"] == "true"
2020-01-11 05:20:19 +01:00
2019-11-30 07:30:45 +01:00
channels =
Channel
2019-11-30 23:40:53 +01:00
|> maybe_show_nsfw(show_nsfw?)
|> where([c], not is_nil(c.last_fetched_at))
2019-11-30 07:30:45 +01:00
|> order_by(desc: :is_live, asc: :title)
2020-05-02 04:33:40 +02:00
|> join(:left, [c], _ in assoc(c, :associated_artist_tag))
|> preload([_c, t], associated_artist_tag: t)
|> maybe_search(params)
2019-11-30 07:30:45 +01:00
|> Repo.paginate(conn.assigns.scrivener)
2019-12-05 05:27:40 +01:00
subscriptions = Channels.subscriptions(channels, conn.assigns.current_user)
2020-01-11 05:20:19 +01:00
render(conn, "index.html",
title: "Livestreams",
layout_class: "layout--wide",
channels: channels,
subscriptions: subscriptions
)
2019-11-30 07:30:45 +01:00
end
def show(conn, _params) do
channel = conn.assigns.channel
user = conn.assigns.current_user
if user, do: Channels.clear_notification(channel, user)
2019-11-30 07:39:44 +01:00
redirect(conn, external: url(channel))
2019-11-30 07:30:45 +01:00
end
2019-11-30 07:39:44 +01:00
2019-12-18 18:01:33 +01:00
def new(conn, _params) do
changeset = Channels.change_channel(%Channel{})
render(conn, "new.html", title: "New Channel", changeset: changeset)
end
def create(conn, %{"channel" => channel_params}) do
case Channels.create_channel(channel_params) do
{:ok, _channel} ->
conn
|> put_flash(:info, "Channel created successfully.")
|> redirect(to: Routes.channel_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def edit(conn, _params) do
changeset = Channels.change_channel(conn.assigns.channel)
render(conn, "edit.html", title: "Editing Channel", changeset: changeset)
end
def update(conn, %{"channel" => channel_params}) do
case Channels.update_channel(conn.assigns.channel, channel_params) do
{:ok, _channel} ->
conn
|> put_flash(:info, "Channel updated successfully.")
|> redirect(to: Routes.channel_path(conn, :index))
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
2020-05-02 04:33:40 +02:00
defp maybe_search(query, %{"cq" => cq}) when is_binary(cq) and cq != "" do
title_query = "#{cq}%"
tag_query = "%#{cq}%"
where(
query,
[c, t],
ilike(c.title, ^title_query) or ilike(c.short_name, ^title_query) or
ilike(t.name, ^tag_query)
)
end
defp maybe_search(query, _params), do: query
2019-11-30 23:40:53 +01:00
defp maybe_show_nsfw(query, true), do: query
defp maybe_show_nsfw(query, _falsy), do: where(query, [c], c.nsfw == false)
2019-11-30 07:39:44 +01:00
defp url(%{type: "LivestreamChannel", short_name: short_name}),
do: "http://www.livestream.com/#{short_name}"
2020-01-11 05:20:19 +01:00
2019-11-30 07:39:44 +01:00
defp url(%{type: "PicartoChannel", short_name: short_name}),
do: "https://picarto.tv/#{short_name}"
2020-01-11 05:20:19 +01:00
2019-11-30 07:39:44 +01:00
defp url(%{type: "PiczelChannel", short_name: short_name}),
do: "https://piczel.tv/watch/#{short_name}"
2020-01-11 05:20:19 +01:00
2019-11-30 07:39:44 +01:00
defp url(%{type: "TwitchChannel", short_name: short_name}),
do: "https://www.twitch.tv/#{short_name}"
2019-11-30 07:30:45 +01:00
end