ML-177: implementation

This commit is contained in:
Claudio Ortolina
2026-05-10 20:38:45 +01:00
parent e322f75079
commit ba62d54cc5
5 changed files with 277 additions and 17 deletions
@@ -1,7 +1,10 @@
defmodule MusicLibraryWeb.CollectionController do
use MusicLibraryWeb, :controller
alias MusicBrainz
alias MusicLibrary.Collection
alias MusicLibrary.Records
alias MusicLibrary.ScrobbleActivity
def latest(conn, _params) do
latest_record = Collection.get_latest_record!()
@@ -39,6 +42,60 @@ defmodule MusicLibraryWeb.CollectionController do
render(conn, :index, total: total, limit: limit, offset: offset, records: records)
end
def scrobble(conn, %{"record_id" => record_id}) do
case Records.get_record(record_id) do
nil ->
conn
|> put_status(404)
|> json(%{status: "error", reason: "not_found"})
record ->
do_scrobble(conn, record)
end
end
defp do_scrobble(conn, record) do
if is_nil(record.selected_release_id) or record.selected_release_id == "" do
conn
|> put_status(422)
|> json(%{status: "error", reason: "no_selected_release"})
else
case MusicBrainz.get_release(record.selected_release_id) do
{:ok, release} ->
release_with_tracks = MusicBrainz.Release.from_api_response(release)
case ScrobbleActivity.scrobble_release(
release_with_tracks,
:finished_at,
DateTime.utc_now()
) do
{:ok, _response} ->
json(conn, %{status: "ok"})
{:error, :no_duration} ->
conn
|> put_status(422)
|> json(%{status: "error", reason: "no_duration"})
{:error, :no_session_key} ->
conn
|> put_status(503)
|> json(%{status: "error", reason: "lastfm_not_configured"})
{:error, _reason} ->
conn
|> put_status(502)
|> json(%{status: "error", reason: "lastfm_error"})
end
{:error, _reason} ->
conn
|> put_status(502)
|> json(%{status: "error", reason: "musicbrainz_error"})
end
end
end
defp parse_int(nil, default), do: default
defp parse_int(value, default) when is_binary(value) do
@@ -31,6 +31,7 @@ defmodule MusicLibraryWeb.CollectionJSON do
genres: record.genres,
release_date: record.release_date,
purchased_at: record.purchased_at,
selected_release_id: record.selected_release_id,
artists: Enum.map(record.artists, & &1.name),
title: record.title,
cover_url: url(~p"/api/v1/assets/#{Transform.new(hash: record.cover_hash)}"),
+1
View File
@@ -130,6 +130,7 @@ defmodule MusicLibraryWeb.Router do
get "/collection/random", CollectionController, :random
get "/collection/on_this_day", CollectionController, :on_this_day
get "/collection", CollectionController, :index
post "/collection/:record_id/scrobble", CollectionController, :scrobble
get "/errors", ErrorController, :index
get "/errors/:id", ErrorController, :show
post "/errors/:id/mute", ErrorController, :mute