2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/PubsubHelpers.hpp"
|
2018-04-15 15:09:31 +02:00
|
|
|
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/PubsubActions.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "providers/twitch/TwitchAccount.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "util/RapidjsonHelpers.hpp"
|
2018-04-15 15:09:31 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
const rapidjson::Value &getArgs(const rapidjson::Value &data)
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!data.HasMember("args"))
|
|
|
|
{
|
2018-04-15 15:09:31 +02:00
|
|
|
throw std::runtime_error("Missing member args");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &args = data["args"];
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!args.IsArray())
|
|
|
|
{
|
2018-04-15 15:09:31 +02:00
|
|
|
throw std::runtime_error("args must be an array");
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:45:59 +01:00
|
|
|
const rapidjson::Value &getMsgID(const rapidjson::Value &data)
|
|
|
|
{
|
|
|
|
if (!data.HasMember("msg_id"))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Missing member msg_id");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &msgID = data["msg_id"];
|
|
|
|
|
|
|
|
return msgID;
|
|
|
|
}
|
|
|
|
|
2018-04-15 15:09:31 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:21:00 +02:00
|
|
|
bool getTargetUserName(const rapidjson::Value &data, ActionUser &user)
|
|
|
|
{
|
|
|
|
return rj::getSafe(data, "target_user_login", user.name);
|
|
|
|
}
|
|
|
|
|
2020-01-03 20:51:37 +01:00
|
|
|
rapidjson::Document createListenMessage(const std::vector<QString> &topicsVec,
|
|
|
|
std::shared_ptr<TwitchAccount> account)
|
2018-04-15 15:09:31 +02:00
|
|
|
{
|
|
|
|
rapidjson::Document msg(rapidjson::kObjectType);
|
|
|
|
auto &a = msg.GetAllocator();
|
|
|
|
|
|
|
|
rj::set(msg, "type", "LISTEN");
|
|
|
|
|
|
|
|
rapidjson::Value data(rapidjson::kObjectType);
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (account)
|
|
|
|
{
|
2018-04-15 15:09:31 +02:00
|
|
|
rj::set(data, "auth_token", account->getOAuthToken(), a);
|
|
|
|
}
|
|
|
|
|
|
|
|
rapidjson::Value topics(rapidjson::kArrayType);
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &topic : topicsVec)
|
|
|
|
{
|
2018-04-15 15:09:31 +02:00
|
|
|
rj::add(topics, topic, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
rj::set(data, "topics", topics, a);
|
|
|
|
|
|
|
|
rj::set(msg, "data", data);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2020-01-03 20:51:37 +01:00
|
|
|
rapidjson::Document createUnlistenMessage(const std::vector<QString> &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);
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &topic : topicsVec)
|
|
|
|
{
|
2018-04-15 15:09:31 +02:00
|
|
|
rj::add(topics, topic, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
rj::set(data, "topics", topics, a);
|
|
|
|
|
|
|
|
rj::set(msg, "data", data);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|