Subsonic API: added genres/moods tags for songs
This commit is contained in:
@@ -28,7 +28,29 @@
|
|||||||
#include "SqlQuery.hpp"
|
#include "SqlQuery.hpp"
|
||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
|
|
||||||
namespace Database {
|
namespace Database
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
Wt::Dbo::Query<ClusterId> createQuery(Session& session, const Cluster::FindParameters& params)
|
||||||
|
{
|
||||||
|
session.checkSharedLocked();
|
||||||
|
|
||||||
|
auto query{ session.getDboSession().query<ClusterId>("SELECT DISTINCT c.id FROM cluster c") };
|
||||||
|
|
||||||
|
if (params.track.isValid())
|
||||||
|
{
|
||||||
|
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)
|
Cluster::Cluster(ObjectPtr<ClusterType> type, std::string_view name)
|
||||||
: _name{ std::string {name, 0, _maxNameLength} },
|
: _name{ std::string {name, 0, _maxNameLength} },
|
||||||
@@ -36,31 +58,27 @@ Cluster::Cluster(ObjectPtr<ClusterType> type, std::string_view name)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Cluster::pointer
|
Cluster::pointer Cluster::create(Session& session, ObjectPtr<ClusterType> type, std::string_view name)
|
||||||
Cluster::create(Session& session, ObjectPtr<ClusterType> type, std::string_view name)
|
|
||||||
{
|
{
|
||||||
return session.getDboSession().add(std::unique_ptr<Cluster> {new Cluster{ type, name }});
|
return session.getDboSession().add(std::unique_ptr<Cluster> {new Cluster{ type, name }});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t
|
std::size_t Cluster::getCount(Session& session)
|
||||||
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, range);
|
return Utils::execQuery(query, params.range);
|
||||||
}
|
}
|
||||||
|
|
||||||
RangeResults<ClusterId>
|
RangeResults<ClusterId> Cluster::findOrphans(Session& session, Range range)
|
||||||
Cluster::findOrphans(Session& session, Range range)
|
|
||||||
{
|
{
|
||||||
session.checkSharedLocked();
|
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)") };
|
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)") };
|
||||||
@@ -68,22 +86,19 @@ Cluster::findOrphans(Session& session, Range range)
|
|||||||
return Utils::execQuery(query, range);
|
return Utils::execQuery(query, range);
|
||||||
}
|
}
|
||||||
|
|
||||||
Cluster::pointer
|
Cluster::pointer Cluster::find(Session& session, ClusterId id)
|
||||||
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());
|
||||||
|
|
||||||
@@ -93,8 +108,7 @@ Cluster::getTracks(Range range) const
|
|||||||
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());
|
||||||
|
|
||||||
@@ -108,14 +122,12 @@ ClusterType::ClusterType(std::string_view 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();
|
||||||
|
|
||||||
@@ -123,8 +135,7 @@ ClusterType::getCount(Session& session)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
RangeResults<ClusterTypeId>
|
RangeResults<ClusterTypeId> ClusterType::findOrphans(Session& session, Range range)
|
||||||
ClusterType::findOrphans(Session& session, Range range)
|
|
||||||
{
|
{
|
||||||
session.checkSharedLocked();
|
session.checkSharedLocked();
|
||||||
|
|
||||||
@@ -136,8 +147,7 @@ ClusterType::findOrphans(Session& session, Range range)
|
|||||||
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();
|
||||||
|
|
||||||
@@ -148,24 +158,21 @@ ClusterType::findUsed(Session& session, Range range)
|
|||||||
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();
|
||||||
|
|
||||||
@@ -174,8 +181,7 @@ ClusterType::find(Session& session, Range range)
|
|||||||
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());
|
||||||
@@ -185,8 +191,7 @@ ClusterType::getCluster(const std::string& name) const
|
|||||||
.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());
|
||||||
@@ -198,7 +203,4 @@ ClusterType::getClusters() const
|
|||||||
|
|
||||||
return std::vector<Cluster::pointer>(res.begin(), res.end());
|
return std::vector<Cluster::pointer>(res.begin(), res.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
|
|
||||||
|
|||||||
@@ -41,11 +41,22 @@ 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);
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ TEST_F(DatabaseFixture, Cluster)
|
|||||||
EXPECT_EQ(Cluster::getCount(session), 1);
|
EXPECT_EQ(Cluster::getCount(session), 1);
|
||||||
EXPECT_EQ(cluster->getType()->getId(), clusterType.getId());
|
EXPECT_EQ(cluster->getType()->getId(), clusterType.getId());
|
||||||
|
|
||||||
auto clusters {Cluster::find(session, Range {})};
|
auto clusters{ Cluster::find(session, Cluster::FindParameters {}) };
|
||||||
ASSERT_EQ(clusters.results.size(), 1);
|
ASSERT_EQ(clusters.results.size(), 1);
|
||||||
EXPECT_EQ(clusters.results.front(), cluster.getId());
|
EXPECT_EQ(clusters.results.front(), cluster.getId());
|
||||||
|
|
||||||
@@ -110,6 +110,13 @@ TEST_F(DatabaseFixture, Cluster_singleTrack)
|
|||||||
cluster1.get().modify()->addTrack(track.get());
|
cluster1.get().modify()->addTrack(track.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto transaction{ session.createSharedTransaction() };
|
||||||
|
auto clusters{ Cluster::find(session, Cluster::FindParameters {}.setTrack(track.getId())) };
|
||||||
|
ASSERT_EQ(clusters.results.size(), 1);
|
||||||
|
EXPECT_EQ(clusters.results.front(), cluster1.getId());
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto transaction{ session.createSharedTransaction() };
|
auto transaction{ session.createSharedTransaction() };
|
||||||
auto clusters{ Cluster::findOrphans(session, Range {}) };
|
auto clusters{ Cluster::findOrphans(session, Range {}) };
|
||||||
|
|||||||
@@ -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,10 +147,10 @@ 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());
|
||||||
}
|
}
|
||||||
@@ -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