diff --git a/lib/last_fm.ex b/lib/last_fm.ex index 964d1927..e242a9a7 100644 --- a/lib/last_fm.ex +++ b/lib/last_fm.ex @@ -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() diff --git a/lib/last_fm/api.ex b/lib/last_fm/api.ex index 4622833d..aa619b75 100644 --- a/lib/last_fm/api.ex +++ b/lib/last_fm/api.ex @@ -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, "") end diff --git a/lib/music_library_web/live/maintenance_live/index.ex b/lib/music_library_web/live/maintenance_live/index.ex index 8a7b91f2..c981d948 100644 --- a/lib/music_library_web/live/maintenance_live/index.ex +++ b/lib/music_library_web/live/maintenance_live/index.ex @@ -214,18 +214,36 @@ defmodule MusicLibraryWeb.MaintenanceLive.Index do

{gettext("Manage your Last.fm connection.")} - - {gettext("Connected")} - - - {gettext("Not connected")} - + <.async_result :let={status} assign={@lastfm_status}> + <:loading> + + {gettext("Checking...")} + + + <:failed :let={_failure}> + + {gettext("Not connected")} + + + + {gettext("Not connected")} + + + {gettext("Outdated token")} + + + {gettext("Connected as %{username}", username: elem(status, 1))} + +