1 parent
df99b6ea7f
commit
d146f11f02
63 files changed
+8331
-2709
No files matched your search
@@ -0,0 +1,115 @@
|
||||
#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 returns true
|
||||
// when a subscribed user came online and playback should
|
||||
// auto-start if tracks are queued.
|
||||
bool 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
|
||||
Reference in new issue
Block a user