Cleaned up IrcMessageHandler (#3021)

This commit is contained in:
Paweł 2021-07-17 15:09:21 +02:00 committed by GitHub
parent 74c5cca890
commit 91ab8b90a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 164 deletions

View file

@ -82,8 +82,6 @@ public:
bool hasMessages() const;
QStringList modList;
// CHANNEL INFO
virtual bool canSendMessage() const;
virtual void sendMessage(const QString &message);

View file

@ -167,7 +167,7 @@ static QMap<QString, QString> parseBadges(QString badgesString)
{
QMap<QString, QString> badges;
for (auto badgeData : badgesString.split(','))
for (const auto &badgeData : badgesString.split(','))
{
auto parts = badgeData.split('/');
if (parts.length() != 2)
@ -332,7 +332,6 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
{
const auto &tags = message->tags();
auto app = getApp();
// get twitch channel
QString chanName;
@ -340,17 +339,19 @@ void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
{
return;
}
auto chan = app->twitch.server->getChannelOrEmpty(chanName);
auto chan = getApp()->twitch.server->getChannelOrEmpty(chanName);
if (auto *twitchChannel = dynamic_cast<TwitchChannel *>(chan.get()))
auto *twitchChannel = dynamic_cast<TwitchChannel *>(chan.get());
if (!twitchChannel)
{
// room-id
decltype(tags.find("xD")) it;
return;
}
if ((it = tags.find("room-id")) != tags.end())
// room-id
if (auto it = tags.find("room-id"); it != tags.end())
{
auto roomId = it.value().toString();
twitchChannel->setRoomId(roomId);
}
@ -358,27 +359,23 @@ void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
{
auto roomModes = *twitchChannel->accessRoomModes();
if ((it = tags.find("emote-only")) != tags.end())
if (auto it = tags.find("emote-only"); it != tags.end())
{
roomModes.emoteOnly = it.value() == "1";
}
if ((it = tags.find("subs-only")) != tags.end())
if (auto it = tags.find("subs-only"); it != tags.end())
{
roomModes.submode = it.value() == "1";
}
if ((it = tags.find("slow")) != tags.end())
if (auto it = tags.find("slow"); it != tags.end())
{
roomModes.slowMode = it.value().toInt();
}
if ((it = tags.find("r9k")) != tags.end())
if (auto it = tags.find("r9k"); it != tags.end())
{
roomModes.r9k = it.value() == "1";
}
if ((it = tags.find("broadcaster-lang")) != tags.end())
{
roomModes.broadcasterLang = it.value().toString();
}
if ((it = tags.find("followers-only")) != tags.end())
if (auto it = tags.find("followers-only"); it != tags.end())
{
roomModes.followerOnly = it.value().toInt();
}
@ -387,7 +384,6 @@ void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
twitchChannel->roomModesChanged.invoke();
}
}
void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
{
@ -403,10 +399,8 @@ void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
return;
}
auto app = getApp();
// get channel
auto chan = app->twitch.server->getChannelOrEmpty(chanName);
auto chan = getApp()->twitch.server->getChannelOrEmpty(chanName);
if (chan->isEmpty())
{
@ -443,10 +437,10 @@ void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
chan->addOrReplaceTimeout(timeoutMsg);
// refresh all
app->windows->repaintVisibleChatWidgets(chan.get());
getApp()->windows->repaintVisibleChatWidgets(chan.get());
if (getSettings()->hideModerated)
{
app->windows->forceLayoutChannelViews();
getApp()->windows->forceLayoutChannelViews();
}
}
@ -464,10 +458,8 @@ void IrcMessageHandler::handleClearMessageMessage(Communi::IrcMessage *message)
return;
}
auto app = getApp();
// get channel
auto chan = app->twitch.server->getChannelOrEmpty(chanName);
auto chan = getApp()->twitch.server->getChannelOrEmpty(chanName);
if (chan->isEmpty())
{
@ -483,8 +475,9 @@ void IrcMessageHandler::handleClearMessageMessage(Communi::IrcMessage *message)
QString targetID = tags.value("target-msg-id").toString();
auto msg = chan->findMessage(targetID);
if (msg != nullptr)
{
if (msg == nullptr)
return;
msg->flags.set(MessageFlag::Disabled);
if (!getSettings()->hideDeletionActions)
{
@ -493,7 +486,6 @@ void IrcMessageHandler::handleClearMessageMessage(Communi::IrcMessage *message)
chan->addMessage(builder.release());
}
}
}
void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message)
{
@ -545,27 +537,29 @@ void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message)
void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
{
auto app = getApp();
MessageParseArgs args;
args.isReceivedWhisper = true;
auto c = app->twitch.server->whispersChannel.get();
auto c = getApp()->twitch.server->whispersChannel.get();
TwitchMessageBuilder builder(c, message, args, message->parameter(1),
false);
if (!builder.isIgnored())
if (builder.isIgnored())
{
return;
}
builder->flags.set(MessageFlag::Whisper);
MessagePtr _message = builder.build();
builder.triggerHighlights();
app->twitch.server->lastUserThatWhisperedMe.set(builder.userName);
getApp()->twitch.server->lastUserThatWhisperedMe.set(builder.userName);
if (_message->flags.has(MessageFlag::Highlighted))
{
app->twitch.server->mentionsChannel->addMessage(_message);
getApp()->twitch.server->mentionsChannel->addMessage(_message);
}
c->addMessage(_message);
@ -576,13 +570,12 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
if (getSettings()->inlineWhispers)
{
app->twitch.server->forEachChannel(
getApp()->twitch.server->forEachChannel(
[&_message, overrideFlags](ChannelPtr channel) {
channel->addMessage(_message, overrideFlags);
});
}
}
}
std::vector<MessagePtr> IrcMessageHandler::parseUserNoticeMessage(
Channel *channel, Communi::IrcMessage *message)
@ -592,7 +585,7 @@ std::vector<MessagePtr> IrcMessageHandler::parseUserNoticeMessage(
auto tags = message->tags();
auto parameters = message->parameters();
QString msgType = tags.value("msg-id", "").toString();
QString msgType = tags.value("msg-id").toString();
QString content;
if (parameters.size() >= 2)
{
@ -624,9 +617,10 @@ std::vector<MessagePtr> IrcMessageHandler::parseUserNoticeMessage(
if (msgType == "bitsbadgetier")
{
messageText = QString("%1 just earned a new %2 Bits badge!")
.arg(tags.value("display-name").toString())
.arg(kFormatNumbers(
messageText =
QString("%1 just earned a new %2 Bits badge!")
.arg(tags.value("display-name").toString(),
kFormatNumbers(
tags.value("msg-param-threshold").toInt()));
}
@ -648,7 +642,7 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
auto parameters = message->parameters();
auto target = parameters[0];
QString msgType = tags.value("msg-id", "").toString();
QString msgType = tags.value("msg-id").toString();
QString content;
if (parameters.size() >= 2)
{
@ -673,9 +667,10 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
if (msgType == "bitsbadgetier")
{
messageText = QString("%1 just earned a new %2 Bits badge!")
.arg(tags.value("display-name").toString())
.arg(kFormatNumbers(
messageText =
QString("%1 just earned a new %2 Bits badge!")
.arg(tags.value("display-name").toString(),
kFormatNumbers(
tags.value("msg-param-threshold").toInt()));
}
@ -706,28 +701,6 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
}
}
void IrcMessageHandler::handleModeMessage(Communi::IrcMessage *message)
{
auto app = getApp();
auto channel = app->twitch.server->getChannelOrEmpty(
message->parameter(0).remove(0, 1));
if (channel->isEmpty())
{
return;
}
if (message->parameter(1) == "+o")
{
channel->modList.append(message->parameter(2));
}
else if (message->parameter(1) == "-o")
{
channel->modList.append(message->parameter(2));
}
}
std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
Communi::IrcNoticeMessage *message)
{
@ -740,7 +713,7 @@ std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
.arg(curUser->getUserName());
const auto loginPromptText = QString("Try adding your account again.");
auto builder = MessageBuilder();
MessageBuilder builder;
auto text = QString("%1 %2").arg(expirationText, loginPromptText);
builder.message().messageText = text;
builder.message().searchText = text;
@ -761,7 +734,7 @@ std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
{
return {generateBannedMessage(true)};
}
else if (message->tags()["msg-id"] == "msg_timedout")
else if (message->tags().value("msg-id") == "msg_timedout")
{
std::vector<MessagePtr> builtMessage;
@ -776,8 +749,8 @@ std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
return builtMessage;
}
else
{
// default case
std::vector<MessagePtr> builtMessages;
builtMessages.emplace_back(makeSystemMessage(
@ -785,14 +758,12 @@ std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
return builtMessages;
}
} // namespace chatterino
void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
{
auto app = getApp();
auto builtMessages = this->parseNoticeMessage(message);
for (auto msg : builtMessages)
for (const auto &msg : builtMessages)
{
QString channelName;
if (!trimChannelName(message->target(), channelName) ||
@ -800,7 +771,7 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
{
// Notice wasn't targeted at a single channel, send to all twitch
// channels
app->twitch.server->forEachChannelAndSpecialChannels(
getApp()->twitch.server->forEachChannelAndSpecialChannels(
[msg](const auto &c) {
c->addMessage(msg);
});
@ -808,7 +779,7 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
return;
}
auto channel = app->twitch.server->getChannelOrEmpty(channelName);
auto channel = getApp()->twitch.server->getChannelOrEmpty(channelName);
if (channel->isEmpty())
{
@ -818,7 +789,7 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
return;
}
QString tags = message->tags().value("msg-id", "").toString();
QString tags = message->tags().value("msg-id").toString();
if (tags == "bad_delete_message_error" || tags == "usage_delete")
{
channel->addMessage(makeSystemMessage(
@ -856,13 +827,15 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message)
{
auto app = getApp();
auto channel = app->twitch.server->getChannelOrEmpty(
auto channel = getApp()->twitch.server->getChannelOrEmpty(
message->parameter(0).remove(0, 1));
if (TwitchChannel *twitchChannel =
dynamic_cast<TwitchChannel *>(channel.get()))
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (!twitchChannel)
{
return;
}
if (message->nick() !=
getApp()->accounts->twitch.getCurrent()->getUserName() &&
getSettings()->showJoins.getValue())
@ -870,17 +843,18 @@ void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message)
twitchChannel->addJoinedUser(message->nick());
}
}
}
void IrcMessageHandler::handlePartMessage(Communi::IrcMessage *message)
{
auto app = getApp();
auto channel = app->twitch.server->getChannelOrEmpty(
auto channel = getApp()->twitch.server->getChannelOrEmpty(
message->parameter(0).remove(0, 1));
if (TwitchChannel *twitchChannel =
dynamic_cast<TwitchChannel *>(channel.get()))
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (!twitchChannel)
{
return;
}
const auto selfAccountName =
getApp()->accounts->twitch.getCurrent()->getUserName();
if (message->nick() != selfAccountName &&
@ -894,5 +868,4 @@ void IrcMessageHandler::handlePartMessage(Communi::IrcMessage *message)
channel->addMessage(generateBannedMessage(false));
}
}
}
} // namespace chatterino

View file

@ -57,7 +57,6 @@ public:
bool emoteOnly = false;
int followerOnly = -1;
int slowMode = 0;
QString broadcasterLang;
};
void initialize();

View file

@ -124,11 +124,7 @@ void TwitchIrcServer::readConnectionMessageReceived(
auto &handler = IrcMessageHandler::instance();
// Below commands enabled through the twitch.tv/membership CAP REQ
if (command == "MODE")
{
handler.handleModeMessage(message);
}
else if (command == "JOIN")
if (command == "JOIN")
{
handler.handleJoinMessage(message);
}