OpenSubsonic API: added support for 'played' fields
This commit is contained in:
@@ -11,6 +11,7 @@ OpenSubsonic is an initiative to patch and extend the legacy Subsonic API. You'l
|
||||
## Extra fields
|
||||
The following extra fields are implemented:
|
||||
* `Album` response:
|
||||
* `played`
|
||||
* `musicBrainzId`
|
||||
* `genres`
|
||||
* `artists`
|
||||
@@ -21,6 +22,7 @@ The following extra fields are implemented:
|
||||
* `isCompilation`
|
||||
* `discTitles`: discs with no subtitle are omitted
|
||||
* `Child` response:
|
||||
* `played`
|
||||
* `musicBrainzId`: note this is actually the recording MBID when this response refers to a song
|
||||
* `genres`
|
||||
* `artists`
|
||||
|
||||
@@ -29,15 +29,14 @@ namespace
|
||||
{
|
||||
using namespace Database;
|
||||
|
||||
Wt::Dbo::Query<ArtistId>
|
||||
createArtistsQuery(Wt::Dbo::Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, std::optional<TrackArtistLinkType> linkType)
|
||||
Wt::Dbo::Query<ArtistId> createArtistsQuery(Wt::Dbo::Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, std::optional<TrackArtistLinkType> linkType)
|
||||
{
|
||||
auto query {session.query<ArtistId>("SELECT a.id from artist a")
|
||||
auto query{ session.query<ArtistId>("SELECT a.id from artist a")
|
||||
.join("track t ON t.id = t_a_l.track_id")
|
||||
.join("track_artist_link t_a_l ON t_a_l.artist_id = a.id")
|
||||
.join("listen l ON l.track_id = t.id")
|
||||
.where("l.user_id = ?").bind(userId)
|
||||
.where("l.scrobbler = ?").bind(scrobbler)};
|
||||
.where("l.scrobbler = ?").bind(scrobbler) };
|
||||
|
||||
if (linkType)
|
||||
query.where("t_a_l.type = ?").bind(*linkType);
|
||||
@@ -67,14 +66,13 @@ namespace
|
||||
return query;
|
||||
}
|
||||
|
||||
Wt::Dbo::Query<ReleaseId>
|
||||
createReleasesQuery(Wt::Dbo::Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds)
|
||||
Wt::Dbo::Query<ReleaseId> createReleasesQuery(Wt::Dbo::Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds)
|
||||
{
|
||||
auto query {session.query<ReleaseId>("SELECT r.id from release r")
|
||||
auto query{ session.query<ReleaseId>("SELECT r.id from release r")
|
||||
.join("track t ON t.release_id = r.id")
|
||||
.join("listen l ON l.track_id = t.id")
|
||||
.where("l.user_id = ?").bind(userId)
|
||||
.where("l.scrobbler = ?").bind(scrobbler)};
|
||||
.where("l.scrobbler = ?").bind(scrobbler) };
|
||||
|
||||
if (!clusterIds.empty())
|
||||
{
|
||||
@@ -100,13 +98,12 @@ namespace
|
||||
return query;
|
||||
}
|
||||
|
||||
Wt::Dbo::Query<TrackId>
|
||||
createTracksQuery(Wt::Dbo::Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds)
|
||||
Wt::Dbo::Query<TrackId> createTracksQuery(Wt::Dbo::Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds)
|
||||
{
|
||||
auto query {session.query<TrackId>("SELECT t.id from track t")
|
||||
auto query{ session.query<TrackId>("SELECT t.id from track t")
|
||||
.join("listen l ON l.track_id = t.id")
|
||||
.where("l.user_id = ?").bind(userId)
|
||||
.where("l.scrobbler = ?").bind(scrobbler)};
|
||||
.where("l.scrobbler = ?").bind(scrobbler) };
|
||||
|
||||
if (!clusterIds.empty())
|
||||
{
|
||||
@@ -135,39 +132,35 @@ namespace
|
||||
namespace Database
|
||||
{
|
||||
Listen::Listen(ObjectPtr<User> user, ObjectPtr<Track> track, Scrobbler scrobbler, const Wt::WDateTime& dateTime)
|
||||
: _dateTime {Wt::WDateTime::fromTime_t(dateTime.toTime_t())}
|
||||
, _scrobbler {scrobbler}
|
||||
, _user {getDboPtr(user)}
|
||||
, _track {getDboPtr(track)}
|
||||
: _dateTime{ Wt::WDateTime::fromTime_t(dateTime.toTime_t()) }
|
||||
, _scrobbler{ scrobbler }
|
||||
, _user{ getDboPtr(user) }
|
||||
, _track{ getDboPtr(track) }
|
||||
{}
|
||||
|
||||
Listen::pointer
|
||||
Listen::create(Session& session, ObjectPtr<User> user, ObjectPtr<Track> track, Scrobbler scrobbler, const Wt::WDateTime& dateTime)
|
||||
Listen::pointer Listen::create(Session& session, ObjectPtr<User> user, ObjectPtr<Track> track, Scrobbler scrobbler, const Wt::WDateTime& dateTime)
|
||||
{
|
||||
return session.getDboSession().add(std::unique_ptr<Listen> {new Listen {user, track, scrobbler, dateTime}});
|
||||
return session.getDboSession().add(std::unique_ptr<Listen> {new Listen{ user, track, scrobbler, dateTime }});
|
||||
}
|
||||
|
||||
std::size_t
|
||||
Listen::getCount(Session& session)
|
||||
std::size_t Listen::getCount(Session& session)
|
||||
{
|
||||
session.checkSharedLocked();
|
||||
return session.getDboSession().query<int>("SELECT COUNT(*) FROM listen");
|
||||
}
|
||||
|
||||
Listen::pointer
|
||||
Listen::find(Session& session, ListenId id)
|
||||
Listen::pointer Listen::find(Session& session, ListenId id)
|
||||
{
|
||||
session.checkSharedLocked();
|
||||
return session.getDboSession().find<Listen>().where("id = ?").bind(id).resultValue();
|
||||
}
|
||||
|
||||
RangeResults<ListenId>
|
||||
Listen::find(Session& session, const FindParameters& parameters)
|
||||
RangeResults<ListenId> Listen::find(Session& session, const FindParameters& parameters)
|
||||
{
|
||||
session.checkSharedLocked();
|
||||
|
||||
auto query {session.getDboSession().query<ListenId>("SELECT id FROM listen")
|
||||
.orderBy("date_time")};
|
||||
auto query{ session.getDboSession().query<ListenId>("SELECT id FROM listen")
|
||||
.orderBy("date_time") };
|
||||
|
||||
if (parameters.user.isValid())
|
||||
query.where("user_id = ?").bind(parameters.user);
|
||||
@@ -181,8 +174,7 @@ namespace Database
|
||||
return Utils::execQuery(query, parameters.range);
|
||||
}
|
||||
|
||||
Listen::pointer
|
||||
Listen::find(Session& session, UserId userId, TrackId trackId, Scrobbler scrobbler, const Wt::WDateTime& dateTime)
|
||||
Listen::pointer Listen::find(Session& session, UserId userId, TrackId trackId, Scrobbler scrobbler, const Wt::WDateTime& dateTime)
|
||||
{
|
||||
session.checkSharedLocked();
|
||||
|
||||
@@ -194,94 +186,86 @@ namespace Database
|
||||
.resultValue();
|
||||
}
|
||||
|
||||
RangeResults<ArtistId>
|
||||
Listen::getTopArtists(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
std::optional<TrackArtistLinkType> linkType,
|
||||
Range range)
|
||||
RangeResults<ArtistId> Listen::getTopArtists(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, std::optional<TrackArtistLinkType> linkType, Range range)
|
||||
{
|
||||
auto query {createArtistsQuery(session.getDboSession(), userId, scrobbler, clusterIds, linkType)};
|
||||
auto query{ createArtistsQuery(session.getDboSession(), userId, scrobbler, clusterIds, linkType) };
|
||||
|
||||
auto collection {query
|
||||
auto collection{ query
|
||||
.orderBy("COUNT(a.id) DESC")
|
||||
.groupBy("a.id")};
|
||||
.groupBy("a.id") };
|
||||
|
||||
return Utils::execQuery(query, range);
|
||||
}
|
||||
|
||||
RangeResults<ReleaseId>
|
||||
Listen::getTopReleases(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
Range range)
|
||||
RangeResults<ReleaseId> Listen::getTopReleases(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, Range range)
|
||||
{
|
||||
auto query {createReleasesQuery(session.getDboSession(), userId, scrobbler, clusterIds)
|
||||
auto query{ createReleasesQuery(session.getDboSession(), userId, scrobbler, clusterIds)
|
||||
.orderBy("COUNT(r.id) DESC")
|
||||
.groupBy("r.id")};
|
||||
.groupBy("r.id") };
|
||||
|
||||
return Utils::execQuery(query, range);
|
||||
}
|
||||
|
||||
RangeResults<TrackId>
|
||||
Listen::getTopTracks(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
Range range)
|
||||
RangeResults<TrackId> Listen::getTopTracks(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, Range range)
|
||||
{
|
||||
auto query {createTracksQuery(session.getDboSession(), userId, scrobbler, clusterIds)
|
||||
auto query{ createTracksQuery(session.getDboSession(), userId, scrobbler, clusterIds)
|
||||
.orderBy("COUNT(t.id) DESC")
|
||||
.groupBy("t.id")};
|
||||
.groupBy("t.id") };
|
||||
|
||||
return Utils::execQuery(query, range);
|
||||
}
|
||||
|
||||
RangeResults<ArtistId>
|
||||
Listen::getRecentArtists(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
std::optional<TrackArtistLinkType> linkType,
|
||||
Range range)
|
||||
RangeResults<ArtistId> Listen::getRecentArtists(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, std::optional<TrackArtistLinkType> linkType, Range range)
|
||||
{
|
||||
auto query {createArtistsQuery(session.getDboSession(), userId, scrobbler, clusterIds, linkType)
|
||||
auto query{ createArtistsQuery(session.getDboSession(), userId, scrobbler, clusterIds, linkType)
|
||||
.groupBy("a.id").having("l.date_time = MAX(l.date_time)")
|
||||
.orderBy("l.date_time DESC")};
|
||||
.orderBy("l.date_time DESC") };
|
||||
|
||||
return Utils::execQuery(query, range);
|
||||
}
|
||||
|
||||
RangeResults<ReleaseId>
|
||||
Listen::getRecentReleases(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
Range range)
|
||||
RangeResults<ReleaseId> Listen::getRecentReleases(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, Range range)
|
||||
{
|
||||
auto query {createReleasesQuery(session.getDboSession(), userId, scrobbler, clusterIds)
|
||||
auto query{ createReleasesQuery(session.getDboSession(), userId, scrobbler, clusterIds)
|
||||
.groupBy("r.id").having("l.date_time = MAX(l.date_time)")
|
||||
.orderBy("l.date_time DESC")};
|
||||
.orderBy("l.date_time DESC") };
|
||||
|
||||
return Utils::execQuery(query, range);
|
||||
}
|
||||
|
||||
RangeResults<TrackId>
|
||||
Listen::getRecentTracks(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
Range range)
|
||||
RangeResults<TrackId> Listen::getRecentTracks(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, Range range)
|
||||
{
|
||||
auto query {createTracksQuery(session.getDboSession(), userId, scrobbler, clusterIds)
|
||||
auto query{ createTracksQuery(session.getDboSession(), userId, scrobbler, clusterIds)
|
||||
.groupBy("t.id").having("l.date_time = MAX(l.date_time)")
|
||||
.orderBy("l.date_time DESC")};
|
||||
.orderBy("l.date_time DESC") };
|
||||
|
||||
return Utils::execQuery(query, range);
|
||||
}
|
||||
|
||||
Listen::pointer Listen::getMostRecentListen(Session& session, UserId userId, Scrobbler scrobbler, ReleaseId releaseId)
|
||||
{
|
||||
// TODO not pending remove?
|
||||
return session.getDboSession().query<Wt::Dbo::ptr<Listen>>("SELECT l from listen l")
|
||||
.join("track t ON l.track_id = t.id")
|
||||
.where("t.release_id = ?").bind(releaseId)
|
||||
.where("l.user_id = ?").bind(userId)
|
||||
.where("l.scrobbler = ?").bind(scrobbler)
|
||||
.orderBy("l.date_time DESC")
|
||||
.limit(1)
|
||||
.resultValue();
|
||||
}
|
||||
|
||||
Listen::pointer Listen::getMostRecentListen(Session& session, UserId userId, Scrobbler scrobbler, TrackId trackId)
|
||||
{
|
||||
// TODO not pending remove?
|
||||
return session.getDboSession().query<Wt::Dbo::ptr<Listen>>("SELECT l from listen l")
|
||||
.join("track t ON track_id = t.id")
|
||||
.where("t.id = ?").bind(trackId)
|
||||
.where("l.user_id = ?").bind(userId)
|
||||
.where("l.scrobbler = ?").bind(scrobbler)
|
||||
.orderBy("l.date_time DESC")
|
||||
.limit(1)
|
||||
.resultValue();
|
||||
}
|
||||
} // namespace Database
|
||||
|
||||
|
||||
@@ -34,12 +34,12 @@
|
||||
namespace Database
|
||||
{
|
||||
|
||||
class Session;
|
||||
class Track;
|
||||
class User;
|
||||
class Session;
|
||||
class Track;
|
||||
class User;
|
||||
|
||||
class Listen final : public Object<Listen, ListenId>
|
||||
{
|
||||
class Listen final : public Object<Listen, ListenId>
|
||||
{
|
||||
public:
|
||||
Listen() = default;
|
||||
|
||||
@@ -53,7 +53,7 @@ class Listen final : public Object<Listen, ListenId>
|
||||
FindParameters& setUser(UserId _user) { user = _user; return *this; }
|
||||
FindParameters& setScrobbler(Scrobbler _scrobbler) { scrobbler = _scrobbler; return *this; }
|
||||
FindParameters& setScrobblingState(ScrobblingState _scrobblingState) { scrobblingState = _scrobblingState; return *this; }
|
||||
FindParameters& setRange(Range _range) {range = _range; return *this; }
|
||||
FindParameters& setRange(Range _range) { range = _range; return *this; }
|
||||
};
|
||||
|
||||
// Accessors
|
||||
@@ -63,39 +63,16 @@ class Listen final : public Object<Listen, ListenId>
|
||||
static RangeResults<ListenId> find(Session& session, const FindParameters& parameters);
|
||||
|
||||
// Stats
|
||||
static RangeResults<ArtistId> getTopArtists(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
std::optional<TrackArtistLinkType> linkType,
|
||||
Range range = {});
|
||||
static RangeResults<ReleaseId> getTopReleases(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
Range range = {});
|
||||
static RangeResults<TrackId> getTopTracks(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
Range range = {});
|
||||
static RangeResults<ArtistId> getTopArtists(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, std::optional<TrackArtistLinkType> linkType, Range range = {});
|
||||
static RangeResults<ReleaseId> getTopReleases(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, Range range = {});
|
||||
static RangeResults<TrackId> getTopTracks(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, Range range = {});
|
||||
|
||||
static RangeResults<ArtistId> getRecentArtists(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
std::optional<TrackArtistLinkType> linkType,
|
||||
Range range = {});
|
||||
static RangeResults<ReleaseId> getRecentReleases(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
Range range = {});
|
||||
static RangeResults<TrackId> getRecentTracks(Session& session,
|
||||
UserId userId,
|
||||
Scrobbler scrobbler,
|
||||
const std::vector<ClusterId>& clusterIds,
|
||||
Range range = {});
|
||||
static RangeResults<ArtistId> getRecentArtists(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, std::optional<TrackArtistLinkType> linkType, Range range = {});
|
||||
static RangeResults<ReleaseId> getRecentReleases(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, Range range = {});
|
||||
static RangeResults<TrackId> getRecentTracks(Session& session, UserId userId, Scrobbler scrobbler, const std::vector<ClusterId>& clusterIds, Range range = {});
|
||||
|
||||
static pointer getMostRecentListen(Session& session, UserId userId, Scrobbler scrobbler, ReleaseId releaseId);
|
||||
static pointer getMostRecentListen(Session& session, UserId userId, Scrobbler scrobbler, TrackId releaseId);
|
||||
|
||||
ScrobblingState getScrobblingState() const { return _scrobblingState; }
|
||||
ObjectPtr<User> getUser() const { return _user; }
|
||||
@@ -122,11 +99,11 @@ class Listen final : public Object<Listen, ListenId>
|
||||
|
||||
Wt::WDateTime _dateTime;
|
||||
Scrobbler _scrobbler;
|
||||
ScrobblingState _scrobblingState {ScrobblingState::PendingAdd};
|
||||
ScrobblingState _scrobblingState{ ScrobblingState::PendingAdd };
|
||||
|
||||
Wt::Dbo::ptr<User> _user;
|
||||
Wt::Dbo::ptr<Track> _track;
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace Database
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -133,6 +133,32 @@ namespace Scrobbling
|
||||
return res;
|
||||
}
|
||||
|
||||
Wt::WDateTime ScrobblingService::getLastListenDateTime(Database::UserId userId, Database::ReleaseId releaseId)
|
||||
{
|
||||
auto scrobbler{ getUserScrobbler(userId) };
|
||||
if (!scrobbler)
|
||||
return {};
|
||||
|
||||
Session& session{ _db.getTLSSession() };
|
||||
auto transaction{ session.createSharedTransaction() };
|
||||
|
||||
const Database::Listen::pointer listen{ Database::Listen::getMostRecentListen(session, userId, *scrobbler, releaseId) };
|
||||
return listen ? listen->getDateTime() : Wt::WDateTime{};
|
||||
}
|
||||
|
||||
Wt::WDateTime ScrobblingService::getLastListenDateTime(Database::UserId userId, Database::TrackId trackId)
|
||||
{
|
||||
auto scrobbler{ getUserScrobbler(userId) };
|
||||
if (!scrobbler)
|
||||
return {};
|
||||
|
||||
Session& session{ _db.getTLSSession() };
|
||||
auto transaction{ session.createSharedTransaction() };
|
||||
|
||||
const Database::Listen::pointer listen{ Database::Listen::getMostRecentListen(session, userId, *scrobbler, trackId) };
|
||||
return listen ? listen->getDateTime() : Wt::WDateTime{};
|
||||
}
|
||||
|
||||
// Top
|
||||
ScrobblingService::ArtistContainer ScrobblingService::getTopArtists(UserId userId, const std::vector<ClusterId>& clusterIds, std::optional<TrackArtistLinkType> linkType, Range range)
|
||||
{
|
||||
|
||||
@@ -52,6 +52,9 @@ namespace Scrobbling
|
||||
const std::vector<Database::ClusterId>& clusterIds,
|
||||
Database::Range range) override;
|
||||
|
||||
Wt::WDateTime getLastListenDateTime(Database::UserId userId, Database::ReleaseId releaseId) override;
|
||||
Wt::WDateTime getLastListenDateTime(Database::UserId userId, Database::TrackId trackId) override;
|
||||
|
||||
ArtistContainer getTopArtists(Database::UserId userId,
|
||||
const std::vector<Database::ClusterId>& clusterIds,
|
||||
std::optional<Database::TrackArtistLinkType> linkType,
|
||||
|
||||
@@ -69,6 +69,9 @@ namespace Scrobbling
|
||||
const std::vector<Database::ClusterId>& clusterIds,
|
||||
Database::Range range) = 0;
|
||||
|
||||
virtual Wt::WDateTime getLastListenDateTime(Database::UserId userId, Database::ReleaseId releaseId) = 0;
|
||||
virtual Wt::WDateTime getLastListenDateTime(Database::UserId userId, Database::TrackId trackId) = 0;
|
||||
|
||||
// Top
|
||||
virtual ArtistContainer getTopArtists(Database::UserId userId,
|
||||
const std::vector<Database::ClusterId>& clusterIds,
|
||||
|
||||
@@ -138,6 +138,11 @@ namespace API::Subsonic
|
||||
if (!id3)
|
||||
albumNode.setAttribute("mediaType", "album");
|
||||
|
||||
{
|
||||
const Wt::WDateTime dateTime{ Service<Scrobbling::IScrobblingService>::get()->getLastListenDateTime(user->getId(), release->getId()) };
|
||||
albumNode.setAttribute("played", dateTime.isValid() ? StringUtils::toISO8601String(dateTime) : "");
|
||||
}
|
||||
|
||||
{
|
||||
std::optional<UUID> mbid{ release->getMBID() };
|
||||
albumNode.setAttribute("musicBrainzId", mbid ? mbid->getAsString() : "");
|
||||
|
||||
@@ -162,6 +162,11 @@ namespace API::Subsonic
|
||||
// OpenSubsonic specific fields (must always be set)
|
||||
trackResponse.setAttribute("mediaType", "song");
|
||||
|
||||
{
|
||||
const Wt::WDateTime dateTime{ Service<Scrobbling::IScrobblingService>::get()->getLastListenDateTime(user->getId(), track->getId()) };
|
||||
trackResponse.setAttribute("played", dateTime.isValid() ? StringUtils::toISO8601String(dateTime) : "");
|
||||
}
|
||||
|
||||
{
|
||||
std::optional<UUID> mbid{ track->getRecordingMBID() };
|
||||
trackResponse.setAttribute("musicBrainzId", mbid ? mbid->getAsString() : "");
|
||||
|
||||
Reference in New Issue
Block a user