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

move thumbnail logic into library context

This commit is contained in:
zafer 2024-05-08 00:19:00 +03:00
parent 454db217d4
commit d8e468f731
2 changed files with 21 additions and 33 deletions

View File

@ -396,6 +396,21 @@ defmodule Algora.Library do
end end
end end
def get_thumbnail_url(%Video{} = video) do
video.thumbnail_url || "#{AlgoraWeb.Endpoint.url()}/images/og/default.png"
end
def get_thumbnail_url(%User{} = user) do
case get_latest_video(user) do
# TODO:
nil ->
"#{AlgoraWeb.Endpoint.url()}/images/og/default.png"
video ->
get_thumbnail_url(video)
end
end
def reconcile_livestream(%Video{} = video, stream_key) do def reconcile_livestream(%Video{} = video, stream_key) do
user = Accounts.get_user_by!(stream_key: stream_key) user = Accounts.get_user_by!(stream_key: stream_key)

View File

@ -4,43 +4,16 @@ defmodule AlgoraWeb.GithubController do
alias Algora.{Accounts, Library} alias Algora.{Accounts, Library}
def get_thumbnail(conn, %{"user_id" => user_id}) do def get_thumbnail(conn, %{"user_id" => user_id}) do
with {:ok, user} <- get_user(user_id), case Accounts.get_user_by_provider_id(:github, user_id) do
{:ok, video} <- get_latest_video(user) do nil -> send_resp(conn, 404, "Not found")
redirect(conn, external: get_thumbnail_url(video)) user -> redirect(conn, external: Library.get_thumbnail_url(user))
else
{:error, :video_not_found} -> redirect(conn, to: ~p"/images/og/default.png")
_ -> send_resp(conn, 404, "Not found")
end end
end end
def get_channel(conn, %{"user_id" => user_id}) do def get_channel(conn, %{"user_id" => user_id}) do
case get_user(user_id) do case Accounts.get_user_by_provider_id(:github, user_id) do
{:ok, user} -> nil -> send_resp(conn, 404, "Not found")
redirect(conn, to: ~p"/#{user.handle}") user -> redirect(conn, to: ~p"/#{user.handle}")
_ ->
send_resp(conn, 404, "Not found")
end
end
defp get_user(id) do
case Accounts.get_user_by_provider_id(:github, id) do
nil -> {:error, :user_not_found}
user -> {:ok, user}
end
end
defp get_latest_video(user) do
case Library.get_latest_video(user) do
nil -> {:error, :video_not_found}
user -> {:ok, user}
end
end
defp get_thumbnail_url(video) do
case video.thumbnail_url do
nil -> ~p"/images/og/default.png"
url -> url
end end
end end
end end