2018-04-28 15:48:40 +02:00
|
|
|
#include "providers/twitch/pubsubhelpers.hpp"
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-04-28 15:48:40 +02:00
|
|
|
#include "providers/twitch/pubsubactions.hpp"
|
2018-04-15 15:09:31 +02:00
|
|
|
#include "util/rapidjson-helpers.hpp"
|
|
|
|
|
|
|
|
namespace chatterino {
|
2018-04-28 15:48:40 +02:00
|
|
|
namespace providers {
|
|
|
|
namespace twitch {
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
rapidjson::Document createListenMessage(const std::vector<std::string> &topicsVec,
|
2018-04-15 15:09:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-28 16:07:18 +02:00
|
|
|
rapidjson::Document createUnlistenMessage(const std::vector<std::string> &topicsVec)
|
2018-04-15 15:09:31 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-28 15:48:40 +02:00
|
|
|
} // namespace twitch
|
|
|
|
} // namespace providers
|
2018-04-15 15:09:31 +02:00
|
|
|
} // namespace chatterino
|