Subsonic API: added genres/moods tags for songs

This commit is contained in:
emeric
2023-10-04 21:38:48 +02:00
parent 5b11009ead
commit a47f463ca9
6 changed files with 1044 additions and 1001 deletions
+135 -133
View File
@@ -28,177 +28,179 @@
#include "SqlQuery.hpp" #include "SqlQuery.hpp"
#include "Utils.hpp" #include "Utils.hpp"
namespace Database { namespace Database
Cluster::Cluster(ObjectPtr<ClusterType> type, std::string_view name)
: _name {std::string {name, 0, _maxNameLength}},
_clusterType {getDboPtr(type)}
{ {
} namespace
{
Wt::Dbo::Query<ClusterId> createQuery(Session& session, const Cluster::FindParameters& params)
{
session.checkSharedLocked();
Cluster::pointer auto query{ session.getDboSession().query<ClusterId>("SELECT DISTINCT c.id FROM cluster c") };
Cluster::create(Session& session, ObjectPtr<ClusterType> type, std::string_view name)
{
return session.getDboSession().add(std::unique_ptr<Cluster> {new Cluster {type, name}});
}
std::size_t if (params.track.isValid())
Cluster::getCount(Session& session) {
{ query.join("track_cluster t_c ON t_c.cluster_id = c.id");
session.checkSharedLocked(); query.join("track t ON t.id = t_c.track_id");
query.where("t.id = ?").bind(params.track);
}
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster"); if (params.clusterType.isValid())
} query.where("c.cluster_type_id = ?").bind(params.clusterType);
RangeResults<ClusterId> return query;
Cluster::find(Session& session, Range range) }
{ }
session.checkSharedLocked();
auto query {session.getDboSession().query<ClusterId>("SELECT id FROM cluster")};
return Utils::execQuery(query, range); Cluster::Cluster(ObjectPtr<ClusterType> type, std::string_view name)
} : _name{ std::string {name, 0, _maxNameLength} },
_clusterType{ getDboPtr(type) }
{
}
RangeResults<ClusterId> Cluster::pointer Cluster::create(Session& session, ObjectPtr<ClusterType> type, std::string_view name)
Cluster::findOrphans(Session& session, Range range) {
{ return session.getDboSession().add(std::unique_ptr<Cluster> {new Cluster{ type, name }});
session.checkSharedLocked(); }
auto query {session.getDboSession().query<ClusterId>("SELECT DISTINCT c.id FROM cluster c WHERE NOT EXISTS(SELECT 1 FROM track_cluster t_c WHERE t_c.cluster_id = c.id)")};
return Utils::execQuery(query, range); std::size_t Cluster::getCount(Session& session)
} {
session.checkSharedLocked();
Cluster::pointer return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster");
Cluster::find(Session& session, ClusterId id) }
{
session.checkSharedLocked();
return session.getDboSession().find<Cluster>().where("id = ?").bind(id).resultValue(); RangeResults<ClusterId> Cluster::find(Session& session, const FindParameters& params)
} {
session.checkSharedLocked();
auto query{ createQuery(session, params) };
void return Utils::execQuery(query, params.range);
Cluster::addTrack(ObjectPtr<Track> track) }
{
_tracks.insert(getDboPtr(track));
}
RangeResults<TrackId> RangeResults<ClusterId> Cluster::findOrphans(Session& session, Range range)
Cluster::getTracks(Range range) const {
{ session.checkSharedLocked();
assert(session()); auto query{ session.getDboSession().query<ClusterId>("SELECT DISTINCT c.id FROM cluster c WHERE NOT EXISTS(SELECT 1 FROM track_cluster t_c WHERE t_c.cluster_id = c.id)") };
auto query {session()->query<TrackId>("SELECT t.id FROM track t INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id") return Utils::execQuery(query, range);
.where("c.id = ?").bind(getId())}; }
return Utils::execQuery(query, range); Cluster::pointer Cluster::find(Session& session, ClusterId id)
} {
session.checkSharedLocked();
std::size_t return session.getDboSession().find<Cluster>().where("id = ?").bind(id).resultValue();
Cluster::getReleasesCount() const }
{
assert(session());
return session()->query<int>("SELECT COUNT(DISTINCT r.id) FROM release r INNER JOIN track t on t.release_id = r.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id") void Cluster::addTrack(ObjectPtr<Track> track)
.where("c.id = ?").bind(getId()); {
} _tracks.insert(getDboPtr(track));
}
RangeResults<TrackId> Cluster::getTracks(Range range) const
{
assert(session());
auto query{ session()->query<TrackId>("SELECT t.id FROM track t INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id")
.where("c.id = ?").bind(getId()) };
return Utils::execQuery(query, range);
}
std::size_t Cluster::getReleasesCount() const
{
assert(session());
return session()->query<int>("SELECT COUNT(DISTINCT r.id) FROM release r INNER JOIN track t on t.release_id = r.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id")
.where("c.id = ?").bind(getId());
}
ClusterType::ClusterType(std::string_view name) ClusterType::ClusterType(std::string_view name)
: _name {name} : _name{ name }
{ {
} }
ClusterType::pointer ClusterType::pointer ClusterType::create(Session& session, const std::string& name)
ClusterType::create(Session& session, const std::string& name) {
{ return session.getDboSession().add(std::unique_ptr<ClusterType> {new ClusterType{ name }});
return session.getDboSession().add(std::unique_ptr<ClusterType> {new ClusterType {name}}); }
}
std::size_t std::size_t ClusterType::getCount(Session& session)
ClusterType::getCount(Session& session) {
{ session.checkSharedLocked();
session.checkSharedLocked();
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster_type"); return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster_type");
} }
RangeResults<ClusterTypeId> RangeResults<ClusterTypeId> ClusterType::findOrphans(Session& session, Range range)
ClusterType::findOrphans(Session& session, Range range) {
{ session.checkSharedLocked();
session.checkSharedLocked();
auto query {session.getDboSession().query<ClusterTypeId>( auto query{ session.getDboSession().query<ClusterTypeId>(
"SELECT c_t.id from cluster_type c_t" "SELECT c_t.id from cluster_type c_t"
" LEFT OUTER JOIN cluster c ON c_t.id = c.cluster_type_id") " LEFT OUTER JOIN cluster c ON c_t.id = c.cluster_type_id")
.where("c.id IS NULL")}; .where("c.id IS NULL") };
return Utils::execQuery(query, range); return Utils::execQuery(query, range);
} }
RangeResults<ClusterTypeId> RangeResults<ClusterTypeId> ClusterType::findUsed(Session& session, Range range)
ClusterType::findUsed(Session& session, Range range) {
{ session.checkSharedLocked();
session.checkSharedLocked();
auto query {session.getDboSession().query<ClusterTypeId>( auto query{ session.getDboSession().query<ClusterTypeId>(
"SELECT DISTINCT c_t.id from cluster_type c_t") "SELECT DISTINCT c_t.id from cluster_type c_t")
.join("cluster c ON c_t.id = c.cluster_type_id")}; .join("cluster c ON c_t.id = c.cluster_type_id") };
return Utils::execQuery(query, range); return Utils::execQuery(query, range);
} }
ClusterType::pointer ClusterType::pointer ClusterType::find(Session& session, std::string_view name)
ClusterType::find(Session& session, std::string_view name) {
{ session.checkSharedLocked();
session.checkSharedLocked();
return session.getDboSession().find<ClusterType>().where("name = ?").bind(std::string {name}).resultValue(); return session.getDboSession().find<ClusterType>().where("name = ?").bind(std::string{ name }).resultValue();
} }
ClusterType::pointer ClusterType::pointer ClusterType::find(Session& session, ClusterTypeId id)
ClusterType::find(Session& session, ClusterTypeId id) {
{ session.checkSharedLocked();
session.checkSharedLocked();
return session.getDboSession().find<ClusterType>().where("id = ?").bind(id).resultValue(); return session.getDboSession().find<ClusterType>().where("id = ?").bind(id).resultValue();
} }
RangeResults<ClusterTypeId> RangeResults<ClusterTypeId> ClusterType::find(Session& session, Range range)
ClusterType::find(Session& session, Range range) {
{ session.checkSharedLocked();
session.checkSharedLocked();
auto query {session.getDboSession().query<ClusterTypeId>("SELECT id from cluster_type")}; auto query{ session.getDboSession().query<ClusterTypeId>("SELECT id from cluster_type") };
return Utils::execQuery(query, range); return Utils::execQuery(query, range);
} }
Cluster::pointer Cluster::pointer ClusterType::getCluster(const std::string& name) const
ClusterType::getCluster(const std::string& name) const {
{ assert(self());
assert(self()); assert(session());
assert(session());
return session()->find<Cluster>() return session()->find<Cluster>()
.where("name = ?").bind(name) .where("name = ?").bind(name)
.where("cluster_type_id = ?").bind(getId()).resultValue(); .where("cluster_type_id = ?").bind(getId()).resultValue();
} }
std::vector<Cluster::pointer> std::vector<Cluster::pointer> ClusterType::getClusters() const
ClusterType::getClusters() const {
{ assert(self());
assert(self()); assert(session());
assert(session());
auto res = session()->find<Cluster>()
.where("cluster_type_id = ?").bind(getId())
.orderBy("name")
.resultList();
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
auto res = session()->find<Cluster>()
.where("cluster_type_id = ?").bind(getId())
.orderBy("name")
.resultList();
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
} // namespace Database } // namespace Database
@@ -33,93 +33,104 @@
namespace Database { namespace Database {
class Track; class Track;
class ClusterType; class ClusterType;
class ScanSettings; class ScanSettings;
class Session; class Session;
class Cluster final : public Object<Cluster, ClusterId> class Cluster final : public Object<Cluster, ClusterId>
{ {
public: public:
Cluster() = default; struct FindParameters
{
Range range;
ClusterTypeId clusterType; // if non empty, clusters that belong to this cluster type
TrackId track; // if set, clusters involved in this track
// Find utility FindParameters& setRange(Range _range) { range = _range; return *this; }
static std::size_t getCount(Session& session); FindParameters& setClusterType(ClusterTypeId _clusterType) { clusterType = _clusterType; return *this; }
static RangeResults<ClusterId> find(Session& session, Range range); FindParameters& setTrack(TrackId _track) { track = _track; return *this; }
static pointer find(Session& session, ClusterId id); };
static RangeResults<ClusterId> findOrphans(Session& session, Range range);
// Accessors Cluster() = default;
const std::string& getName() const { return _name; }
ObjectPtr<ClusterType> getType() const { return _clusterType; }
std::size_t getTracksCount() const { return _tracks.size(); }
RangeResults<TrackId> getTracks(Range range) const;
std::size_t getReleasesCount() const;
void addTrack(ObjectPtr<Track> track); // Find utility
static std::size_t getCount(Session& session);
static RangeResults<ClusterId> find(Session& session, const FindParameters& range);
static pointer find(Session& session, ClusterId id);
static RangeResults<ClusterId> findOrphans(Session& session, Range range);
template<class Action> // Accessors
void persist(Action& a) const std::string& getName() const { return _name; }
{ ObjectPtr<ClusterType> getType() const { return _clusterType; }
Wt::Dbo::field(a, _name, "name"); std::size_t getTracksCount() const { return _tracks.size(); }
RangeResults<TrackId> getTracks(Range range) const;
std::size_t getReleasesCount() const;
Wt::Dbo::belongsTo(a, _clusterType, "cluster_type", Wt::Dbo::OnDeleteCascade); void addTrack(ObjectPtr<Track> track);
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_cluster", "", Wt::Dbo::OnDeleteCascade);
}
private: template<class Action>
friend class Session; void persist(Action& a)
Cluster(ObjectPtr<ClusterType> type, std::string_view name); {
static pointer create(Session& session, ObjectPtr<ClusterType> type, std::string_view name); Wt::Dbo::field(a, _name, "name");
static const std::size_t _maxNameLength = 128; Wt::Dbo::belongsTo(a, _clusterType, "cluster_type", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_cluster", "", Wt::Dbo::OnDeleteCascade);
}
std::string _name; private:
friend class Session;
Cluster(ObjectPtr<ClusterType> type, std::string_view name);
static pointer create(Session& session, ObjectPtr<ClusterType> type, std::string_view name);
Wt::Dbo::ptr<ClusterType> _clusterType; static const std::size_t _maxNameLength = 128;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
}; std::string _name;
Wt::Dbo::ptr<ClusterType> _clusterType;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
};
class ClusterType final : public Object<ClusterType, ClusterTypeId> class ClusterType final : public Object<ClusterType, ClusterTypeId>
{ {
public: public:
ClusterType() = default; ClusterType() = default;
// Getters // Getters
static std::size_t getCount(Session& session); static std::size_t getCount(Session& session);
static RangeResults<ClusterTypeId> find(Session& session, Range range); static RangeResults<ClusterTypeId> find(Session& session, Range range);
static pointer find(Session& session, std::string_view name); static pointer find(Session& session, std::string_view name);
static pointer find(Session& session, ClusterTypeId id); static pointer find(Session& session, ClusterTypeId id);
static RangeResults<ClusterTypeId> findOrphans(Session& session, Range range); static RangeResults<ClusterTypeId> findOrphans(Session& session, Range range);
static RangeResults<ClusterTypeId> findUsed(Session& session, Range range); static RangeResults<ClusterTypeId> findUsed(Session& session, Range range);
static void remove(Session& session, const std::string& name); static void remove(Session& session, const std::string& name);
// Accessors // Accessors
const std::string& getName() const { return _name; } const std::string& getName() const { return _name; }
std::vector<Cluster::pointer> getClusters() const; std::vector<Cluster::pointer> getClusters() const;
Cluster::pointer getCluster(const std::string& name) const; Cluster::pointer getCluster(const std::string& name) const;
template<class Action> template<class Action>
void persist(Action& a) void persist(Action& a)
{ {
Wt::Dbo::field(a, _name, "name"); Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToOne, "cluster_type"); Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToOne, "cluster_type");
Wt::Dbo::belongsTo(a, _scanSettings, "scan_settings", Wt::Dbo::OnDeleteCascade); Wt::Dbo::belongsTo(a, _scanSettings, "scan_settings", Wt::Dbo::OnDeleteCascade);
} }
private: private:
friend class Session; friend class Session;
ClusterType(std::string_view name); ClusterType(std::string_view name);
static pointer create(Session& session, const std::string& name); static pointer create(Session& session, const std::string& name);
static const std::size_t _maxNameLength = 128; static const std::size_t _maxNameLength = 128;
std::string _name; std::string _name;
Wt::Dbo::collection< Wt::Dbo::ptr<Cluster> > _clusters; Wt::Dbo::collection< Wt::Dbo::ptr<Cluster> > _clusters;
Wt::Dbo::ptr<ScanSettings> _scanSettings; Wt::Dbo::ptr<ScanSettings> _scanSettings;
}; };
} // namespace Database } // namespace Database
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -90,7 +90,7 @@ namespace API::Subsonic
_childrenArrays[std::string{ key }].emplace_back(std::move(node)); _childrenArrays[std::string{ key }].emplace_back(std::move(node));
} }
void Response::Node::createEmptyArrayValue(const std::string& key) void Response::Node::createEmptyArrayValue(std::string_view key)
{ {
if (_value) if (_value)
throw LmsException{ "Node already has a value" }; throw LmsException{ "Node already has a value" };
@@ -98,12 +98,12 @@ namespace API::Subsonic
_childrenValues.emplace(key, std::vector<std::string>{}); _childrenValues.emplace(key, std::vector<std::string>{});
} }
void Response::Node::addArrayValue(const std::string& key, std::string_view value) void Response::Node::addArrayValue(std::string_view key, std::string_view value)
{ {
if (_value) if (_value)
throw LmsException{ "Node already has a value" }; throw LmsException{ "Node already has a value" };
_childrenValues[key].push_back(std::string{ value }); _childrenValues[std::string{ key }].push_back(std::string{ value });
} }
Response::Node& Response::Node::createChild(const std::string& key) Response::Node& Response::Node::createChild(const std::string& key)
+2 -2
View File
@@ -211,8 +211,8 @@ namespace API::Subsonic
void addChild(const std::string& key, Node node); void addChild(const std::string& key, Node node);
void createEmptyArrayChild(std::string_view key); void createEmptyArrayChild(std::string_view key);
void addArrayChild(std::string_view key, Node node); void addArrayChild(std::string_view key, Node node);
void createEmptyArrayValue(const std::string& key); void createEmptyArrayValue(std::string_view key);
void addArrayValue(const std::string& key, std::string_view value); void addArrayValue(std::string_view key, std::string_view value);
private: private:
void setVersionAttribute(ProtocolVersion version); void setVersionAttribute(ProtocolVersion version);
+28 -5
View File
@@ -147,17 +147,17 @@ namespace API::Subsonic
trackResponse.setAttribute("starred", StringUtils::toISO8601String(dateTime)); trackResponse.setAttribute("starred", StringUtils::toISO8601String(dateTime));
// Report the first GENRE for this track // Report the first GENRE for this track
ClusterType::pointer clusterType{ ClusterType::find(dbSession, "GENRE") }; ClusterType::pointer genreClusterType{ ClusterType::find(dbSession, "GENRE") };
if (clusterType) if (genreClusterType)
{ {
auto clusters{ track->getClusterGroups({clusterType}, 1) }; auto clusters{ track->getClusterGroups({genreClusterType}, 1) };
if (!clusters.empty() && !clusters.front().empty()) if (!clusters.empty() && !clusters.front().empty())
trackResponse.setAttribute("genre", clusters.front().front()->getName()); trackResponse.setAttribute("genre", clusters.front().front()->getName());
} }
// OpenSubsonic specific fields (must always be set) // OpenSubsonic specific fields (must always be set)
{ {
std::optional<UUID> mbid {track->getRecordingMBID()}; std::optional<UUID> mbid{ track->getRecordingMBID() };
trackResponse.setAttribute("musicBrainzId", mbid ? mbid->getAsString() : ""); trackResponse.setAttribute("musicBrainzId", mbid ? mbid->getAsString() : "");
} }
@@ -172,7 +172,7 @@ namespace API::Subsonic
// Don't report artists nor release artists as they are set in dedicated fields // Don't report artists nor release artists as they are set in dedicated fields
if (link && link->getType() != TrackArtistLinkType::Artist && link->getType() != TrackArtistLinkType::ReleaseArtist) if (link && link->getType() != TrackArtistLinkType::Artist && link->getType() != TrackArtistLinkType::ReleaseArtist)
trackResponse.addArrayChild("contributors", createContributorNode(link)); trackResponse.addArrayChild("contributors", createContributorNode(link));
} }
} }
auto addArtistLinks{ [&](std::string_view nodeName, TrackArtistLinkType type) auto addArtistLinks{ [&](std::string_view nodeName, TrackArtistLinkType type)
@@ -194,6 +194,29 @@ namespace API::Subsonic
addArtistLinks("artists", TrackArtistLinkType::Artist); addArtistLinks("artists", TrackArtistLinkType::Artist);
addArtistLinks("albumartists", TrackArtistLinkType::ReleaseArtist); addArtistLinks("albumartists", TrackArtistLinkType::ReleaseArtist);
auto addClusters{ [&](std::string_view field, std::string_view clusterTypeName)
{
trackResponse.createEmptyArrayValue(field);
ClusterType::pointer clusterType{ ClusterType::find(dbSession, clusterTypeName) };
if (clusterType)
{
Cluster::FindParameters params;
params.setTrack(track->getId());
params.setClusterType(clusterType->getId());
for (const ClusterId clusterId : Cluster::find(dbSession, params).results)
{
Cluster::pointer cluster {Cluster::find(dbSession, clusterId)};
if (cluster)
trackResponse.addArrayValue(field, cluster->getName());
}
}
} };
addClusters("genres", "GENRE");
addClusters("moods", "MOOD");
return trackResponse; return trackResponse;
} }
} }