move pubsub stuff to the providers/twitch namespace and folder

This commit is contained in:
Rasmus Karlsson 2018-04-28 15:48:40 +02:00
parent ebbe8dcfae
commit 0f22d9d002
12 changed files with 104 additions and 81 deletions

View file

@ -104,6 +104,7 @@ SOURCES += \
src/providers/twitch/twitchchannel.cpp \
src/providers/twitch/twitchmessagebuilder.cpp \
src/providers/twitch/twitchserver.cpp \
src/providers/twitch/pubsub.cpp \
src/singletons/accountmanager.cpp \
src/singletons/commandmanager.cpp \
src/singletons/emotemanager.cpp \
@ -175,10 +176,9 @@ SOURCES += \
src/widgets/helper/debugpopup.cpp \
src/util/debugcount.cpp \
src/singletons/nativemessagingmanager.cpp \
src/singletons/pubsubmanager.cpp \
src/util/rapidjson-helpers.cpp \
src/singletons/helper/pubsubhelpers.cpp \
src/singletons/helper/pubsubactions.cpp \
src/providers/twitch/pubsubhelpers.cpp \
src/providers/twitch/pubsubactions.cpp \
src/widgets/selectchanneldialog.cpp \
src/singletons/updatemanager.cpp \
src/widgets/lastruncrashdialog.cpp \
@ -213,6 +213,7 @@ HEADERS += \
src/providers/twitch/twitchchannel.hpp \
src/providers/twitch/twitchmessagebuilder.hpp \
src/providers/twitch/twitchserver.hpp \
src/providers/twitch/pubsub.hpp \
src/singletons/accountmanager.hpp \
src/singletons/commandmanager.hpp \
src/singletons/emotemanager.hpp \
@ -304,8 +305,8 @@ HEADERS += \
src/singletons/nativemessagingmanager.hpp \
src/singletons/pubsubmanager.hpp \
src/util/rapidjson-helpers.hpp \
src/singletons/helper/pubsubhelpers.hpp \
src/singletons/helper/pubsubactions.hpp \
src/providers/twitch/pubsubhelpers.hpp \
src/providers/twitch/pubsubactions.hpp \
src/widgets/selectchanneldialog.hpp \
src/singletons/updatemanager.hpp \
src/widgets/lastruncrashdialog.hpp \

View file

@ -1,5 +1,6 @@
#include "application.hpp"
#include "providers/twitch/pubsub.hpp"
#include "providers/twitch/twitchserver.hpp"
#include "singletons/accountmanager.hpp"
#include "singletons/commandmanager.hpp"
@ -8,7 +9,6 @@
#include "singletons/loggingmanager.hpp"
#include "singletons/nativemessagingmanager.hpp"
#include "singletons/pathmanager.hpp"
#include "singletons/pubsubmanager.hpp"
#include "singletons/resourcemanager.hpp"
#include "singletons/settingsmanager.hpp"
#include "singletons/thememanager.hpp"
@ -66,12 +66,12 @@ void Application::construct()
this->commands = new singletons::CommandManager;
this->accounts = new singletons::AccountManager;
this->emotes = new singletons::EmoteManager;
this->pubsub = new singletons::PubSubManager;
this->settings = new singletons::SettingManager;
this->fonts = new singletons::FontManager;
this->resources = new singletons::ResourceManager;
this->twitch.server = new providers::twitch::TwitchServer;
this->twitch.pubsub = new providers::twitch::PubSub;
}
void Application::instantiate(int argc, char **argv)
@ -106,29 +106,29 @@ void Application::initialize()
this->nativeMessaging->openGuiMessageQueue();
this->pubsub->sig.whisper.sent.connect([](const auto &msg) {
this->twitch.pubsub->sig.whisper.sent.connect([](const auto &msg) {
debug::Log("WHISPER SENT LOL"); //
});
this->pubsub->sig.whisper.received.connect([](const auto &msg) {
this->twitch.pubsub->sig.whisper.received.connect([](const auto &msg) {
debug::Log("WHISPER RECEIVED LOL"); //
});
this->pubsub->sig.moderation.chatCleared.connect([&](const auto &action) {
this->twitch.pubsub->sig.moderation.chatCleared.connect([&](const auto &action) {
debug::Log("Chat cleared by {}", action.source.name); //
});
this->pubsub->sig.moderation.modeChanged.connect([&](const auto &action) {
this->twitch.pubsub->sig.moderation.modeChanged.connect([&](const auto &action) {
debug::Log("Mode {} was turned {} by {} (duration {})", (int &)action.mode,
(bool &)action.state, action.source.name, action.args.duration);
});
this->pubsub->sig.moderation.moderationStateChanged.connect([&](const auto &action) {
this->twitch.pubsub->sig.moderation.moderationStateChanged.connect([&](const auto &action) {
debug::Log("User {} was {} by {}", action.target.id, action.modded ? "modded" : "unmodded",
action.source.name);
});
this->pubsub->sig.moderation.userBanned.connect([&](const auto &action) {
this->twitch.pubsub->sig.moderation.userBanned.connect([&](const auto &action) {
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty()) {
@ -140,7 +140,7 @@ void Application::initialize()
util::postToThread([chan, msg] { chan->addMessage(msg); });
});
this->pubsub->sig.moderation.userUnbanned.connect([&](const auto &action) {
this->twitch.pubsub->sig.moderation.userUnbanned.connect([&](const auto &action) {
auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID);
if (chan->isEmpty()) {
@ -152,14 +152,14 @@ void Application::initialize()
util::postToThread([chan, msg] { chan->addMessage(msg); });
});
this->pubsub->Start();
this->twitch.pubsub->Start();
auto RequestModerationActions = [=]() {
this->pubsub->UnlistenAllModerationActions();
this->twitch.pubsub->UnlistenAllModerationActions();
// TODO(pajlada): Unlisten to all authed topics instead of only moderation topics
// this->pubsub->UnlistenAllAuthedTopics();
// this->twitch.pubsub->UnlistenAllAuthedTopics();
this->pubsub->ListenToWhispers(this->accounts->Twitch.getCurrent()); //
this->twitch.pubsub->ListenToWhispers(this->accounts->Twitch.getCurrent()); //
};
this->accounts->Twitch.userChanged.connect(RequestModerationActions);

View file

@ -11,6 +11,7 @@ namespace providers {
namespace twitch {
class TwitchServer;
class PubSub;
} // namespace twitch
} // namespace providers
@ -24,7 +25,6 @@ class PathManager;
class CommandManager;
class AccountManager;
class EmoteManager;
class PubSubManager;
class NativeMessagingManager;
class SettingManager;
class FontManager;
@ -56,7 +56,6 @@ public:
singletons::CommandManager *commands = nullptr;
singletons::AccountManager *accounts = nullptr;
singletons::EmoteManager *emotes = nullptr;
singletons::PubSubManager *pubsub = nullptr;
singletons::NativeMessagingManager *nativeMessaging = nullptr;
singletons::SettingManager *settings = nullptr;
singletons::FontManager *fonts = nullptr;
@ -64,7 +63,8 @@ public:
/// Provider-specific
struct {
providers::twitch::TwitchServer *server;
providers::twitch::TwitchServer *server = nullptr;
providers::twitch::PubSub *pubsub = nullptr;
} twitch;
void save();

View file

@ -1,6 +1,6 @@
#include "messages/message.hpp"
#include "messageelement.hpp"
#include "singletons/helper/pubsubactions.hpp"
#include "providers/twitch/pubsubactions.hpp"
#include "util/irchelpers.hpp"
using SBHighlight = chatterino::widgets::ScrollbarHighlight;
@ -79,7 +79,7 @@ MessagePtr Message::createTimeoutMessage(const QString &username, const QString
return message;
}
MessagePtr Message::createTimeoutMessage(const singletons::BanAction &action, uint32_t count)
MessagePtr Message::createTimeoutMessage(const providers::twitch::BanAction &action, uint32_t count)
{
MessagePtr msg(new Message);
@ -89,7 +89,7 @@ MessagePtr Message::createTimeoutMessage(const singletons::BanAction &action, ui
msg->timeoutUser = action.target.name;
msg->count = count;
msg->banAction.reset(new singletons::BanAction(action));
msg->banAction.reset(new providers::twitch::BanAction(action));
QString text;
@ -130,7 +130,7 @@ MessagePtr Message::createTimeoutMessage(const singletons::BanAction &action, ui
return msg;
}
MessagePtr Message::createUntimeoutMessage(const singletons::UnbanAction &action)
MessagePtr Message::createUntimeoutMessage(const providers::twitch::UnbanAction &action)
{
MessagePtr msg(new Message);

View file

@ -1,7 +1,7 @@
#pragma once
#include "messages/messageelement.hpp"
#include "singletons/helper/pubsubactions.hpp"
#include "providers/twitch/pubsubactions.hpp"
#include "util/flagsenum.hpp"
#include "widgets/helper/scrollbarhighlight.hpp"
@ -50,7 +50,7 @@ struct Message {
QString localizedName;
QString timeoutUser;
std::unique_ptr<singletons::BanAction> banAction;
std::unique_ptr<providers::twitch::BanAction> banAction;
uint32_t count = 1;
// Messages should not be added after the message is done initializing.
@ -70,9 +70,10 @@ public:
const QString &durationInSeconds,
const QString &reason, bool multipleTimes);
static std::shared_ptr<Message> createTimeoutMessage(const singletons::BanAction &action,
static std::shared_ptr<Message> createTimeoutMessage(const providers::twitch::BanAction &action,
uint32_t count = 1);
static std::shared_ptr<Message> createUntimeoutMessage(const singletons::UnbanAction &action);
static std::shared_ptr<Message> createUntimeoutMessage(
const providers::twitch::UnbanAction &action);
};
using MessagePtr = std::shared_ptr<Message>;

View file

@ -1,9 +1,9 @@
#include "singletons/pubsubmanager.hpp"
#include "providers/twitch/pubsub.hpp"
#include "debug/log.hpp"
#include "providers/twitch/pubsubactions.hpp"
#include "providers/twitch/pubsubhelpers.hpp"
#include "singletons/accountmanager.hpp"
#include "singletons/helper/pubsubactions.hpp"
#include "singletons/helper/pubsubhelpers.hpp"
#include "util/rapidjson-helpers.hpp"
#include <rapidjson/error/en.h>
@ -18,12 +18,15 @@ using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
namespace chatterino {
namespace singletons {
namespace providers {
namespace twitch {
static const char *pingPayload = "{\"type\":\"PING\"}";
static std::map<std::string, std::string> sentMessages;
namespace detail {
PubSubClient::PubSubClient(WebsocketClient &_websocketClient, WebsocketHandle _handle)
: websocketClient(_websocketClient)
, handle(_handle)
@ -170,9 +173,11 @@ bool PubSubClient::Send(const char *payload)
return true;
}
PubSubManager::PubSubManager()
} // namespace detail
PubSub::PubSub()
{
qDebug() << "init PubSubManager";
qDebug() << "init PubSub";
this->moderationActionHandlers["clear"] = [this](const auto &data, const auto &roomID) {
ClearChatAction action(data, roomID);
@ -420,17 +425,17 @@ PubSubManager::PubSubManager()
this->websocketClient.init_asio();
// SSL Handshake
this->websocketClient.set_tls_init_handler(bind(&PubSubManager::OnTLSInit, this, ::_1));
this->websocketClient.set_tls_init_handler(bind(&PubSub::OnTLSInit, this, ::_1));
this->websocketClient.set_message_handler(bind(&PubSubManager::OnMessage, this, ::_1, ::_2));
this->websocketClient.set_open_handler(bind(&PubSubManager::OnConnectionOpen, this, ::_1));
this->websocketClient.set_close_handler(bind(&PubSubManager::OnConnectionClose, this, ::_1));
this->websocketClient.set_message_handler(bind(&PubSub::OnMessage, this, ::_1, ::_2));
this->websocketClient.set_open_handler(bind(&PubSub::OnConnectionOpen, this, ::_1));
this->websocketClient.set_close_handler(bind(&PubSub::OnConnectionClose, this, ::_1));
// Add an initial client
this->AddClient();
}
void PubSubManager::AddClient()
void PubSub::AddClient()
{
websocketpp::lib::error_code ec;
auto con = this->websocketClient.get_connection(TWITCH_PUBSUB_URL, ec);
@ -443,12 +448,12 @@ void PubSubManager::AddClient()
this->websocketClient.connect(con);
}
void PubSubManager::Start()
void PubSub::Start()
{
this->mainThread.reset(new std::thread(std::bind(&PubSubManager::RunThread, this)));
this->mainThread.reset(new std::thread(std::bind(&PubSub::RunThread, this)));
}
void PubSubManager::ListenToWhispers(std::shared_ptr<providers::twitch::TwitchAccount> account)
void PubSub::ListenToWhispers(std::shared_ptr<providers::twitch::TwitchAccount> account)
{
assert(account != nullptr);
@ -467,7 +472,7 @@ void PubSubManager::ListenToWhispers(std::shared_ptr<providers::twitch::TwitchAc
}
}
void PubSubManager::UnlistenAllModerationActions()
void PubSub::UnlistenAllModerationActions()
{
for (const auto &p : this->clients) {
const auto &client = p.second;
@ -475,7 +480,7 @@ void PubSubManager::UnlistenAllModerationActions()
}
}
void PubSubManager::ListenToChannelModerationActions(
void PubSub::ListenToChannelModerationActions(
const QString &channelID, std::shared_ptr<providers::twitch::TwitchAccount> account)
{
assert(!channelID.isEmpty());
@ -495,15 +500,15 @@ void PubSubManager::ListenToChannelModerationActions(
this->listenToTopic(topic, account);
}
void PubSubManager::listenToTopic(const std::string &topic,
std::shared_ptr<providers::twitch::TwitchAccount> account)
void PubSub::listenToTopic(const std::string &topic,
std::shared_ptr<providers::twitch::TwitchAccount> account)
{
auto message = CreateListenMessage({topic}, account);
this->Listen(std::move(message));
}
void PubSubManager::Listen(rapidjson::Document &&msg)
void PubSub::Listen(rapidjson::Document &&msg)
{
if (this->TryListen(msg)) {
debug::Log("Successfully listened!");
@ -514,7 +519,7 @@ void PubSubManager::Listen(rapidjson::Document &&msg)
this->requests.emplace_back(std::make_unique<rapidjson::Document>(std::move(msg)));
}
bool PubSubManager::TryListen(rapidjson::Document &msg)
bool PubSub::TryListen(rapidjson::Document &msg)
{
debug::Log("TryListen with {} clients", this->clients.size());
for (const auto &p : this->clients) {
@ -527,7 +532,7 @@ bool PubSubManager::TryListen(rapidjson::Document &msg)
return false;
}
bool PubSubManager::isListeningToTopic(const std::string &topic)
bool PubSub::isListeningToTopic(const std::string &topic)
{
for (const auto &p : this->clients) {
const auto &client = p.second;
@ -539,7 +544,7 @@ bool PubSubManager::isListeningToTopic(const std::string &topic)
return false;
}
void PubSubManager::OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr websocketMessage)
void PubSub::OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr websocketMessage)
{
const std::string &payload = websocketMessage->get_payload();
@ -596,9 +601,9 @@ void PubSubManager::OnMessage(websocketpp::connection_hdl hdl, WebsocketMessageP
}
}
void PubSubManager::OnConnectionOpen(WebsocketHandle hdl)
void PubSub::OnConnectionOpen(WebsocketHandle hdl)
{
auto client = std::make_shared<PubSubClient>(this->websocketClient, hdl);
auto client = std::make_shared<detail::PubSubClient>(this->websocketClient, hdl);
// We separate the starting from the constructor because we will want to use shared_from_this
client->Start();
@ -608,7 +613,7 @@ void PubSubManager::OnConnectionOpen(WebsocketHandle hdl)
this->connected.invoke();
}
void PubSubManager::OnConnectionClose(WebsocketHandle hdl)
void PubSub::OnConnectionClose(WebsocketHandle hdl)
{
auto clientIt = this->clients.find(hdl);
@ -625,7 +630,7 @@ void PubSubManager::OnConnectionClose(WebsocketHandle hdl)
this->connected.invoke();
}
PubSubManager::WebsocketContextPtr PubSubManager::OnTLSInit(websocketpp::connection_hdl hdl)
PubSub::WebsocketContextPtr PubSub::OnTLSInit(websocketpp::connection_hdl hdl)
{
WebsocketContextPtr ctx(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1));
@ -640,7 +645,7 @@ PubSubManager::WebsocketContextPtr PubSubManager::OnTLSInit(websocketpp::connect
return ctx;
}
void PubSubManager::HandleListenResponse(const rapidjson::Document &msg)
void PubSub::HandleListenResponse(const rapidjson::Document &msg)
{
std::string error;
@ -661,7 +666,7 @@ void PubSubManager::HandleListenResponse(const rapidjson::Document &msg)
}
}
void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData)
void PubSub::HandleMessageResponse(const rapidjson::Value &outerData)
{
QString topic;
@ -733,12 +738,13 @@ void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData)
}
}
void PubSubManager::RunThread()
void PubSub::RunThread()
{
debug::Log("Start pubsub manager thread");
this->websocketClient.run();
debug::Log("Done with pubsub manager thread");
}
} // namespace singletons
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -1,8 +1,8 @@
#pragma once
#include "providers/twitch/pubsubactions.hpp"
#include "providers/twitch/twitchaccount.hpp"
#include "providers/twitch/twitchserver.hpp"
#include "singletons/helper/pubsubactions.hpp"
#include <rapidjson/document.h>
#include <QString>
@ -21,7 +21,8 @@
#include <vector>
namespace chatterino {
namespace singletons {
namespace providers {
namespace twitch {
using WebsocketClient = websocketpp::client<websocketpp::config::asio_tls_client>;
using WebsocketHandle = websocketpp::connection_hdl;
@ -30,6 +31,8 @@ using WebsocketErrorCode = websocketpp::lib::error_code;
#define MAX_PUBSUB_LISTENS 50
#define MAX_PUBSUB_CONNECTIONS 10
namespace detail {
struct Listener {
std::string topic;
bool authed;
@ -66,7 +69,9 @@ private:
bool Send(const char *payload);
};
class PubSubManager
} // namespace detail
class PubSub
{
using WebsocketMessagePtr = websocketpp::config::asio_tls_client::message_type::ptr;
using WebsocketContextPtr = websocketpp::lib::shared_ptr<boost::asio::ssl::context>;
@ -78,9 +83,9 @@ class PubSubManager
std::unique_ptr<std::thread> mainThread;
public:
PubSubManager();
PubSub();
~PubSubManager() = delete;
~PubSub() = delete;
enum class State {
Connected,
@ -136,7 +141,8 @@ private:
State state = State::Connected;
std::map<WebsocketHandle, std::shared_ptr<PubSubClient>, std::owner_less<WebsocketHandle>>
std::map<WebsocketHandle, std::shared_ptr<detail::PubSubClient>,
std::owner_less<WebsocketHandle>>
clients;
std::unordered_map<std::string, std::function<void(const rapidjson::Value &, const QString &)>>
@ -153,5 +159,6 @@ private:
void RunThread();
};
} // namespace singletons
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -1,9 +1,10 @@
#include "singletons/helper/pubsubactions.hpp"
#include "providers/twitch/pubsubactions.hpp"
#include "singletons/helper/pubsubhelpers.hpp"
#include "providers/twitch/pubsubhelpers.hpp"
namespace chatterino {
namespace singletons {
namespace providers {
namespace twitch {
PubSubAction::PubSubAction(const rapidjson::Value &data, const QString &_roomID)
: timestamp(std::chrono::steady_clock::now())
@ -12,5 +13,6 @@ PubSubAction::PubSubAction(const rapidjson::Value &data, const QString &_roomID)
getCreatedByUser(data, this->source);
}
} // namespace singletons
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -1,14 +1,14 @@
#pragma once
#include <rapidjson/document.h>
#include <QString>
#include <chrono>
#include <cinttypes>
namespace chatterino {
namespace singletons {
namespace providers {
namespace twitch {
struct ActionUser {
QString id;
@ -91,5 +91,6 @@ struct ModerationStateAction : PubSubAction {
bool modded;
};
} // namespace singletons
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -1,11 +1,12 @@
#include "singletons/helper/pubsubhelpers.hpp"
#include "providers/twitch/pubsubhelpers.hpp"
#include "providers/twitch/pubsubactions.hpp"
#include "singletons/accountmanager.hpp"
#include "singletons/helper/pubsubactions.hpp"
#include "util/rapidjson-helpers.hpp"
namespace chatterino {
namespace singletons {
namespace providers {
namespace twitch {
const rapidjson::Value &getArgs(const rapidjson::Value &data)
{
@ -85,5 +86,6 @@ rapidjson::Document CreateUnlistenMessage(const std::vector<std::string> &topics
return msg;
}
} // namespace singletons
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -10,7 +10,8 @@
#include <memory>
namespace chatterino {
namespace singletons {
namespace providers {
namespace twitch {
struct ActionUser;
@ -59,5 +60,6 @@ void RunAfter(std::shared_ptr<boost::asio::steady_timer> timer, Duration duratio
});
}
} // namespace singletons
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -1,12 +1,13 @@
#include "providers/twitch/twitchchannel.hpp"
#include "common.hpp"
#include "debug/log.hpp"
#include "messages/message.hpp"
#include "providers/twitch/pubsub.hpp"
#include "providers/twitch/twitchmessagebuilder.hpp"
#include "singletons/accountmanager.hpp"
#include "singletons/emotemanager.hpp"
#include "singletons/ircmanager.hpp"
#include "singletons/pubsubmanager.hpp"
#include "singletons/settingsmanager.hpp"
#include "util/posttothread.hpp"
#include "util/urlfetch.hpp"
@ -57,7 +58,7 @@ TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection
auto account = app->accounts->Twitch.getCurrent();
if (account && !account->getUserId().isEmpty()) {
app->pubsub->ListenToChannelModerationActions(this->roomID, account);
app->twitch.pubsub->ListenToChannelModerationActions(this->roomID, account);
}
};