Fix automod messages not being parsed/showing up properly (#2742)

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
apa420 2021-05-08 14:14:49 +02:00 committed by GitHub
parent 0c2617dc99
commit c40bdf812b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 33 deletions

View file

@ -2,6 +2,7 @@
## Unversioned
- Bugfix: Automod messages now work properly again. (#2682)
- Bugfix: `Login expired` message no longer highlights all tabs. (#2735)
## 2.3.1

View file

@ -336,7 +336,7 @@ MessageBuilder::MessageBuilder(const AutomodUserAction &action)
break;
case AutomodUserAction::RemovePermitted: {
text = QString("%1 removed %2 as a permitted term term on AutoMod.")
text = QString("%1 removed %2 as a permitted term on AutoMod.")
.arg(action.source.name)
.arg(action.message);
}

View file

@ -583,7 +583,7 @@ PubSub::PubSub()
}
};
this->moderationActionHandlers["add_permitted_term"] =
this->channelTermsActionHandlers["add_permitted_term"] =
[this](const auto &data, const auto &roomID) {
// This term got a pass through automod
AutomodUserAction action(data, roomID);
@ -591,15 +591,13 @@ PubSub::PubSub()
try
{
const auto &args = getArgs(data);
action.type = AutomodUserAction::AddPermitted;
if (args.Size() < 1)
if (!rj::getSafe(data, "text", action.message))
{
return;
}
if (!rj::getSafe(args[0], action.message))
if (!rj::getSafe(data, "requester_login", action.source.name))
{
return;
}
@ -609,11 +607,11 @@ PubSub::PubSub()
catch (const std::runtime_error &ex)
{
qCDebug(chatterinoPubsub)
<< "Error parsing moderation action:" << ex.what();
<< "Error parsing channel terms action:" << ex.what();
}
};
this->moderationActionHandlers["add_blocked_term"] =
this->channelTermsActionHandlers["add_blocked_term"] =
[this](const auto &data, const auto &roomID) {
// A term has been added
AutomodUserAction action(data, roomID);
@ -621,15 +619,13 @@ PubSub::PubSub()
try
{
const auto &args = getArgs(data);
action.type = AutomodUserAction::AddBlocked;
if (args.Size() < 1)
if (!rj::getSafe(data, "text", action.message))
{
return;
}
if (!rj::getSafe(args[0], action.message))
if (!rj::getSafe(data, "requester_login", action.source.name))
{
return;
}
@ -639,7 +635,7 @@ PubSub::PubSub()
catch (const std::runtime_error &ex)
{
qCDebug(chatterinoPubsub)
<< "Error parsing moderation action:" << ex.what();
<< "Error parsing channel terms action:" << ex.what();
}
};
@ -672,6 +668,33 @@ PubSub::PubSub()
<< "Error parsing moderation action:" << ex.what();
}
};
this->channelTermsActionHandlers["delete_permitted_term"] =
[this](const auto &data, const auto &roomID) {
// This term got deleted
AutomodUserAction action(data, roomID);
getCreatedByUser(data, action.source);
try
{
action.type = AutomodUserAction::RemovePermitted;
if (!rj::getSafe(data, "text", action.message))
{
return;
}
if (!rj::getSafe(data, "requester_login", action.source.name))
{
return;
}
this->signals_.moderation.automodUserMessage.invoke(action);
}
catch (const std::runtime_error &ex)
{
qCDebug(chatterinoPubsub)
<< "Error parsing channel terms action:" << ex.what();
}
};
this->moderationActionHandlers["delete_blocked_term"] =
[this](const auto &data, const auto &roomID) {
@ -703,16 +726,47 @@ PubSub::PubSub()
<< "Error parsing moderation action:" << ex.what();
}
};
this->moderationActionHandlers["modified_automod_properties"] =
this->channelTermsActionHandlers["delete_blocked_term"] =
[this](const auto &data, const auto &roomID) {
// The automod settings got modified
// This term got deleted
AutomodUserAction action(data, roomID);
getCreatedByUser(data, action.source);
action.type = AutomodUserAction::Properties;
this->signals_.moderation.automodUserMessage.invoke(action);
try
{
action.type = AutomodUserAction::RemoveBlocked;
if (!rj::getSafe(data, "text", action.message))
{
return;
}
if (!rj::getSafe(data, "requester_login", action.source.name))
{
return;
}
this->signals_.moderation.automodUserMessage.invoke(action);
}
catch (const std::runtime_error &ex)
{
qCDebug(chatterinoPubsub)
<< "Error parsing channel terms action:" << ex.what();
}
};
// We don't get this one anymore or anything similiar
// We need some new topic so we can listen
//
//this->moderationActionHandlers["modified_automod_properties"] =
// [this](const auto &data, const auto &roomID) {
// // The automod settings got modified
// AutomodUserAction action(data, roomID);
// getCreatedByUser(data, action.source);
// action.type = AutomodUserAction::Properties;
// this->signals_.moderation.automodUserMessage.invoke(action);
// };
this->moderationActionHandlers["denied_automod_message"] = [](const auto
&data,
const auto
@ -1059,6 +1113,7 @@ void PubSub::handleListenResponse(const rapidjson::Document &msg)
void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
{
QString topic;
qCDebug(chatterinoPubsub) << rj::stringify(outerData).c_str();
if (!rj::getSafe(outerData, "topic", topic))
{
@ -1122,27 +1177,63 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData)
assert(topicParts.length() == 3);
const auto &data = msg["data"];
std::string moderationAction;
std::string moderationEventType;
if (!rj::getSafe(data, "moderation_action", moderationAction))
if (!rj::getSafe(msg, "type", moderationEventType))
{
qCDebug(chatterinoPubsub) << "Missing moderation action in data:"
<< rj::stringify(data).c_str();
qCDebug(chatterinoPubsub) << "Bad moderator event data";
return;
}
auto handlerIt = this->moderationActionHandlers.find(moderationAction);
if (handlerIt == this->moderationActionHandlers.end())
if (moderationEventType == "moderation_action")
{
qCDebug(chatterinoPubsub)
<< "No handler found for moderation action"
<< moderationAction.c_str();
return;
}
std::string moderationAction;
// Invoke handler function
handlerIt->second(data, topicParts[2]);
if (!rj::getSafe(data, "moderation_action", moderationAction))
{
qCDebug(chatterinoPubsub)
<< "Missing moderation action in data:"
<< rj::stringify(data).c_str();
return;
}
auto handlerIt =
this->moderationActionHandlers.find(moderationAction);
if (handlerIt == this->moderationActionHandlers.end())
{
qCDebug(chatterinoPubsub)
<< "No handler found for moderation action"
<< moderationAction.c_str();
return;
}
// Invoke handler function
handlerIt->second(data, topicParts[2]);
}
else if (moderationEventType == "channel_terms_action")
{
std::string channelTermsAction;
if (!rj::getSafe(data, "type", channelTermsAction))
{
qCDebug(chatterinoPubsub)
<< "Missing channel terms action in data:"
<< rj::stringify(data).c_str();
return;
}
auto handlerIt =
this->channelTermsActionHandlers.find(channelTermsAction);
if (handlerIt == this->channelTermsActionHandlers.end())
{
qCDebug(chatterinoPubsub)
<< "No handler found for channel terms action"
<< channelTermsAction.c_str();
return;
}
// Invoke handler function
handlerIt->second(data, topicParts[2]);
}
}
else if (topic.startsWith("community-points-channel-v1."))
{

View file

@ -179,6 +179,10 @@ private:
const QString &)>>
moderationActionHandlers;
std::unordered_map<std::string, std::function<void(const rapidjson::Value &,
const QString &)>>
channelTermsActionHandlers;
void onMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg);
void onConnectionOpen(websocketpp::connection_hdl hdl);
void onConnectionClose(websocketpp::connection_hdl hdl);