diff --git a/lib/algora/library.ex b/lib/algora/library.ex index 35890da..f096866 100644 --- a/lib/algora/library.ex +++ b/lib/algora/library.ex @@ -396,6 +396,21 @@ defmodule Algora.Library do 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 user = Accounts.get_user_by!(stream_key: stream_key) diff --git a/lib/algora_web/controllers/github_controller.ex b/lib/algora_web/controllers/github_controller.ex index 565c8c7..541201c 100644 --- a/lib/algora_web/controllers/github_controller.ex +++ b/lib/algora_web/controllers/github_controller.ex @@ -4,43 +4,16 @@ defmodule AlgoraWeb.GithubController do alias Algora.{Accounts, Library} def get_thumbnail(conn, %{"user_id" => user_id}) do - with {:ok, user} <- get_user(user_id), - {:ok, video} <- get_latest_video(user) do - redirect(conn, external: get_thumbnail_url(video)) - else - {:error, :video_not_found} -> redirect(conn, to: ~p"/images/og/default.png") - _ -> send_resp(conn, 404, "Not found") + case Accounts.get_user_by_provider_id(:github, user_id) do + nil -> send_resp(conn, 404, "Not found") + user -> redirect(conn, external: Library.get_thumbnail_url(user)) end end def get_channel(conn, %{"user_id" => user_id}) do - case get_user(user_id) do - {:ok, 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 + case Accounts.get_user_by_provider_id(:github, user_id) do + nil -> send_resp(conn, 404, "Not found") + user -> redirect(conn, to: ~p"/#{user.handle}") end end end