2018-04-15 15:09:31 +02:00
|
|
|
#pragma once
|
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/PubsubActions.hpp"
|
|
|
|
#include "providers/twitch/TwitchAccount.hpp"
|
|
|
|
#include "providers/twitch/TwitchServer.hpp"
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
#include <rapidjson/document.h>
|
|
|
|
#include <QString>
|
|
|
|
#include <pajlada/signals/signal.hpp>
|
|
|
|
#include <websocketpp/client.hpp>
|
|
|
|
#include <websocketpp/config/asio_client.hpp>
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
using WebsocketClient =
|
|
|
|
websocketpp::client<websocketpp::config::asio_tls_client>;
|
2018-04-15 15:09:31 +02:00
|
|
|
using WebsocketHandle = websocketpp::connection_hdl;
|
|
|
|
using WebsocketErrorCode = websocketpp::lib::error_code;
|
|
|
|
|
|
|
|
#define MAX_PUBSUB_LISTENS 50
|
|
|
|
#define MAX_PUBSUB_CONNECTIONS 10
|
|
|
|
|
2018-04-28 15:48:40 +02:00
|
|
|
namespace detail {
|
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
struct Listener {
|
|
|
|
std::string topic;
|
|
|
|
bool authed;
|
|
|
|
bool persistent;
|
|
|
|
bool confirmed = false;
|
|
|
|
};
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
class PubSubClient : public std::enable_shared_from_this<PubSubClient>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PubSubClient(WebsocketClient &_websocketClient,
|
|
|
|
WebsocketHandle _handle);
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void stop();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
bool listen(rapidjson::Document &message);
|
|
|
|
void unlistenPrefix(const std::string &prefix);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
void handlePong();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
bool isListeningToTopic(const std::string &topic);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
private:
|
|
|
|
void ping();
|
|
|
|
bool send(const char *payload);
|
2018-07-06 19:23:47 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
WebsocketClient &websocketClient_;
|
|
|
|
WebsocketHandle handle_;
|
|
|
|
uint16_t numListens_ = 0;
|
2018-07-06 19:23:47 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
std::vector<Listener> listeners_;
|
2018-07-06 19:23:47 +02:00
|
|
|
|
2018-08-15 22:46:20 +02:00
|
|
|
std::atomic<bool> awaitingPong_{false};
|
|
|
|
std::atomic<bool> started_{false};
|
|
|
|
};
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-28 15:48:40 +02:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
class PubSub
|
2018-04-15 15:09:31 +02:00
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
using WebsocketMessagePtr =
|
|
|
|
websocketpp::config::asio_tls_client::message_type::ptr;
|
|
|
|
using WebsocketContextPtr =
|
|
|
|
websocketpp::lib::shared_ptr<boost::asio::ssl::context>;
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2018-08-06 21:17:03 +02:00
|
|
|
using Signal =
|
|
|
|
pajlada::Signals::Signal<T>; // type-id is vector<T, Alloc<T>>
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
WebsocketClient websocketClient;
|
|
|
|
std::unique_ptr<std::thread> mainThread;
|
|
|
|
|
|
|
|
public:
|
2018-04-28 15:48:40 +02:00
|
|
|
PubSub();
|
2018-04-28 15:20:18 +02:00
|
|
|
|
2018-04-28 15:48:40 +02:00
|
|
|
~PubSub() = delete;
|
2018-04-27 22:11:19 +02:00
|
|
|
|
2018-04-15 15:09:31 +02:00
|
|
|
enum class State {
|
|
|
|
Connected,
|
|
|
|
Disconnected,
|
|
|
|
};
|
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
void start();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
bool isConnected() const
|
2018-04-15 15:09:31 +02:00
|
|
|
{
|
|
|
|
return this->state == State::Connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
pajlada::Signals::NoArgSignal connected;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
struct {
|
|
|
|
Signal<ClearChatAction> chatCleared;
|
|
|
|
Signal<ModeChangedAction> modeChanged;
|
|
|
|
Signal<ModerationStateAction> moderationStateChanged;
|
|
|
|
|
|
|
|
Signal<BanAction> userBanned;
|
|
|
|
Signal<UnbanAction> userUnbanned;
|
2019-01-20 01:02:04 +01:00
|
|
|
|
|
|
|
Signal<AutomodAction> automodMessage;
|
2018-04-15 15:09:31 +02:00
|
|
|
} moderation;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
// Parsing should be done in PubSubManager as well,
|
|
|
|
// but for now we just send the raw data
|
|
|
|
Signal<const rapidjson::Value &> received;
|
|
|
|
Signal<const rapidjson::Value &> sent;
|
|
|
|
} whisper;
|
2018-06-26 17:42:35 +02:00
|
|
|
} signals_;
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-06-26 17:06:17 +02:00
|
|
|
void listenToWhispers(std::shared_ptr<TwitchAccount> account);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
void unlistenAllModerationActions();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void listenToChannelModerationActions(
|
|
|
|
const QString &channelID, std::shared_ptr<TwitchAccount> account);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<rapidjson::Document>> requests;
|
|
|
|
|
|
|
|
private:
|
2018-08-06 21:17:03 +02:00
|
|
|
void listenToTopic(const std::string &topic,
|
|
|
|
std::shared_ptr<TwitchAccount> account);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
void listen(rapidjson::Document &&msg);
|
|
|
|
bool tryListen(rapidjson::Document &msg);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
bool isListeningToTopic(const std::string &topic);
|
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
void addClient();
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
State state = State::Connected;
|
|
|
|
|
2018-04-28 15:48:40 +02:00
|
|
|
std::map<WebsocketHandle, std::shared_ptr<detail::PubSubClient>,
|
|
|
|
std::owner_less<WebsocketHandle>>
|
2018-04-15 15:09:31 +02:00
|
|
|
clients;
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
std::unordered_map<std::string, std::function<void(const rapidjson::Value &,
|
|
|
|
const QString &)>>
|
2018-04-15 15:09:31 +02:00
|
|
|
moderationActionHandlers;
|
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
void onMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg);
|
|
|
|
void onConnectionOpen(websocketpp::connection_hdl hdl);
|
|
|
|
void onConnectionClose(websocketpp::connection_hdl hdl);
|
|
|
|
WebsocketContextPtr onTLSInit(websocketpp::connection_hdl hdl);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
void handleListenResponse(const rapidjson::Document &msg);
|
|
|
|
void handleMessageResponse(const rapidjson::Value &data);
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
void runThread();
|
2018-04-15 15:09:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace chatterino
|