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

View file

@ -179,12 +179,12 @@ private:
std::owner_less<WebsocketHandle>> std::owner_less<WebsocketHandle>>
clients; clients;
std::unordered_map<std::string, std::function<void(const rapidjson::Value &, std::unordered_map<
const QString &)>> QString, std::function<void(const rapidjson::Value &, const QString &)>>
moderationActionHandlers; moderationActionHandlers;
std::unordered_map<std::string, std::function<void(const rapidjson::Value &, std::unordered_map<
const QString &)>> QString, std::function<void(const rapidjson::Value &, const QString &)>>
channelTermsActionHandlers; channelTermsActionHandlers;
void onMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg); 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); 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::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
value.Accept(writer); value.Accept(writer);
return std::string(buffer.GetString()); return buffer.GetString();
} }
bool getSafeObject(rapidjson::Value &obj, const char *key, bool getSafeObject(rapidjson::Value &obj, const char *key,

View file

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