philomena/lib/philomena_web/controllers/channel_controller.ex

116 lines
3.3 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
plug :load_and_authorize_resource,
model: Channel,
only: [:show, :new, :create, :edit, :update, :delete]
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
2024-07-08 00:09:20 +02:00
Channels.clear_channel_notification(channel, user)
2019-11-30 07:30:45 +01:00
2024-04-28 04:58:43 +02:00
redirect(conn, external: channel_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: ~p"/channels")
2019-12-18 18:01:33 +01:00
{: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: ~p"/channels")
2019-12-18 18:01:33 +01:00
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
def delete(conn, _params) do
{:ok, _channel} = Channels.delete_channel(conn.assigns.channel)
conn
|> put_flash(:info, "Channel destroyed successfully.")
|> redirect(to: ~p"/channels")
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)
2024-04-28 04:58:43 +02:00
defp channel_url(%{type: "LivestreamChannel", short_name: short_name}),
2019-11-30 07:39:44 +01:00
do: "http://www.livestream.com/#{short_name}"
2020-01-11 05:20:19 +01:00
2024-04-28 04:58:43 +02:00
defp channel_url(%{type: "PicartoChannel", short_name: short_name}),
2019-11-30 07:39:44 +01:00
do: "https://picarto.tv/#{short_name}"
2020-01-11 05:20:19 +01:00
2024-04-28 04:58:43 +02:00
defp channel_url(%{type: "PiczelChannel", short_name: short_name}),
2019-11-30 07:39:44 +01:00
do: "https://piczel.tv/watch/#{short_name}"
2020-01-11 05:20:19 +01:00
2024-04-28 04:58:43 +02:00
defp channel_url(%{type: "TwitchChannel", short_name: short_name}),
2019-11-30 07:39:44 +01:00
do: "https://www.twitch.tv/#{short_name}"
2019-11-30 07:30:45 +01:00
end