Extend last.fm token diagnostics for maintenance page

This commit is contained in:
Claudio Ortolina
2026-03-30 10:55:20 +01:00
parent 5a71a78392
commit 19b7091358
6 changed files with 133 additions and 27 deletions
+6
View File
@@ -83,6 +83,12 @@ defmodule LastFm do
|> API.scrobble(session_key, last_fm_config)
end
@spec get_profile(String.t()) :: {:ok, String.t()} | {:error, term()}
def get_profile(session_key) do
last_fm_config = last_fm_config()
API.get_user_info(session_key, last_fm_config)
end
@spec auth_url() :: String.t()
def auth_url do
last_fm_config = last_fm_config()
+26
View File
@@ -121,6 +121,27 @@ defmodule LastFm.API do
|> get_request()
end
@spec get_user_info(String.t(), LastFm.Config.t()) :: {:ok, String.t()} | {:error, term()}
def get_user_info(session_key, config) do
params =
%{
api_key: config.api_key,
method: "user.getInfo",
sk: session_key,
format: "json"
}
signature = Signature.generate(params, config.shared_secret)
params = Map.put(params, "api_sig", signature)
config
|> new_request()
|> Req.merge(url: "/", params: params)
|> Req.Request.append_response_steps(parse_user_info: &parse_user_info/1)
|> get_request()
end
defp put_musicbrainz_id_or_name(params, {:musicbrainz_id, musicbrainz_id}) do
Keyword.put(params, :mbid, musicbrainz_id)
end
@@ -273,6 +294,11 @@ defmodule LastFm.API do
defp parse_tag_count(_), do: 0
defp parse_user_info({request, response}) do
username = get_in(response.body, ["user", "name"])
{request, Map.put(response, :body, username)}
end
defp sanitize_url(url, api_key) do
String.replace(url, api_key, "<redacted_api_key>")
end
@@ -214,18 +214,36 @@ defmodule MusicLibraryWeb.MaintenanceLive.Index do
</h3>
<p class="mt-2 max-w-4xl text-sm text-zinc-500 dark:text-zinc-400">
{gettext("Manage your Last.fm connection.")}
<span
:if={@lastfm_connected}
class="ml-2 inline-flex items-center rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-800 dark:bg-green-900 dark:text-green-200"
>
{gettext("Connected")}
</span>
<span
:if={!@lastfm_connected}
class="ml-2 inline-flex items-center rounded-full bg-zinc-100 px-2 py-0.5 text-xs font-medium text-zinc-600 dark:bg-zinc-700 dark:text-zinc-300"
>
{gettext("Not connected")}
</span>
<.async_result :let={status} assign={@lastfm_status}>
<:loading>
<span class="ml-2 inline-flex items-center rounded-full bg-zinc-100 px-2 py-0.5 text-xs font-medium text-zinc-600 dark:bg-zinc-700 dark:text-zinc-300">
{gettext("Checking...")}
</span>
</:loading>
<:failed :let={_failure}>
<span class="ml-2 inline-flex items-center rounded-full bg-red-100 px-2 py-0.5 text-xs font-medium text-red-800 dark:bg-red-900 dark:text-red-200">
{gettext("Not connected")}
</span>
</:failed>
<span
:if={status == :not_connected}
class="ml-2 inline-flex items-center rounded-full bg-red-100 px-2 py-0.5 text-xs font-medium text-red-800 dark:bg-red-900 dark:text-red-200"
>
{gettext("Not connected")}
</span>
<span
:if={status == :outdated_token}
class="ml-2 inline-flex items-center rounded-full bg-yellow-100 px-2 py-0.5 text-xs font-medium text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200"
>
{gettext("Outdated token")}
</span>
<span
:if={is_tuple(status) and elem(status, 0) == :connected}
class="ml-2 inline-flex items-center rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-800 dark:bg-green-900 dark:text-green-200"
>
{gettext("Connected as %{username}", username: elem(status, 1))}
</span>
</.async_result>
</p>
<ul class="mt-4">
<li class="space-y-4">
@@ -262,7 +280,21 @@ defmodule MusicLibraryWeb.MaintenanceLive.Index do
current_section: :maintenance
)
|> assign_job_counts()
|> assign_lastfm_status()}
|> assign_async(:lastfm_status, fn ->
status =
case Secrets.get("last_fm_session_key") do
nil ->
:not_connected
%{value: value} ->
case LastFm.get_profile(value) do
{:ok, username} -> {:connected, username}
{:error, _} -> :outdated_token
end
end
{:ok, %{lastfm_status: status}}
end)}
end
@impl true
@@ -272,10 +304,6 @@ defmodule MusicLibraryWeb.MaintenanceLive.Index do
{:noreply, assign_job_counts(socket)}
end
defp assign_lastfm_status(socket) do
assign(socket, :lastfm_connected, Secrets.get("last_fm_session_key") != nil)
end
defp assign_job_counts(socket) do
socket
|> assign(