Subsonic API: added genres/moods tags for songs
This commit is contained in:
@@ -28,166 +28,171 @@
|
|||||||
#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");
|
||||||
|
query.join("track t ON t.id = t_c.track_id");
|
||||||
|
query.where("t.id = ?").bind(params.track);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.clusterType.isValid())
|
||||||
|
query.where("c.cluster_type_id = ?").bind(params.clusterType);
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Cluster::Cluster(ObjectPtr<ClusterType> type, std::string_view name)
|
||||||
|
: _name{ std::string {name, 0, _maxNameLength} },
|
||||||
|
_clusterType{ getDboPtr(type) }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Cluster::pointer 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 Cluster::getCount(Session& session)
|
||||||
|
{
|
||||||
session.checkSharedLocked();
|
session.checkSharedLocked();
|
||||||
|
|
||||||
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster");
|
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster");
|
||||||
}
|
}
|
||||||
|
|
||||||
RangeResults<ClusterId>
|
RangeResults<ClusterId> Cluster::find(Session& session, const FindParameters& params)
|
||||||
Cluster::find(Session& session, Range range)
|
{
|
||||||
{
|
|
||||||
session.checkSharedLocked();
|
session.checkSharedLocked();
|
||||||
auto query {session.getDboSession().query<ClusterId>("SELECT id FROM cluster")};
|
auto query{ createQuery(session, params) };
|
||||||
|
|
||||||
|
return Utils::execQuery(query, params.range);
|
||||||
|
}
|
||||||
|
|
||||||
|
RangeResults<ClusterId> Cluster::findOrphans(Session& session, Range range)
|
||||||
|
{
|
||||||
|
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);
|
return Utils::execQuery(query, range);
|
||||||
}
|
}
|
||||||
|
|
||||||
RangeResults<ClusterId>
|
Cluster::pointer Cluster::find(Session& session, ClusterId id)
|
||||||
Cluster::findOrphans(Session& session, Range range)
|
{
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
Cluster::pointer
|
|
||||||
Cluster::find(Session& session, ClusterId id)
|
|
||||||
{
|
|
||||||
session.checkSharedLocked();
|
session.checkSharedLocked();
|
||||||
|
|
||||||
return session.getDboSession().find<Cluster>().where("id = ?").bind(id).resultValue();
|
return session.getDboSession().find<Cluster>().where("id = ?").bind(id).resultValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void Cluster::addTrack(ObjectPtr<Track> track)
|
||||||
Cluster::addTrack(ObjectPtr<Track> track)
|
{
|
||||||
{
|
|
||||||
_tracks.insert(getDboPtr(track));
|
_tracks.insert(getDboPtr(track));
|
||||||
}
|
}
|
||||||
|
|
||||||
RangeResults<TrackId>
|
RangeResults<TrackId> Cluster::getTracks(Range range) const
|
||||||
Cluster::getTracks(Range range) const
|
{
|
||||||
{
|
|
||||||
assert(session());
|
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")
|
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())};
|
.where("c.id = ?").bind(getId()) };
|
||||||
|
|
||||||
return Utils::execQuery(query, range);
|
return Utils::execQuery(query, range);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t
|
std::size_t Cluster::getReleasesCount() const
|
||||||
Cluster::getReleasesCount() const
|
{
|
||||||
{
|
|
||||||
assert(session());
|
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")
|
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());
|
.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());
|
||||||
|
|
||||||
@@ -197,8 +202,5 @@ ClusterType::getClusters() const
|
|||||||
.resultList();
|
.resultList();
|
||||||
|
|
||||||
return std::vector<Cluster::pointer>(res.begin(), res.end());
|
return std::vector<Cluster::pointer>(res.begin(), res.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
|
|
||||||
|
|||||||
@@ -33,19 +33,30 @@
|
|||||||
|
|
||||||
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:
|
||||||
|
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
|
||||||
|
|
||||||
|
FindParameters& setRange(Range _range) { range = _range; return *this; }
|
||||||
|
FindParameters& setClusterType(ClusterTypeId _clusterType) { clusterType = _clusterType; return *this; }
|
||||||
|
FindParameters& setTrack(TrackId _track) { track = _track; return *this; }
|
||||||
|
};
|
||||||
|
|
||||||
Cluster() = default;
|
Cluster() = default;
|
||||||
|
|
||||||
// Find utility
|
// Find utility
|
||||||
static std::size_t getCount(Session& session);
|
static std::size_t getCount(Session& session);
|
||||||
static RangeResults<ClusterId> find(Session& session, Range range);
|
static RangeResults<ClusterId> find(Session& session, const FindParameters& range);
|
||||||
static pointer find(Session& session, ClusterId id);
|
static pointer find(Session& session, ClusterId id);
|
||||||
static RangeResults<ClusterId> findOrphans(Session& session, Range range);
|
static RangeResults<ClusterId> findOrphans(Session& session, Range range);
|
||||||
|
|
||||||
@@ -78,11 +89,11 @@ class Cluster final : public Object<Cluster, ClusterId>
|
|||||||
|
|
||||||
Wt::Dbo::ptr<ClusterType> _clusterType;
|
Wt::Dbo::ptr<ClusterType> _clusterType;
|
||||||
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
|
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;
|
||||||
|
|
||||||
@@ -119,7 +130,7 @@ class ClusterType final : public Object<ClusterType, ClusterTypeId>
|
|||||||
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
@@ -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)
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user