Subsonci API: added savePlayQueue / getPlayQueue support

This commit is contained in:
emeric
2024-10-31 18:49:57 +01:00
parent 0ca2d8b34c
commit 472a5907ad
10 changed files with 315 additions and 9 deletions
+1
View File
@@ -8,6 +8,7 @@ add_library(lmsdatabase SHARED
impl/Listen.cpp
impl/MediaLibrary.cpp
impl/Migration.cpp
impl/PlayQueue.cpp
impl/TrackArtistLink.cpp
impl/TrackFeatures.cpp
impl/TrackList.cpp
+27 -1
View File
@@ -35,7 +35,7 @@ namespace lms::db
{
namespace
{
static constexpr Version LMS_DATABASE_VERSION{ 71 };
static constexpr Version LMS_DATABASE_VERSION{ 72 };
}
VersionInfo::VersionInfo()
@@ -907,6 +907,31 @@ SELECT
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET scan_version = scan_version + 1");
}
void migrateFromV71(Session& session)
{
// Add a file name/stem in tracks
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "playqueue" (
"id" integer primary key autoincrement,
"version" integer not null,
"name" text not null,
"current_index" integer not null,
"current_position_in_track" integer,
"last_modified_date_time" text,
"user_id" bigint,
constraint "fk_playqueue_user" foreign key ("user_id") references "user" ("id") on delete cascade deferrable initially deferred
))");
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "playqueue_track" (
"playqueue_id" bigint,
"track_id" bigint not null,
primary key ("playqueue_id", "track_id"),
constraint "fk_playqueue_track_key1" foreign key ("playqueue_id") references "playqueue" ("id") on delete cascade deferrable initially deferred,
constraint "fk_playqueue_track_key2" foreign key ("track_id") references "track" ("id") on delete cascade deferrable initially deferred
))");
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "playqueue_track_playqueue" on "playqueue_track" ("playqueue_id"))");
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "playqueue_track_track" on "playqueue_track" ("track_id"))");
}
bool doDbMigration(Session& session)
{
constexpr std::string_view outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" };
@@ -954,6 +979,7 @@ SELECT
{ 68, migrateFromV68 },
{ 69, migrateFromV69 },
{ 70, migrateFromV70 },
{ 71, migrateFromV71 },
};
bool migrationPerformed{};
+97
View File
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "database/PlayQueue.hpp"
#include <Wt/Dbo/SqlTraits.h>
#include "database/Directory.hpp"
#include "database/MediaLibrary.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/User.hpp"
#include "IdTypeTraits.hpp"
#include "StringViewTraits.hpp"
#include "Utils.hpp"
namespace lms::db
{
PlayQueue::PlayQueue(const ObjectPtr<User>& user, std::string_view name)
: _name{ name }
, _user{ getDboPtr(user) }
{
}
PlayQueue::pointer PlayQueue::create(Session& session, const ObjectPtr<User>& user, std::string_view name)
{
return session.getDboSession()->add(std::unique_ptr<PlayQueue>{ new PlayQueue{ user, name } });
}
std::size_t PlayQueue::getCount(Session& session)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM playqueue"));
}
PlayQueue::pointer PlayQueue::find(Session& session, PlayQueueId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<PlayQueue>>("SELECT p from playqueue p").where("p.id = ?").bind(id));
}
PlayQueue::pointer PlayQueue::find(Session& session, UserId userId, std::string_view name)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<PlayQueue>>("SELECT p from playqueue p") };
query.where("p.user_id = ?").bind(userId);
query.where("p.name = ?").bind(name);
return utils::fetchQuerySingleResult(query);
}
void PlayQueue::clear()
{
_tracks.clear();
}
void PlayQueue::addTrack(const ObjectPtr<Track>& track)
{
_tracks.insert(getDboPtr(track));
}
Track::pointer PlayQueue::getTrackAtCurrentIndex() const
{
auto query{ _tracks.find() };
query.offset(_currentIndex);
query.limit(1);
return utils::fetchQuerySingleResult(query);
}
void PlayQueue::visitTracks(const std::function<void(const ObjectPtr<Track>& track)>& visitor) const
{
utils::forEachQueryResult(_tracks.find(), visitor);
}
} // namespace lms::db
+2
View File
@@ -30,6 +30,7 @@
#include "database/Image.hpp"
#include "database/Listen.hpp"
#include "database/MediaLibrary.hpp"
#include "database/PlayQueue.hpp"
#include "database/RatedArtist.hpp"
#include "database/RatedRelease.hpp"
#include "database/RatedTrack.hpp"
@@ -104,6 +105,7 @@ namespace lms::db
_session.mapClass<Label>("label");
_session.mapClass<Listen>("listen");
_session.mapClass<MediaLibrary>("media_library");
_session.mapClass<PlayQueue>("playqueue");
_session.mapClass<RatedArtist>("rated_artist");
_session.mapClass<RatedRelease>("rated_release");
_session.mapClass<RatedTrack>("rated_track");
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
#include <span>
#include <string>
#include <string_view>
#include <Wt/Dbo/Dbo.h>
#include <Wt/WDateTime.h>
#include "database/Object.hpp"
#include "database/TrackId.hpp"
#include "database/Types.hpp"
#include "database/UserId.hpp"
LMS_DECLARE_IDTYPE(PlayQueueId)
namespace lms::db
{
class Session;
class Track;
class User;
// Usage only for subsonic API
class PlayQueue final : public Object<PlayQueue, PlayQueueId>
{
public:
PlayQueue() = default;
static std::size_t getCount(Session& session);
static pointer find(Session& session, PlayQueueId playQueueId);
static pointer find(Session& session, UserId userId, std::string_view name);
// Accessors
std::string_view getName() const { return _name; }
std::size_t getCurrentIndex() const { return _currentIndex; }
std::chrono::milliseconds getCurrentPositionInTrack() const { return _currentPositionInTrack; }
ObjectPtr<User> getUser() const { return _user; }
ObjectPtr<Track> getTrack(std::size_t index) const;
ObjectPtr<Track> getTrackAtCurrentIndex() const;
// Get tracks, ordered by position
std::vector<TrackId> getTrackIds() const;
void visitTracks(const std::function<void(const ObjectPtr<Track>& track)>& visitor) const;
const Wt::WDateTime getLastModifiedDateTime() const { return _lastModifiedDateTime; }
// Modifiers
void setCurrentIndex(std::size_t index) { _currentIndex = index; }
void setCurrentPositionInTrack(std::chrono::milliseconds position) { _currentPositionInTrack = position; }
void clear();
void addTrack(const ObjectPtr<Track>& track);
void setLastModifiedDateTime(const Wt::WDateTime& lastModified) { _lastModifiedDateTime = lastModified; }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _currentIndex, "current_index");
Wt::Dbo::field(a, _currentPositionInTrack, "current_position_in_track");
Wt::Dbo::field(a, _lastModifiedDateTime, "last_modified_date_time");
Wt::Dbo::belongsTo(a, _user, "user", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "playqueue_track", "", Wt::Dbo::OnDeleteCascade);
}
private:
friend class Session;
PlayQueue(const ObjectPtr<User>& user, std::string_view name);
static pointer create(Session& session, const ObjectPtr<User>& user, std::string_view name);
std::string _name;
int _currentIndex{};
std::chrono::duration<int, std::milli> _currentPositionInTrack{};
Wt::WDateTime _lastModifiedDateTime;
Wt::Dbo::ptr<User> _user;
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> _tracks;
};
} // namespace lms::db
+2
View File
@@ -23,6 +23,7 @@
#include "database/Db.hpp"
#include "database/Directory.hpp"
#include "database/Image.hpp"
#include "database/PlayQueue.hpp"
#include "database/RatedArtist.hpp"
#include "database/RatedRelease.hpp"
#include "database/RatedTrack.hpp"
@@ -342,6 +343,7 @@ VALUES
EXPECT_FALSE(Image::find(session, ImageId{}));
EXPECT_FALSE(Label::find(session, LabelId{}));
EXPECT_FALSE(Listen::find(session, ListenId{}));
EXPECT_FALSE(PlayQueue::find(session, PlayQueueId{}));
EXPECT_FALSE(RatedArtist::find(session, RatedArtistId{}));
EXPECT_FALSE(RatedRelease::find(session, RatedReleaseId{}));
EXPECT_FALSE(RatedTrack::find(session, RatedTrackId{}));