Skip to content

Commit

Permalink
Subsonci API: added savePlayQueue / getPlayQueue support
Browse files Browse the repository at this point in the history
  • Loading branch information
epoupon committed Oct 31, 2024
1 parent 0ca2d8b commit 472a590
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/libs/database/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion src/libs/database/impl/Migration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace lms::db
{
namespace
{
static constexpr Version LMS_DATABASE_VERSION{ 71 };
static constexpr Version LMS_DATABASE_VERSION{ 72 };
}

VersionInfo::VersionInfo()
Expand Down Expand Up @@ -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)" };
Expand Down Expand Up @@ -954,6 +979,7 @@ SELECT
{ 68, migrateFromV68 },
{ 69, migrateFromV69 },
{ 70, migrateFromV70 },
{ 71, migrateFromV71 },
};

bool migrationPerformed{};
Expand Down
97 changes: 97 additions & 0 deletions src/libs/database/impl/PlayQueue.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions src/libs/database/impl/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");
Expand Down
97 changes: 97 additions & 0 deletions src/libs/database/include/database/PlayQueue.hpp
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions src/libs/database/test/Migration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}));
Expand Down
4 changes: 2 additions & 2 deletions src/libs/subsonic/impl/SubsonicResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ namespace lms::api::subsonic
{ "/getBookmarks", { handleGetBookmarks } },
{ "/createBookmark", { handleCreateBookmark } },
{ "/deleteBookmark", { handleDeleteBookmark } },
{ "/getPlayQueue", { handleNotImplemented } },
{ "/savePlayQueue", { handleNotImplemented } },
{ "/getPlayQueue", { handleGetPlayQueue } },
{ "/savePlayQueue", { handleSavePlayQueue } },

// Media library scanning
{ "/getScanStatus", { Scan::handleGetScanStatus, { db::UserType::ADMIN } } },
Expand Down
80 changes: 80 additions & 0 deletions src/libs/subsonic/impl/entrypoints/Bookmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "Bookmarks.hpp"

#include "database/PlayQueue.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackBookmark.hpp"
Expand All @@ -29,6 +30,8 @@
#include "responses/Bookmark.hpp"
#include "responses/Song.hpp"

#include "core/String.hpp"

namespace lms::api::subsonic
{
using namespace db;
Expand Down Expand Up @@ -93,4 +96,81 @@ namespace lms::api::subsonic

return Response::createOkResponse(context.serverProtocolVersion);
}

// Use a dedicated internal playlist
Response handleGetPlayQueue(RequestContext& context)
{
Response response{ Response::createOkResponse(context.serverProtocolVersion) };

auto transaction{ context.dbSession.createReadTransaction() };
const db::PlayQueue::pointer playQueue{ db::PlayQueue::find(context.dbSession, context.user->getId(), "subsonic") };
if (playQueue)
{
Response::Node& playQueueNode{ response.createNode("playQueue") };
if (auto currentTrack{ playQueue->getTrackAtCurrentIndex() })
{
// optional fields
playQueueNode.setAttribute("current", idToString(currentTrack->getId()));
playQueueNode.setAttribute("position", playQueue->getCurrentPositionInTrack().count());
}

// mandatory fields
playQueueNode.setAttribute("username", context.user->getLoginName());
playQueueNode.setAttribute("changed", core::stringUtils::toISO8601String(playQueue->getLastModifiedDateTime()));
playQueueNode.setAttribute("changedBy", "unknown"); // we don't store the client name (could be several same clients on several devices...)

playQueue->visitTracks([&](const db::Track::pointer& track) {
playQueueNode.addArrayChild("entry", createSongNode(context, track, true /* id3 */));
});
}

return response;
}

Response handleSavePlayQueue(RequestContext& context)
{
// optional params
std::vector<db::TrackId> trackIds{ getMultiParametersAs<TrackId>(context.parameters, "id") };
const std::optional<db::TrackId> currentTrackId{ getParameterAs<db::TrackId>(context.parameters, "current") };
const std::chrono::milliseconds currentPositionInTrack{ getParameterAs<std::size_t>(context.parameters, "current").value_or(0) };

std::vector<db::Track::pointer> tracks;
tracks.reserve(trackIds.size());

// no id means we clear the play queue (see https://github.com/opensubsonic/open-subsonic-api/pull/106)
if (!trackIds.empty())
{
auto transaction{ context.dbSession.createReadTransaction() };
for (db::TrackId trackId : trackIds)
{
if (db::Track::pointer track{ db::Track::find(context.dbSession, trackId) })
tracks.push_back(track);
}
}

{
auto transaction{ context.dbSession.createWriteTransaction() };

db::PlayQueue::pointer playQueue{ db::PlayQueue::find(context.dbSession, context.user->getId(), "subsonic") };
if (!playQueue)
playQueue = context.dbSession.create<db::PlayQueue>(context.user, "subsonic");

playQueue.modify()->clear();
std::size_t index{};
for (std::size_t i{}; i < tracks.size(); ++i)
{
db::Track::pointer& track{ tracks[i] };
playQueue.modify()->addTrack(track);

if (track->getId() == currentTrackId)
index = i;
}

playQueue.modify()->setCurrentIndex(index);
playQueue.modify()->setCurrentPositionInTrack(currentPositionInTrack);
playQueue.modify()->setLastModifiedDateTime(Wt::WDateTime::currentDateTime());
}

return Response::createOkResponse(context.serverProtocolVersion);
}
} // namespace lms::api::subsonic
Loading

0 comments on commit 472a590

Please sign in to comment.