282 lines
6.6 KiB
C++
282 lines
6.6 KiB
C++
#include "echoion/core/SubscriptionService.hpp"
|
|
|
|
#include <Events.hpp>
|
|
#include <Void/Log/Log.hpp>
|
|
#include <format>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
Api::Result<void>
|
|
SubscriptionService::initialize(std::shared_ptr<Api::Database> database)
|
|
{
|
|
manager_ = std::make_unique<SubscriptionManager>();
|
|
return manager_->initialize(std::move(database));
|
|
}
|
|
|
|
Api::Result<std::string> SubscriptionService::subscribe(uint64_t clientId)
|
|
{
|
|
if (!manager_) {
|
|
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
|
"Subscription manager not initialized");
|
|
}
|
|
if (!clientInfo_) {
|
|
return Api::fail(Api::ErrorCode::NotInitialized,
|
|
"Client info callback not set");
|
|
}
|
|
|
|
auto clientInfo = clientInfo_(clientId);
|
|
if (!clientInfo) {
|
|
return Api::fail(Api::ErrorCode::NotFound,
|
|
"Could not get client info");
|
|
}
|
|
|
|
const auto& [uid, nickname] = *clientInfo;
|
|
const bool added = manager_->subscribe(uid, nickname);
|
|
|
|
Void::Log::info("SubscriptionService", "{}",
|
|
std::format("Subscribe: client {} ({}), added={}",
|
|
clientId, nickname, added));
|
|
|
|
// A subscriber appeared: if this policy paused playback earlier,
|
|
// bring it back.
|
|
if (added && pausedForNoSubscribers_.load() &&
|
|
resumeOnReturn_.load() && requestResume_) {
|
|
if (requestResume_()) {
|
|
pausedForNoSubscribers_ = false;
|
|
}
|
|
}
|
|
|
|
// Always refresh the whisper list on subscribe so offline->online
|
|
// transitions take effect.
|
|
if (onChange_) {
|
|
onChange_();
|
|
}
|
|
|
|
emit(Api::Events::UserSubscribed{
|
|
.uid = uid, .nickname = nickname, .clientId = clientId});
|
|
|
|
if (added) {
|
|
return "You are now subscribed to the bot's audio output!";
|
|
}
|
|
return "You are still subscribed (was already in database).";
|
|
}
|
|
|
|
Api::Result<std::string> SubscriptionService::unsubscribe(uint64_t clientId)
|
|
{
|
|
if (!manager_) {
|
|
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
|
"Subscription manager not initialized");
|
|
}
|
|
if (!clientInfo_) {
|
|
return Api::fail(Api::ErrorCode::NotInitialized,
|
|
"Client info callback not set");
|
|
}
|
|
|
|
auto clientInfo = clientInfo_(clientId);
|
|
if (!clientInfo) {
|
|
return Api::fail(Api::ErrorCode::NotFound,
|
|
"Could not get client info");
|
|
}
|
|
|
|
const auto& [uid, nickname] = *clientInfo;
|
|
const bool removed = manager_->unsubscribe(uid);
|
|
|
|
if (removed) {
|
|
pauseIfAudienceGone();
|
|
if (onChange_) {
|
|
onChange_();
|
|
}
|
|
emit(Api::Events::UserUnsubscribed{
|
|
.uid = uid, .nickname = nickname, .clientId = clientId});
|
|
return "You are now unsubscribed from the bot's audio output.";
|
|
}
|
|
return "You were not subscribed.";
|
|
}
|
|
|
|
Api::Result<std::string>
|
|
SubscriptionService::unsubscribeByUid(const std::string& uid)
|
|
{
|
|
if (!manager_) {
|
|
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
|
"Subscription manager not initialized");
|
|
}
|
|
|
|
const bool removed = manager_->unsubscribe(uid);
|
|
if (removed) {
|
|
pauseIfAudienceGone();
|
|
if (onChange_) {
|
|
onChange_();
|
|
}
|
|
return "Subscriber removed successfully.";
|
|
}
|
|
return "Subscriber not found.";
|
|
}
|
|
|
|
Api::Result<std::string> SubscriptionService::clearAllSubscriptions()
|
|
{
|
|
if (!manager_) {
|
|
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
|
"Subscription manager not initialized");
|
|
}
|
|
|
|
const bool cleared = manager_->clearAll();
|
|
if (cleared) {
|
|
// Audience is definitionally gone now.
|
|
if (pauseOnEmpty_.load() && requestPause_ && requestPause_()) {
|
|
pausedForNoSubscribers_ = true;
|
|
}
|
|
if (onChange_) {
|
|
onChange_();
|
|
}
|
|
return "All subscriptions cleared.";
|
|
}
|
|
return "Failed to clear subscriptions.";
|
|
}
|
|
|
|
bool SubscriptionService::isSubscribed(uint64_t clientId) const
|
|
{
|
|
if (!manager_ || !clientInfo_) {
|
|
return false;
|
|
}
|
|
auto clientInfo = clientInfo_(clientId);
|
|
if (!clientInfo) {
|
|
return false;
|
|
}
|
|
return manager_->isSubscribed(clientInfo->first);
|
|
}
|
|
|
|
bool SubscriptionService::hasSubscribers() const
|
|
{
|
|
return manager_ && manager_->hasSubscribers();
|
|
}
|
|
|
|
bool SubscriptionService::hasOnlineSubscribers() const
|
|
{
|
|
if (!manager_ || !uidToClientId_) {
|
|
return false;
|
|
}
|
|
for (const auto& uid : manager_->getSubscribedUids()) {
|
|
if (uidToClientId_(uid).has_value()) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::set<uint64_t> SubscriptionService::getSubscribedClients() const
|
|
{
|
|
std::set<uint64_t> clientIds;
|
|
if (!manager_ || !uidToClientId_) {
|
|
return clientIds;
|
|
}
|
|
for (const auto& uid : manager_->getSubscribedUids()) {
|
|
if (auto clientId = uidToClientId_(uid)) {
|
|
clientIds.insert(*clientId);
|
|
}
|
|
}
|
|
return clientIds;
|
|
}
|
|
|
|
std::set<std::string> SubscriptionService::getOnlineSubscriberUids() const
|
|
{
|
|
std::set<std::string> onlineUids;
|
|
if (!manager_ || !uidToClientId_) {
|
|
return onlineUids;
|
|
}
|
|
for (const auto& uid : manager_->getSubscribedUids()) {
|
|
if (uidToClientId_(uid).has_value()) {
|
|
onlineUids.insert(uid);
|
|
}
|
|
}
|
|
return onlineUids;
|
|
}
|
|
|
|
std::vector<SubscriptionManager::Subscriber>
|
|
SubscriptionService::getSubscribers() const
|
|
{
|
|
if (!manager_) {
|
|
return {};
|
|
}
|
|
return manager_->getSubscribers();
|
|
}
|
|
|
|
bool SubscriptionService::onClientConnected(uint64_t clientId)
|
|
{
|
|
if (!manager_ || !clientInfo_) {
|
|
return false;
|
|
}
|
|
|
|
auto clientInfo = clientInfo_(clientId);
|
|
if (!clientInfo) {
|
|
return false;
|
|
}
|
|
|
|
const auto& [uid, nickname] = *clientInfo;
|
|
if (!manager_->isSubscribed(uid)) {
|
|
return false;
|
|
}
|
|
|
|
Void::Log::info(
|
|
"SubscriptionService", "{}",
|
|
std::format("Subscribed user {} ({}) connected, moving to online",
|
|
nickname, uid));
|
|
|
|
manager_->updateNickname(uid, nickname);
|
|
|
|
if (onChange_) {
|
|
onChange_();
|
|
}
|
|
|
|
if (pausedForNoSubscribers_.load() && resumeOnReturn_.load() &&
|
|
requestResume_) {
|
|
if (requestResume_()) {
|
|
pausedForNoSubscribers_ = false;
|
|
}
|
|
return false; // resumed; no auto-start needed
|
|
}
|
|
|
|
return true; // subscribed user online; caller may auto-start playback
|
|
}
|
|
|
|
void SubscriptionService::onClientDisconnected(uint64_t clientId)
|
|
{
|
|
if (!manager_ || !clientInfo_) {
|
|
return;
|
|
}
|
|
|
|
auto clientInfo = clientInfo_(clientId);
|
|
if (!clientInfo) {
|
|
return;
|
|
}
|
|
|
|
const auto& [uid, nickname] = *clientInfo;
|
|
if (!manager_->isSubscribed(uid)) {
|
|
return;
|
|
}
|
|
|
|
Void::Log::info(
|
|
"SubscriptionService", "{}",
|
|
std::format("Subscribed user {} ({}) disconnected, moving offline",
|
|
nickname, uid));
|
|
|
|
if (onChange_) {
|
|
onChange_();
|
|
}
|
|
pauseIfAudienceGone();
|
|
}
|
|
|
|
void SubscriptionService::pauseIfAudienceGone()
|
|
{
|
|
if (!pauseOnEmpty_.load() || hasOnlineSubscribers()) {
|
|
return;
|
|
}
|
|
if (requestPause_ && requestPause_()) {
|
|
Void::Log::info("SubscriptionService", "{}",
|
|
"No online subscribers left, paused playback");
|
|
pausedForNoSubscribers_ = true;
|
|
}
|
|
}
|
|
|
|
} // namespace Echoion::Core
|