Made username and color in AutoMod mod messages use correct values (#2967)

We use values given in pubsub messages and handle their edge-cases properly.
This commit is contained in:
Paweł
2021-07-11 13:33:35 +02:00
committed by GitHub
parent 735c0ad1ac
commit 975b39fe10
7 changed files with 132 additions and 81 deletions
+77 -28
View File
@@ -2,6 +2,7 @@
#include "providers/twitch/PubsubActions.hpp"
#include "providers/twitch/PubsubHelpers.hpp"
#include "singletons/Settings.hpp"
#include "util/Helpers.hpp"
#include "util/RapidjsonHelpers.hpp"
@@ -333,7 +334,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(args[0], action.target.name))
if (!rj::getSafe(args[0], action.target.login))
{
return;
}
@@ -398,7 +399,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(args[0], action.target.name))
if (!rj::getSafe(args[0], action.target.login))
{
return;
}
@@ -444,7 +445,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(args[0], action.target.name))
if (!rj::getSafe(args[0], action.target.login))
{
return;
}
@@ -484,7 +485,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(args[0], action.target.name))
if (!rj::getSafe(args[0], action.target.login))
{
return;
}
@@ -524,7 +525,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(args[0], action.target.name))
if (!rj::getSafe(args[0], action.target.login))
{
return;
}
@@ -556,7 +557,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(args[0], action.target.name))
if (!rj::getSafe(args[0], action.target.login))
{
return;
}
@@ -588,7 +589,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(args[0], action.target.name))
if (!rj::getSafe(args[0], action.target.login))
{
return;
}
@@ -658,7 +659,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(data, "requester_login", action.source.name))
if (!rj::getSafe(data, "requester_login", action.source.login))
{
return;
}
@@ -686,7 +687,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(data, "requester_login", action.source.name))
if (!rj::getSafe(data, "requester_login", action.source.login))
{
return;
}
@@ -743,7 +744,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(data, "requester_login", action.source.name))
if (!rj::getSafe(data, "requester_login", action.source.login))
{
return;
}
@@ -802,7 +803,7 @@ PubSub::PubSub()
return;
}
if (!rj::getSafe(data, "requester_login", action.source.name))
if (!rj::getSafe(data, "requester_login", action.source.login))
{
return;
}
@@ -1367,7 +1368,7 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
QString status;
if (!rj::getSafe(data, "status", status))
{
qDebug() << "Failed to get status";
qCDebug(chatterinoPubsub) << "Failed to get status";
return;
}
if (status == "PENDING")
@@ -1377,20 +1378,22 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
if (!rj::getSafeObject(data, "content_classification",
classification))
{
qDebug() << "Failed to get content_classification";
qCDebug(chatterinoPubsub)
<< "Failed to get content_classification";
return;
}
QString contentCategory;
if (!rj::getSafe(classification, "category", contentCategory))
{
qDebug() << "Failed to get content category";
qCDebug(chatterinoPubsub)
<< "Failed to get content category";
return;
}
int contentLevel;
if (!rj::getSafe(classification, "level", contentLevel))
{
qDebug() << "Failed to get content level";
qCDebug(chatterinoPubsub) << "Failed to get content level";
return;
}
action.reason = QString("%1 level %2")
@@ -1400,25 +1403,26 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
rapidjson::Value messageData;
if (!rj::getSafeObject(data, "message", messageData))
{
qDebug() << "Failed to get message data";
qCDebug(chatterinoPubsub) << "Failed to get message data";
return;
}
rapidjson::Value messageContent;
if (!rj::getSafeObject(messageData, "content", messageContent))
{
qDebug() << "Failed to get message content";
qCDebug(chatterinoPubsub)
<< "Failed to get message content";
return;
}
if (!rj::getSafe(messageData, "id", action.msgID))
{
qDebug() << "Failed to get message id";
qCDebug(chatterinoPubsub) << "Failed to get message id";
return;
}
if (!rj::getSafe(messageContent, "text", action.message))
{
qDebug() << "Failed to get message text";
qCDebug(chatterinoPubsub) << "Failed to get message text";
return;
}
@@ -1428,23 +1432,68 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
rapidjson::Value senderData;
if (!rj::getSafeObject(messageData, "sender", senderData))
{
qDebug() << "Failed to get sender";
qCDebug(chatterinoPubsub) << "Failed to get sender";
return;
}
QString sender_id;
if (!rj::getSafe(senderData, "user_id", sender_id))
QString senderId;
if (!rj::getSafe(senderData, "user_id", senderId))
{
qDebug() << "Failed to get sender user id";
qCDebug(chatterinoPubsub) << "Failed to get sender user id";
return;
}
QString sender_login;
if (!rj::getSafe(senderData, "login", sender_login))
QString senderLogin;
if (!rj::getSafe(senderData, "login", senderLogin))
{
qDebug() << "Failed to get sender login";
qCDebug(chatterinoPubsub) << "Failed to get sender login";
return;
}
action.target = ActionUser{sender_id, sender_login};
qDebug() << action.msgID;
QString senderDisplayName = senderLogin;
bool hasLocalizedName = false;
if (rj::getSafe(senderData, "display_name", senderDisplayName))
{
// check for non-ascii display names
if (QString::compare(senderLogin, senderDisplayName,
Qt::CaseInsensitive) != 0)
{
hasLocalizedName = true;
}
}
QColor senderColor;
QString senderColor_;
if (rj::getSafe(senderData, "chat_color", senderColor_))
{
senderColor = QColor(senderColor_);
}
else if (getSettings()->colorizeNicknames)
{
// color may be not present if user is a grey-name
senderColor = getRandomColor(senderId);
}
// handle username style based on prefered setting
switch (getSettings()->usernameDisplayMode.getValue())
{
case UsernameDisplayMode::Username: {
if (hasLocalizedName)
{
senderDisplayName = senderLogin;
}
break;
}
case UsernameDisplayMode::LocalizedName: {
break;
}
case UsernameDisplayMode::UsernameAndLocalizedName: {
if (hasLocalizedName)
{
senderDisplayName = QString("%1(%2)").arg(
senderLogin, senderDisplayName);
}
break;
}
}
action.target = ActionUser{senderId, senderLogin,
senderDisplayName, senderColor};
this->signals_.moderation.automodMessage.invoke(action);
}
// "ALLOWED" and "DENIED" statuses remain unimplemented