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 "Utils.hpp"
namespace Database {
Cluster::Cluster(ObjectPtr<ClusterType> type, std::string_view name)
: _name {std::string {name, 0, _maxNameLength}},
_clusterType {getDboPtr(type)}
namespace Database
{
}
namespace
{
Wt::Dbo::Query<ClusterId> createQuery(Session& session, const Cluster::FindParameters& params)
{
session.checkSharedLocked();
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}});
}
auto query{ session.getDboSession().query<ClusterId>("SELECT DISTINCT c.id FROM cluster c") };
std::size_t
Cluster::getCount(Session& session)
{
session.checkSharedLocked();
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);
}
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster");
}
if (params.clusterType.isValid())
query.where("c.cluster_type_id = ?").bind(params.clusterType);
RangeResults<ClusterId>
Cluster::find(Session& session, Range range)
{
session.checkSharedLocked();
auto query {session.getDboSession().query<ClusterId>("SELECT id FROM cluster")};
return query;
}
}
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::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)")};
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 }});
}
return Utils::execQuery(query, range);
}
std::size_t Cluster::getCount(Session& session)
{
session.checkSharedLocked();
Cluster::pointer
Cluster::find(Session& session, ClusterId id)
{
session.checkSharedLocked();
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster");
}
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
Cluster::addTrack(ObjectPtr<Track> track)
{
_tracks.insert(getDboPtr(track));
}
return Utils::execQuery(query, params.range);
}
RangeResults<TrackId>
Cluster::getTracks(Range range) const
{
assert(session());
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)") };
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);
}
return Utils::execQuery(query, range);
}
Cluster::pointer Cluster::find(Session& session, ClusterId id)
{
session.checkSharedLocked();
std::size_t
Cluster::getReleasesCount() const
{
assert(session());
return session.getDboSession().find<Cluster>().where("id = ?").bind(id).resultValue();
}
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());
}
void Cluster::addTrack(ObjectPtr<Track> track)
{
_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)
: _name {name}
{
}
ClusterType::ClusterType(std::string_view name)
: _name{ name }
{
}
ClusterType::pointer
ClusterType::create(Session& session, const std::string& name)
{
return session.getDboSession().add(std::unique_ptr<ClusterType> {new ClusterType {name}});
}
ClusterType::pointer ClusterType::create(Session& session, const std::string& name)
{
return session.getDboSession().add(std::unique_ptr<ClusterType> {new ClusterType{ name }});
}
std::size_t
ClusterType::getCount(Session& session)
{
session.checkSharedLocked();
std::size_t ClusterType::getCount(Session& session)
{
session.checkSharedLocked();
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster_type");
}
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster_type");
}
RangeResults<ClusterTypeId>
ClusterType::findOrphans(Session& session, Range range)
{
session.checkSharedLocked();
RangeResults<ClusterTypeId> ClusterType::findOrphans(Session& session, Range range)
{
session.checkSharedLocked();
auto query {session.getDboSession().query<ClusterTypeId>(
"SELECT c_t.id from cluster_type c_t"
" LEFT OUTER JOIN cluster c ON c_t.id = c.cluster_type_id")
.where("c.id IS NULL")};
auto query{ session.getDboSession().query<ClusterTypeId>(
"SELECT c_t.id from cluster_type c_t"
" LEFT OUTER JOIN cluster c ON c_t.id = c.cluster_type_id")
.where("c.id IS NULL") };
return Utils::execQuery(query, range);
}
return Utils::execQuery(query, range);
}
RangeResults<ClusterTypeId>
ClusterType::findUsed(Session& session, Range range)
{
session.checkSharedLocked();
RangeResults<ClusterTypeId> ClusterType::findUsed(Session& session, Range range)
{
session.checkSharedLocked();
auto query {session.getDboSession().query<ClusterTypeId>(
"SELECT DISTINCT c_t.id from cluster_type c_t")
.join("cluster c ON c_t.id = c.cluster_type_id")};
auto query{ session.getDboSession().query<ClusterTypeId>(
"SELECT DISTINCT c_t.id from cluster_type c_t")
.join("cluster c ON c_t.id = c.cluster_type_id") };
return Utils::execQuery(query, range);
}
return Utils::execQuery(query, range);
}
ClusterType::pointer
ClusterType::find(Session& session, std::string_view name)
{
session.checkSharedLocked();
ClusterType::pointer ClusterType::find(Session& session, std::string_view name)
{
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::find(Session& session, ClusterTypeId id)
{
session.checkSharedLocked();
ClusterType::pointer ClusterType::find(Session& session, ClusterTypeId id)
{
session.checkSharedLocked();
return session.getDboSession().find<ClusterType>().where("id = ?").bind(id).resultValue();
}
return session.getDboSession().find<ClusterType>().where("id = ?").bind(id).resultValue();
}
RangeResults<ClusterTypeId>
ClusterType::find(Session& session, Range range)
{
session.checkSharedLocked();
RangeResults<ClusterTypeId> ClusterType::find(Session& session, Range range)
{
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
ClusterType::getCluster(const std::string& name) const
{
assert(self());
assert(session());
Cluster::pointer ClusterType::getCluster(const std::string& name) const
{
assert(self());
assert(session());
return session()->find<Cluster>()
.where("name = ?").bind(name)
.where("cluster_type_id = ?").bind(getId()).resultValue();
}
return session()->find<Cluster>()
.where("name = ?").bind(name)
.where("cluster_type_id = ?").bind(getId()).resultValue();
}
std::vector<Cluster::pointer>
ClusterType::getClusters() const
{
assert(self());
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());
}
std::vector<Cluster::pointer> ClusterType::getClusters() const
{
assert(self());
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());
}
} // namespace Database
@@ -33,93 +33,104 @@
namespace Database {
class Track;
class ClusterType;
class ScanSettings;
class Session;
class Track;
class ClusterType;
class ScanSettings;
class Session;
class Cluster final : public Object<Cluster, ClusterId>
{
public:
Cluster() = default;
class Cluster final : public Object<Cluster, ClusterId>
{
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
// Find utility
static std::size_t getCount(Session& session);
static RangeResults<ClusterId> find(Session& session, Range range);
static pointer find(Session& session, ClusterId id);
static RangeResults<ClusterId> findOrphans(Session& session, Range range);
FindParameters& setRange(Range _range) { range = _range; return *this; }
FindParameters& setClusterType(ClusterTypeId _clusterType) { clusterType = _clusterType; return *this; }
FindParameters& setTrack(TrackId _track) { track = _track; return *this; }
};
// Accessors
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;
Cluster() = default;
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>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
// Accessors
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;
Wt::Dbo::belongsTo(a, _clusterType, "cluster_type", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_cluster", "", Wt::Dbo::OnDeleteCascade);
}
void addTrack(ObjectPtr<Track> track);
private:
friend class Session;
Cluster(ObjectPtr<ClusterType> type, std::string_view name);
static pointer create(Session& session, ObjectPtr<ClusterType> type, std::string_view name);
template<class Action>
void persist(Action& a)
{
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;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
};
static const std::size_t _maxNameLength = 128;
std::string _name;
Wt::Dbo::ptr<ClusterType> _clusterType;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
};
class ClusterType final : public Object<ClusterType, ClusterTypeId>
{
public:
ClusterType() = default;
class ClusterType final : public Object<ClusterType, ClusterTypeId>
{
public:
ClusterType() = default;
// Getters
static std::size_t getCount(Session& session);
static RangeResults<ClusterTypeId> find(Session& session, Range range);
static pointer find(Session& session, std::string_view name);
static pointer find(Session& session, ClusterTypeId id);
static RangeResults<ClusterTypeId> findOrphans(Session& session, Range range);
static RangeResults<ClusterTypeId> findUsed(Session& session, Range range);
// Getters
static std::size_t getCount(Session& session);
static RangeResults<ClusterTypeId> find(Session& session, Range range);
static pointer find(Session& session, std::string_view name);
static pointer find(Session& session, ClusterTypeId id);
static RangeResults<ClusterTypeId> findOrphans(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
const std::string& getName() const { return _name; }
std::vector<Cluster::pointer> getClusters() const;
Cluster::pointer getCluster(const std::string& name) const;
// Accessors
const std::string& getName() const { return _name; }
std::vector<Cluster::pointer> getClusters() const;
Cluster::pointer getCluster(const std::string& name) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToOne, "cluster_type");
Wt::Dbo::belongsTo(a, _scanSettings, "scan_settings", Wt::Dbo::OnDeleteCascade);
}
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToOne, "cluster_type");
Wt::Dbo::belongsTo(a, _scanSettings, "scan_settings", Wt::Dbo::OnDeleteCascade);
}
private:
friend class Session;
ClusterType(std::string_view name);
static pointer create(Session& session, const std::string& name);
private:
friend class Session;
ClusterType(std::string_view 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;
Wt::Dbo::collection< Wt::Dbo::ptr<Cluster> > _clusters;
Wt::Dbo::ptr<ScanSettings> _scanSettings;
};
std::string _name;
Wt::Dbo::collection< Wt::Dbo::ptr<Cluster> > _clusters;
Wt::Dbo::ptr<ScanSettings> _scanSettings;
};
} // 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));
}
void Response::Node::createEmptyArrayValue(const std::string& key)
void Response::Node::createEmptyArrayValue(std::string_view key)
{
if (_value)
throw LmsException{ "Node already has a value" };
@@ -98,12 +98,12 @@ namespace API::Subsonic
_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)
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)
+2 -2
View File
@@ -211,8 +211,8 @@ namespace API::Subsonic
void addChild(const std::string& key, Node node);
void createEmptyArrayChild(std::string_view key);
void addArrayChild(std::string_view key, Node node);
void createEmptyArrayValue(const std::string& key);
void addArrayValue(const std::string& key, std::string_view value);
void createEmptyArrayValue(std::string_view key);
void addArrayValue(std::string_view key, std::string_view value);
private:
void setVersionAttribute(ProtocolVersion version);
+28 -5
View File
@@ -147,17 +147,17 @@ namespace API::Subsonic
trackResponse.setAttribute("starred", StringUtils::toISO8601String(dateTime));
// Report the first GENRE for this track
ClusterType::pointer clusterType{ ClusterType::find(dbSession, "GENRE") };
if (clusterType)
ClusterType::pointer genreClusterType{ ClusterType::find(dbSession, "GENRE") };
if (genreClusterType)
{
auto clusters{ track->getClusterGroups({clusterType}, 1) };
auto clusters{ track->getClusterGroups({genreClusterType}, 1) };
if (!clusters.empty() && !clusters.front().empty())
trackResponse.setAttribute("genre", clusters.front().front()->getName());
}
// 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() : "");
}
@@ -172,7 +172,7 @@ namespace API::Subsonic
// Don't report artists nor release artists as they are set in dedicated fields
if (link && link->getType() != TrackArtistLinkType::Artist && link->getType() != TrackArtistLinkType::ReleaseArtist)
trackResponse.addArrayChild("contributors", createContributorNode(link));
}
}
}
auto addArtistLinks{ [&](std::string_view nodeName, TrackArtistLinkType type)
@@ -194,6 +194,29 @@ namespace API::Subsonic
addArtistLinks("artists", TrackArtistLinkType::Artist);
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;
}
}