Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-12 02:56:29 +03:00
1 parent 5c036d91fb
commit 01362d2e51
14 files changed
+1324 -51

No files matched your search

+3
View File
@@ -35,6 +35,7 @@ namespace Void::Logging
namespace Echoion::Core
{
class ListeningRoomManager;
// Main Core implementation of ICoreInterface
class Core : public Api::ICoreInterface
@@ -191,6 +192,8 @@ namespace Echoion::Core
std::shared_ptr<NetworkClient> networkClient_;
std::shared_ptr<LyricsService> lyricsService_;
std::shared_ptr<MusicManager> musicManager_;
std::shared_ptr<ListeningRoomManager> listeningRooms_;
std::string pendingListeningRoomState_;
std::unique_ptr<CommandManager> commandManager_;
Api::PluginInterfacePtr pluginInterface_;
@@ -0,0 +1,106 @@
#pragma once
#include "echoion/core/MusicManager.hpp"
#include <PluginInterface.hpp>
#include <Void/Async/EventBus.hpp>
#include <map>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <vector>
namespace Echoion::Core
{
class NetworkClient;
class ListeningRoomManager final
{
public:
struct RoomSummary {
std::string name;
uint64_t ownerId{};
std::vector<uint64_t> members;
bool isPlaying{};
bool isPaused{};
std::size_t queueSize{};
};
struct PersistedRoom {
std::string name;
uint64_t ownerId{};
std::vector<uint64_t> members;
Api::HotReloadState state;
};
ListeningRoomManager(std::shared_ptr<NetworkClient> network,
Api::ConfigManager& config, Void::EventBus::EventBus& events);
~ListeningRoomManager();
ListeningRoomManager(const ListeningRoomManager&) = delete;
ListeningRoomManager& operator=(const ListeningRoomManager&) = delete;
void setPluginInterface(Api::PluginInterfacePtr plugin);
void setChangeCallback(std::function<void()> callback);
[[nodiscard]] Api::Result<void>
create(std::string name, uint64_t ownerId);
[[nodiscard]] Api::Result<void> remove(const std::string& name,
uint64_t requesterId);
[[nodiscard]] Api::Result<void> join(const std::string& name,
uint64_t clientId);
[[nodiscard]] Api::Result<void> leave(uint64_t clientId);
[[nodiscard]] Api::Result<std::string> play(const std::string& name,
std::string query, uint64_t clientId);
[[nodiscard]] Api::Result<void> pause(const std::string& name,
uint64_t requesterId);
[[nodiscard]] Api::Result<void> resume(const std::string& name,
uint64_t requesterId);
[[nodiscard]] Api::Result<void> stop(const std::string& name,
uint64_t requesterId);
[[nodiscard]] Api::Result<void> skip(const std::string& name,
uint64_t requesterId);
[[nodiscard]] Api::Result<void> removeQueueEntry(const std::string& name,
uint64_t queueId, uint64_t requesterId);
[[nodiscard]] Api::Result<void> moveQueueEntry(const std::string& name,
uint64_t queueId, uint32_t newPosition, uint64_t requesterId);
[[nodiscard]] Api::Result<void> clearQueue(const std::string& name,
uint64_t requesterId);
[[nodiscard]] Api::Result<std::vector<Api::QueueEntry>>
queue(const std::string& name) const;
[[nodiscard]] std::vector<RoomSummary> list() const;
[[nodiscard]] std::optional<std::string>
roomForClient(uint64_t clientId) const;
[[nodiscard]] std::vector<PersistedRoom> snapshot() const;
[[nodiscard]] Api::Result<void>
restore(const std::vector<PersistedRoom>& rooms);
void shutdown();
private:
struct Room {
std::string name;
uint64_t ownerId{};
Api::IPluginInterface::AudioStreamId streamId{};
std::set<uint64_t> members;
std::unique_ptr<MusicManager> music;
};
[[nodiscard]] Api::ServerConfig serverConfigFor(
const std::string& roomName) const;
[[nodiscard]] Api::Result<void> refreshRecipients(Room& room);
[[nodiscard]] Room* findLocked(const std::string& name);
[[nodiscard]] const Room* findLocked(const std::string& name) const;
std::shared_ptr<NetworkClient> network_;
Api::ConfigManager& config_;
Void::EventBus::EventBus& events_;
Api::PluginInterfacePtr plugin_;
std::map<std::string, std::unique_ptr<Room>> rooms_;
std::map<uint64_t, std::string> membership_;
mutable std::mutex mutex_;
std::function<void()> changeCallback_;
void notifyChanged() const;
};
}
@@ -0,0 +1,37 @@
#pragma once
#include <Types.hpp>
#include <algorithm>
#include <cctype>
#include <string>
#include <string_view>
namespace Echoion::Core
{
[[nodiscard]] inline Api::Result<std::string>
validateListeningRoomName(std::string name)
{
const auto first = name.find_first_not_of(" \t\r\n");
if (first == std::string::npos) return Api::fail(
Api::ErrorCode::InvalidArgument, "room name is required");
const auto last = name.find_last_not_of(" \t\r\n");
name = name.substr(first, last - first + 1);
if (name.size() > 48 || std::ranges::any_of(name,
[](unsigned char value) { return std::iscntrl(value) != 0; })) {
return Api::fail(Api::ErrorCode::InvalidArgument,
"room name must be 1-48 printable characters");
}
return name;
}
[[nodiscard]] inline std::string
canonicalListeningRoomName(std::string_view name)
{
std::string key{name};
std::ranges::transform(key, key.begin(), [](unsigned char value) {
return static_cast<char>(std::tolower(value));
});
return key;
}
}
+35
View File
@@ -85,6 +85,26 @@ namespace Echoion::Core::Dto
std::string text;
};
struct ListeningRoomRequest {
std::string room;
};
struct ListeningRoomPlayRequest {
std::string room;
std::string query;
};
struct ListeningRoomQueueRequest {
std::string room;
std::uint64_t queueId = 0;
};
struct ListeningRoomMoveRequest {
std::string room;
std::uint64_t queueId = 0;
std::uint32_t newPosition = 0;
};
// ── RPC handler responses ───────────────────────────────────────────────
struct Track {
@@ -181,6 +201,21 @@ namespace Echoion::Core::Dto
float volume = 0.0f;
};
struct ListeningRoom {
std::string name;
std::uint64_t ownerId = 0;
std::vector<std::uint64_t> members;
bool isPlaying = false;
bool isPaused = false;
std::uint64_t queueSize = 0;
bool isOwner = false;
};
struct ListeningRoomList {
std::vector<ListeningRoom> rooms;
std::optional<std::string> currentRoom;
};
struct Autoplay {
bool autoplay = false;
};
@@ -19,6 +19,7 @@
#include "ReloadConfigCommand.hpp"
#include "RemoveCommand.hpp"
#include "RepeatCommand.hpp"
#include "RoomCommand.hpp"
#include "ResumeCommand.hpp"
#include "RpcInfoCommand.hpp"
#include "SearchCommand.hpp"
@@ -0,0 +1,218 @@
#pragma once
#include "Command.hpp"
#include "echoion/core/ListeningRoomManager.hpp"
#include <algorithm>
#include <charconv>
#include <cctype>
#include <format>
#include <sstream>
#include <string>
#include <string_view>
namespace Echoion::Core
{
class RoomCommand final : public Command
{
public:
void execute(const CommandContext& ctx) override
{
auto rooms = ctx.service<ListeningRoomManager>();
if (!rooms) {
ctx.reply(ctx.tr("Listening rooms are currently unavailable."));
return;
}
auto [action, argument] = split(ctx.args);
if (action.empty() || action == "list") {
list(ctx, *rooms);
return;
}
if (action == "help") {
help(ctx);
return;
}
if (action == "create") {
reply(ctx, rooms->create(argument, ctx.clientId),
ctx.trf("Created and joined room '{}'.", argument));
return;
}
if (action == "join") {
reply(ctx, rooms->join(argument, ctx.clientId),
ctx.trf("Joined room '{}'.", argument));
return;
}
if (action == "leave") {
reply(ctx, rooms->leave(ctx.clientId),
ctx.tr("Left the listening room."));
return;
}
const auto current = rooms->roomForClient(ctx.clientId);
if (!current) {
ctx.reply(ctx.trf(
"Join a room first with {}room join <name>.", ctx.prefix));
return;
}
if (action == "delete") {
reply(ctx, rooms->remove(*current, ctx.clientId),
ctx.trf("Deleted room '{}'.", *current));
} else if (action == "play") {
if (argument.empty()) {
help(ctx);
return;
}
auto result = rooms->play(*current, argument, ctx.clientId);
if (!result) {
ctx.reply(ctx.trf("Room action failed: {}",
result.error().message));
} else {
ctx.reply(ctx.trf("Added to '{}': {}", *current, *result));
}
} else if (action == "pause") {
reply(ctx, rooms->pause(*current, ctx.clientId),
ctx.trf("Paused room '{}'.", *current));
} else if (action == "resume") {
reply(ctx, rooms->resume(*current, ctx.clientId),
ctx.trf("Resumed room '{}'.", *current));
} else if (action == "stop") {
reply(ctx, rooms->stop(*current, ctx.clientId),
ctx.trf("Stopped room '{}'.", *current));
} else if (action == "skip") {
reply(ctx, rooms->skip(*current, ctx.clientId),
ctx.trf("Skipped in room '{}'.", *current));
} else if (action == "clear") {
reply(ctx, rooms->clearQueue(*current, ctx.clientId),
ctx.trf("Cleared queue for '{}'.", *current));
} else if (action == "remove") {
const auto queueId = number(argument);
if (!queueId) {
help(ctx);
return;
}
reply(ctx, rooms->removeQueueEntry(
*current, *queueId, ctx.clientId),
ctx.trf("Removed queue entry {}.", *queueId));
} else if (action == "move") {
auto [queueIdText, positionText] = split(argument);
const auto queueId = number(queueIdText);
const auto position = number(positionText);
if (!queueId || !position || *position == 0) {
help(ctx);
return;
}
reply(ctx, rooms->moveQueueEntry(*current, *queueId,
static_cast<uint32_t>(*position - 1), ctx.clientId),
ctx.trf("Moved queue entry {} to position {}.",
*queueId, *position));
} else if (action == "queue") {
queue(ctx, *rooms, *current);
} else {
help(ctx);
}
}
[[nodiscard]] std::string_view getDescription() const override
{
return "Creates, joins and controls independent listening rooms.";
}
[[nodiscard]] std::string_view getUsage() const override
{
return "<list|create|join|leave|delete|play|pause|resume|stop|skip|queue|remove|move|clear>";
}
[[nodiscard]] CommandCategory getCategory() const override
{
return CommandCategory::Music;
}
private:
static std::pair<std::string, std::string> split(std::string_view input)
{
const auto first = input.find_first_not_of(" \t");
if (first == std::string_view::npos) return {};
const auto separator = input.find_first_of(" \t", first);
std::string action(input.substr(first, separator - first));
std::ranges::transform(action, action.begin(),
[](unsigned char value) { return std::tolower(value); });
if (separator == std::string_view::npos) return {action, {}};
const auto argumentStart = input.find_first_not_of(" \t", separator);
return {action, argumentStart == std::string_view::npos
? std::string{} : std::string(input.substr(argumentStart))};
}
static std::optional<uint64_t> number(std::string_view text)
{
uint64_t value = 0;
const auto [end, error] = std::from_chars(
text.data(), text.data() + text.size(), value);
return error == std::errc{} && end == text.data() + text.size()
? std::optional<uint64_t>{value} : std::nullopt;
}
static void list(const CommandContext& ctx, ListeningRoomManager& rooms)
{
const auto all = rooms.list();
if (all.empty()) {
ctx.reply(ctx.trf("No listening rooms. Create one with {}room create <name>.",
ctx.prefix));
return;
}
const auto current = rooms.roomForClient(ctx.clientId);
std::stringstream output;
output << ctx.tr("Listening rooms:") << '\n';
for (const auto& room : all) {
const bool selected = current == room.name;
output << (selected ? "**> " : "- ")
<< escapeMarkdown(room.name) << (selected ? "** " : " ")
<< ctx.trf("({} listeners, {} queued{})", room.members.size(),
room.queueSize, room.isPlaying ? ", playing" : "") << '\n';
}
ctx.reply(output.str());
}
static void queue(const CommandContext& ctx, ListeningRoomManager& rooms,
const std::string& name)
{
auto entries = rooms.queue(name);
if (!entries) {
ctx.reply(ctx.trf("Room action failed: {}", entries.error().message));
return;
}
std::stringstream output;
output << ctx.trf("Queue for '{}' ({} tracks):", name, entries->size());
for (std::size_t index = 0; index < entries->size(); ++index) {
output << '\n' << index + 1 << ". "
<< trackLink((*entries)[index].track) << " `#"
<< (*entries)[index].queueId << '`';
}
ctx.reply(output.str());
}
static void reply(const CommandContext& ctx, const Api::Result<void>& result,
std::string_view success)
{
if (result) {
ctx.reply(success);
} else {
ctx.reply(ctx.trf("Room action failed: {}", result.error().message));
}
}
static void help(const CommandContext& ctx)
{
ctx.reply(ctx.trf(
"Room commands:\n"
"`{}room list`\n`{}room create <name>`\n"
"`{}room join <name>`\n`{}room leave`\n"
"`{}room play <song>`\n`{}room pause|resume|stop|skip`\n"
"`{}room queue`\n`{}room remove <queue-id>`\n"
"`{}room move <queue-id> <position>`\n"
"`{}room clear`\n`{}room delete`",
ctx.prefix, ctx.prefix, ctx.prefix, ctx.prefix, ctx.prefix,
ctx.prefix, ctx.prefix, ctx.prefix, ctx.prefix, ctx.prefix,
ctx.prefix));
}
};
}
@@ -1,5 +1,6 @@
#pragma once
#include "TrackTypes.hpp"
#include "RpcTypes.hpp"
#include <chrono>
#include <cstdint>
#include <optional>
@@ -45,6 +46,18 @@ namespace Echoion::Core
std::string lastActivity; // ISO 8601
};
struct ListeningRoomState {
std::string name;
uint64_t ownerId = 0;
std::vector<uint64_t> members;
Echoion::Api::PlaybackState playback;
std::vector<QueueEntry> queue;
std::optional<std::string> currentStreamUrl;
int64_t playbackPositionMs = 0;
BotConfig config;
bool autoplay = false;
};
// Main state
PlaybackState playback;
bool hasPlayback =
@@ -52,6 +65,7 @@ namespace Echoion::Core
bool autoplay = false; // Radio mode on/off (persisted for restart)
std::vector<QueueEntryState> queue;
std::vector<SessionState> sessions;
std::vector<ListeningRoomState> listeningRooms;
// Version info
uint32_t version = 1;