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
-16
View File
@@ -1,16 +0,0 @@
#include "singletons/helper/pubsubactions.hpp"
#include "singletons/helper/pubsubhelpers.hpp"
namespace chatterino {
namespace singletons {
PubSubAction::PubSubAction(const rapidjson::Value &data, const QString &_roomID)
: timestamp(std::chrono::steady_clock::now())
, roomID(_roomID)
{
getCreatedByUser(data, this->source);
}
} // namespace singletons
} // namespace chatterino
-95
View File
@@ -1,95 +0,0 @@
#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, const QString &_roomID);
ActionUser source;
std::chrono::steady_clock::time_point timestamp;
QString roomID;
};
// Used when a chat mode (i.e. slowmode, subscribers only mode) is enabled or disabled
struct ModeChangedAction : PubSubAction {
using PubSubAction::PubSubAction;
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 BanAction : PubSubAction {
using PubSubAction::PubSubAction;
ActionUser target;
QString reason;
uint32_t duration = 0;
bool isBan() const
{
return this->duration == 0;
}
};
struct UnbanAction : PubSubAction {
using PubSubAction::PubSubAction;
ActionUser target;
enum {
Banned,
TimedOut,
} previousState;
bool wasBan() const
{
return this->previousState == Banned;
}
};
struct ClearChatAction : PubSubAction {
using PubSubAction::PubSubAction;
};
struct ModerationStateAction : PubSubAction {
using PubSubAction::PubSubAction;
ActionUser target;
// true = modded
// false = unmodded
bool modded;
};
} // namespace singletons
} // namespace chatterino
-89
View File
@@ -1,89 +0,0 @@
#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");
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
@@ -1,63 +0,0 @@
#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