chore: refactor TwitchIrcServer (#5421)

This commit is contained in:
pajlada
2024-06-01 14:56:40 +02:00
committed by GitHub
parent 2a46ee708e
commit b6dc5d9e03
28 changed files with 373 additions and 163 deletions
+25 -8
View File
@@ -17,7 +17,24 @@ class Channel;
using ChannelPtr = std::shared_ptr<Channel>;
class RatelimitBucket;
class AbstractIrcServer : public QObject
class IAbstractIrcServer
{
public:
virtual void connect() = 0;
virtual void sendRawMessage(const QString &rawMessage) = 0;
virtual ChannelPtr getOrAddChannel(const QString &dirtyChannelName) = 0;
virtual ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName) = 0;
virtual void addFakeMessage(const QString &data) = 0;
virtual void addGlobalSystemMessage(const QString &messageText) = 0;
virtual void forEachChannel(std::function<void(ChannelPtr)> func) = 0;
};
class AbstractIrcServer : public IAbstractIrcServer, public QObject
{
public:
enum ConnectionType { Read = 1, Write = 2, Both = 3 };
@@ -33,27 +50,27 @@ public:
void initializeIrc();
// connection
void connect();
void connect() final;
void disconnect();
void sendMessage(const QString &channelName, const QString &message);
virtual void sendRawMessage(const QString &rawMessage);
void sendRawMessage(const QString &rawMessage) override;
// channels
ChannelPtr getOrAddChannel(const QString &dirtyChannelName);
ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName);
ChannelPtr getOrAddChannel(const QString &dirtyChannelName) final;
ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName) final;
std::vector<std::weak_ptr<Channel>> getChannels();
// signals
pajlada::Signals::NoArgSignal connected;
pajlada::Signals::NoArgSignal disconnected;
void addFakeMessage(const QString &data);
void addFakeMessage(const QString &data) final;
void addGlobalSystemMessage(const QString &messageText);
void addGlobalSystemMessage(const QString &messageText) final;
// iteration
void forEachChannel(std::function<void(ChannelPtr)> func);
void forEachChannel(std::function<void(ChannelPtr)> func) final;
protected:
AbstractIrcServer();
+1 -1
View File
@@ -244,7 +244,7 @@ void IrcServer::privateMessageReceived(Communi::IrcPrivateMessage *message)
if (highlighted && showInMentions)
{
getApp()->twitch->mentionsChannel->addMessage(msg);
getIApp()->getTwitch()->getMentionsChannel()->addMessage(msg);
}
}
else
+30 -26
View File
@@ -13,6 +13,7 @@
#include "messages/MessageColor.hpp"
#include "messages/MessageElement.hpp"
#include "messages/MessageThread.hpp"
#include "providers/irc/AbstractIrcServer.hpp"
#include "providers/twitch/ChannelPointReward.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchAccountManager.hpp"
@@ -169,7 +170,7 @@ void updateReplyParticipatedStatus(const QVariantMap &tags,
}
ChannelPtr channelOrEmptyByTarget(const QString &target,
TwitchIrcServer &server)
IAbstractIrcServer &server)
{
QString channelName;
if (!trimChannelName(target, channelName))
@@ -677,9 +678,10 @@ std::vector<MessagePtr> IrcMessageHandler::parseMessageWithReply(
}
void IrcMessageHandler::handlePrivMessage(Communi::IrcPrivateMessage *message,
TwitchIrcServer &server)
ITwitchIrcServer &twitchServer,
IAbstractIrcServer &abstractIrcServer)
{
auto chan = channelOrEmptyByTarget(message->target(), server);
auto chan = channelOrEmptyByTarget(message->target(), abstractIrcServer);
if (chan->isEmpty())
{
return;
@@ -710,8 +712,8 @@ void IrcMessageHandler::handlePrivMessage(Communi::IrcPrivateMessage *message,
// https://mm2pl.github.io/emoji_rfc.pdf for more details
this->addMessage(
message, chan,
message->content().replace(COMBINED_FIXER, ZERO_WIDTH_JOINER), server,
false, message->isAction());
message->content().replace(COMBINED_FIXER, ZERO_WIDTH_JOINER),
twitchServer, false, message->isAction());
if (message->tags().contains(u"pinned-chat-paid-amount"_s))
{
@@ -733,7 +735,7 @@ void IrcMessageHandler::handleRoomStateMessage(Communi::IrcMessage *message)
{
return;
}
auto chan = getApp()->twitch->getChannelOrEmpty(chanName);
auto chan = getIApp()->getTwitchAbstract()->getChannelOrEmpty(chanName);
auto *twitchChannel = dynamic_cast<TwitchChannel *>(chan.get());
if (!twitchChannel)
@@ -795,7 +797,7 @@ void IrcMessageHandler::handleClearChatMessage(Communi::IrcMessage *message)
}
// get channel
auto chan = getApp()->twitch->getChannelOrEmpty(chanName);
auto chan = getIApp()->getTwitchAbstract()->getChannelOrEmpty(chanName);
if (chan->isEmpty())
{
@@ -839,7 +841,7 @@ void IrcMessageHandler::handleClearMessageMessage(Communi::IrcMessage *message)
}
// get channel
auto chan = getApp()->twitch->getChannelOrEmpty(chanName);
auto chan = getIApp()->getTwitchAbstract()->getChannelOrEmpty(chanName);
if (chan->isEmpty())
{
@@ -888,7 +890,7 @@ void IrcMessageHandler::handleUserStateMessage(Communi::IrcMessage *message)
return;
}
auto c = getApp()->twitch->getChannelOrEmpty(channelName);
auto c = getIApp()->getTwitchAbstract()->getChannelOrEmpty(channelName);
if (c->isEmpty())
{
return;
@@ -943,7 +945,7 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *ircMessage)
args.isReceivedWhisper = true;
auto *c = getApp()->twitch->whispersChannel.get();
auto *c = getIApp()->getTwitch()->getWhispersChannel().get();
TwitchMessageBuilder builder(
c, ircMessage, args,
@@ -959,11 +961,11 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *ircMessage)
MessagePtr message = builder.build();
builder.triggerHighlights();
getApp()->twitch->lastUserThatWhisperedMe.set(builder.userName);
getIApp()->getTwitch()->setLastUserThatWhisperedMe(builder.userName);
if (message->flags.has(MessageFlag::ShowInMentions))
{
getApp()->twitch->mentionsChannel->addMessage(message);
getIApp()->getTwitch()->getMentionsChannel()->addMessage(message);
}
c->addMessage(message);
@@ -976,15 +978,16 @@ void IrcMessageHandler::handleWhisperMessage(Communi::IrcMessage *ircMessage)
!(getSettings()->streamerModeSuppressInlineWhispers &&
getIApp()->getStreamerMode()->isEnabled()))
{
getApp()->twitch->forEachChannel(
getIApp()->getTwitchAbstract()->forEachChannel(
[&message, overrideFlags](ChannelPtr channel) {
channel->addMessage(message, overrideFlags);
});
}
}
void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
TwitchIrcServer &server)
void IrcMessageHandler::handleUserNoticeMessage(
Communi::IrcMessage *message, ITwitchIrcServer &twitchServer,
IAbstractIrcServer &abstractIrcServer)
{
auto tags = message->tags();
auto parameters = message->parameters();
@@ -997,7 +1000,7 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
content = parameters[1];
}
auto chn = server.getChannelOrEmpty(target);
auto chn = abstractIrcServer.getChannelOrEmpty(target);
if (isIgnoredMessage({
.message = content,
.twitchUserID = tags.value("user-id").toString(),
@@ -1013,7 +1016,7 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
// Messages are not required, so they might be empty
if (!content.isEmpty())
{
this->addMessage(message, chn, content, server, true, false);
this->addMessage(message, chn, content, twitchServer, true, false);
}
}
@@ -1090,7 +1093,7 @@ void IrcMessageHandler::handleUserNoticeMessage(Communi::IrcMessage *message,
return;
}
auto chan = server.getChannelOrEmpty(channelName);
auto chan = abstractIrcServer.getChannelOrEmpty(channelName);
if (!chan->isEmpty())
{
@@ -1111,7 +1114,7 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
{
// Notice wasn't targeted at a single channel, send to all twitch
// channels
getApp()->twitch->forEachChannelAndSpecialChannels(
getIApp()->getTwitch()->forEachChannelAndSpecialChannels(
[msg](const auto &c) {
c->addMessage(msg);
});
@@ -1119,7 +1122,8 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
return;
}
auto channel = getApp()->twitch->getChannelOrEmpty(channelName);
auto channel =
getIApp()->getTwitchAbstract()->getChannelOrEmpty(channelName);
if (channel->isEmpty())
{
@@ -1202,8 +1206,8 @@ void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message)
void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message)
{
auto channel =
getApp()->twitch->getChannelOrEmpty(message->parameter(0).remove(0, 1));
auto channel = getIApp()->getTwitchAbstract()->getChannelOrEmpty(
message->parameter(0).remove(0, 1));
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (!twitchChannel)
@@ -1225,8 +1229,8 @@ void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message)
void IrcMessageHandler::handlePartMessage(Communi::IrcMessage *message)
{
auto channel =
getApp()->twitch->getChannelOrEmpty(message->parameter(0).remove(0, 1));
auto channel = getIApp()->getTwitchAbstract()->getChannelOrEmpty(
message->parameter(0).remove(0, 1));
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
if (!twitchChannel)
@@ -1311,7 +1315,7 @@ void IrcMessageHandler::setSimilarityFlags(const MessagePtr &message,
void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
const ChannelPtr &chan,
const QString &originalContent,
TwitchIrcServer &server, bool isSub,
ITwitchIrcServer &server, bool isSub,
bool isAction)
{
if (chan->isEmpty())
@@ -1446,7 +1450,7 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *message,
if (highlighted && showInMentions)
{
server.mentionsChannel->addMessage(msg);
server.getMentionsChannel()->addMessage(msg);
}
chan->addMessage(msg);
+7 -4
View File
@@ -9,7 +9,8 @@
namespace chatterino {
class TwitchIrcServer;
class IAbstractIrcServer;
class ITwitchIrcServer;
class Channel;
using ChannelPtr = std::shared_ptr<Channel>;
struct Message;
@@ -38,7 +39,8 @@ public:
std::vector<MessagePtr> &otherLoaded);
void handlePrivMessage(Communi::IrcPrivateMessage *message,
TwitchIrcServer &server);
ITwitchIrcServer &twitchServer,
IAbstractIrcServer &abstractIrcServer);
void handleRoomStateMessage(Communi::IrcMessage *message);
void handleClearChatMessage(Communi::IrcMessage *message);
@@ -48,7 +50,8 @@ public:
void handleWhisperMessage(Communi::IrcMessage *ircMessage);
void handleUserNoticeMessage(Communi::IrcMessage *message,
TwitchIrcServer &server);
ITwitchIrcServer &twitchServer,
IAbstractIrcServer &abstractIrcServer);
void handleNoticeMessage(Communi::IrcNoticeMessage *message);
@@ -56,7 +59,7 @@ public:
void handlePartMessage(Communi::IrcMessage *message);
void addMessage(Communi::IrcMessage *message, const ChannelPtr &chan,
const QString &originalContent, TwitchIrcServer &server,
const QString &originalContent, ITwitchIrcServer &server,
bool isSub, bool isAction);
private:
+22 -19
View File
@@ -176,7 +176,8 @@ TwitchChannel::TwitchChannel(const QString &name)
TwitchMessageBuilder::liveMessage(this->getDisplayName(),
&builder2);
builder2.message().id = this->roomId();
getApp()->twitch->liveChannel->addMessage(builder2.release());
getIApp()->getTwitch()->getLiveChannel()->addMessage(
builder2.release());
// Notify on all channels with a ping sound
if (getSettings()->notificationOnAnyChannel &&
@@ -198,7 +199,7 @@ TwitchChannel::TwitchChannel(const QString &name)
// "delete" old 'CHANNEL is live' message
LimitedQueueSnapshot<MessagePtr> snapshot =
getApp()->twitch->liveChannel->getMessageSnapshot();
getIApp()->getTwitch()->getLiveChannel()->getMessageSnapshot();
int snapshotLength = snapshot.size();
// MSVC hates this code if the parens are not there
@@ -237,17 +238,18 @@ TwitchChannel::~TwitchChannel()
return;
}
getApp()->twitch->dropSeventvChannel(this->seventvUserID_,
this->seventvEmoteSetID_);
getIApp()->getTwitch()->dropSeventvChannel(this->seventvUserID_,
this->seventvEmoteSetID_);
if (getApp()->twitch->bttvLiveUpdates)
if (getIApp()->getTwitch()->getBTTVLiveUpdates())
{
getApp()->twitch->bttvLiveUpdates->partChannel(this->roomId());
getIApp()->getTwitch()->getBTTVLiveUpdates()->partChannel(
this->roomId());
}
if (getApp()->twitch->seventvEventAPI)
if (getIApp()->getTwitch()->getSeventvEventAPI())
{
getApp()->twitch->seventvEventAPI->unsubscribeTwitchChannel(
getIApp()->getTwitch()->getSeventvEventAPI()->unsubscribeTwitchChannel(
this->roomId());
}
}
@@ -425,7 +427,7 @@ void TwitchChannel::addChannelPointReward(const ChannelPointReward &reward)
<< "] Channel point reward added:" << reward.id << ","
<< reward.title << "," << reward.isUserInputRequired;
auto *server = getApp()->twitch;
auto *server = getIApp()->getTwitch();
auto it = std::remove_if(
this->waitingRedemptions_.begin(), this->waitingRedemptions_.end(),
[&](const QueuedRedemption &msg) {
@@ -776,7 +778,7 @@ bool TwitchChannel::canReconnect() const
void TwitchChannel::reconnect()
{
getApp()->twitch->connect();
getIApp()->getTwitchAbstract()->connect();
}
QString TwitchChannel::roomId() const
@@ -891,7 +893,7 @@ const QString &TwitchChannel::seventvEmoteSetID() const
void TwitchChannel::joinBttvChannel() const
{
if (getApp()->twitch->bttvLiveUpdates)
if (getIApp()->getTwitch()->getBTTVLiveUpdates())
{
const auto currentAccount =
getIApp()->getAccounts()->twitch.getCurrent();
@@ -900,8 +902,8 @@ void TwitchChannel::joinBttvChannel() const
{
userName = currentAccount->getUserName();
}
getApp()->twitch->bttvLiveUpdates->joinChannel(this->roomId(),
userName);
getIApp()->getTwitch()->getBTTVLiveUpdates()->joinChannel(
this->roomId(), userName);
}
}
@@ -1048,14 +1050,14 @@ void TwitchChannel::updateSeventvData(const QString &newUserID,
this->seventvUserID_ = newUserID;
this->seventvEmoteSetID_ = newEmoteSetID;
runInGuiThread([this, oldUserID, oldEmoteSetID]() {
if (getApp()->twitch->seventvEventAPI)
if (getIApp()->getTwitch()->getSeventvEventAPI())
{
getApp()->twitch->seventvEventAPI->subscribeUser(
getIApp()->getTwitch()->getSeventvEventAPI()->subscribeUser(
this->seventvUserID_, this->seventvEmoteSetID_);
if (oldUserID || oldEmoteSetID)
{
getApp()->twitch->dropSeventvChannel(
getIApp()->getTwitch()->dropSeventvChannel(
oldUserID.value_or(QString()),
oldEmoteSetID.value_or(QString()));
}
@@ -1251,7 +1253,8 @@ void TwitchChannel::loadRecentMessages()
tc->addRecentChatter(msg->displayName);
}
getApp()->twitch->mentionsChannel->fillInMissingMessages(msgs);
getIApp()->getTwitch()->getMentionsChannel()->fillInMissingMessages(
msgs);
},
[weak]() {
auto shared = weak.lock();
@@ -1841,9 +1844,9 @@ void TwitchChannel::updateSevenTVActivity()
void TwitchChannel::listenSevenTVCosmetics() const
{
if (getApp()->twitch->seventvEventAPI)
if (getIApp()->getTwitch()->getSeventvEventAPI())
{
getApp()->twitch->seventvEventAPI->subscribeTwitchChannel(
getIApp()->getTwitch()->getSeventvEventAPI()->subscribeTwitchChannel(
this->roomId());
}
}
+42 -2
View File
@@ -263,7 +263,7 @@ std::shared_ptr<Channel> TwitchIrcServer::createChannel(
void TwitchIrcServer::privateMessageReceived(
Communi::IrcPrivateMessage *message)
{
IrcMessageHandler::instance().handlePrivMessage(message, *this);
IrcMessageHandler::instance().handlePrivMessage(message, *this, *this);
}
void TwitchIrcServer::readConnectionMessageReceived(
@@ -310,7 +310,7 @@ void TwitchIrcServer::readConnectionMessageReceived(
}
else if (command == "USERNOTICE")
{
handler.handleUserNoticeMessage(message, *this);
handler.handleUserNoticeMessage(message, *this, *this);
}
else if (command == "NOTICE")
{
@@ -645,16 +645,56 @@ void TwitchIrcServer::onReplySendRequested(
sent = true;
}
std::unique_ptr<BttvLiveUpdates> &TwitchIrcServer::getBTTVLiveUpdates()
{
return this->bttvLiveUpdates;
}
std::unique_ptr<SeventvEventAPI> &TwitchIrcServer::getSeventvEventAPI()
{
return this->seventvEventAPI;
}
const IndirectChannel &TwitchIrcServer::getWatchingChannel() const
{
return this->watchingChannel;
}
void TwitchIrcServer::setWatchingChannel(ChannelPtr newWatchingChannel)
{
this->watchingChannel.reset(newWatchingChannel);
}
ChannelPtr TwitchIrcServer::getWhispersChannel() const
{
return this->whispersChannel;
}
ChannelPtr TwitchIrcServer::getMentionsChannel() const
{
return this->mentionsChannel;
}
ChannelPtr TwitchIrcServer::getLiveChannel() const
{
return this->liveChannel;
}
ChannelPtr TwitchIrcServer::getAutomodChannel() const
{
return this->automodChannel;
}
QString TwitchIrcServer::getLastUserThatWhisperedMe() const
{
return this->lastUserThatWhisperedMe.get();
}
void TwitchIrcServer::setLastUserThatWhisperedMe(const QString &user)
{
this->lastUserThatWhisperedMe.set(user);
}
void TwitchIrcServer::reloadBTTVGlobalEmotes()
{
getIApp()->getBttvEmotes()->loadEmotes();
+35 -3
View File
@@ -27,9 +27,27 @@ class ITwitchIrcServer
public:
virtual ~ITwitchIrcServer() = default;
virtual void forEachChannelAndSpecialChannels(
std::function<void(ChannelPtr)> func) = 0;
virtual std::shared_ptr<Channel> getChannelOrEmptyByID(
const QString &channelID) = 0;
virtual void dropSeventvChannel(const QString &userID,
const QString &emoteSetID) = 0;
virtual std::unique_ptr<BttvLiveUpdates> &getBTTVLiveUpdates() = 0;
virtual std::unique_ptr<SeventvEventAPI> &getSeventvEventAPI() = 0;
virtual const IndirectChannel &getWatchingChannel() const = 0;
virtual void setWatchingChannel(ChannelPtr newWatchingChannel) = 0;
virtual ChannelPtr getWhispersChannel() const = 0;
virtual ChannelPtr getMentionsChannel() const = 0;
virtual ChannelPtr getLiveChannel() const = 0;
virtual ChannelPtr getAutomodChannel() const = 0;
virtual QString getLastUserThatWhisperedMe() const = 0;
virtual void setLastUserThatWhisperedMe(const QString &user) = 0;
// Update this interface with TwitchIrcServer methods as needed
};
@@ -44,9 +62,11 @@ public:
void initialize(Settings &settings, const Paths &paths) override;
void forEachChannelAndSpecialChannels(std::function<void(ChannelPtr)> func);
void forEachChannelAndSpecialChannels(
std::function<void(ChannelPtr)> func) override;
std::shared_ptr<Channel> getChannelOrEmptyByID(const QString &channelID);
std::shared_ptr<Channel> getChannelOrEmptyByID(
const QString &channelID) override;
void reloadBTTVGlobalEmotes();
void reloadAllBTTVChannelEmotes();
@@ -68,8 +88,10 @@ public:
* It's currently not possible to share emote sets among users,
* but it's a commonly requested feature.
*/
void dropSeventvChannel(const QString &userID, const QString &emoteSetID);
void dropSeventvChannel(const QString &userID,
const QString &emoteSetID) override;
private:
Atomic<QString> lastUserThatWhisperedMe;
const ChannelPtr whispersChannel;
@@ -81,9 +103,19 @@ public:
std::unique_ptr<BttvLiveUpdates> bttvLiveUpdates;
std::unique_ptr<SeventvEventAPI> seventvEventAPI;
public:
std::unique_ptr<BttvLiveUpdates> &getBTTVLiveUpdates() override;
std::unique_ptr<SeventvEventAPI> &getSeventvEventAPI() override;
const IndirectChannel &getWatchingChannel() const override;
void setWatchingChannel(ChannelPtr newWatchingChannel) override;
ChannelPtr getWhispersChannel() const override;
ChannelPtr getMentionsChannel() const override;
ChannelPtr getLiveChannel() const override;
ChannelPtr getAutomodChannel() const override;
QString getLastUserThatWhisperedMe() const override;
void setLastUserThatWhisperedMe(const QString &user) override;
protected:
void initializeConnection(IrcConnection *connection,