PubSub system (#346)

* Add websocketpp dependency

* Initial pubsub commit

Renamed selection min and max variables to selectionMin and selectionMax
to bypass windows min/max macros being stupid.

TwitchAccount is now initialized with its User ID. It cannot be changed
after it has been initialized.

* Update openssl folder

* Update installation instructions

* Split up websocketpp dependency to its own code only and openssl.pri

* Add missing include to asio steady_timer

* Update dependencies for linux
This commit is contained in:
pajlada
2018-04-15 15:09:31 +02:00
committed by GitHub
parent d5097e71a3
commit 23cf8cc484
33 changed files with 1502 additions and 68 deletions
+15
View File
@@ -0,0 +1,15 @@
#include "singletons/helper/pubsubactions.hpp"
#include "singletons/helper/pubsubhelpers.hpp"
namespace chatterino {
namespace singletons {
PubSubAction::PubSubAction(const rapidjson::Value &data)
: timestamp(std::chrono::steady_clock::now())
{
getCreatedByUser(data, this->source);
}
} // namespace singletons
} // namespace chatterino
+109
View File
@@ -0,0 +1,109 @@
#pragma once
#include <rapidjson/document.h>
#include <QString>
#include <chrono>
#include <cinttypes>
namespace chatterino {
namespace singletons {
struct ActionUser {
QString id;
QString name;
};
struct PubSubAction {
PubSubAction(const rapidjson::Value &data);
ActionUser source;
std::chrono::steady_clock::time_point timestamp;
};
// Used when a chat mode (i.e. slowmode, subscribers only mode) is enabled or disabled
struct ModeChangedAction : PubSubAction {
ModeChangedAction(const rapidjson::Value &data)
: PubSubAction(data)
{
}
enum Mode {
Unknown,
Slow,
R9K,
SubscribersOnly,
EmoteOnly,
} mode;
// Whether the mode was turned on or off
enum State {
Off,
On,
} state;
union {
uint32_t duration;
} args;
};
struct TimeoutAction : PubSubAction {
TimeoutAction(const rapidjson::Value &data)
: PubSubAction(data)
{
}
ActionUser target;
QString reason;
uint32_t duration;
};
struct BanAction : PubSubAction {
BanAction(const rapidjson::Value &data)
: PubSubAction(data)
{
}
ActionUser target;
QString reason;
};
struct UnbanAction : PubSubAction {
UnbanAction(const rapidjson::Value &data)
: PubSubAction(data)
{
}
ActionUser target;
enum {
Banned,
TimedOut,
} previousState;
};
struct ClearChatAction : PubSubAction {
ClearChatAction(const rapidjson::Value &data)
: PubSubAction(data)
{
}
};
struct ModerationStateAction : PubSubAction {
ModerationStateAction(const rapidjson::Value &data)
: PubSubAction(data)
{
}
ActionUser target;
// true = modded
// false = unmodded
bool modded;
};
} // namespace singletons
} // namespace chatterino
+91
View File
@@ -0,0 +1,91 @@
#include "singletons/helper/pubsubhelpers.hpp"
#include "singletons/accountmanager.hpp"
#include "singletons/helper/pubsubactions.hpp"
#include "util/rapidjson-helpers.hpp"
namespace chatterino {
namespace singletons {
const rapidjson::Value &getArgs(const rapidjson::Value &data)
{
if (!data.HasMember("args")) {
throw std::runtime_error("Missing member args");
}
const auto &args = data["args"];
if (!args.IsArray()) {
throw std::runtime_error("args must be an array");
}
return args;
}
bool getCreatedByUser(const rapidjson::Value &data, ActionUser &user)
{
return rj::getSafe(data, "created_by", user.name) &&
rj::getSafe(data, "created_by_user_id", user.id);
}
bool getTargetUser(const rapidjson::Value &data, ActionUser &user)
{
return rj::getSafe(data, "target_user_id", user.id);
}
std::string Stringify(const rapidjson::Value &v)
{
return pajlada::Settings::SettingManager::stringify(v);
}
rapidjson::Document CreateListenMessage(const std::vector<std::string> &topicsVec,
std::shared_ptr<providers::twitch::TwitchAccount> account)
{
rapidjson::Document msg(rapidjson::kObjectType);
auto &a = msg.GetAllocator();
rj::set(msg, "type", "LISTEN");
rapidjson::Value data(rapidjson::kObjectType);
if (account) {
rj::set(data, "auth_token", account->getOAuthToken(), a);
}
rapidjson::Value topics(rapidjson::kArrayType);
for (const auto &topic : topicsVec) {
rj::add(topics, topic, a);
}
rj::set(data, "topics", topics, a);
rj::set(msg, "data", data);
return msg;
}
rapidjson::Document CreateUnlistenMessage(const std::vector<std::string> &topicsVec)
{
rapidjson::Document msg(rapidjson::kObjectType);
auto &a = msg.GetAllocator();
rj::set(msg, "type", "UNLISTEN");
auto &accountManager = AccountManager::getInstance();
rapidjson::Value data(rapidjson::kObjectType);
rapidjson::Value topics(rapidjson::kArrayType);
for (const auto &topic : topicsVec) {
rj::add(topics, topic, a);
}
rj::set(data, "topics", topics, a);
rj::set(msg, "data", data);
return msg;
}
} // namespace singletons
} // namespace chatterino
+63
View File
@@ -0,0 +1,63 @@
#pragma once
#include "debug/log.hpp"
#include "providers/twitch/twitchaccount.hpp"
#include "util/rapidjson-helpers.hpp"
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <memory>
namespace chatterino {
namespace singletons {
struct ActionUser;
const rapidjson::Value &getArgs(const rapidjson::Value &data);
bool getCreatedByUser(const rapidjson::Value &data, ActionUser &user);
bool getTargetUser(const rapidjson::Value &data, ActionUser &user);
std::string Stringify(const rapidjson::Value &v);
rapidjson::Document CreateListenMessage(const std::vector<std::string> &topicsVec,
std::shared_ptr<providers::twitch::TwitchAccount> account);
rapidjson::Document CreateUnlistenMessage(const std::vector<std::string> &topicsVec);
// Create timer using given ioService
template <typename Duration, typename Callback>
void RunAfter(boost::asio::io_service &ioService, Duration duration, Callback cb)
{
auto timer = std::make_shared<boost::asio::steady_timer>(ioService);
timer->expires_from_now(duration);
timer->async_wait([timer, cb](const boost::system::error_code &ec) {
if (ec) {
debug::Log("Error in RunAfter: {}", ec.message());
return;
}
cb(timer);
});
}
// Use provided timer
template <typename Duration, typename Callback>
void RunAfter(std::shared_ptr<boost::asio::steady_timer> timer, Duration duration, Callback cb)
{
timer->expires_from_now(duration);
timer->async_wait([timer, cb](const boost::system::error_code &ec) {
if (ec) {
debug::Log("Error in RunAfter: {}", ec.message());
return;
}
cb(timer);
});
}
} // namespace singletons
} // namespace chatterino