Skip to content

Commit

Permalink
fix, ui: bugs that prevented adding songs to classic playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
dxstiny committed Sep 22, 2023
1 parent 2514f4e commit fa78e2d
Show file tree
Hide file tree
Showing 8 changed files with 583 additions and 477 deletions.
8 changes: 4 additions & 4 deletions src/server/db/table/smartPlaylists.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class SmartPlaylistModel(IModel, IPlaylistModel):
def __init__(
self,
name: str,
description: str,
cover: str,
definition: str,
plays: int,
description: Optional[str] = None,
cover: Optional[str] = None,
definition: Optional[str] = None,
plays: Optional[int] = None,
id_: Optional[int] = None,
) -> None:
self._id = id_
Expand Down
19 changes: 16 additions & 3 deletions src/server/handler/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,22 @@ async def getPlaylists(self, _: web.Request) -> web.Response:
[playlist.toDict() for playlist in self._playlistManager.playlists]
)

async def createPlaylist(self, _: web.Request) -> web.Response:
@withObjectPayload(
Object(
{
"type": String().enum("classic", "smart").default("classic"),
}
),
inQuery=True,
)
async def createPlaylist(self, payload: Dict[str, str]) -> web.Response:
"""get(/api/playlists/new)"""
return web.Response(status=200, text=str(await self._playlistManager.addPlaylist()))
href = ""
if payload["type"] == "classic":
href = self._playlistManager.addClassicPlaylist()
elif payload["type"] == "smart":
href = self._playlistManager.addSmartPlaylist()
return web.Response(text=href)

@withObjectPayload(
Object(
Expand Down Expand Up @@ -142,7 +155,7 @@ async def peekSmartPlaylist(self, payload: Dict[str, Any]) -> web.Response:
"""post(/api/playlists/smart/preview)"""
playlist = SpecialPlayerPlaylist("Peek", "", payload, "peek", "")
await playlist.waitForLoad()
return web.json_response({})
return web.json_response(playlist.toDict())

@withObjectPayload(
Object(
Expand Down
13 changes: 12 additions & 1 deletion src/server/player/playlistManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from player.iPlayerPlaylist import IPlayerPlaylist
from player.smartPlayerPlaylist import SmartPlayerPlaylist, SpecialPlayerPlaylist
from player.classicPlayerPlaylist import ClassicPlayerPlaylist
from db.table.smartPlaylists import SmartPlaylistModel


T = TypeVar("T") # pylint: disable=invalid-name
Expand Down Expand Up @@ -117,7 +118,7 @@ def playlistLength(self) -> int:
"""return number of playlists"""
return len(self._playlists)

async def addPlaylist(self, name: Optional[str] = None) -> str:
async def addClassicPlaylist(self, name: Optional[str] = None) -> str:
"""creates a playlist"""
plId = self.playlistLength
name = name or f"My Playlist #{plId + 1}"
Expand All @@ -127,6 +128,16 @@ async def addPlaylist(self, name: Optional[str] = None) -> str:
playlist.id = id_
return ClassicPlayerPlaylist(playlist).href

async def addSmartPlaylist(self, name: Optional[str] = None) -> str:
"""creates a playlist"""
plId = self.playlistLength
name = name or f"My Smart Playlist #{plId + 1}"
playlist = SmartPlaylistModel(name)
id_ = await self._dbManager.smartPlaylists.insert(playlist)
if id_:
playlist.id = id_
return SmartPlayerPlaylist(playlist).href

async def removePlaylist(self, playlistId: str) -> bool:
"""removes a playlist"""
self._logger.info("removing playlist %s", playlistId)
Expand Down
147 changes: 75 additions & 72 deletions src/server/router/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@

class Router:
"""Router class"""

@staticmethod
async def _exitHandler(_: web.Request) -> web.Response:
"""force quits the application"""
logger = logging.getLogger()
logger.info("quitting")
os._exit(0) # pylint: disable=protected-access
os._exit(0) # pylint: disable=protected-access

@staticmethod
async def _restartNginx(_: web.Request) -> web.Response:
Expand All @@ -42,124 +43,126 @@ async def _restartNginx(_: web.Request) -> web.Response:
@staticmethod
async def _corsAnywhere(request: web.Request) -> web.Response:
"""proxies any request to the given url"""
url = request.match_info.get('url', "")
url = request.match_info.get("url", "")
url += "?" + request.query_string if request.query_string else ""
logging.getLogger().info("cors-anywhere: %s", url)

if not url:
return web.Response(status = 400)
return web.Response(status=400)
async with aiohttp.ClientSession() as session:
response = await session.get(url)
return web.Response(body = await response.read(),
status = response.status)
return web.Response(body=await response.read(), status=response.status)

@staticmethod
def applyRoutes(app: web.Application, # pylint: disable=too-many-statements, too-many-arguments
playerHandler: PlayerHandler,
downloadHandler: DownloadHandler,
metaHandler: MetaHandler,
sportsHandler: SportsHandler,
newsHandler: NewsHandler,
playlistHandler: PlaylistHandler,
configHandler: ConfigHandler,
websocket: Websocket,
spotify: SpotifyAuth) -> None:
def applyRoutes(
app: web.Application, # pylint: disable=too-many-statements, too-many-arguments
playerHandler: PlayerHandler,
downloadHandler: DownloadHandler,
metaHandler: MetaHandler,
sportsHandler: SportsHandler,
newsHandler: NewsHandler,
playlistHandler: PlaylistHandler,
configHandler: ConfigHandler,
websocket: Websocket,
spotify: SpotifyAuth,
) -> None:
"""Apply routes to the app"""
# /api/player/
app.router.add_get('/api/player/previous', playerHandler.getPrevious)
app.router.add_get('/api/player/next', playerHandler.getNext)
app.router.add_get("/api/player/previous", playerHandler.getPrevious)
app.router.add_get("/api/player/next", playerHandler.getNext)

app.router.add_post('/api/player/at', playerHandler.loadSongAt)
app.router.add_post("/api/player/at", playerHandler.loadSongAt)

app.router.add_post('/api/player/load', playerHandler.loadPlaylist)
app.router.add_post("/api/player/load", playerHandler.loadPlaylist)

app.router.add_post('/api/player/shuffle', playerHandler.postShuffle)
app.router.add_get('/api/player/shuffle', playerHandler.getShuffle)
app.router.add_post("/api/player/shuffle", playerHandler.postShuffle)
app.router.add_get("/api/player/shuffle", playerHandler.getShuffle)

app.router.add_get('/api/player/stream', downloadHandler.stream)
app.router.add_get('/api/player/stream/{id}', downloadHandler.streamFromCache)
app.router.add_get("/api/player/stream", downloadHandler.stream)
app.router.add_get("/api/player/stream/{id}", downloadHandler.streamFromCache)

# UNGROUPED
app.router.add_get("/api/cors/{url:.*}", Router._corsAnywhere)

app.router.add_post('/api/browse/track', metaHandler.getMetadata)
app.router.add_post("/api/browse/track", metaHandler.getMetadata)

app.router.add_post('/api/search', metaHandler.search)
app.router.add_post("/api/search", metaHandler.search)

app.router.add_get('/api/releases', metaHandler.releases)
app.router.add_get("/api/releases", metaHandler.releases)

app.router.add_post('/api/sports', sportsHandler.getMatches)
app.router.add_post("/api/sports", sportsHandler.getMatches)

# /api/spotify/
app.router.add_get('/api/spotify/albums/{id}', metaHandler.spotifyAlbum)
app.router.add_get('/api/spotify/artists/{id}', metaHandler.spotifyArtist)
app.router.add_get('/api/spotify/artists', metaHandler.spotifyArtists)
app.router.add_get('/api/spotify/playlists/{id}', metaHandler.spotifyPlaylist)
app.router.add_get('/api/spotify/playlists', metaHandler.spotifyPlaylists)
app.router.add_post('/api/spotify/following', metaHandler.spotifyFollow)
app.router.add_delete('/api/spotify/following', metaHandler.spotifyUnfollow)
app.router.add_get("/api/spotify/albums/{id}", metaHandler.spotifyAlbum)
app.router.add_get("/api/spotify/artists/{id}", metaHandler.spotifyArtist)
app.router.add_get("/api/spotify/artists", metaHandler.spotifyArtists)
app.router.add_get("/api/spotify/playlists/{id}", metaHandler.spotifyPlaylist)
app.router.add_get("/api/spotify/playlists", metaHandler.spotifyPlaylists)
app.router.add_post("/api/spotify/following", metaHandler.spotifyFollow)
app.router.add_delete("/api/spotify/following", metaHandler.spotifyUnfollow)
app.router.add_post("/api/spotify/meta", metaHandler.fetchSongMeta)
app.router.add_post('/api/spotify/recommendations', metaHandler.spotifyRecommend)
app.router.add_get('/api/spotify/recommendations/{id}', metaHandler.spotifyRecommendSong)
app.router.add_get('/api/spotify/callback', spotify.callbackHandler)
app.router.add_get('/api/spotify/authorise', spotify.clientSideAuthHandler)
app.router.add_post("/api/spotify/recommendations", metaHandler.spotifyRecommend)
app.router.add_get("/api/spotify/recommendations/{id}", metaHandler.spotifyRecommendSong)
app.router.add_get("/api/spotify/callback", spotify.callbackHandler)
app.router.add_get("/api/spotify/authorise", spotify.clientSideAuthHandler)

# /api/me/player
app.router.add_get('/api/me/player/current-track', playerHandler.getCurrentTrack)
app.router.add_get('/api/me/player/current-playlist', playerHandler.getCurrentPlaylist)
app.router.add_get("/api/me/player/current-track", playerHandler.getCurrentTrack)
app.router.add_get("/api/me/player/current-playlist", playerHandler.getCurrentPlaylist)

# /api/news/articles/
app.router.add_get('/api/news/articles', newsHandler.getSomeNews)
app.router.add_post('/api/news/articles', newsHandler.registerArticle)
app.router.add_get('/api/news/articles/{hash}', newsHandler.getArticle)
app.router.add_get("/api/news/articles", newsHandler.getSomeNews)
app.router.add_post("/api/news/articles", newsHandler.registerArticle)
app.router.add_get("/api/news/articles/{hash}", newsHandler.getArticle)

# /api/tracks
app.router.add_get('/api/tracks/{id}', metaHandler.getTrack)
app.router.add_put('/api/tracks/{id}', playerHandler.updateSong)
app.router.add_get('/api/tracks/{id}/download', downloadHandler.downloadTrack)
app.router.add_get("/api/tracks/{id}", metaHandler.getTrack)
app.router.add_put("/api/tracks/{id}", playerHandler.updateSong)
app.router.add_get("/api/tracks/{id}/download", downloadHandler.downloadTrack)

# /api/artists
app.router.add_get('/api/artists/{name}', metaHandler.getArtist)
app.router.add_put('/api/artists/{name}', metaHandler.putArtist)
app.router.add_get("/api/artists/{name}", metaHandler.getArtist)
app.router.add_put("/api/artists/{name}", metaHandler.putArtist)

# /api/playlists/
app.router.add_get('/api/playlists/new', playlistHandler.createPlaylist)
app.router.add_get('/api/playlists', playlistHandler.getPlaylists)
app.router.add_get("/api/playlists/new", playlistHandler.createPlaylist)
app.router.add_get("/api/playlists", playlistHandler.getPlaylists)

app.router.add_get('/api/playlists/{id}', playlistHandler.getPlaylist)
app.router.add_delete('/api/playlists/{id}', playlistHandler.deletePlaylist)
app.router.add_post('/api/playlists/{id}', playlistHandler.updatePlaylist)
app.router.add_get("/api/playlists/{id}", playlistHandler.getPlaylist)
app.router.add_delete("/api/playlists/{id}", playlistHandler.deletePlaylist)
app.router.add_post("/api/playlists/{id}", playlistHandler.updatePlaylist)

app.router.add_post('/api/playlists/{id}/tracks', playlistHandler.addSong)
app.router.add_put('/api/playlists/{id}/tracks', playlistHandler.moveSong)
app.router.add_delete('/api/playlists/{id}/tracks', playlistHandler.removeSong)
app.router.add_post("/api/playlists/{id}/tracks", playlistHandler.addSong)
app.router.add_put("/api/playlists/{id}/tracks", playlistHandler.moveSong)
app.router.add_delete("/api/playlists/{id}/tracks", playlistHandler.removeSong)

# /api/playlists/smart
app.router.add_post('/api/playlists/smart/peek', playlistHandler.peekSmartPlaylist)
app.router.add_post("/api/playlists/smart/peek", playlistHandler.peekSmartPlaylist)
app.router.add_get("/api/playlists/smart/{id}", playlistHandler.getSmartPlaylist)

# /api/config
app.router.add_get('/api/config/first-time', configHandler.firstTime)
app.router.add_put('/api/config', configHandler.updateConfig)
app.router.add_get('/api/config', configHandler.getConfig)
app.router.add_get("/api/config/first-time", configHandler.firstTime)
app.router.add_put("/api/config", configHandler.updateConfig)
app.router.add_get("/api/config", configHandler.getConfig)

app.router.add_post('/api/config/images', metaHandler.upload)
app.router.add_get('/api/config/images', configHandler.getLocalImages)
app.router.add_delete('/api/config/images', configHandler.deleteLocalImage)
app.router.add_post('/api/config/tracks', metaHandler.uploadSong)
app.router.add_get('/api/config/tracks', configHandler.getLocalTracks)
app.router.add_delete('/api/config/tracks', configHandler.deleteLocalTrack)
app.router.add_post("/api/config/images", metaHandler.upload)
app.router.add_get("/api/config/images", configHandler.getLocalImages)
app.router.add_delete("/api/config/images", configHandler.deleteLocalImage)
app.router.add_post("/api/config/tracks", metaHandler.uploadSong)
app.router.add_get("/api/config/tracks", configHandler.getLocalTracks)
app.router.add_delete("/api/config/tracks", configHandler.deleteLocalTrack)

app.router.add_post('/api/config/spotify', configHandler.spotifyConfig)
app.router.add_get('/api/config/spotify', spotify.getSpotifyConfig)
app.router.add_post("/api/config/spotify", configHandler.spotifyConfig)
app.router.add_get("/api/config/spotify", spotify.getSpotifyConfig)

# /api/system
app.router.add_get('/api/system/kill', Router._exitHandler)
app.router.add_get('/api/system/nginx/restart', Router._restartNginx)
app.router.add_get("/api/system/kill", Router._exitHandler)
app.router.add_get("/api/system/nginx/restart", Router._restartNginx)

# websockets
app.router.add_get("/download/ws", Downloader().websocketEndpoint)
app.router.add_get("/player/ws", Connection.websocketEndpoint)
app.router.add_get('/ws', websocket.wsHandler)
app.router.add_get("/ws", websocket.wsHandler)

if not Runtime.args.apiOnly and not Runtime.args.withDocker:
app.router.add_static('/', '../ui/dist')
app.router.add_static("/", "../ui/dist")
Loading

0 comments on commit fa78e2d

Please sign in to comment.