From 4a2709cbc398e5c2504ac3c4718dd76a3fa019b3 Mon Sep 17 00:00:00 2001 From: fourtf Date: Mon, 4 Jun 2018 12:23:23 +0200 Subject: [PATCH] fixed resub messages --- src/messages/layouts/messagelayout.cpp | 2 + src/messages/message.cpp | 15 +++- src/messages/message.hpp | 2 + src/messages/messageparseargs.hpp | 1 + src/providers/irc/abstractircserver.hpp | 2 +- src/providers/twitch/ircmessagehandler.cpp | 85 ++++++++++++++++++- src/providers/twitch/ircmessagehandler.hpp | 10 ++- src/providers/twitch/twitchmessagebuilder.cpp | 15 ++++ src/providers/twitch/twitchserver.cpp | 28 +----- src/singletons/thememanager.cpp | 8 +- src/singletons/thememanager.hpp | 3 +- src/util/irchelpers.hpp | 2 +- src/widgets/helper/scrollbarhighlight.hpp | 2 +- src/widgets/scrollbar.cpp | 2 + 14 files changed, 141 insertions(+), 36 deletions(-) diff --git a/src/messages/layouts/messagelayout.cpp b/src/messages/layouts/messagelayout.cpp index 372c10255..b80ef6f81 100644 --- a/src/messages/layouts/messagelayout.cpp +++ b/src/messages/layouts/messagelayout.cpp @@ -225,6 +225,8 @@ void MessageLayout::updateBuffer(QPixmap *buffer, int /*messageIndex*/, Selectio QColor backgroundColor; if (this->message_->flags & Message::Highlighted) { backgroundColor = app->themes->messages.backgrounds.highlighted; + } else if (this->message_->flags & Message::Subscription) { + backgroundColor = app->themes->messages.backgrounds.subscription; } else if (app->settings->alternateMessageBackground.getValue() && this->flags & MessageLayout::AlternateBackground) { backgroundColor = app->themes->messages.backgrounds.alternate; diff --git a/src/messages/message.cpp b/src/messages/message.cpp index 3b91f583d..5d0b13552 100644 --- a/src/messages/message.cpp +++ b/src/messages/message.cpp @@ -21,6 +21,8 @@ SBHighlight Message::getScrollBarHighlight() const { if (this->flags & Message::Highlighted) { return SBHighlight(SBHighlight::Highlight); + } else if (this->flags & Message::Subscription) { + return SBHighlight(SBHighlight::Subscription); } return SBHighlight(); } @@ -39,6 +41,17 @@ MessagePtr Message::createSystemMessage(const QString &text) return message; } +MessagePtr Message::createMessage(const QString &text) +{ + MessagePtr message(new Message); + + message->addElement(new TimestampElement(QTime::currentTime())); + message->addElement(new TextElement(text, MessageElement::Text, MessageColor::Text)); + message->searchText = text; + + return message; +} + MessagePtr Message::createTimeoutMessage(const QString &username, const QString &durationInSeconds, const QString &reason, bool multipleTimes) { @@ -64,7 +77,7 @@ MessagePtr Message::createTimeoutMessage(const QString &username, const QString if (reason.length() > 0) { text.append(": \""); - text.append(util::ParseTagString(reason)); + text.append(util::parseTagString(reason)); text.append("\""); } text.append("."); diff --git a/src/messages/message.hpp b/src/messages/message.hpp index 5465ac960..7e95eb45e 100644 --- a/src/messages/message.hpp +++ b/src/messages/message.hpp @@ -41,6 +41,7 @@ struct Message { DisconnectedMessage = (1 << 8), Untimeout = (1 << 9), PubSub = (1 << 10), + Subscription = (1 << 11), }; util::FlagsEnum flags; @@ -66,6 +67,7 @@ private: public: static std::shared_ptr createSystemMessage(const QString &text); + static std::shared_ptr createMessage(const QString &text); static std::shared_ptr createTimeoutMessage(const QString &username, const QString &durationInSeconds, diff --git a/src/messages/messageparseargs.hpp b/src/messages/messageparseargs.hpp index e9b0b4db6..4196f6552 100644 --- a/src/messages/messageparseargs.hpp +++ b/src/messages/messageparseargs.hpp @@ -7,6 +7,7 @@ struct MessageParseArgs { bool disablePingSounds = false; bool isReceivedWhisper = false; bool isSentWhisper = false; + bool trimSubscriberUsername = false; }; } // namespace messages diff --git a/src/providers/irc/abstractircserver.hpp b/src/providers/irc/abstractircserver.hpp index ac4b0274b..fb41b1313 100644 --- a/src/providers/irc/abstractircserver.hpp +++ b/src/providers/irc/abstractircserver.hpp @@ -33,7 +33,7 @@ public: // signals pajlada::Signals::NoArgSignal connected; pajlada::Signals::NoArgSignal disconnected; - pajlada::Signals::Signal onPrivateMessage; + // pajlada::Signals::Signal onPrivateMessage; void addFakeMessage(const QString &data); diff --git a/src/providers/twitch/ircmessagehandler.cpp b/src/providers/twitch/ircmessagehandler.cpp index e6c1802ca..e7fb47955 100644 --- a/src/providers/twitch/ircmessagehandler.cpp +++ b/src/providers/twitch/ircmessagehandler.cpp @@ -1,6 +1,7 @@ #include "ircmessagehandler.hpp" #include "application.hpp" +#include "controllers/highlights/highlightcontroller.hpp" #include "debug/log.hpp" #include "messages/limitedqueue.hpp" #include "messages/message.hpp" @@ -10,6 +11,9 @@ #include "providers/twitch/twitchserver.hpp" #include "singletons/resourcemanager.hpp" #include "singletons/windowmanager.hpp" +#include "util/irchelpers.hpp" + +#include using namespace chatterino::singletons; using namespace chatterino::messages; @@ -24,6 +28,49 @@ IrcMessageHandler &IrcMessageHandler::getInstance() return instance; } +void IrcMessageHandler::handlePrivMessage(Communi::IrcPrivateMessage *message, TwitchServer &server) +{ + this->addMessage(message, message->target(), message->content(), server, false); +} + +void IrcMessageHandler::addMessage(Communi::IrcMessage *message, const QString &target, + const QString &content, TwitchServer &server, bool isSub) +{ + QString channelName; + if (!trimChannelName(target, channelName)) { + return; + } + + auto chan = server.getChannelOrEmpty(channelName); + + if (chan->isEmpty()) { + return; + } + + messages::MessageParseArgs args; + if (isSub) { + args.trimSubscriberUsername = true; + } + + TwitchMessageBuilder builder(chan.get(), message, content, args); + + if (isSub || !builder.isIgnored()) { + messages::MessagePtr msg = builder.build(); + + if (isSub) { + msg->flags |= messages::Message::Subscription; + msg->flags &= ~messages::Message::Highlighted; + } else { + if (msg->flags & messages::Message::Subscription) { + server.mentionsChannel->addMessage(msg); + getApp()->highlights->addHighlight(msg); + } + } + + chan->addMessage(msg); + } +} + void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message) { const auto &tags = message->tags(); @@ -178,9 +225,43 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message) } } -void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message) +void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message, TwitchServer &server) { - // do nothing + auto data = message->toData(); + static QRegularExpression findMessage(" USERNOTICE (#\\w+) :(.+)$"); + + auto match = findMessage.match(data); + auto target = match.captured(1); + + if (match.hasMatch()) { + this->addMessage(message, target, match.captured(2), server, true); + } + + auto tags = message->tags(); + auto it = tags.find("system-msg"); + + if (it != tags.end()) { + auto newMessage = + messages::Message::createSystemMessage(util::parseTagString(it.value().toString())); + + newMessage->flags |= messages::Message::Subscription; + + QString channelName; + + if (message->parameters().size() < 1) { + return; + } + + if (!trimChannelName(message->parameter(0), channelName)) { + return; + } + + auto chan = server.getChannelOrEmpty(channelName); + + if (!chan->isEmpty()) { + chan->addMessage(newMessage); + } + } } void IrcMessageHandler::handleModeMessage(Communi::IrcMessage *message) diff --git a/src/providers/twitch/ircmessagehandler.hpp b/src/providers/twitch/ircmessagehandler.hpp index d0c242860..dca4c16b8 100644 --- a/src/providers/twitch/ircmessagehandler.hpp +++ b/src/providers/twitch/ircmessagehandler.hpp @@ -6,6 +6,8 @@ namespace chatterino { namespace providers { namespace twitch { +class TwitchServer; + class IrcMessageHandler { IrcMessageHandler() = default; @@ -13,17 +15,23 @@ class IrcMessageHandler public: static IrcMessageHandler &getInstance(); + void handlePrivMessage(Communi::IrcPrivateMessage *message, TwitchServer &server); + void handleRoomStateMessage(Communi::IrcMessage *message); void handleClearChatMessage(Communi::IrcMessage *message); void handleUserStateMessage(Communi::IrcMessage *message); void handleWhisperMessage(Communi::IrcMessage *message); - void handleUserNoticeMessage(Communi::IrcMessage *message); + void handleUserNoticeMessage(Communi::IrcMessage *message, TwitchServer &server); void handleModeMessage(Communi::IrcMessage *message); void handleNoticeMessage(Communi::IrcNoticeMessage *message); void handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message); void handleJoinMessage(Communi::IrcMessage *message); void handlePartMessage(Communi::IrcMessage *message); + +private: + void addMessage(Communi::IrcMessage *message, const QString &target, const QString &content, + TwitchServer &server, bool isResub); }; } // namespace twitch diff --git a/src/providers/twitch/twitchmessagebuilder.cpp b/src/providers/twitch/twitchmessagebuilder.cpp index 040269740..8f1ca38b1 100644 --- a/src/providers/twitch/twitchmessagebuilder.cpp +++ b/src/providers/twitch/twitchmessagebuilder.cpp @@ -292,6 +292,21 @@ void TwitchMessageBuilder::parseUsername() this->userName = this->tags.value(QLatin1String("login")).toString(); } + if (this->args.trimSubscriberUsername) { + static QRegularExpression fixName("^tmi.twitch.tv\\((\\w+)\\)$"); + + auto match = fixName.match(this->userName); + if (match.hasMatch()) { + this->userName = match.captured(1); + } + } + + // display name + // auto displayNameVariant = this->tags.value("display-name"); + // if (displayNameVariant.isValid()) { + // this->userName = displayNameVariant.toString() + " (" + this->userName + ")"; + // } + this->message->loginName = this->userName; } diff --git a/src/providers/twitch/twitchserver.cpp b/src/providers/twitch/twitchserver.cpp index 59661a259..62fd410c0 100644 --- a/src/providers/twitch/twitchserver.cpp +++ b/src/providers/twitch/twitchserver.cpp @@ -77,31 +77,7 @@ std::shared_ptr TwitchServer::createChannel(const QString &channelName) void TwitchServer::privateMessageReceived(IrcPrivateMessage *message) { - QString channelName; - if (!trimChannelName(message->target(), channelName)) { - return; - } - - this->onPrivateMessage.invoke(message); - auto chan = this->getChannelOrEmpty(channelName); - - if (chan->isEmpty()) { - return; - } - - messages::MessageParseArgs args; - - TwitchMessageBuilder builder(chan.get(), message, args); - - if (!builder.isIgnored()) { - messages::MessagePtr msg = builder.build(); - if (msg->flags & messages::Message::Highlighted) { - this->mentionsChannel->addMessage(msg); - getApp()->highlights->addHighlight(msg); - } - - chan->addMessage(msg); - } + IrcMessageHandler::getInstance().handlePrivMessage(message, *this); } void TwitchServer::messageReceived(IrcMessage *message) @@ -125,7 +101,7 @@ void TwitchServer::messageReceived(IrcMessage *message) } else if (command == "WHISPER") { handler.handleWhisperMessage(message); } else if (command == "USERNOTICE") { - handler.handleUserNoticeMessage(message); + handler.handleUserNoticeMessage(message, *this); } else if (command == "MODE") { handler.handleModeMessage(message); } else if (command == "NOTICE") { diff --git a/src/singletons/thememanager.cpp b/src/singletons/thememanager.cpp index b61692e53..983d4711a 100644 --- a/src/singletons/thememanager.cpp +++ b/src/singletons/thememanager.cpp @@ -117,6 +117,7 @@ void ThemeManager::actuallyUpdate(double hue, double multiplier) // scrollbar this->scrollbars.highlights.highlight = QColor("#ee6166"); + this->scrollbars.highlights.subscription = QColor("#C466FF"); // this->tabs.newMessage = { // fg, @@ -171,7 +172,10 @@ void ThemeManager::actuallyUpdate(double hue, double multiplier) this->messages.backgrounds.regular = splits.background; this->messages.backgrounds.alternate = getColor(0, sat, 0.93); this->messages.backgrounds.highlighted = - blendColors(themeColor, this->messages.backgrounds.regular, 0.8); + blendColors(themeColor, this->messages.backgrounds.regular, 0.6); + this->messages.backgrounds.subscription = + blendColors(QColor("#C466FF"), this->messages.backgrounds.regular, 0.7); + // this->messages.backgrounds.resub // this->messages.backgrounds.whisper this->messages.disabled = getColor(0, sat, 1, 0.6); @@ -180,7 +184,7 @@ void ThemeManager::actuallyUpdate(double hue, double multiplier) // Scrollbar this->scrollbars.background = splits.background; - this->scrollbars.background.setAlphaF(qreal(0.4)); + this->scrollbars.background.setAlphaF(qreal(0.2)); this->scrollbars.thumb = getColor(0, sat, 0.80); this->scrollbars.thumbSelected = getColor(0, sat, 0.7); diff --git a/src/singletons/thememanager.hpp b/src/singletons/thememanager.hpp index 04ebcbd4f..632541308 100644 --- a/src/singletons/thememanager.hpp +++ b/src/singletons/thememanager.hpp @@ -98,7 +98,7 @@ public: QColor regular; QColor alternate; QColor highlighted; - // QColor resub; + QColor subscription; // QColor whisper; } backgrounds; @@ -115,6 +115,7 @@ public: QColor thumbSelected; struct { QColor highlight; + QColor subscription; } highlights; } scrollbars; diff --git a/src/util/irchelpers.hpp b/src/util/irchelpers.hpp index 9bf215022..fa0b559f8 100644 --- a/src/util/irchelpers.hpp +++ b/src/util/irchelpers.hpp @@ -5,7 +5,7 @@ namespace chatterino { namespace util { -inline QString ParseTagString(const QString &input) +inline QString parseTagString(const QString &input) { QString output = input; output.detach(); diff --git a/src/widgets/helper/scrollbarhighlight.hpp b/src/widgets/helper/scrollbarhighlight.hpp index 7dbccee15..4fb9ee93b 100644 --- a/src/widgets/helper/scrollbarhighlight.hpp +++ b/src/widgets/helper/scrollbarhighlight.hpp @@ -9,7 +9,7 @@ class ScrollbarHighlight { public: enum Style : char { None, Default, Line }; - enum Color : char { Highlight }; + enum Color : char { Highlight, Subscription }; ScrollbarHighlight(); ScrollbarHighlight(Color _color, Style _style = Default); diff --git a/src/widgets/scrollbar.cpp b/src/widgets/scrollbar.cpp index e75506902..ae8c193d1 100644 --- a/src/widgets/scrollbar.cpp +++ b/src/widgets/scrollbar.cpp @@ -242,6 +242,8 @@ void Scrollbar::paintEvent(QPaintEvent *) switch (highlight.getColor()) { case ScrollbarHighlight::Highlight: return app->themes->scrollbars.highlights.highlight; + case ScrollbarHighlight::Subscription: + return app->themes->scrollbars.highlights.subscription; } return QColor(); }();