Fix whisper receiving

Added setting to display them inline under "Special Channels" page

Store a twitch users color in the TwitchUser struct
this is useful if we ever want to use the users own color.
The users own color is only updated once he has written once in chat

Add helper method for calling function only on normal channels

Fixes #54
This commit is contained in:
Rasmus Karlsson 2018-02-04 16:33:46 +01:00
parent f9a25171bf
commit 556dbe0456
8 changed files with 106 additions and 27 deletions

View file

@ -138,5 +138,12 @@ void ChannelManager::doOnAll(std::function<void(ChannelPtr)> func)
func(this->mentionsChannel); func(this->mentionsChannel);
} }
void ChannelManager::doOnAllNormalChannels(std::function<void(ChannelPtr)> func)
{
for (const auto &channel : this->twitchChannels) {
func(std::get<0>(channel));
}
}
} // namespace chatterino } // namespace chatterino
} }

View file

@ -26,6 +26,7 @@ public:
const std::string &getUserID(const std::string &username); const std::string &getUserID(const std::string &username);
void doOnAll(std::function<void(ChannelPtr)> func); void doOnAll(std::function<void(ChannelPtr)> func);
void doOnAllNormalChannels(std::function<void(ChannelPtr)> func);
// Special channels // Special channels
const ChannelPtr whispersChannel; const ChannelPtr whispersChannel;

View file

@ -9,6 +9,7 @@
#include "singletons/resourcemanager.hpp" #include "singletons/resourcemanager.hpp"
#include "singletons/windowmanager.hpp" #include "singletons/windowmanager.hpp"
#include "twitch/twitchchannel.hpp" #include "twitch/twitchchannel.hpp"
#include "twitch/twitchmessagebuilder.hpp"
using namespace chatterino::messages; using namespace chatterino::messages;
@ -139,7 +140,29 @@ void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message)
void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *message)
{ {
// TODO: Implement debug::Log("Received whisper!");
messages::MessageParseArgs args;
args.isReceivedWhisper = true;
auto c = this->channelManager.whispersChannel.get();
twitch::TwitchMessageBuilder builder(c, message, message->parameter(1), args);
if (!builder.isIgnored()) {
messages::MessagePtr _message = builder.build();
if (_message->flags & messages::Message::Highlighted) {
singletons::ChannelManager::getInstance().mentionsChannel->addMessage(_message);
}
c->addMessage(_message);
if (SettingManager::getInstance().inlineWhispers) {
this->channelManager.doOnAllNormalChannels([_message](ChannelPtr channel) {
channel->addMessage(_message); //
});
}
}
} }
void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message)
@ -202,6 +225,7 @@ void IrcMessageHandler::handleWriteConnectionNoticeMessage(Communi::IrcNoticeMes
this->handleNoticeMessage(message); this->handleNoticeMessage(message);
} }
} // namespace helper } // namespace helper
} // namespace singletons } // namespace singletons
} // namespace chatterino } // namespace chatterino

View file

@ -27,6 +27,7 @@ public:
void handleNoticeMessage(Communi::IrcNoticeMessage *message); void handleNoticeMessage(Communi::IrcNoticeMessage *message);
void handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message); void handleWriteConnectionNoticeMessage(Communi::IrcNoticeMessage *message);
}; };
}
} } // namespace helper
} } // namespace singletons
} // namespace chatterino

View file

@ -1,5 +1,6 @@
#include "twitchmessagebuilder.hpp" #include "twitchmessagebuilder.hpp"
#include "debug/log.hpp" #include "debug/log.hpp"
#include "singletons/accountmanager.hpp"
#include "singletons/emotemanager.hpp" #include "singletons/emotemanager.hpp"
#include "singletons/ircmanager.hpp" #include "singletons/ircmanager.hpp"
#include "singletons/resourcemanager.hpp" #include "singletons/resourcemanager.hpp"
@ -26,8 +27,22 @@ TwitchMessageBuilder::TwitchMessageBuilder(Channel *_channel,
, args(_args) , args(_args)
, tags(this->ircMessage->tags()) , tags(this->ircMessage->tags())
, usernameColor(singletons::ThemeManager::getInstance().messages.textColors.system) , usernameColor(singletons::ThemeManager::getInstance().messages.textColors.system)
, originalMessage(_ircMessage->content())
, action(_ircMessage->isAction())
{
}
TwitchMessageBuilder::TwitchMessageBuilder(Channel *_channel,
const Communi::IrcMessage *_ircMessage, QString content,
const messages::MessageParseArgs &_args)
: channel(_channel)
, twitchChannel(dynamic_cast<TwitchChannel *>(_channel))
, ircMessage(_ircMessage)
, args(_args)
, tags(this->ircMessage->tags())
, usernameColor(singletons::ThemeManager::getInstance().messages.textColors.system)
, originalMessage(content)
{ {
this->originalMessage = this->ircMessage->content();
} }
bool TwitchMessageBuilder::isIgnored() const bool TwitchMessageBuilder::isIgnored() const
@ -124,8 +139,8 @@ MessagePtr TwitchMessageBuilder::build()
long int i = 0; long int i = 0;
for (QString split : splits) { for (QString split : splits) {
MessageColor textColor = ircMessage->isAction() ? MessageColor(this->usernameColor) MessageColor textColor =
: MessageColor(MessageColor::Text); this->action ? MessageColor(this->usernameColor) : MessageColor(MessageColor::Text);
// twitch emote // twitch emote
if (currentTwitchEmote != twitchEmotes.end() && currentTwitchEmote->first == i) { if (currentTwitchEmote != twitchEmotes.end() && currentTwitchEmote->first == i) {
@ -243,7 +258,7 @@ void TwitchMessageBuilder::parseUsername()
} }
// username // username
this->userName = ircMessage->nick(); this->userName = this->ircMessage->nick();
if (this->userName.isEmpty()) { if (this->userName.isEmpty()) {
this->userName = this->tags.value(QLatin1String("login")).toString(); this->userName = this->tags.value(QLatin1String("login")).toString();
@ -308,20 +323,36 @@ void TwitchMessageBuilder::appendUsername()
if (this->args.isSentWhisper) { if (this->args.isSentWhisper) {
// TODO(pajlada): Re-implement // TODO(pajlada): Re-implement
// userDisplayString += IrcManager::getInstance().getUser().getUserName(); // userDisplayString += IrcManager::getInstance().getUser().getUserName();
} } else if (this->args.isReceivedWhisper) {
// Sender username
this->emplace<TextElement>(usernameText, MessageElement::Text, this->usernameColor,
FontStyle::MediumBold)
->setLink({Link::UserInfo, this->userName});
if (this->args.isReceivedWhisper) { auto currentUser = singletons::AccountManager::getInstance().Twitch.getCurrent();
// TODO(pajlada): Re-implement
// userDisplayString += " -> " + IrcManager::getInstance().getUser().getUserName();
}
if (!ircMessage->isAction()) { // Separator
usernameText += ":"; this->emplace<TextElement>(
} "->", MessageElement::Text,
singletons::ThemeManager::getInstance().messages.textColors.system, FontStyle::Medium);
this->emplace<TextElement>(usernameText, MessageElement::Text, this->usernameColor, QColor selfColor = currentUser->color;
FontStyle::MediumBold) if (!selfColor.isValid()) {
->setLink({Link::UserInfo, this->userName}); selfColor = singletons::ThemeManager::getInstance().messages.textColors.system;
}
// Your own username
this->emplace<TextElement>(currentUser->getUserName() + ":", MessageElement::Text,
selfColor, FontStyle::MediumBold);
} else {
if (!this->action) {
usernameText += ":";
}
this->emplace<TextElement>(usernameText, MessageElement::Text, this->usernameColor,
FontStyle::MediumBold)
->setLink({Link::UserInfo, this->userName});
}
} }
void TwitchMessageBuilder::parseHighlights() void TwitchMessageBuilder::parseHighlights()
@ -329,11 +360,12 @@ void TwitchMessageBuilder::parseHighlights()
static auto player = new QMediaPlayer; static auto player = new QMediaPlayer;
static QUrl currentPlayerUrl; static QUrl currentPlayerUrl;
singletons::SettingManager &settings = singletons::SettingManager::getInstance(); singletons::SettingManager &settings = singletons::SettingManager::getInstance();
static pajlada::Settings::Setting<std::string> currentUser("/accounts/current"); auto currentUser = singletons::AccountManager::getInstance().Twitch.getCurrent();
QString currentUsername = QString::fromStdString(currentUser.getValue()); QString currentUsername = currentUser->getUserName();
if (this->ircMessage->nick() == currentUsername) { if (this->ircMessage->nick() == currentUsername) {
currentUser->color = this->usernameColor;
// Do nothing. Highlights cannot be triggered by yourself // Do nothing. Highlights cannot be triggered by yourself
return; return;
} }
@ -412,7 +444,7 @@ void TwitchMessageBuilder::parseHighlights()
} }
} }
void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcPrivateMessage *ircMessage, void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcMessage *ircMessage,
const QString &emote, const QString &emote,
std::vector<std::pair<long int, util::EmoteData>> &vec) std::vector<std::pair<long int, util::EmoteData>> &vec)
{ {
@ -441,11 +473,11 @@ void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcPrivateMessage *i
long int start = std::stol(coords.at(0).toStdString(), nullptr, 10); long int start = std::stol(coords.at(0).toStdString(), nullptr, 10);
long int end = std::stol(coords.at(1).toStdString(), nullptr, 10); long int end = std::stol(coords.at(1).toStdString(), nullptr, 10);
if (start >= end || start < 0 || end > ircMessage->content().length()) { if (start >= end || start < 0 || end > this->originalMessage.length()) {
return; return;
} }
QString name = ircMessage->content().mid(start, end - start + 1); QString name = this->originalMessage.mid(start, end - start + 1);
vec.push_back( vec.push_back(
std::pair<long int, util::EmoteData>(start, emoteManager.getTwitchEmoteById(id, name))); std::pair<long int, util::EmoteData>(start, emoteManager.getTwitchEmoteById(id, name)));

View file

@ -28,10 +28,12 @@ public:
explicit TwitchMessageBuilder(Channel *_channel, const Communi::IrcPrivateMessage *_ircMessage, explicit TwitchMessageBuilder(Channel *_channel, const Communi::IrcPrivateMessage *_ircMessage,
const messages::MessageParseArgs &_args); const messages::MessageParseArgs &_args);
explicit TwitchMessageBuilder(Channel *_channel, const Communi::IrcMessage *_ircMessage,
QString content, const messages::MessageParseArgs &_args);
Channel *channel; Channel *channel;
TwitchChannel *twitchChannel; TwitchChannel *twitchChannel;
const Communi::IrcPrivateMessage *ircMessage; const Communi::IrcMessage *ircMessage;
messages::MessageParseArgs args; messages::MessageParseArgs args;
const QVariantMap tags; const QVariantMap tags;
@ -45,7 +47,9 @@ private:
QString roomID; QString roomID;
QColor usernameColor; QColor usernameColor;
QString originalMessage; const QString originalMessage;
const bool action = false;
void parseMessageID(); void parseMessageID();
void parseRoomID(); void parseRoomID();
@ -54,7 +58,7 @@ private:
void appendUsername(); void appendUsername();
void parseHighlights(); void parseHighlights();
void appendTwitchEmote(const Communi::IrcPrivateMessage *ircMessage, const QString &emote, void appendTwitchEmote(const Communi::IrcMessage *ircMessage, const QString &emote,
std::vector<std::pair<long, util::EmoteData>> &vec); std::vector<std::pair<long, util::EmoteData>> &vec);
bool tryAppendEmote(QString &emoteString); bool tryAppendEmote(QString &emoteString);

View file

@ -2,6 +2,7 @@
#include "ircaccount.hpp" #include "ircaccount.hpp"
#include <QColor>
#include <QString> #include <QString>
namespace chatterino { namespace chatterino {
@ -27,6 +28,8 @@ public:
bool isAnon() const; bool isAnon() const;
QColor color;
private: private:
QString _oauthClient; QString _oauthClient;
QString _oauthToken; QString _oauthToken;

View file

@ -13,6 +13,7 @@ namespace settingspages {
SpecialChannelsPage::SpecialChannelsPage() SpecialChannelsPage::SpecialChannelsPage()
: SettingsPage("Special channels", "") : SettingsPage("Special channels", "")
{ {
singletons::SettingManager &settings = singletons::SettingManager::getInstance();
util::LayoutCreator<SpecialChannelsPage> layoutCreator(this); util::LayoutCreator<SpecialChannelsPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>(); auto layout = layoutCreator.setLayoutType<QVBoxLayout>();
@ -21,6 +22,12 @@ SpecialChannelsPage::SpecialChannelsPage()
mentions.emplace<QLabel>("Join /mentions to view your mentions."); mentions.emplace<QLabel>("Join /mentions to view your mentions.");
} }
auto whispers = layout.emplace<QGroupBox>("Whispers").setLayoutType<QVBoxLayout>();
{
whispers.emplace<QLabel>("Join /whispers to view your mentions.");
whispers.append(this->createCheckBox("Show whispers inline", settings.inlineWhispers));
}
layout->addStretch(1); layout->addStretch(1);
} }
} // namespace settingspages } // namespace settingspages