1
0
mirror of https://github.com/algora-io/tv.git synced 2024-11-26 01:00:20 +02:00

add routes /:channel_handle/{chat,embed}

This commit is contained in:
zafer 2024-04-05 18:17:54 +03:00
parent eccc8c1c42
commit 04cef55c1d
4 changed files with 67 additions and 18 deletions

View File

@ -178,6 +178,18 @@ defmodule Algora.Library do
|> Repo.update!()
end
def get_latest_video(%User{} = user) do
from(v in Video,
join: u in User,
on: v.user_id == u.id,
where: u.id == ^user.id,
select_merge: %{channel_handle: u.handle, channel_name: u.name},
order_by: [desc: v.inserted_at],
limit: 1
)
|> Repo.one()
end
def get_mp4_video(id) do
from(v in Video,
where: v.format == :mp4 and (v.transmuxed_from_id == ^id or v.id == ^id),

View File

@ -0,0 +1,17 @@
defmodule AlgoraWeb.ChatPopoutController do
use AlgoraWeb, :controller
alias Algora.{Accounts, Library}
def get(conn, %{"channel_handle" => channel_handle}) do
user = Accounts.get_user_by!(handle: channel_handle)
case Library.get_latest_video(user) do
nil ->
redirect(conn, to: ~p"/#{user.handle}")
video ->
redirect(conn, to: ~p"/#{user.handle}/#{video.id}/chat")
end
end
end

View File

@ -0,0 +1,17 @@
defmodule AlgoraWeb.EmbedPopoutController do
use AlgoraWeb, :controller
alias Algora.{Accounts, Library}
def get(conn, %{"channel_handle" => channel_handle}) do
user = Accounts.get_user_by!(handle: channel_handle)
case Library.get_latest_video(user) do
nil ->
redirect(conn, to: ~p"/#{user.handle}")
video ->
redirect(conn, to: ~p"/#{user.handle}/#{video.id}/embed")
end
end
end

View File

@ -45,6 +45,27 @@ defmodule AlgoraWeb.Router do
end
end
scope "/", AlgoraWeb do
pipe_through [:browser, :embed]
get "/:channel_handle/chat", ChatPopoutController, :get
get "/:channel_handle/embed", EmbedPopoutController, :get
# TODO: consolidate live_sessions
live_session :chat,
layout: {AlgoraWeb.Layouts, :live_chat},
root_layout: {AlgoraWeb.Layouts, :root_bare} do
live "/:channel_handle/:video_id/chat", ChatLive, :show
end
live_session :embed,
layout: {AlgoraWeb.Layouts, :live_bare},
root_layout: {AlgoraWeb.Layouts, :root_bare} do
live "/:channel_handle/:video_id/embed", EmbedLive, :show
end
end
scope "/", AlgoraWeb do
pipe_through :browser
@ -71,22 +92,4 @@ defmodule AlgoraWeb.Router do
live "/:channel_handle/:video_id", VideoLive, :show
end
end
scope "/", AlgoraWeb do
pipe_through [:browser, :embed]
# TODO: consolidate live_sessions
live_session :chat,
layout: {AlgoraWeb.Layouts, :live_chat},
root_layout: {AlgoraWeb.Layouts, :root_bare} do
live "/:channel_handle/:video_id/chat", ChatLive, :show
end
live_session :embed,
layout: {AlgoraWeb.Layouts, :live_bare},
root_layout: {AlgoraWeb.Layouts, :root_bare} do
live "/:channel_handle/:video_id/embed", EmbedLive, :show
end
end
end