refactor(liveupdates): use WebSocketPool over websocketpp (#6308)

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2025-11-08 13:46:10 +01:00
committed by GitHub
parent 6aeb1ca6a7
commit 7f393e2401
26 changed files with 984 additions and 973 deletions
+54 -69
View File
@@ -1,105 +1,90 @@
#include "providers/bttv/BttvLiveUpdates.hpp"
#include "common/Literals.hpp"
#include "liveupdates/BttvLiveUpdateClient.hpp"
#include "providers/liveupdates/BasicPubSubManager.hpp"
#include <QJsonDocument>
#include <unordered_set>
#include <utility>
namespace chatterino {
using namespace chatterino::literals;
using namespace Qt::StringLiterals;
BttvLiveUpdates::BttvLiveUpdates(QString host)
class BttvLiveUpdatesPrivate
: public BasicPubSubManager<BttvLiveUpdatesPrivate, BttvLiveUpdateClient>
{
public:
BttvLiveUpdatesPrivate(BttvLiveUpdates &parent, QString host);
~BttvLiveUpdatesPrivate() override;
BttvLiveUpdatesPrivate(const BttvLiveUpdatesPrivate &) = delete;
BttvLiveUpdatesPrivate(const BttvLiveUpdatesPrivate &&) = delete;
BttvLiveUpdatesPrivate &operator=(const BttvLiveUpdatesPrivate &) = delete;
BttvLiveUpdatesPrivate &operator=(const BttvLiveUpdatesPrivate &&) = delete;
std::shared_ptr<BttvLiveUpdateClient> makeClient();
// Contains all joined Twitch channel-ids
std::unordered_set<QString> joinedChannels;
BttvLiveUpdates &parent;
friend BasicPubSubManager<BttvLiveUpdates, BttvLiveUpdateClient>;
friend BttvLiveUpdates;
};
BttvLiveUpdatesPrivate::BttvLiveUpdatesPrivate(BttvLiveUpdates &parent,
QString host)
: BasicPubSubManager(std::move(host), u"BTTV"_s)
, parent(parent)
{
}
BttvLiveUpdates::~BttvLiveUpdates()
BttvLiveUpdatesPrivate::~BttvLiveUpdatesPrivate()
{
this->stop();
}
std::shared_ptr<BttvLiveUpdateClient> BttvLiveUpdatesPrivate::makeClient()
{
return std::make_shared<BttvLiveUpdateClient>(this->parent);
}
BttvLiveUpdates::BttvLiveUpdates(QString host)
: private_(std::make_unique<BttvLiveUpdatesPrivate>(*this, std::move(host)))
{
}
BttvLiveUpdates::~BttvLiveUpdates() = default;
void BttvLiveUpdates::joinChannel(const QString &channelID,
const QString &userID)
{
if (this->joinedChannels_.insert(channelID).second)
if (this->private_->joinedChannels.insert(channelID).second)
{
this->subscribe({BttvLiveUpdateSubscriptionChannel{channelID}});
this->subscribe({BttvLiveUpdateBroadcastMe{.twitchID = channelID,
.userID = userID}});
this->private_->subscribe(
{BttvLiveUpdateSubscriptionChannel{channelID}});
this->private_->subscribe({BttvLiveUpdateBroadcastMe{
.twitchID = channelID, .userID = userID}});
}
}
void BttvLiveUpdates::partChannel(const QString &id)
{
if (this->joinedChannels_.erase(id) > 0)
if (this->private_->joinedChannels.erase(id) > 0)
{
this->unsubscribe({BttvLiveUpdateSubscriptionChannel{id}});
this->private_->unsubscribe({BttvLiveUpdateSubscriptionChannel{id}});
}
}
void BttvLiveUpdates::onMessage(
websocketpp::connection_hdl /*hdl*/,
BasicPubSubManager<BttvLiveUpdateSubscription>::WebsocketMessagePtr msg)
void BttvLiveUpdates::stop()
{
const auto &payload = QString::fromStdString(msg->get_payload());
QJsonDocument jsonDoc(QJsonDocument::fromJson(payload.toUtf8()));
this->private_->stop();
}
if (jsonDoc.isNull())
{
qCDebug(chatterinoBttv) << "Failed to parse live update JSON";
return;
}
auto json = jsonDoc.object();
auto eventType = json["name"].toString();
auto eventData = json["data"].toObject();
if (eventType == "emote_create")
{
auto message = BttvLiveUpdateEmoteUpdateAddMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid add message" << json;
return;
}
this->signals_.emoteAdded.invoke(message);
}
else if (eventType == "emote_update")
{
auto message = BttvLiveUpdateEmoteUpdateAddMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid update message" << json;
return;
}
this->signals_.emoteUpdated.invoke(message);
}
else if (eventType == "emote_delete")
{
auto message = BttvLiveUpdateEmoteRemoveMessage(eventData);
if (!message.validate())
{
qCDebug(chatterinoBttv) << "Invalid deletion message" << json;
return;
}
this->signals_.emoteRemoved.invoke(message);
}
else if (eventType == "lookup_user")
{
// ignored
}
else
{
qCDebug(chatterinoBttv) << "Unhandled event:" << json;
}
const liveupdates::Diag &BttvLiveUpdates::diag() const
{
return this->private_->diag;
}
} // namespace chatterino