port recents endpoints to use stores

This commit is contained in:
cwilvx
2024-07-19 23:07:22 +03:00
parent 2a1f178da2
commit 8f592a4636
7 changed files with 90 additions and 68 deletions
+3 -5
View File
@@ -125,11 +125,9 @@ class AlbumStore:
"""
Returns an album by its hash.
"""
for album in cls.albums:
if album.albumhash == albumhash:
return album
return None
entry = cls.albummap.get(albumhash)
if entry is not None:
return entry.album
@classmethod
def get_albums_by_hashes(cls, albumhashes: Iterable[str]) -> list[Album]:
+27 -3
View File
@@ -1,3 +1,4 @@
from operator import index
from app.db.userdata import PlaylistTable
from app.lib.playlistlib import get_first_4_images
from app.models.playlist import Playlist
@@ -11,9 +12,12 @@ class PlaylistEntry:
self.playlist.clear_lists()
if not playlist.has_image:
self.playlist.images = get_first_4_images(
TrackStore.get_tracks_by_trackhashes(self.trackhashes)
)
self.rebuild_images()
def rebuild_images(self):
self.playlist.images = get_first_4_images(
TrackStore.get_tracks_by_trackhashes(self.trackhashes)
)
class PlaylistStore:
@@ -48,3 +52,23 @@ class PlaylistStore:
@classmethod
def add_playlist(cls, playlist: Playlist):
cls.playlistmap[str(playlist.id)] = PlaylistEntry(playlist)
@classmethod
def get_playlist_by_id(cls, id: str):
entry = cls.playlistmap.get(id)
if entry is not None:
return entry.playlist
@classmethod
def remove_from_playlist(cls, pid: str, tracks: list[dict[str, str]]):
playlist = cls.playlistmap.get(pid)
if not playlist:
return
for track in tracks:
if playlist.trackhashes.index(track["trackhash"]) == track["index"]:
playlist.trackhashes.remove(track["trackhash"])
playlist.rebuild_images()
+17
View File
@@ -350,3 +350,20 @@ class TrackStore:
predicate=predicate,
including_duplicates=True,
)
@classmethod
def get_recently_added(cls, start: int, limit: int | None):
"""
Returns the most recently added tracks.
"""
tracks = cls.get_flat_list()
if limit is None:
return sorted(tracks, key=lambda x: x.last_mod, reverse=True)[start:]
return sorted(tracks, key=lambda x: x.last_mod, reverse=True)[start:limit]
@classmethod
def get_recently_played(cls, limit: int):
tracks = cls.get_flat_list()
return sorted(tracks, key=lambda x: x.lastplayed, reverse=True)[:limit]