Release 1.0.0

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-04 01:49:28 +03:00
1 parent df99b6ea7f
commit d146f11f02
63 files changed
+8331 -2709

No files matched your search

+362 -38
View File
@@ -264,11 +264,55 @@ namespace Echoion::Api
[[maybe_unused]] auto ins = impl_->voidDb.exec(
"INSERT INTO schema_migrations (version) VALUES (4)");
Void::Log::info("Database", "{}",
Void::Log::info("Database", "{}",
"Database migration 4 applied: RPC tokens table added");
}
impl_->version = 4;
// Migration 5: generic key/value store (instance persistence, small
// bits of state that don't deserve their own table)
if (currentVersion < 5) {
const char* migration5 = R"(
CREATE TABLE IF NOT EXISTS kv_store (
key TEXT PRIMARY KEY,
value TEXT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
)";
if (auto r = impl_->voidDb.exec(migration5); !r) {
return fail(ErrorCode::DatabaseError, r.error().message());
}
[[maybe_unused]] auto ins = impl_->voidDb.exec(
"INSERT INTO schema_migrations (version) VALUES (5)");
Void::Log::info("Database", "{}",
"Database migration 5 applied: kv_store table added");
}
// Migration 6: persist the shortened track URL alongside history,
// favorites and playlist tracks so YouTube links render short
// everywhere (they are reconstructed from the DB, which previously
// only kept the long originalUrl).
if (currentVersion < 6) {
const char* migration6 = R"(
ALTER TABLE playback_history ADD COLUMN track_short_url TEXT;
ALTER TABLE user_favorites ADD COLUMN track_short_url TEXT;
ALTER TABLE playlist_tracks ADD COLUMN track_short_url TEXT;
)";
if (auto r = impl_->voidDb.exec(migration6); !r) {
return fail(ErrorCode::DatabaseError, r.error().message());
}
[[maybe_unused]] auto ins = impl_->voidDb.exec(
"INSERT INTO schema_migrations (version) VALUES (6)");
Void::Log::info("Database", "{}",
"Database migration 6 applied: track_short_url columns added");
}
impl_->version = 6;
return {};
}
@@ -374,7 +418,7 @@ namespace Echoion::Api
// Load tracks
const char* tracksSql = R"(
SELECT track_url, track_title, track_artist, track_thumbnail
SELECT track_url, track_title, track_artist, track_thumbnail, track_short_url
FROM playlist_tracks
WHERE playlist_id = ?
ORDER BY position;
@@ -396,6 +440,12 @@ namespace Echoion::Api
if (!tracks.is_null(3)) {
track.thumbnailUrl = tracks.column_text(3);
}
if (!tracks.is_null(4)) {
if (auto shortUrl = tracks.column_text(4);
!shortUrl.empty()) {
track.shortenedUrl = shortUrl;
}
}
playlist.tracks.push_back(track);
}
}
@@ -434,6 +484,37 @@ namespace Echoion::Api
return playlists;
}
Result<std::vector<Playlist>> Database::getAllPlaylists()
{
std::shared_lock<std::shared_mutex> lock(mutex_);
std::vector<Playlist> playlists;
const char* sql = R"(
SELECT id, name, description, created_by, is_public, external_id
FROM playlists
ORDER BY updated_at DESC;
)";
auto stmtRes = impl_->voidDb.prepare(sql);
if (!stmtRes) {
return fail(ErrorCode::DatabaseError, stmtRes.error().message());
}
auto& stmt = *stmtRes;
while (stmt.step().value_or(false)) {
Playlist playlist;
playlist.id = static_cast<uint64_t>(stmt.column_int(0));
playlist.name = stmt.column_text(1);
playlist.description = stmt.column_text(2);
playlist.createdBy = static_cast<uint64_t>(stmt.column_int(3));
playlist.isPublic = stmt.column_int(4) != 0;
playlist.externalId = stmt.column_text(5);
playlists.push_back(playlist);
}
return playlists;
}
Result<void> Database::deletePlaylist(uint64_t id)
{
std::unique_lock<std::shared_mutex> lock(mutex_);
@@ -457,9 +538,9 @@ namespace Echoion::Api
std::unique_lock<std::shared_mutex> lock(mutex_);
const char* sql = R"(
INSERT INTO playlist_tracks
(playlist_id, track_url, track_title, track_artist, track_thumbnail, position)
VALUES (?, ?, ?, ?, ?, ?);
INSERT INTO playlist_tracks
(playlist_id, track_url, track_title, track_artist, track_thumbnail, track_short_url, position)
VALUES (?, ?, ?, ?, ?, ?, ?);
)";
auto stmtRes = impl_->voidDb.prepare(sql);
@@ -472,7 +553,8 @@ namespace Echoion::Api
.bind(3, track.title)
.bind(4, track.artist)
.bind(5, track.thumbnailUrl.value_or(""))
.bind(6, static_cast<int>(position));
.bind(6, track.shortenedUrl.value_or(""))
.bind(7, static_cast<int>(position));
[[maybe_unused]] auto stepRes = stmt.step();
// Update playlist timestamp
@@ -493,9 +575,9 @@ namespace Echoion::Api
std::unique_lock<std::shared_mutex> lock(mutex_);
const char* sql = R"(
INSERT INTO playback_history
(track_url, track_title, track_artist, played_by, duration_ms, completed)
VALUES (?, ?, ?, ?, ?, ?);
INSERT INTO playback_history
(track_url, track_title, track_artist, played_by, duration_ms, completed, track_short_url)
VALUES (?, ?, ?, ?, ?, ?, ?);
)";
auto stmtRes = impl_->voidDb.prepare(sql);
@@ -508,7 +590,8 @@ namespace Echoion::Api
.bind(3, track.artist)
.bind(4, static_cast<std::int64_t>(playedBy))
.bind(5, static_cast<std::int64_t>(duration.count()))
.bind(6, completed ? 1 : 0);
.bind(6, completed ? 1 : 0)
.bind(7, track.shortenedUrl.value_or(""));
[[maybe_unused]] auto stepRes = stmt.step();
return {};
@@ -523,7 +606,7 @@ namespace Echoion::Api
std::vector<PlaybackHistoryEntry> history;
const char* sql = R"(
SELECT id, track_url, track_title, track_artist, played_at, duration_ms, completed
SELECT id, track_url, track_title, track_artist, played_at, duration_ms, completed, track_short_url
FROM playback_history
WHERE played_by = ?
ORDER BY played_at DESC
@@ -549,6 +632,9 @@ namespace Echoion::Api
entry.duration =
std::chrono::milliseconds(stmt.column_int(5));
entry.completed = stmt.column_int(6) != 0;
if (auto shortUrl = stmt.column_text(7); !shortUrl.empty()) {
entry.track.shortenedUrl = shortUrl;
}
history.push_back(entry);
}
@@ -560,9 +646,9 @@ namespace Echoion::Api
{
std::unique_lock<std::shared_mutex> lock(mutex_);
const char* sql = R"(
INSERT OR REPLACE INTO user_favorites
(user_id, track_url, track_title, track_artist)
VALUES (?, ?, ?, ?);
INSERT OR REPLACE INTO user_favorites
(user_id, track_url, track_title, track_artist, track_short_url)
VALUES (?, ?, ?, ?, ?);
)";
auto stmtRes = impl_->voidDb.prepare(sql);
@@ -575,6 +661,7 @@ namespace Echoion::Api
stmt.bind(2, track.originalUrl);
stmt.bind(3, track.title);
stmt.bind(4, track.artist);
stmt.bind(5, track.shortenedUrl.value_or(""));
[[maybe_unused]] auto stepRes = stmt.step();
@@ -629,7 +716,7 @@ namespace Echoion::Api
std::vector<FavoriteTrack> favorites;
const char* sql = R"(
SELECT track_url, track_title, track_artist, added_at
SELECT track_url, track_title, track_artist, added_at, track_short_url
FROM user_favorites
WHERE user_id = ?
ORDER BY added_at DESC;
@@ -651,6 +738,9 @@ namespace Echoion::Api
stmt.column_text(1);
fav.track.artist =
stmt.column_text(2);
if (auto shortUrl = stmt.column_text(4); !shortUrl.empty()) {
fav.track.shortenedUrl = shortUrl;
}
fav.userId = userId;
favorites.push_back(fav);
}
@@ -680,8 +770,9 @@ namespace Echoion::Api
stmt.bind(1, static_cast<std::int64_t>(keepCount));
[[maybe_unused]] auto stepRes = stmt.step();
// Vacuum to reclaim space
[[maybe_unused]] auto execRes = impl_->voidDb.exec("VACUUM;");
// No VACUUM here: this runs after every finished track and a full
// vacuum would rewrite the whole file each time. Use vacuum()
// explicitly for space reclamation.
return {};
}
@@ -822,25 +913,79 @@ namespace Echoion::Api
return count;
}
// Stub implementations for missing methods
Result<void>
Database::updatePlaylist(uint64_t id,
const std::optional<std::string>& name,
const std::optional<std::string>& description,
const std::optional<bool>& isPublic)
{
(void)id;
(void)name;
(void)description;
(void)isPublic;
std::unique_lock<std::shared_mutex> lock(mutex_);
const char* sql = R"(
UPDATE playlists SET
name = COALESCE(?, name),
description = COALESCE(?, description),
is_public = COALESCE(?, is_public),
updated_at = CURRENT_TIMESTAMP
WHERE id = ?;
)";
auto stmtRes = impl_->voidDb.prepare(sql);
if (!stmtRes) {
return fail(ErrorCode::DatabaseError, stmtRes.error().message());
}
auto& stmt = *stmtRes;
stmt.bind(1, name);
stmt.bind(2, description);
if (isPublic) {
stmt.bind(3, *isPublic ? 1 : 0);
} else {
stmt.bind(3, nullptr);
}
stmt.bind(4, static_cast<std::int64_t>(id));
[[maybe_unused]] auto stepRes = stmt.step();
return {};
}
Result<void> Database::removeTrackFromPlaylist(uint64_t playlistId,
uint64_t trackId)
{
(void)playlistId;
(void)trackId;
std::unique_lock<std::shared_mutex> lock(mutex_);
// Delete the row, then compact positions so ordering stays dense.
[[maybe_unused]] auto begin = impl_->voidDb.exec("BEGIN;");
std::int64_t removedPos = -1;
if (auto posRes = impl_->voidDb.prepare(
"SELECT position FROM playlist_tracks WHERE id = ? AND "
"playlist_id = ?;")) {
auto& posStmt = *posRes;
posStmt.bind(1, static_cast<std::int64_t>(trackId))
.bind(2, static_cast<std::int64_t>(playlistId));
if (posStmt.step().value_or(false)) {
removedPos = posStmt.column_int(0);
}
}
if (removedPos < 0) {
[[maybe_unused]] auto rollback = impl_->voidDb.exec("ROLLBACK;");
return fail(ErrorCode::NotFound, "Track not in playlist");
}
if (auto delRes = impl_->voidDb.prepare(
"DELETE FROM playlist_tracks WHERE id = ?;")) {
auto& delStmt = *delRes;
delStmt.bind(1, static_cast<std::int64_t>(trackId));
[[maybe_unused]] auto s = delStmt.step();
}
if (auto shiftRes = impl_->voidDb.prepare(
"UPDATE playlist_tracks SET position = position - 1 WHERE "
"playlist_id = ? AND position > ?;")) {
auto& shiftStmt = *shiftRes;
shiftStmt.bind(1, static_cast<std::int64_t>(playlistId))
.bind(2, removedPos);
[[maybe_unused]] auto s = shiftStmt.step();
}
[[maybe_unused]] auto commit = impl_->voidDb.exec("COMMIT;");
return {};
}
@@ -848,18 +993,92 @@ namespace Echoion::Api
uint64_t trackId,
uint32_t newPosition)
{
(void)playlistId;
(void)trackId;
(void)newPosition;
std::unique_lock<std::shared_mutex> lock(mutex_);
[[maybe_unused]] auto begin = impl_->voidDb.exec("BEGIN;");
std::int64_t oldPos = -1;
if (auto posRes = impl_->voidDb.prepare(
"SELECT position FROM playlist_tracks WHERE id = ? AND "
"playlist_id = ?;")) {
auto& posStmt = *posRes;
posStmt.bind(1, static_cast<std::int64_t>(trackId))
.bind(2, static_cast<std::int64_t>(playlistId));
if (posStmt.step().value_or(false)) {
oldPos = posStmt.column_int(0);
}
}
if (oldPos < 0) {
[[maybe_unused]] auto rollback = impl_->voidDb.exec("ROLLBACK;");
return fail(ErrorCode::NotFound, "Track not in playlist");
}
const auto newPos = static_cast<std::int64_t>(newPosition);
const char* shiftSql =
oldPos < newPos
? "UPDATE playlist_tracks SET position = position - 1 WHERE "
"playlist_id = ? AND position > ? AND position <= ?;"
: "UPDATE playlist_tracks SET position = position + 1 WHERE "
"playlist_id = ? AND position >= ? AND position < ?;";
if (auto shiftRes = impl_->voidDb.prepare(shiftSql)) {
auto& shiftStmt = *shiftRes;
if (oldPos < newPos) {
shiftStmt.bind(1, static_cast<std::int64_t>(playlistId))
.bind(2, oldPos)
.bind(3, newPos);
} else {
shiftStmt.bind(1, static_cast<std::int64_t>(playlistId))
.bind(2, newPos)
.bind(3, oldPos);
}
[[maybe_unused]] auto s = shiftStmt.step();
}
if (auto moveRes = impl_->voidDb.prepare(
"UPDATE playlist_tracks SET position = ? WHERE id = ?;")) {
auto& moveStmt = *moveRes;
moveStmt.bind(1, newPos).bind(2, static_cast<std::int64_t>(trackId));
[[maybe_unused]] auto s = moveStmt.step();
}
[[maybe_unused]] auto commit = impl_->voidDb.exec("COMMIT;");
return {};
}
Result<std::vector<TrackInfo>> Database::getRecentlyPlayed(uint64_t userId,
size_t limit)
{
(void)userId;
(void)limit;
return std::vector<TrackInfo>{};
std::shared_lock<std::shared_mutex> lock(mutex_);
std::vector<TrackInfo> tracks;
// userId 0 = global history. Deduplicate by URL, newest first.
const char* sql = R"(
SELECT track_url, track_title, track_artist, MAX(played_at), MAX(track_short_url)
FROM playback_history
WHERE (?1 = 0 OR played_by = ?1)
GROUP BY track_url
ORDER BY MAX(played_at) DESC
LIMIT ?2;
)";
auto stmtRes = impl_->voidDb.prepare(sql);
if (!stmtRes) {
return fail(ErrorCode::DatabaseError, stmtRes.error().message());
}
auto& stmt = *stmtRes;
stmt.bind(1, static_cast<std::int64_t>(userId))
.bind(2, static_cast<std::int64_t>(limit));
while (stmt.step().value_or(false)) {
TrackInfo track;
track.originalUrl = stmt.column_text(0);
track.title = stmt.column_text(1);
track.artist = stmt.column_text(2);
if (auto shortUrl = stmt.column_text(4); !shortUrl.empty()) {
track.shortenedUrl = shortUrl;
}
tracks.push_back(std::move(track));
}
return tracks;
}
Result<PlaybackStatistics> Database::getStatistics(
@@ -867,25 +1086,130 @@ namespace Echoion::Api
std::optional<std::chrono::system_clock::time_point>
since)
{
(void)userId;
(void)since;
return PlaybackStatistics{};
std::shared_lock<std::shared_mutex> lock(mutex_);
(void)since; // "since" filtering can come with a real need for it
PlaybackStatistics stats;
if (auto totalsRes = impl_->voidDb.prepare(R"(
SELECT COUNT(*), COUNT(DISTINCT track_url), COALESCE(SUM(duration_ms), 0)
FROM playback_history
WHERE (?1 = 0 OR played_by = ?1);
)")) {
auto& stmt = *totalsRes;
stmt.bind(1, static_cast<std::int64_t>(userId));
if (stmt.step().value_or(false)) {
stats.totalPlays = static_cast<uint64_t>(stmt.column_int(0));
stats.totalTracks = static_cast<uint64_t>(stmt.column_int(1));
stats.totalPlaytimeMs =
static_cast<uint64_t>(stmt.column_int(2));
}
}
if (auto topRes = impl_->voidDb.prepare(R"(
SELECT track_url, track_title, track_artist, COUNT(*) AS plays, MAX(track_short_url)
FROM playback_history
WHERE (?1 = 0 OR played_by = ?1)
GROUP BY track_url
ORDER BY plays DESC
LIMIT 1;
)")) {
auto& stmt = *topRes;
stmt.bind(1, static_cast<std::int64_t>(userId));
if (stmt.step().value_or(false)) {
TrackInfo track;
track.originalUrl = stmt.column_text(0);
track.title = stmt.column_text(1);
track.artist = stmt.column_text(2);
if (auto shortUrl = stmt.column_text(4); !shortUrl.empty()) {
track.shortenedUrl = shortUrl;
}
stats.mostPlayedTrack = std::move(track);
stats.mostPlayedCount =
static_cast<uint64_t>(stmt.column_int(3));
}
}
return stats;
}
Result<std::vector<std::pair<TrackInfo, uint64_t>>>
Database::getMostPlayed(uint64_t userId, size_t limit)
{
(void)userId;
(void)limit;
return std::vector<std::pair<TrackInfo, uint64_t>>{};
std::shared_lock<std::shared_mutex> lock(mutex_);
std::vector<std::pair<TrackInfo, uint64_t>> result;
const char* sql = R"(
SELECT track_url, track_title, track_artist, COUNT(*) AS plays, MAX(track_short_url)
FROM playback_history
WHERE (?1 = 0 OR played_by = ?1)
GROUP BY track_url
ORDER BY plays DESC
LIMIT ?2;
)";
auto stmtRes = impl_->voidDb.prepare(sql);
if (!stmtRes) {
return fail(ErrorCode::DatabaseError, stmtRes.error().message());
}
auto& stmt = *stmtRes;
stmt.bind(1, static_cast<std::int64_t>(userId))
.bind(2, static_cast<std::int64_t>(limit));
while (stmt.step().value_or(false)) {
TrackInfo track;
track.originalUrl = stmt.column_text(0);
track.title = stmt.column_text(1);
track.artist = stmt.column_text(2);
if (auto shortUrl = stmt.column_text(4); !shortUrl.empty()) {
track.shortenedUrl = shortUrl;
}
result.emplace_back(std::move(track),
static_cast<uint64_t>(stmt.column_int(3)));
}
return result;
}
Result<std::optional<std::string>> Database::getKv(const std::string& key)
{
std::shared_lock<std::shared_mutex> lock(mutex_);
auto stmtRes = impl_->voidDb.prepare(
"SELECT value FROM kv_store WHERE key = ?;");
if (!stmtRes) {
return fail(ErrorCode::DatabaseError, stmtRes.error().message());
}
auto& stmt = *stmtRes;
stmt.bind(1, key);
if (stmt.step().value_or(false)) {
return std::optional<std::string>{stmt.column_text(0)};
}
return std::optional<std::string>{};
}
Result<void> Database::setKv(const std::string& key,
const std::string& value)
{
std::unique_lock<std::shared_mutex> lock(mutex_);
auto stmtRes = impl_->voidDb.prepare(R"(
INSERT OR REPLACE INTO kv_store (key, value, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP);
)");
if (!stmtRes) {
return fail(ErrorCode::DatabaseError, stmtRes.error().message());
}
auto& stmt = *stmtRes;
stmt.bind(1, key).bind(2, value);
[[maybe_unused]] auto stepRes = stmt.step();
return {};
}
Result<void> Database::executeQuery(const std::string& sql,
RowCallback callback)
{
// Deliberately unimplemented: free-form SQL over the RPC surface is
// a footgun. Dedicated accessors (getKv/setKv, ...) cover the needs.
(void)sql;
(void)callback;
return {};
return fail(ErrorCode::NotSupported, "executeQuery is not available");
}
// Database manager