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:
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
#include <QColor>
|
||||
#include <QString>
|
||||
|
||||
#include <chrono>
|
||||
@@ -10,7 +11,10 @@ namespace chatterino {
|
||||
|
||||
struct ActionUser {
|
||||
QString id;
|
||||
QString name;
|
||||
QString login;
|
||||
// displayName should be in format "login(localizedName)" for non-ascii usernames
|
||||
QString displayName;
|
||||
QColor color;
|
||||
};
|
||||
|
||||
struct PubSubAction {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -37,7 +37,7 @@ const rapidjson::Value &getMsgID(const rapidjson::Value &data)
|
||||
|
||||
bool getCreatedByUser(const rapidjson::Value &data, ActionUser &user)
|
||||
{
|
||||
return rj::getSafe(data, "created_by", user.name) &&
|
||||
return rj::getSafe(data, "created_by", user.login) &&
|
||||
rj::getSafe(data, "created_by_user_id", user.id);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ bool getTargetUser(const rapidjson::Value &data, ActionUser &user)
|
||||
|
||||
bool getTargetUserName(const rapidjson::Value &data, ActionUser &user)
|
||||
{
|
||||
return rj::getSafe(data, "target_user_login", user.name);
|
||||
return rj::getSafe(data, "target_user_login", user.login);
|
||||
}
|
||||
|
||||
rapidjson::Document createListenMessage(const std::vector<QString> &topicsVec,
|
||||
|
||||
@@ -1416,17 +1416,19 @@ void TwitchMessageBuilder::deletionMessage(const DeleteAction &action,
|
||||
builder->message().flags.set(MessageFlag::Timeout);
|
||||
|
||||
builder
|
||||
->emplace<TextElement>(action.source.name, MessageElementFlag::Username,
|
||||
->emplace<TextElement>(action.source.login,
|
||||
MessageElementFlag::Username,
|
||||
MessageColor::System, FontStyle::ChatMediumBold)
|
||||
->setLink({Link::UserInfo, action.source.name});
|
||||
->setLink({Link::UserInfo, action.source.login});
|
||||
// TODO(mm2pl): If or when jumping to a single message gets implemented a link,
|
||||
// add a link to the originalMessage
|
||||
builder->emplace<TextElement>(
|
||||
"deleted message from", MessageElementFlag::Text, MessageColor::System);
|
||||
builder
|
||||
->emplace<TextElement>(action.target.name, MessageElementFlag::Username,
|
||||
->emplace<TextElement>(action.target.login,
|
||||
MessageElementFlag::Username,
|
||||
MessageColor::System, FontStyle::ChatMediumBold)
|
||||
->setLink({Link::UserInfo, action.target.name});
|
||||
->setLink({Link::UserInfo, action.target.login});
|
||||
builder->emplace<TextElement>("saying:", MessageElementFlag::Text,
|
||||
MessageColor::System);
|
||||
if (action.messageText.length() > 50)
|
||||
|
||||
Reference in New Issue
Block a user