Display number of scrobbles when viewing a record

This commit is contained in:
Claudio Ortolina
2025-11-19 13:50:43 +00:00
parent a597ff4032
commit cc7d00c750
5 changed files with 55 additions and 12 deletions
+20 -10
View File
@@ -242,6 +242,21 @@ defmodule MusicLibrary.Records do
end
def get_last_listened_track(record) do
q =
from t in scrobbles_for_record_query(record),
order_by: [desc: t.scrobbled_at_uts],
limit: 1
Repo.one(q)
end
def play_count(record) do
record
|> scrobbles_for_record_query()
|> Repo.aggregate(:count)
end
defp scrobbles_for_record_query(record) do
record_id = record.id
q =
@@ -253,16 +268,11 @@ defmodule MusicLibrary.Records do
main_artist_name = Record.main_artist(record).name
record_title = record.title
q =
from t in LastFm.Track,
where: fragment("? ->> '$.musicbrainz_id'", t.album) in ^release_ids,
or_where:
fragment("? ->> '$.title'", t.album) == ^record_title and
fragment("? ->> '$.name'", t.artist) == ^main_artist_name,
order_by: [desc: t.scrobbled_at_uts],
limit: 1
Repo.one(q)
from t in LastFm.Track,
where: fragment("? ->> '$.musicbrainz_id'", t.album) in ^release_ids,
or_where:
fragment("? ->> '$.title'", t.album) == ^record_title and
fragment("? ->> '$.name'", t.artist) == ^main_artist_name
end
def refresh_cover(record) do
@@ -35,6 +35,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
def handle_params(%{"id" => id}, _, socket) do
record = Records.get_record!(id)
last_listened_track = Records.get_last_listened_track(record)
play_count = Records.play_count(record) || 0
socket =
if record.selected_release_id do
@@ -48,6 +49,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do
|> assign(:page_title, page_title(socket.assigns.live_action, record))
|> assign(:record, record)
|> assign(:last_listened_track, last_listened_track)
|> assign(:play_count, play_count)
|> assign_similar_records()}
end
@@ -321,8 +321,15 @@
{gettext("Last listened at")}
</dt>
<dd class="mt-1 text-xs md:text-sm leading-6 text-zinc-700 dark:text-zinc-300 sm:col-span-2 sm:mt-0">
{@last_listened_track &&
localize_scrobbled_at(@last_listened_track.scrobbled_at_uts, @timezone)}
<span :if={@last_listened_track}>
{localize_scrobbled_at(@last_listened_track.scrobbled_at_uts, @timezone)}
</span>
<span :if={@play_count == 0}>
{gettext("Never")}
</span>
<span :if={@play_count > 0}>
{ngettext("(1 scrobble)", "(%{count} scrobbles)", @play_count)}
</span>
</dd>
</div>
</dl>
+12
View File
@@ -1634,3 +1634,15 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "No record found"
msgstr ""
#: lib/music_library_web/live/collection_live/show.html.heex
#, elixir-autogen, elixir-format
msgid "(1 scrobble)"
msgid_plural "(%{count} scrobbles)"
msgstr[0] ""
msgstr[1] ""
#: lib/music_library_web/live/collection_live/show.html.heex
#, elixir-autogen, elixir-format
msgid "Never"
msgstr ""
+12
View File
@@ -1634,3 +1634,15 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "No record found"
msgstr ""
#: lib/music_library_web/live/collection_live/show.html.heex
#, elixir-autogen, elixir-format, fuzzy
msgid "(1 scrobble)"
msgid_plural "(%{count} scrobbles)"
msgstr[0] ""
msgstr[1] ""
#: lib/music_library_web/live/collection_live/show.html.heex
#, elixir-autogen, elixir-format
msgid "Never"
msgstr ""