1
0
mirror of https://github.com/algora-io/tv.git synced 2024-11-16 00:58:59 +02:00

add routes /gh/:user_id/{thumbnail,channel} (#33)

This commit is contained in:
Zafer Cesur 2024-05-06 01:44:09 +03:00 committed by GitHub
parent 795e92289f
commit 0ab70fc530
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 61 additions and 2 deletions

View File

@ -65,7 +65,7 @@ defmodule Algora.Accounts do
Registers a user from their GithHub information.
"""
def register_github_user(primary_email, info, emails, token) do
if user = get_user_by_provider(:github, primary_email) do
if user = get_user_by_provider_email(:github, primary_email) do
update_github_token(user, token)
else
info
@ -74,7 +74,7 @@ defmodule Algora.Accounts do
end
end
def get_user_by_provider(provider, email) when provider in [:github] do
def get_user_by_provider_email(provider, email) when provider in [:github] do
query =
from(u in User,
join: i in assoc(u, :identities),
@ -86,6 +86,16 @@ defmodule Algora.Accounts do
Repo.one(query)
end
def get_user_by_provider_id(provider, id) when provider in [:github] do
query =
from(u in User,
join: i in assoc(u, :identities),
where: i.provider == ^to_string(provider) and i.provider_id == ^id
)
Repo.one(query)
end
def change_settings(%User{} = user, attrs) do
User.settings_changeset(user, attrs)
end

View File

@ -0,0 +1,46 @@
defmodule AlgoraWeb.GithubController do
use AlgoraWeb, :controller
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")
_ -> put_status(conn, :not_found)
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}")
_ ->
put_status(conn, :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

View File

@ -86,6 +86,9 @@ defmodule AlgoraWeb.Router do
live "/og/cossgpt", COSSGPTOGLive, :index
live "/:channel_handle", ChannelLive, :show
live "/:channel_handle/:video_id", VideoLive, :show
get "/gh/:user_id/thumbnail", GithubController, :get_thumbnail
get "/gh/:user_id/channel", GithubController, :get_channel
end
end
end