philomena/lib/philomena_web/controllers/channel_controller.ex

45 lines
1.4 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_resource, model: Channel
def index(conn, _params) do
2019-11-30 23:40:53 +01:00
show_nsfw? = conn.cookies["chan_nsfw"] == "true"
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)
|> preload(:associated_artist_tag)
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", layout_class: "layout--wide", channels: channels)
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-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}"
defp url(%{type: "PicartoChannel", short_name: short_name}),
do: "https://picarto.tv/#{short_name}"
defp url(%{type: "PiczelChannel", short_name: short_name}),
do: "https://piczel.tv/watch/#{short_name}"
defp url(%{type: "TwitchChannel", short_name: short_name}),
do: "https://www.twitch.tv/#{short_name}"
2019-11-30 07:30:45 +01:00
end