217 lines
5.1 KiB
C++
217 lines
5.1 KiB
C++
#include "LoggerInterface.hpp"
|
|
#include "echoion/core/SubscriptionManager.hpp"
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
SubscriptionManager::SubscriptionManager() = default;
|
|
|
|
SubscriptionManager::~SubscriptionManager() { shutdown(); }
|
|
|
|
Api::Result<void>
|
|
SubscriptionManager::initialize(std::shared_ptr<Api::Database> database)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (initialized_) {
|
|
return {};
|
|
}
|
|
|
|
database_ = std::move(database);
|
|
if (!database_) {
|
|
return std::unexpected(Api::Error(
|
|
Api::ErrorCode::ServiceUnavailable, "Database not available"));
|
|
}
|
|
|
|
// Load existing subscriptions from database
|
|
auto result = database_->getAllSubscribers();
|
|
if (!result) {
|
|
ECHOION_LOG_WARNING(std::format("Failed to load subscriptions: {}",
|
|
result.error().message));
|
|
// Not fatal - start with empty subscriptions
|
|
} else {
|
|
for (const auto& subscriber : *result) {
|
|
subscribedUids_.insert(subscriber.uid);
|
|
}
|
|
}
|
|
|
|
initialized_ = true;
|
|
ECHOION_LOG_INFO(
|
|
std::format("SubscriptionManager initialized ({} subscribers)",
|
|
subscribedUids_.size()));
|
|
return {};
|
|
}
|
|
|
|
void SubscriptionManager::shutdown()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (!initialized_) {
|
|
return;
|
|
}
|
|
|
|
initialized_ = false;
|
|
database_.reset();
|
|
subscribedUids_.clear();
|
|
ECHOION_LOG_INFO("SubscriptionManager shutdown");
|
|
}
|
|
|
|
bool SubscriptionManager::subscribe(const std::string& uid,
|
|
const std::string& nickname)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
// Check if already subscribed (ensures uniqueness)
|
|
if (subscribedUids_.contains(uid)) {
|
|
// Update nickname even if already subscribed
|
|
if (database_) {
|
|
database_->addSubscriber(uid, nickname);
|
|
}
|
|
return false; // Already subscribed
|
|
}
|
|
|
|
// Add to database first
|
|
if (database_) {
|
|
auto result = database_->addSubscriber(uid, nickname);
|
|
if (!result) {
|
|
ECHOION_LOG_ERROR(
|
|
std::format("Failed to add subscriber {} to database: {}",
|
|
uid, result.error().message));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
subscribedUids_.insert(uid);
|
|
ECHOION_LOG_INFO(
|
|
std::format("Client {} ({}) subscribed", nickname, uid));
|
|
return true;
|
|
}
|
|
|
|
bool SubscriptionManager::unsubscribe(const std::string& uid)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
bool removed = subscribedUids_.erase(uid) > 0;
|
|
if (removed) {
|
|
// Remove from database
|
|
if (database_) {
|
|
auto result = database_->removeSubscriber(uid);
|
|
if (!result) {
|
|
ECHOION_LOG_ERROR(std::format(
|
|
"Failed to remove subscriber {} from database: {}", uid,
|
|
result.error().message));
|
|
}
|
|
}
|
|
ECHOION_LOG_INFO(std::format("Client {} unsubscribed", uid));
|
|
}
|
|
return removed;
|
|
}
|
|
|
|
bool SubscriptionManager::isSubscribed(const std::string& uid) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return subscribedUids_.contains(uid);
|
|
}
|
|
|
|
std::vector<std::string> SubscriptionManager::getSubscribedUids() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return std::vector<std::string>(subscribedUids_.begin(),
|
|
subscribedUids_.end());
|
|
}
|
|
|
|
std::vector<SubscriptionManager::Subscriber>
|
|
SubscriptionManager::getSubscribers() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
std::vector<Subscriber> result;
|
|
|
|
if (!database_) {
|
|
return result;
|
|
}
|
|
|
|
auto dbSubscribers = database_->getAllSubscribers();
|
|
if (dbSubscribers) {
|
|
for (const auto& info : *dbSubscribers) {
|
|
if (subscribedUids_.contains(info.uid)) {
|
|
result.push_back({info.uid, info.nickname});
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::optional<std::string>
|
|
SubscriptionManager::getSubscriberNickname(const std::string& uid) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (!subscribedUids_.contains(uid) || !database_) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
auto subscribers = database_->getAllSubscribers();
|
|
if (subscribers) {
|
|
for (const auto& info : *subscribers) {
|
|
if (info.uid == uid) {
|
|
return info.nickname;
|
|
}
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
bool SubscriptionManager::updateNickname(const std::string& uid,
|
|
const std::string& nickname)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (!subscribedUids_.contains(uid)) {
|
|
return false;
|
|
}
|
|
|
|
if (database_) {
|
|
auto result = database_->addSubscriber(uid, nickname);
|
|
if (!result) {
|
|
ECHOION_LOG_ERROR(
|
|
std::format("Failed to update nickname for {}: {}", uid,
|
|
result.error().message));
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool SubscriptionManager::hasSubscribers() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return !subscribedUids_.empty();
|
|
}
|
|
|
|
size_t SubscriptionManager::getSubscriberCount() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return subscribedUids_.size();
|
|
}
|
|
|
|
bool SubscriptionManager::clearAll()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (database_) {
|
|
auto result = database_->clearAllSubscribers();
|
|
if (!result) {
|
|
ECHOION_LOG_ERROR(
|
|
std::format("Failed to clear all subscribers: {}",
|
|
result.error().message));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
subscribedUids_.clear();
|
|
ECHOION_LOG_INFO("All subscriptions cleared");
|
|
return true;
|
|
}
|
|
|
|
} // namespace Echoion::Core
|