extracted album handling

This commit is contained in:
emeric
2023-10-01 16:04:28 +02:00
parent 0f1a5efffe
commit 0da12bec0f
9 changed files with 541 additions and 428 deletions
+13 -106
View File
@@ -21,7 +21,6 @@
#include <atomic>
#include <ctime>
#include <iomanip>
#include <map>
#include <unordered_map>
@@ -55,6 +54,9 @@
#include "SubsonicId.hpp"
#include "SubsonicResponse.hpp"
#include "responses/Artist.hpp"
#include "responses/Album.hpp"
using namespace Database;
static const std::string_view genreClusterName {"GENRE"};
@@ -183,25 +185,6 @@ checkUserTypeIsAllowed(RequestContext& context, EnumSet<Database::UserType> allo
throw UserNotAuthorizedError {};
}
static
std::string
getArtistNames(const std::vector<Artist::pointer>& artists)
{
if (artists.size() == 1)
return artists.front()->getName();
std::vector<std::string> names;
names.resize(artists.size());
std::transform(std::cbegin(artists), std::cend(artists), std::begin(names),
[](const Artist::pointer& artist)
{
return artist->getName();
});
return StringUtils::joinStrings(names, ", ");
}
static
std::string
getTrackPath(const Track::pointer& track)
@@ -254,17 +237,6 @@ formatToSuffix(AudioFormat format)
return "";
}
static
std::string
dateTimeToCreatedString(const Wt::WDateTime& dateTime)
{
const std::time_t t {dateTime.toTime_t()};
std::tm gmTime;
std::ostringstream oss;
oss << std::put_time(::gmtime_r(&t, &gmTime), "%FT%T");
return oss.str();
}
static
Response::Node
trackToResponseNode(const Track::pointer& track, Session& dbSession, const User::pointer& user)
@@ -303,7 +275,7 @@ trackToResponseNode(const Track::pointer& track, Session& dbSession, const User:
const std::vector<Artist::pointer>& artists {track->getArtists({TrackArtistLinkType::Artist})};
if (!artists.empty())
{
trackResponse.setAttribute("artist", getArtistNames(artists));
trackResponse.setAttribute("artist", utils::joinArtistNames(artists));
if (artists.size() == 1)
trackResponse.setAttribute("artistId", idToString(artists.front()->getId()));
@@ -318,7 +290,7 @@ trackToResponseNode(const Track::pointer& track, Session& dbSession, const User:
trackResponse.setAttribute("duration", std::chrono::duration_cast<std::chrono::seconds>(track->getDuration()).count());
trackResponse.setAttribute("type", "music");
trackResponse.setAttribute("created", dateTimeToCreatedString(track->getLastWritten()));
trackResponse.setAttribute("created", StringUtils::toISO8601String(track->getLastWritten()));
if (Service<Scrobbling::IScrobblingService>::get()->isStarred(user->getId(), track->getId()))
trackResponse.setAttribute("starred", reportedStarredDate);
@@ -351,71 +323,6 @@ trackBookmarkToResponseNode(const TrackBookmark::pointer& trackBookmark)
return trackBookmarkNode;
}
static
Response::Node
releaseToResponseNode(const Release::pointer& release, Session& dbSession, const User::pointer& user, bool id3)
{
Response::Node albumNode;
if (id3)
{
albumNode.setAttribute("name", release->getName());
albumNode.setAttribute("songCount", release->getTracksCount());
albumNode.setAttribute("duration", std::chrono::duration_cast<std::chrono::seconds>(release->getDuration()).count());
}
else
{
albumNode.setAttribute("title", release->getName());
albumNode.setAttribute("isDir", true);
}
albumNode.setAttribute("created", dateTimeToCreatedString(release->getLastWritten()));
albumNode.setAttribute("id", idToString(release->getId()));
albumNode.setAttribute("coverArt", idToString(release->getId()));
if (const Wt::WDate releaseDate {release->getReleaseDate()}; releaseDate.isValid())
albumNode.setAttribute("year", releaseDate.year());
auto artists {release->getReleaseArtists()};
if (artists.empty())
artists = release->getArtists();
if (artists.empty() && !id3)
{
albumNode.setAttribute("parent", idToString(RootId {}));
}
else if (!artists.empty())
{
albumNode.setAttribute("artist", getArtistNames(artists));
if (artists.size() == 1)
{
albumNode.setAttribute(id3 ? "artistId" : "parent", idToString(artists.front()->getId()));
}
else
{
if (!id3)
albumNode.setAttribute("parent", idToString(RootId {}));
}
}
if (id3)
{
// Report the first GENRE for this track
ClusterType::pointer clusterType{ClusterType::find(dbSession, genreClusterName)};
if (clusterType)
{
auto clusters{release->getClusterGroups({clusterType}, 1)};
if (!clusters.empty() && !clusters.front().empty())
albumNode.setAttribute("genre", clusters.front().front()->getName());
}
}
if (Service<Scrobbling::IScrobblingService>::get()->isStarred(user->getId(), release->getId()))
albumNode.setAttribute("starred", reportedStarredDate);
return albumNode;
}
static
Response::Node
artistToResponseNode(const Artist::pointer& artist, Session& session, const User::pointer& user, bool id3)
@@ -816,7 +723,7 @@ handleGetAlbumListRequestCommon(const RequestContext& context, bool id3)
for (const ReleaseId releaseId : releases.results)
{
const Release::pointer release {Release::find(context.dbSession, releaseId)};
albumListNode.addArrayChild("album", releaseToResponseNode(release, context.dbSession, user, id3));
albumListNode.addArrayChild("album", createAlbumNode(release, context.dbSession, user, id3));
}
return response;
@@ -854,16 +761,16 @@ handleGetAlbumRequest(RequestContext& context)
throw UserNotAuthorizedError {};
Response response {Response::createOkResponse(context.serverProtocolVersion)};
Response::Node releaseNode {releaseToResponseNode(release, context.dbSession, user, true /* id3 */)};
Response::Node albumNode {createAlbumNode(release, context.dbSession, user, true /* id3 */)};
const auto tracks {Track::find(context.dbSession, Track::FindParameters {}.setRelease(id).setSortMethod(TrackSortMethod::Release))};
for (const TrackId trackId : tracks.results)
{
const Track::pointer track {Track::find(context.dbSession, trackId)};
releaseNode.addArrayChild("song", trackToResponseNode(track, context.dbSession, user));
albumNode.addArrayChild("song", trackToResponseNode(track, context.dbSession, user));
}
response.addNode("album", std::move(releaseNode));
response.addNode("album", std::move(albumNode));
return response;
}
@@ -915,7 +822,7 @@ handleGetArtistRequest(RequestContext& context)
for (const ReleaseId releaseId : releases.results)
{
const Release::pointer release {Release::find(context.dbSession, releaseId)};
artistNode.addArrayChild("album", releaseToResponseNode(release, context.dbSession, user, true /* id3 */));
artistNode.addArrayChild("album", createAlbumNode(release, context.dbSession, user, true /* id3 */));
}
response.addNode("artist", std::move(artistNode));
@@ -1029,7 +936,7 @@ handleGetMusicDirectoryRequest(RequestContext& context)
for (const ReleaseId artistReleaseId : artistReleases.results)
{
const Release::pointer release {Release::find(context.dbSession, artistReleaseId)};
directoryNode.addArrayChild("child", releaseToResponseNode(release, context.dbSession, user, false /* no id3 */));
directoryNode.addArrayChild("child", createAlbumNode(release, context.dbSession, user, false /* no id3 */));
}
}
else if (releaseId)
@@ -1311,7 +1218,7 @@ handleGetStarredRequestCommon(RequestContext& context, bool id3)
for (const ReleaseId releaseId : scrobbling.getStarredReleases(context.userId, {} /* clusters */, Range {}).results)
{
if (auto release {Release::find(context.dbSession, releaseId)})
starredNode.addArrayChild("album", releaseToResponseNode(release, context.dbSession, user, id3));
starredNode.addArrayChild("album", createAlbumNode(release, context.dbSession, user, id3));
}
for (const TrackId trackId : scrobbling.getStarredTracks(context.userId, {} /* clusters */, Range {}).results)
@@ -1547,7 +1454,7 @@ handleSearchRequestCommon(RequestContext& context, bool id3)
for (const ReleaseId releaseId : releaseIds.results)
{
const auto release {Release::find(context.dbSession, releaseId)};
searchResult2Node.addArrayChild("album", releaseToResponseNode(release, context.dbSession, user, id3));
searchResult2Node.addArrayChild("album", createAlbumNode(release, context.dbSession, user, id3));
}
}
+102
View File
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "services/database/Cluster.hpp"
#include "services/database/Artist.hpp"
#include "services/database/Release.hpp"
#include "services/database/User.hpp"
#include "services/scrobbling/IScrobblingService.hpp"
#include "utils/Service.hpp"
#include "utils/String.hpp"
#include "responses/Album.hpp"
#include "responses/Artist.hpp"
#include "SubsonicId.hpp"
namespace API::Subsonic
{
static const std::string_view reportedDummyStarredDate {"2000-01-01T00:00:00"};
using namespace Database;
Response::Node createAlbumNode(const Release::pointer& release, Session& dbSession, const User::pointer& user, bool id3)
{
Response::Node albumNode;
if (id3) {
albumNode.setAttribute("name", release->getName());
albumNode.setAttribute("songCount", release->getTracksCount());
albumNode.setAttribute(
"duration", std::chrono::duration_cast<std::chrono::seconds>(
release->getDuration())
.count());
}
else
{
albumNode.setAttribute("title", release->getName());
albumNode.setAttribute("isDir", true);
}
albumNode.setAttribute("created", StringUtils::toISO8601String(release->getLastWritten()));
albumNode.setAttribute("id", idToString(release->getId()));
albumNode.setAttribute("coverArt", idToString(release->getId()));
if (const Wt::WDate releaseDate{ release->getReleaseDate() }; releaseDate.isValid())
albumNode.setAttribute("year", releaseDate.year());
auto artists{ release->getReleaseArtists() };
if (artists.empty())
artists = release->getArtists();
if (artists.empty() && !id3)
{
albumNode.setAttribute("parent", idToString(RootId{}));
}
else if (!artists.empty())
{
albumNode.setAttribute("artist", utils::joinArtistNames(artists));
if (artists.size() == 1)
{
albumNode.setAttribute(id3 ? "artistId" : "parent", idToString(artists.front()->getId()));
}
else
{
if (!id3)
albumNode.setAttribute("parent", idToString(RootId{}));
}
}
if (id3)
{
// Report the first GENRE for this track
ClusterType::pointer clusterType{ ClusterType::find(dbSession, "GENRE") };
if (clusterType)
{
auto clusters{ release->getClusterGroups({clusterType}, 1) };
if (!clusters.empty() && !clusters.front().empty())
albumNode.setAttribute("genre", clusters.front().front()->getName());
}
}
if (Service<Scrobbling::IScrobblingService>::get()->isStarred(user->getId(), release->getId()))
albumNode.setAttribute("starred", reportedDummyStarredDate); // TODO report correct date/time
return albumNode;
}
}
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "services/database/Object.hpp"
#include "SubsonicResponse.hpp"
namespace Database
{
class Release;
class User;
}
namespace API::Subsonic
{
Response::Node createAlbumNode(const Database::ObjectPtr<Database::Release>& release, Database::Session& dbSession, const Database::ObjectPtr<Database::User>& user, bool id3);
}
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "responses/Artist.hpp"
#include "services/database/Artist.hpp"
namespace API::Subsonic
{
using namespace Database;
namespace utils
{
std::string joinArtistNames(const std::vector<Artist::pointer>& artists)
{
if (artists.size() == 1)
return artists.front()->getName();
std::vector<std::string> names;
names.resize(artists.size());
std::transform(std::cbegin(artists), std::cend(artists), std::begin(names),
[](const Artist::pointer& artist)
{
return artist->getName();
});
return StringUtils::joinStrings(names, ", ");
}
}
}
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <vector>
#include "services/database/Object.hpp"
#include "SubsonicResponse.hpp"
namespace Database
{
class Artist;
class User;
}
namespace API::Subsonic
{
namespace utils
{
std::string joinArtistNames(const std::vector<Database::ObjectPtr<Database::Artist>>& artists);
}
}