diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 9bf5ad55..ca764bd4 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -211,6 +211,23 @@ defmodule MusicLibrary.Records do end end + def get_last_listened_track(record_id) do + q = + from r in fragment("records, json_each(records.release_ids)"), + where: fragment("records.id = ?", ^record_id), + select: r.value + + release_ids = Repo.all(q) + + q = + from t in LastFm.Track, + where: fragment("? ->> '$.musicbrainz_id'", t.album) in ^release_ids, + order_by: [desc: t.scrobbled_at_uts], + limit: 1 + + Repo.one(q) + end + def refresh_cover(record) do with {:ok, cover_data} <- MusicBrainz.get_cover_art({:url, record.cover_url}) do {:ok, thumb_data} = Cover.resize(cover_data) diff --git a/lib/music_library/scrobble_activity.ex b/lib/music_library/scrobble_activity.ex index dca5b218..512b976f 100644 --- a/lib/music_library/scrobble_activity.ex +++ b/lib/music_library/scrobble_activity.ex @@ -177,7 +177,7 @@ defmodule MusicLibrary.ScrobbleActivity do track.artist.musicbrainz_id end - defp localize_scrobbled_at(uts, timezone) do + def localize_scrobbled_at(uts, timezone) do ldt = uts |> DateTime.from_unix!() diff --git a/lib/music_library_web/live/collection_live/show.ex b/lib/music_library_web/live/collection_live/show.ex index 592fe142..6dc2738d 100644 --- a/lib/music_library_web/live/collection_live/show.ex +++ b/lib/music_library_web/live/collection_live/show.ex @@ -1,6 +1,8 @@ defmodule MusicLibraryWeb.CollectionLive.Show do use MusicLibraryWeb, :live_view + import MusicLibrary.ScrobbleActivity, only: [localize_scrobbled_at: 2] + import MusicLibraryWeb.RecordComponents, only: [ format_label: 1, @@ -19,6 +21,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do {:ok, socket + |> assign(:timezone, resolve_timezone!()) |> assign(:can_scrobble?, ScrobbleActivity.can_scrobble?()) |> assign(:release_with_tracks, nil)} end @@ -26,6 +29,7 @@ defmodule MusicLibraryWeb.CollectionLive.Show do @impl true def handle_params(%{"id" => id}, _, socket) do record = Records.get_record!(id) + last_listened_track = Records.get_last_listened_track(id) socket = if record.selected_release_id do @@ -38,7 +42,8 @@ defmodule MusicLibraryWeb.CollectionLive.Show do socket |> assign(:current_section, :collection) |> assign(:page_title, page_title(socket.assigns.live_action, record)) - |> assign(:record, record)} + |> assign(:record, record) + |> assign(:last_listened_track, last_listened_track)} end @impl true @@ -154,4 +159,9 @@ defmodule MusicLibraryWeb.CollectionLive.Show do defp title_segment(:show), do: gettext("Show") defp title_segment(:edit), do: gettext("Edit") + + defp resolve_timezone! do + Application.get_env(:music_library, MusicLibraryWeb) + |> Keyword.fetch!(:timezone) + end end diff --git a/lib/music_library_web/live/collection_live/show.html.heex b/lib/music_library_web/live/collection_live/show.html.heex index 50bb2e59..aca9b9fd 100644 --- a/lib/music_library_web/live/collection_live/show.html.heex +++ b/lib/music_library_web/live/collection_live/show.html.heex @@ -251,6 +251,15 @@ {Records.Record.format_as_date(@record.updated_at)} +
+
+ {gettext("Last listened at")} +
+
+ {@last_listened_track && + localize_scrobbled_at(@last_listened_track.scrobbled_at_uts, @timezone)} +
+
diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index ffe3fb5e..48c61052 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -879,3 +879,8 @@ msgstr "" #, elixir-autogen, elixir-format msgid "Insertion" msgstr "" + +#: lib/music_library_web/live/collection_live/show.html.heex +#, elixir-autogen, elixir-format +msgid "Last listened at" +msgstr "" diff --git a/priv/gettext/en/LC_MESSAGES/default.po b/priv/gettext/en/LC_MESSAGES/default.po index 336768bf..54cb884d 100644 --- a/priv/gettext/en/LC_MESSAGES/default.po +++ b/priv/gettext/en/LC_MESSAGES/default.po @@ -879,3 +879,8 @@ msgstr "" #, elixir-autogen, elixir-format msgid "Insertion" msgstr "" + +#: lib/music_library_web/live/collection_live/show.html.heex +#, elixir-autogen, elixir-format +msgid "Last listened at" +msgstr ""