122 lines
3.9 KiB
C++
122 lines
3.9 KiB
C++
#pragma once
|
|
|
|
#include "SubscriptionManager.hpp"
|
|
#include <Database.hpp>
|
|
#include <Types.hpp>
|
|
#include <Void/Async/EventBus.hpp>
|
|
#include <atomic>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
// Subscription bookkeeping + audience policy, extracted from
|
|
// MusicManager. Owns the persistent SubscriptionManager and the
|
|
// client-resolution callbacks; drives the pause-on-empty /
|
|
// resume-on-return policy through two injected hooks so it never touches
|
|
// playback internals directly.
|
|
class SubscriptionService
|
|
{
|
|
public:
|
|
|
|
using ClientInfoCallback = std::function<std::optional<
|
|
std::pair<std::string, std::string>>(uint64_t clientId)>;
|
|
using UidToClientIdCallback =
|
|
std::function<std::optional<uint64_t>(const std::string& uid)>;
|
|
using ChangeCallback = std::function<void()>;
|
|
// requestPause returns true when playback was actually paused by
|
|
// this policy (so a returning subscriber should resume it).
|
|
using RequestPause = std::function<bool()>;
|
|
using RequestResume = std::function<bool()>;
|
|
|
|
Api::Result<void>
|
|
initialize(std::shared_ptr<Api::Database> database);
|
|
|
|
void setEventBus(Void::EventBus::EventBus* bus) { eventBus_ = bus; }
|
|
void setClientInfoCallback(ClientInfoCallback cb)
|
|
{
|
|
clientInfo_ = std::move(cb);
|
|
}
|
|
void setUidToClientIdCallback(UidToClientIdCallback cb)
|
|
{
|
|
uidToClientId_ = std::move(cb);
|
|
}
|
|
void setChangeCallback(ChangeCallback cb)
|
|
{
|
|
onChange_ = std::move(cb);
|
|
}
|
|
void setPauseResumeHooks(RequestPause pause, RequestResume resume)
|
|
{
|
|
requestPause_ = std::move(pause);
|
|
requestResume_ = std::move(resume);
|
|
}
|
|
|
|
// Audience policy toggles (features.pauseOnEmptySubscribers /
|
|
// features.resumeOnSubscriberReturn); re-applied on config
|
|
// reload.
|
|
void setPolicy(bool pauseOnEmpty, bool resumeOnReturn)
|
|
{
|
|
pauseOnEmpty_ = pauseOnEmpty;
|
|
resumeOnReturn_ = resumeOnReturn;
|
|
}
|
|
|
|
// Command surface (message strings go back to the chat user).
|
|
[[nodiscard]] Api::Result<std::string> subscribe(uint64_t clientId);
|
|
[[nodiscard]] Api::Result<std::string>
|
|
unsubscribe(uint64_t clientId);
|
|
[[nodiscard]] Api::Result<std::string>
|
|
unsubscribeByUid(const std::string& uid);
|
|
[[nodiscard]] Api::Result<std::string> clearAllSubscriptions();
|
|
|
|
[[nodiscard]] bool isSubscribed(uint64_t clientId) const;
|
|
[[nodiscard]] bool hasSubscribers() const;
|
|
[[nodiscard]] bool hasOnlineSubscribers() const;
|
|
[[nodiscard]] std::set<uint64_t> getSubscribedClients() const;
|
|
[[nodiscard]] std::set<std::string> getOnlineSubscriberUids() const;
|
|
[[nodiscard]] std::vector<SubscriptionManager::Subscriber>
|
|
getSubscribers() const;
|
|
|
|
// Presence events from the plugin. onClientConnected resolves the
|
|
// client once and hands the uid back so callers don't repeat the
|
|
// (plugin round-trip) lookup: subscribedUid is set when the
|
|
// client is a subscriber; mayAutoStart when playback should
|
|
// auto-start if tracks are queued.
|
|
struct ClientConnectResult {
|
|
std::optional<std::string> subscribedUid;
|
|
bool mayAutoStart = false;
|
|
};
|
|
ClientConnectResult onClientConnected(uint64_t clientId);
|
|
void onClientDisconnected(uint64_t clientId);
|
|
|
|
private:
|
|
|
|
template <typename T> void emit(T event)
|
|
{
|
|
if (eventBus_) {
|
|
eventBus_->publish<T>(std::move(event));
|
|
}
|
|
}
|
|
|
|
// Pause playback if the audience vanished; remember that WE
|
|
// paused so the next returning subscriber resumes.
|
|
void pauseIfAudienceGone();
|
|
|
|
std::unique_ptr<SubscriptionManager> manager_;
|
|
Void::EventBus::EventBus* eventBus_ = nullptr;
|
|
ClientInfoCallback clientInfo_;
|
|
UidToClientIdCallback uidToClientId_;
|
|
ChangeCallback onChange_;
|
|
RequestPause requestPause_;
|
|
RequestResume requestResume_;
|
|
std::atomic<bool> pausedForNoSubscribers_{false};
|
|
std::atomic<bool> pauseOnEmpty_{true};
|
|
std::atomic<bool> resumeOnReturn_{true};
|
|
};
|
|
|
|
} // namespace Echoion::Core
|