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; bool hasMessages() const;
QStringList modList;
// CHANNEL INFO // CHANNEL INFO
virtual bool canSendMessage() const; virtual bool canSendMessage() const;
virtual void sendMessage(const QString &message); virtual void sendMessage(const QString &message);

View file

@ -167,7 +167,7 @@ static QMap<QString, QString> parseBadges(QString badgesString)
{ {
QMap<QString, QString> badges; QMap<QString, QString> badges;
for (auto badgeData : badgesString.split(',')) for (const auto &badgeData : badgesString.split(','))
{ {
auto parts = badgeData.split('/'); auto parts = badgeData.split('/');
if (parts.length() != 2) if (parts.length() != 2)
@ -332,7 +332,6 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
{ {
const auto &tags = message->tags(); const auto &tags = message->tags();
auto app = getApp();
// get twitch channel // get twitch channel
QString chanName; QString chanName;
@ -340,53 +339,50 @@ void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
{ {
return; 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 return;
decltype(tags.find("xD")) it;
if ((it = tags.find("room-id")) != tags.end())
{
auto roomId = it.value().toString();
twitchChannel->setRoomId(roomId);
}
// Room modes
{
auto roomModes = *twitchChannel->accessRoomModes();
if ((it = tags.find("emote-only")) != tags.end())
{
roomModes.emoteOnly = it.value() == "1";
}
if ((it = tags.find("subs-only")) != tags.end())
{
roomModes.submode = it.value() == "1";
}
if ((it = tags.find("slow")) != tags.end())
{
roomModes.slowMode = it.value().toInt();
}
if ((it = tags.find("r9k")) != 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())
{
roomModes.followerOnly = it.value().toInt();
}
twitchChannel->setRoomModes(roomModes);
}
twitchChannel->roomModesChanged.invoke();
} }
// room-id
if (auto it = tags.find("room-id"); it != tags.end())
{
auto roomId = it.value().toString();
twitchChannel->setRoomId(roomId);
}
// Room modes
{
auto roomModes = *twitchChannel->accessRoomModes();
if (auto it = tags.find("emote-only"); it != tags.end())
{
roomModes.emoteOnly = it.value() == "1";
}
if (auto it = tags.find("subs-only"); it != tags.end())
{
roomModes.submode = it.value() == "1";
}
if (auto it = tags.find("slow"); it != tags.end())
{
roomModes.slowMode = it.value().toInt();
}
if (auto it = tags.find("r9k"); it != tags.end())
{
roomModes.r9k = it.value() == "1";
}
if (auto it = tags.find("followers-only"); it != tags.end())
{
roomModes.followerOnly = it.value().toInt();
}
twitchChannel->setRoomModes(roomModes);
}
twitchChannel->roomModesChanged.invoke();
} }
void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
@ -403,10 +399,8 @@ void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
return; return;
} }
auto app = getApp();
// get channel // get channel
auto chan = app->twitch.server->getChannelOrEmpty(chanName); auto chan = getApp()->twitch.server->getChannelOrEmpty(chanName);
if (chan->isEmpty()) if (chan->isEmpty())
{ {
@ -443,10 +437,10 @@ void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
chan->addOrReplaceTimeout(timeoutMsg); chan->addOrReplaceTimeout(timeoutMsg);
// refresh all // refresh all
app->windows->repaintVisibleChatWidgets(chan.get()); getApp()->windows->repaintVisibleChatWidgets(chan.get());
if (getSettings()->hideModerated) if (getSettings()->hideModerated)
{ {
app->windows->forceLayoutChannelViews(); getApp()->windows->forceLayoutChannelViews();
} }
} }
@ -464,10 +458,8 @@ void IrcMessageHandler::handleClearMessageMessage(Communi::IrcMessage *message)
return; return;
} }
auto app = getApp();
// get channel // get channel
auto chan = app->twitch.server->getChannelOrEmpty(chanName); auto chan = getApp()->twitch.server->getChannelOrEmpty(chanName);
if (chan->isEmpty()) if (chan->isEmpty())
{ {
@ -483,15 +475,15 @@ void IrcMessageHandler::handleClearMessageMessage(Communi::IrcMessage *message)
QString targetID = tags.value("target-msg-id").toString(); QString targetID = tags.value("target-msg-id").toString();
auto msg = chan->findMessage(targetID); auto msg = chan->findMessage(targetID);
if (msg != nullptr) if (msg == nullptr)
return;
msg->flags.set(MessageFlag::Disabled);
if (!getSettings()->hideDeletionActions)
{ {
msg->flags.set(MessageFlag::Disabled); MessageBuilder builder;
if (!getSettings()->hideDeletionActions) TwitchMessageBuilder::deletionMessage(msg, &builder);
{ chan->addMessage(builder.release());
MessageBuilder builder;
TwitchMessageBuilder::deletionMessage(msg, &builder);
chan->addMessage(builder.release());
}
} }
} }
@ -545,42 +537,43 @@ void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message)
void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
{ {
auto app = getApp();
MessageParseArgs args; MessageParseArgs args;
args.isReceivedWhisper = true; 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), TwitchMessageBuilder builder(c, message, args, message->parameter(1),
false); false);
if (!builder.isIgnored()) if (builder.isIgnored())
{ {
builder->flags.set(MessageFlag::Whisper); return;
MessagePtr _message = builder.build(); }
builder.triggerHighlights();
app->twitch.server->lastUserThatWhisperedMe.set(builder.userName); builder->flags.set(MessageFlag::Whisper);
MessagePtr _message = builder.build();
builder.triggerHighlights();
if (_message->flags.has(MessageFlag::Highlighted)) getApp()->twitch.server->lastUserThatWhisperedMe.set(builder.userName);
{
app->twitch.server->mentionsChannel->addMessage(_message);
}
c->addMessage(_message); if (_message->flags.has(MessageFlag::Highlighted))
{
getApp()->twitch.server->mentionsChannel->addMessage(_message);
}
auto overrideFlags = boost::optional<MessageFlags>(_message->flags); c->addMessage(_message);
overrideFlags->set(MessageFlag::DoNotTriggerNotification);
overrideFlags->set(MessageFlag::DoNotLog);
if (getSettings()->inlineWhispers) auto overrideFlags = boost::optional<MessageFlags>(_message->flags);
{ overrideFlags->set(MessageFlag::DoNotTriggerNotification);
app->twitch.server->forEachChannel( overrideFlags->set(MessageFlag::DoNotLog);
[&_message, overrideFlags](ChannelPtr channel) {
channel->addMessage(_message, overrideFlags); if (getSettings()->inlineWhispers)
}); {
} getApp()->twitch.server->forEachChannel(
[&_message, overrideFlags](ChannelPtr channel) {
channel->addMessage(_message, overrideFlags);
});
} }
} }
@ -592,7 +585,7 @@ std::vector<MessagePtr> IrcMessageHandler::parseUserNoticeMessage(
auto tags = message->tags(); auto tags = message->tags();
auto parameters = message->parameters(); auto parameters = message->parameters();
QString msgType = tags.value("msg-id", "").toString(); QString msgType = tags.value("msg-id").toString();
QString content; QString content;
if (parameters.size() >= 2) if (parameters.size() >= 2)
{ {
@ -624,10 +617,11 @@ std::vector<MessagePtr> IrcMessageHandler::parseUserNoticeMessage(
if (msgType == "bitsbadgetier") if (msgType == "bitsbadgetier")
{ {
messageText = QString("%1 just earned a new %2 Bits badge!") messageText =
.arg(tags.value("display-name").toString()) QString("%1 just earned a new %2 Bits badge!")
.arg(kFormatNumbers( .arg(tags.value("display-name").toString(),
tags.value("msg-param-threshold").toInt())); kFormatNumbers(
tags.value("msg-param-threshold").toInt()));
} }
auto b = MessageBuilder(systemMessage, parseTagString(messageText), auto b = MessageBuilder(systemMessage, parseTagString(messageText),
@ -648,7 +642,7 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
auto parameters = message->parameters(); auto parameters = message->parameters();
auto target = parameters[0]; auto target = parameters[0];
QString msgType = tags.value("msg-id", "").toString(); QString msgType = tags.value("msg-id").toString();
QString content; QString content;
if (parameters.size() >= 2) if (parameters.size() >= 2)
{ {
@ -673,10 +667,11 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
if (msgType == "bitsbadgetier") if (msgType == "bitsbadgetier")
{ {
messageText = QString("%1 just earned a new %2 Bits badge!") messageText =
.arg(tags.value("display-name").toString()) QString("%1 just earned a new %2 Bits badge!")
.arg(kFormatNumbers( .arg(tags.value("display-name").toString(),
tags.value("msg-param-threshold").toInt())); kFormatNumbers(
tags.value("msg-param-threshold").toInt()));
} }
auto b = MessageBuilder(systemMessage, parseTagString(messageText), auto b = MessageBuilder(systemMessage, parseTagString(messageText),
@ -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( std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
Communi::IrcNoticeMessage *message) Communi::IrcNoticeMessage *message)
{ {
@ -740,7 +713,7 @@ std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
.arg(curUser->getUserName()); .arg(curUser->getUserName());
const auto loginPromptText = QString("Try adding your account again."); const auto loginPromptText = QString("Try adding your account again.");
auto builder = MessageBuilder(); MessageBuilder builder;
auto text = QString("%1 %2").arg(expirationText, loginPromptText); auto text = QString("%1 %2").arg(expirationText, loginPromptText);
builder.message().messageText = text; builder.message().messageText = text;
builder.message().searchText = text; builder.message().searchText = text;
@ -761,7 +734,7 @@ std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
{ {
return {generateBannedMessage(true)}; return {generateBannedMessage(true)};
} }
else if (message->tags()["msg-id"] == "msg_timedout") else if (message->tags().value("msg-id") == "msg_timedout")
{ {
std::vector<MessagePtr> builtMessage; std::vector<MessagePtr> builtMessage;
@ -776,23 +749,21 @@ std::vector<MessagePtr> IrcMessageHandler::parseNoticeMessage(
return builtMessage; return builtMessage;
} }
else
{
std::vector<MessagePtr> builtMessages;
builtMessages.emplace_back(makeSystemMessage( // default case
message->content(), calculateMessageTimestamp(message))); std::vector<MessagePtr> builtMessages;
return builtMessages; builtMessages.emplace_back(makeSystemMessage(
} message->content(), calculateMessageTimestamp(message)));
} // namespace chatterino
return builtMessages;
}
void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message) void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
{ {
auto app = getApp();
auto builtMessages = this->parseNoticeMessage(message); auto builtMessages = this->parseNoticeMessage(message);
for (auto msg : builtMessages) for (const auto &msg : builtMessages)
{ {
QString channelName; QString channelName;
if (!trimChannelName(message->target(), 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 // Notice wasn't targeted at a single channel, send to all twitch
// channels // channels
app->twitch.server->forEachChannelAndSpecialChannels( getApp()->twitch.server->forEachChannelAndSpecialChannels(
[msg](const auto &c) { [msg](const auto &c) {
c->addMessage(msg); c->addMessage(msg);
}); });
@ -808,7 +779,7 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
return; return;
} }
auto channel = app->twitch.server->getChannelOrEmpty(channelName); auto channel = getApp()->twitch.server->getChannelOrEmpty(channelName);
if (channel->isEmpty()) if (channel->isEmpty())
{ {
@ -818,7 +789,7 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
return; 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") if (tags == "bad_delete_message_error" || tags == "usage_delete")
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(makeSystemMessage(
@ -856,43 +827,45 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message)
{ {
auto app = getApp(); auto channel = getApp()->twitch.server->getChannelOrEmpty(
auto channel = app->twitch.server->getChannelOrEmpty(
message->parameter(0).remove(0, 1)); message->parameter(0).remove(0, 1));
if (TwitchChannel *twitchChannel = auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
dynamic_cast<TwitchChannel *>(channel.get())) if (!twitchChannel)
{ {
if (message->nick() != return;
getApp()->accounts->twitch.getCurrent()->getUserName() && }
getSettings()->showJoins.getValue())
{ if (message->nick() !=
twitchChannel->addJoinedUser(message->nick()); getApp()->accounts->twitch.getCurrent()->getUserName() &&
} getSettings()->showJoins.getValue())
{
twitchChannel->addJoinedUser(message->nick());
} }
} }
void IrcMessageHandler::handlePartMessage(Communi::IrcMessage *message) void IrcMessageHandler::handlePartMessage(Communi::IrcMessage *message)
{ {
auto app = getApp(); auto channel = getApp()->twitch.server->getChannelOrEmpty(
auto channel = app->twitch.server->getChannelOrEmpty(
message->parameter(0).remove(0, 1)); message->parameter(0).remove(0, 1));
if (TwitchChannel *twitchChannel = auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
dynamic_cast<TwitchChannel *>(channel.get())) if (!twitchChannel)
{ {
const auto selfAccountName = return;
getApp()->accounts->twitch.getCurrent()->getUserName(); }
if (message->nick() != selfAccountName &&
getSettings()->showParts.getValue())
{
twitchChannel->addPartedUser(message->nick());
}
if (message->nick() == selfAccountName) const auto selfAccountName =
{ getApp()->accounts->twitch.getCurrent()->getUserName();
channel->addMessage(generateBannedMessage(false)); if (message->nick() != selfAccountName &&
} getSettings()->showParts.getValue())
{
twitchChannel->addPartedUser(message->nick());
}
if (message->nick() == selfAccountName)
{
channel->addMessage(generateBannedMessage(false));
} }
} }
} // namespace chatterino } // namespace chatterino

View file

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

View file

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