Replace std::string with QString in rapidjson and PubSub Client (#3018)

This commit is contained in:
Paweł 2021-07-18 14:37:53 +02:00 committed by GitHub
parent 1f19d31a67
commit e5fe0999ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 53 deletions

View file

@ -23,7 +23,7 @@ namespace chatterino {
static const char *pingPayload = "{\"type\":\"PING\"}";
static std::map<QString, std::string> sentMessages;
static std::map<QString, QString> sentMessages;
namespace detail {
@ -72,10 +72,10 @@ namespace detail {
rj::set(message, "nonce", uuid);
std::string payload = rj::stringify(message);
QString payload = rj::stringify(message);
sentMessages[uuid] = payload;
this->send(payload.c_str());
this->send(payload.toUtf8());
return true;
}
@ -109,10 +109,10 @@ namespace detail {
rj::set(message, "nonce", generateUuid());
std::string payload = rj::stringify(message);
QString payload = rj::stringify(message);
sentMessages[uuid] = payload;
this->send(payload.c_str());
this->send(payload.toUtf8());
}
void PubSubClient::handlePong()
@ -829,19 +829,16 @@ PubSub::PubSub()
// this->signals_.moderation.automodUserMessage.invoke(action);
// };
this->moderationActionHandlers["denied_automod_message"] = [](const auto
&data,
const auto
&roomID) {
this->moderationActionHandlers["denied_automod_message"] =
[](const auto &data, const auto &roomID) {
// This message got denied by a moderator
// qCDebug(chatterinoPubsub) << QString::fromStdString(rj::stringify(data));
// qCDebug(chatterinoPubsub) << rj::stringify(data);
};
this->moderationActionHandlers
["approved_automod_message"] = [](const auto &data,
const auto &roomID) {
this->moderationActionHandlers["approved_automod_message"] =
[](const auto &data, const auto &roomID) {
// This message got approved by a moderator
// qCDebug(chatterinoPubsub) << QString::fromStdString(rj::stringify(data));
// qCDebug(chatterinoPubsub) << rj::stringify(data);
};
this->websocketClient.set_access_channels(websocketpp::log::alevel::all);
@ -930,7 +927,7 @@ void PubSub::listenToChannelModerationActions(
if (userID.isEmpty())
return;
auto topic = topicFormat.arg(userID).arg(channelID);
auto topic = topicFormat.arg(userID, channelID);
if (this->isListeningToTopic(topic))
{
@ -952,7 +949,7 @@ void PubSub::listenToAutomod(const QString &channelID,
if (userID.isEmpty())
return;
auto topic = topicFormat.arg(userID).arg(channelID);
auto topic = topicFormat.arg(userID, channelID);
if (this->isListeningToTopic(topic))
{
@ -970,7 +967,6 @@ void PubSub::listenToChannelPointRewards(const QString &channelID,
static const QString topicFormat("community-points-channel-v1.%1");
assert(!channelID.isEmpty());
assert(account != nullptr);
QString userID = account->getUserId();
auto topic = topicFormat.arg(channelID);
@ -1035,26 +1031,27 @@ bool PubSub::isListeningToTopic(const QString &topic)
void PubSub::onMessage(websocketpp::connection_hdl hdl,
WebsocketMessagePtr websocketMessage)
{
const std::string &payload = websocketMessage->get_payload();
const auto &payload =
QString::fromStdString(websocketMessage->get_payload());
rapidjson::Document msg;
rapidjson::ParseResult res = msg.Parse(payload.c_str());
rapidjson::ParseResult res = msg.Parse(payload.toUtf8());
if (!res)
{
qCDebug(chatterinoPubsub)
<< "Error parsing message '" << payload.c_str()
<< "' from PubSub:" << rapidjson::GetParseError_En(res.Code());
<< QString("Error parsing message '%1' from PubSub: %2")
.arg(payload, rapidjson::GetParseError_En(res.Code()));
return;
}
if (!msg.IsObject())
{
qCDebug(chatterinoPubsub)
<< "Error parsing message '" << payload.c_str()
<< "' from PubSub. Root object is not an "
"object";
<< QString("Error parsing message '%1' from PubSub. Root object is "
"not an object")
.arg(payload);
return;
}
@ -1198,7 +1195,7 @@ void PubSub::handleListenResponse(const rapidjson::Document &msg)
void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
{
QString topic;
qCDebug(chatterinoPubsub) << rj::stringify(outerData).c_str();
qCDebug(chatterinoPubsub) << rj::stringify(outerData);
if (!rj::getSafe(outerData, "topic", topic))
{
@ -1207,7 +1204,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
return;
}
std::string payload;
QString payload;
if (!rj::getSafe(outerData, "message", payload))
{
@ -1217,19 +1214,19 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
rapidjson::Document msg;
rapidjson::ParseResult res = msg.Parse(payload.c_str());
rapidjson::ParseResult res = msg.Parse(payload.toUtf8());
if (!res)
{
qCDebug(chatterinoPubsub)
<< "Error parsing message '" << payload.c_str()
<< "' from PubSub:" << rapidjson::GetParseError_En(res.Code());
<< QString("Error parsing message '%1' from PubSub: %2")
.arg(payload, rapidjson::GetParseError_En(res.Code()));
return;
}
if (topic.startsWith("whispers."))
{
std::string whisperType;
QString whisperType;
if (!rj::getSafe(msg, "type", whisperType))
{
@ -1251,8 +1248,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
}
else
{
qCDebug(chatterinoPubsub)
<< "Invalid whisper type:" << whisperType.c_str();
qCDebug(chatterinoPubsub) << "Invalid whisper type:" << whisperType;
return;
}
}
@ -1262,7 +1258,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
assert(topicParts.length() == 3);
const auto &data = msg["data"];
std::string moderationEventType;
QString moderationEventType;
if (!rj::getSafe(msg, "type", moderationEventType))
{
@ -1271,13 +1267,13 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
}
if (moderationEventType == "moderation_action")
{
std::string moderationAction;
QString moderationAction;
if (!rj::getSafe(data, "moderation_action", moderationAction))
{
qCDebug(chatterinoPubsub)
<< "Missing moderation action in data:"
<< rj::stringify(data).c_str();
<< rj::stringify(data);
return;
}
@ -1288,7 +1284,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
{
qCDebug(chatterinoPubsub)
<< "No handler found for moderation action"
<< moderationAction.c_str();
<< moderationAction;
return;
}
// Invoke handler function
@ -1296,13 +1292,13 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
}
else if (moderationEventType == "channel_terms_action")
{
std::string channelTermsAction;
QString channelTermsAction;
if (!rj::getSafe(data, "type", channelTermsAction))
{
qCDebug(chatterinoPubsub)
<< "Missing channel terms action in data:"
<< rj::stringify(data).c_str();
<< rj::stringify(data);
return;
}
@ -1313,7 +1309,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
{
qCDebug(chatterinoPubsub)
<< "No handler found for channel terms action"
<< channelTermsAction.c_str();
<< channelTermsAction;
return;
}
// Invoke handler function
@ -1322,7 +1318,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
}
else if (topic.startsWith("community-points-channel-v1."))
{
std::string pointEventType;
QString pointEventType;
if (!rj::getSafe(msg, "type", pointEventType))
{
qCDebug(chatterinoPubsub) << "Bad channel point event data";
@ -1348,7 +1344,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
else
{
qCDebug(chatterinoPubsub)
<< "Invalid point event type:" << pointEventType.c_str();
<< "Invalid point event type:" << pointEventType;
}
}
else if (topic.startsWith("automod-queue."))
@ -1357,7 +1353,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
assert(topicParts.length() == 3);
auto &data = msg["data"];
std::string automodEventType;
QString automodEventType;
if (!rj::getSafe(msg, "type", automodEventType))
{
qCDebug(chatterinoPubsub) << "Bad automod event data";

View file

@ -179,12 +179,12 @@ private:
std::owner_less<WebsocketHandle>>
clients;
std::unordered_map<std::string, std::function<void(const rapidjson::Value &,
const QString &)>>
std::unordered_map<
QString, std::function<void(const rapidjson::Value &, const QString &)>>
moderationActionHandlers;
std::unordered_map<std::string, std::function<void(const rapidjson::Value &,
const QString &)>>
std::unordered_map<
QString, std::function<void(const rapidjson::Value &, const QString &)>>
channelTermsActionHandlers;
void onMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg);

View file

@ -19,13 +19,13 @@ namespace rj {
obj.AddMember(rapidjson::Value(key, a).Move(), value.Move(), a);
}
std::string stringify(const rapidjson::Value &value)
QString stringify(const rapidjson::Value &value)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
value.Accept(writer);
return std::string(buffer.GetString());
return buffer.GetString();
}
bool getSafeObject(rapidjson::Value &obj, const char *key,

View file

@ -95,7 +95,7 @@ namespace rj {
bool getSafeObject(rapidjson::Value &obj, const char *key,
rapidjson::Value &out);
std::string stringify(const rapidjson::Value &value);
QString stringify(const rapidjson::Value &value);
} // namespace rj
} // namespace chatterino