#pragma once #include "SubscriptionManager.hpp" #include #include #include #include #include #include #include #include #include #include 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>(uint64_t clientId)>; using UidToClientIdCallback = std::function(const std::string& uid)>; using ChangeCallback = std::function; // requestPause returns true when playback was actually paused by // this policy (so a returning subscriber should resume it). using RequestPause = std::function; using RequestResume = std::function; Api::Result initialize(std::shared_ptr 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 subscribe(uint64_t clientId); [[nodiscard]] Api::Result unsubscribe(uint64_t clientId); [[nodiscard]] Api::Result unsubscribeByUid(const std::string& uid); [[nodiscard]] Api::Result clearAllSubscriptions(); [[nodiscard]] bool isSubscribed(uint64_t clientId) const; [[nodiscard]] bool hasSubscribers() const; [[nodiscard]] bool hasOnlineSubscribers() const; [[nodiscard]] std::set getSubscribedClients() const; [[nodiscard]] std::set getOnlineSubscriberUids() const; [[nodiscard]] std::vector 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 subscribedUid; bool mayAutoStart = false; }; ClientConnectResult onClientConnected(uint64_t clientId); void onClientDisconnected(uint64_t clientId); private: template void emit(T event) { if (eventBus_) { eventBus_->publish(std::move(event)); } } // Pause playback if the audience vanished; remember that WE // paused so the next returning subscriber resumes. void pauseIfAudienceGone(); std::unique_ptr manager_; Void::EventBus::EventBus* eventBus_ = nullptr; ClientInfoCallback clientInfo_; UidToClientIdCallback uidToClientId_; ChangeCallback onChange_; RequestPause requestPause_; RequestResume requestResume_; std::atomic pausedForNoSubscribers_{false}; std::atomic pauseOnEmpty_{true}; std::atomic resumeOnReturn_{true}; }; } // namespace Echoion::Core