From 5628605de456a7587fe6add6beff49a5f71c9f8c Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 21 Jan 2024 14:20:21 +0100 Subject: [PATCH] refactor: Move Emotes to Application (#5120) --- CHANGELOG.md | 1 + mocks/include/mocks/EmptyApplication.hpp | 21 ++++++++++++ mocks/include/mocks/TwitchIrcServer.hpp | 21 ++---------- src/Application.cpp | 33 +++++++++++++++++++ src/Application.hpp | 13 ++++++++ .../commands/builtin/twitch/SendWhisper.cpp | 11 ++++--- .../completion/sources/EmoteSource.cpp | 9 +++-- src/providers/twitch/TwitchChannel.cpp | 1 + src/providers/twitch/TwitchIrcServer.cpp | 22 ++++--------- src/providers/twitch/TwitchIrcServer.hpp | 16 ++------- src/providers/twitch/TwitchMessageBuilder.cpp | 15 +++++---- src/widgets/dialogs/EmotePopup.cpp | 19 ++++++----- tests/src/InputCompletion.cpp | 24 ++++++++++++-- tests/src/TwitchMessageBuilder.cpp | 18 ++++++++++ 14 files changed, 152 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b82cbc268..dca7de288 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -112,6 +112,7 @@ - Dev: Refactored the Image Uploader feature. (#4971) - Dev: Refactored the SplitOverlay code. (#5082) - Dev: Refactored the TwitchBadges structure, making it less of a singleton. (#5096) +- Dev: Refactored emotes out of TwitchIrcServer. (#5120) - Dev: Refactored the ChatterinoBadges structure, making it less of a singleton. (#5103) - Dev: Refactored the ColorProvider class a bit. (#5112) - Dev: Moved the Network files to their own folder. (#5089) diff --git a/mocks/include/mocks/EmptyApplication.hpp b/mocks/include/mocks/EmptyApplication.hpp index c549ea949..a6a1745d2 100644 --- a/mocks/include/mocks/EmptyApplication.hpp +++ b/mocks/include/mocks/EmptyApplication.hpp @@ -191,6 +191,27 @@ public: return this->updates_; } + BttvEmotes *getBttvEmotes() override + { + assert(false && "EmptyApplication::getBttvEmotes was called without " + "being initialized"); + return nullptr; + } + + FfzEmotes *getFfzEmotes() override + { + assert(false && "EmptyApplication::getFfzEmotes was called without " + "being initialized"); + return nullptr; + } + + SeventvEmotes *getSeventvEmotes() override + { + assert(false && "EmptyApplication::getSeventvEmotes was called without " + "being initialized"); + return nullptr; + } + private: Paths paths_; Args args_; diff --git a/mocks/include/mocks/TwitchIrcServer.hpp b/mocks/include/mocks/TwitchIrcServer.hpp index aca17e4b7..2ef163db6 100644 --- a/mocks/include/mocks/TwitchIrcServer.hpp +++ b/mocks/include/mocks/TwitchIrcServer.hpp @@ -1,6 +1,9 @@ #pragma once #include "mocks/Channel.hpp" +#include "providers/bttv/BttvEmotes.hpp" +#include "providers/ffz/FfzEmotes.hpp" +#include "providers/seventv/SeventvEmotes.hpp" #include "providers/twitch/TwitchIrcServer.hpp" namespace chatterino::mock { @@ -16,29 +19,11 @@ public: { } - const BttvEmotes &getBttvEmotes() const override - { - return this->bttv; - } - - const FfzEmotes &getFfzEmotes() const override - { - return this->ffz; - } - - const SeventvEmotes &getSeventvEmotes() const override - { - return this->seventv; - } - const IndirectChannel &getWatchingChannel() const override { return this->watchingChannel; } - BttvEmotes bttv; - FfzEmotes ffz; - SeventvEmotes seventv; ChannelPtr watchingChannelInner; IndirectChannel watchingChannel; }; diff --git a/src/Application.cpp b/src/Application.cpp index 1070bde3d..0df907907 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -11,7 +11,10 @@ #include "controllers/ignores/IgnoreController.hpp" #include "controllers/notifications/NotificationController.hpp" #include "controllers/sound/ISoundController.hpp" +#include "providers/bttv/BttvEmotes.hpp" +#include "providers/ffz/FfzEmotes.hpp" #include "providers/seventv/SeventvAPI.hpp" +#include "providers/seventv/SeventvEmotes.hpp" #include "providers/twitch/TwitchBadges.hpp" #include "singletons/ImageUploader.hpp" #ifdef CHATTERINO_HAVE_PLUGINS @@ -135,6 +138,9 @@ Application::Application(Settings &_settings, const Paths &paths, , twitchPubSub(new PubSub(TWITCH_PUBSUB_URL)) , twitchBadges(new TwitchBadges) , chatterinoBadges(new ChatterinoBadges) + , bttvEmotes(new BttvEmotes) + , ffzEmotes(new FfzEmotes) + , seventvEmotes(new SeventvEmotes) , logging(new Logging(_settings)) #ifdef CHATTERINO_HAVE_PLUGINS , plugins(&this->emplace(new PluginController(paths))) @@ -157,6 +163,9 @@ void Application::fakeDtor() this->twitchPubSub.reset(); this->twitchBadges.reset(); this->chatterinoBadges.reset(); + this->bttvEmotes.reset(); + this->ffzEmotes.reset(); + this->seventvEmotes.reset(); } void Application::initialize(Settings &settings, const Paths &paths) @@ -481,6 +490,30 @@ Logging *Application::getChatLogger() return this->logging.get(); } +BttvEmotes *Application::getBttvEmotes() +{ + assertInGuiThread(); + assert(this->bttvEmotes); + + return this->bttvEmotes.get(); +} + +FfzEmotes *Application::getFfzEmotes() +{ + assertInGuiThread(); + assert(this->ffzEmotes); + + return this->ffzEmotes.get(); +} + +SeventvEmotes *Application::getSeventvEmotes() +{ + assertInGuiThread(); + assert(this->seventvEmotes); + + return this->seventvEmotes.get(); +} + void Application::save() { for (auto &singleton : this->singletons_) diff --git a/src/Application.hpp b/src/Application.hpp index 83a8b90ec..eef02be70 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -51,6 +51,9 @@ class SeventvBadges; class ImageUploader; class SeventvAPI; class CrashHandler; +class BttvEmotes; +class FfzEmotes; +class SeventvEmotes; class IApplication { @@ -89,6 +92,9 @@ public: virtual PluginController *getPlugins() = 0; #endif virtual Updates &getUpdates() = 0; + virtual BttvEmotes *getBttvEmotes() = 0; + virtual FfzEmotes *getFfzEmotes() = 0; + virtual SeventvEmotes *getSeventvEmotes() = 0; }; class Application : public IApplication @@ -152,6 +158,9 @@ private: std::unique_ptr twitchPubSub; std::unique_ptr twitchBadges; std::unique_ptr chatterinoBadges; + std::unique_ptr bttvEmotes; + std::unique_ptr ffzEmotes; + std::unique_ptr seventvEmotes; const std::unique_ptr logging; #ifdef CHATTERINO_HAVE_PLUGINS PluginController *const plugins{}; @@ -199,6 +208,10 @@ public: return this->updates; } + BttvEmotes *getBttvEmotes() override; + FfzEmotes *getFfzEmotes() override; + SeventvEmotes *getSeventvEmotes() override; + pajlada::Signals::NoArgSignal streamerModeChanged; private: diff --git a/src/controllers/commands/builtin/twitch/SendWhisper.cpp b/src/controllers/commands/builtin/twitch/SendWhisper.cpp index bcccdc92d..ffb4ac48c 100644 --- a/src/controllers/commands/builtin/twitch/SendWhisper.cpp +++ b/src/controllers/commands/builtin/twitch/SendWhisper.cpp @@ -7,6 +7,8 @@ #include "messages/Message.hpp" #include "messages/MessageBuilder.hpp" #include "messages/MessageElement.hpp" +#include "providers/bttv/BttvEmotes.hpp" +#include "providers/ffz/FfzEmotes.hpp" #include "providers/irc/IrcChannel2.hpp" #include "providers/irc/IrcServer.hpp" #include "providers/twitch/api/Helix.hpp" @@ -105,8 +107,8 @@ bool appendWhisperMessageWordsLocally(const QStringList &words) const auto &acc = app->getAccounts()->twitch.getCurrent(); const auto &accemotes = *acc->accessEmotes(); - const auto &bttvemotes = app->twitch->getBttvEmotes(); - const auto &ffzemotes = app->twitch->getFfzEmotes(); + const auto *bttvemotes = app->getBttvEmotes(); + const auto *ffzemotes = app->getFfzEmotes(); auto flags = MessageElementFlags(); auto emote = std::optional{}; for (int i = 2; i < words.length(); i++) @@ -122,14 +124,15 @@ bool appendWhisperMessageWordsLocally(const QStringList &words) } // Twitch emote { // bttv/ffz emote - if ((emote = bttvemotes.emote({words[i]}))) + if ((emote = bttvemotes->emote({words[i]}))) { flags = MessageElementFlag::BttvEmote; } - else if ((emote = ffzemotes.emote({words[i]}))) + else if ((emote = ffzemotes->emote({words[i]}))) { flags = MessageElementFlag::FfzEmote; } + // TODO: Load 7tv global emotes if (emote) { b.emplace(*emote, flags); diff --git a/src/controllers/completion/sources/EmoteSource.cpp b/src/controllers/completion/sources/EmoteSource.cpp index d5c71f8a7..ded6d72be 100644 --- a/src/controllers/completion/sources/EmoteSource.cpp +++ b/src/controllers/completion/sources/EmoteSource.cpp @@ -3,7 +3,10 @@ #include "Application.hpp" #include "controllers/accounts/AccountController.hpp" #include "controllers/completion/sources/Helpers.hpp" +#include "providers/bttv/BttvEmotes.hpp" #include "providers/emoji/Emojis.hpp" +#include "providers/ffz/FfzEmotes.hpp" +#include "providers/seventv/SeventvEmotes.hpp" #include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchIrcServer.hpp" @@ -127,15 +130,15 @@ void EmoteSource::initializeFromChannel(const Channel *channel) } } - if (auto bttvG = app->getTwitch()->getBttvEmotes().emotes()) + if (auto bttvG = app->getBttvEmotes()->emotes()) { addEmotes(emotes, *bttvG, "Global BetterTTV"); } - if (auto ffzG = app->getTwitch()->getFfzEmotes().emotes()) + if (auto ffzG = app->getFfzEmotes()->emotes()) { addEmotes(emotes, *ffzG, "Global FrankerFaceZ"); } - if (auto seventvG = app->getTwitch()->getSeventvEmotes().globalEmotes()) + if (auto seventvG = app->getSeventvEmotes()->globalEmotes()) { addEmotes(emotes, *seventvG, "Global 7TV"); } diff --git a/src/providers/twitch/TwitchChannel.cpp b/src/providers/twitch/TwitchChannel.cpp index 704ecfa33..a8eace253 100644 --- a/src/providers/twitch/TwitchChannel.cpp +++ b/src/providers/twitch/TwitchChannel.cpp @@ -18,6 +18,7 @@ #include "providers/bttv/BttvEmotes.hpp" #include "providers/bttv/BttvLiveUpdates.hpp" #include "providers/bttv/liveupdates/BttvLiveUpdateMessages.hpp" +#include "providers/ffz/FfzEmotes.hpp" #include "providers/recentmessages/Api.hpp" #include "providers/seventv/eventapi/Dispatch.hpp" #include "providers/seventv/SeventvAPI.hpp" diff --git a/src/providers/twitch/TwitchIrcServer.cpp b/src/providers/twitch/TwitchIrcServer.cpp index ee9c8c3b1..acad48f86 100644 --- a/src/providers/twitch/TwitchIrcServer.cpp +++ b/src/providers/twitch/TwitchIrcServer.cpp @@ -7,8 +7,11 @@ #include "controllers/accounts/AccountController.hpp" #include "messages/Message.hpp" #include "messages/MessageBuilder.hpp" +#include "providers/bttv/BttvEmotes.hpp" #include "providers/bttv/BttvLiveUpdates.hpp" +#include "providers/ffz/FfzEmotes.hpp" #include "providers/seventv/eventapi/Subscription.hpp" +#include "providers/seventv/SeventvEmotes.hpp" #include "providers/seventv/SeventvEventAPI.hpp" #include "providers/twitch/api/Helix.hpp" #include "providers/twitch/ChannelPointReward.hpp" @@ -517,19 +520,6 @@ void TwitchIrcServer::onReplySendRequested(TwitchChannel *channel, sent = true; } -const BttvEmotes &TwitchIrcServer::getBttvEmotes() const -{ - return this->bttv; -} -const FfzEmotes &TwitchIrcServer::getFfzEmotes() const -{ - return this->ffz; -} -const SeventvEmotes &TwitchIrcServer::getSeventvEmotes() const -{ - return this->seventv_; -} - const IndirectChannel &TwitchIrcServer::getWatchingChannel() const { return this->watchingChannel; @@ -537,7 +527,7 @@ const IndirectChannel &TwitchIrcServer::getWatchingChannel() const void TwitchIrcServer::reloadBTTVGlobalEmotes() { - this->bttv.loadEmotes(); + getIApp()->getBttvEmotes()->loadEmotes(); } void TwitchIrcServer::reloadAllBTTVChannelEmotes() @@ -552,7 +542,7 @@ void TwitchIrcServer::reloadAllBTTVChannelEmotes() void TwitchIrcServer::reloadFFZGlobalEmotes() { - this->ffz.loadEmotes(); + getIApp()->getFfzEmotes()->loadEmotes(); } void TwitchIrcServer::reloadAllFFZChannelEmotes() @@ -567,7 +557,7 @@ void TwitchIrcServer::reloadAllFFZChannelEmotes() void TwitchIrcServer::reloadSevenTVGlobalEmotes() { - this->seventv_.loadGlobalEmotes(); + getIApp()->getSeventvEmotes()->loadGlobalEmotes(); } void TwitchIrcServer::reloadAllSevenTVChannelEmotes() diff --git a/src/providers/twitch/TwitchIrcServer.hpp b/src/providers/twitch/TwitchIrcServer.hpp index d00f8e40b..6fe36c0a2 100644 --- a/src/providers/twitch/TwitchIrcServer.hpp +++ b/src/providers/twitch/TwitchIrcServer.hpp @@ -3,10 +3,7 @@ #include "common/Atomic.hpp" #include "common/Channel.hpp" #include "common/Singleton.hpp" -#include "providers/bttv/BttvEmotes.hpp" -#include "providers/ffz/FfzEmotes.hpp" #include "providers/irc/AbstractIrcServer.hpp" -#include "providers/seventv/SeventvEmotes.hpp" #include @@ -21,15 +18,15 @@ class Paths; class TwitchChannel; class BttvLiveUpdates; class SeventvEventAPI; +class BttvEmotes; +class FfzEmotes; +class SeventvEmotes; class ITwitchIrcServer { public: virtual ~ITwitchIrcServer() = default; - virtual const BttvEmotes &getBttvEmotes() const = 0; - virtual const FfzEmotes &getFfzEmotes() const = 0; - virtual const SeventvEmotes &getSeventvEmotes() const = 0; virtual const IndirectChannel &getWatchingChannel() const = 0; // Update this interface with TwitchIrcServer methods as needed @@ -82,9 +79,6 @@ public: std::unique_ptr bttvLiveUpdates; std::unique_ptr seventvEventAPI; - const BttvEmotes &getBttvEmotes() const override; - const FfzEmotes &getFfzEmotes() const override; - const SeventvEmotes &getSeventvEmotes() const override; const IndirectChannel &getWatchingChannel() const override; protected: @@ -116,10 +110,6 @@ private: std::chrono::steady_clock::time_point lastErrorTimeSpeed_; std::chrono::steady_clock::time_point lastErrorTimeAmount_; - BttvEmotes bttv; - FfzEmotes ffz; - SeventvEmotes seventv_; - pajlada::Signals::SignalHolder signalHolder_; }; diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 9b14c6cd3..960c65ada 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -13,10 +13,13 @@ #include "messages/Image.hpp" #include "messages/Message.hpp" #include "messages/MessageThread.hpp" +#include "providers/bttv/BttvEmotes.hpp" #include "providers/chatterino/ChatterinoBadges.hpp" #include "providers/colors/ColorProvider.hpp" #include "providers/ffz/FfzBadges.hpp" +#include "providers/ffz/FfzEmotes.hpp" #include "providers/seventv/SeventvBadges.hpp" +#include "providers/seventv/SeventvEmotes.hpp" #include "providers/twitch/api/Helix.hpp" #include "providers/twitch/ChannelPointReward.hpp" #include "providers/twitch/PubSubActions.hpp" @@ -1140,9 +1143,9 @@ Outcome TwitchMessageBuilder::tryAppendEmote(const EmoteName &name) { auto *app = getIApp(); - const auto &globalBttvEmotes = app->getTwitch()->getBttvEmotes(); - const auto &globalFfzEmotes = app->getTwitch()->getFfzEmotes(); - const auto &globalSeventvEmotes = app->getTwitch()->getSeventvEmotes(); + const auto *globalBttvEmotes = app->getBttvEmotes(); + const auto *globalFfzEmotes = app->getFfzEmotes(); + const auto *globalSeventvEmotes = app->getSeventvEmotes(); auto flags = MessageElementFlags(); auto emote = std::optional{}; @@ -1170,16 +1173,16 @@ Outcome TwitchMessageBuilder::tryAppendEmote(const EmoteName &name) flags = MessageElementFlag::SevenTVEmote; zeroWidth = emote.value()->zeroWidth; } - else if ((emote = globalFfzEmotes.emote(name))) + else if ((emote = globalFfzEmotes->emote(name))) { flags = MessageElementFlag::FfzEmote; } - else if ((emote = globalBttvEmotes.emote(name))) + else if ((emote = globalBttvEmotes->emote(name))) { flags = MessageElementFlag::BttvEmote; zeroWidth = zeroWidthEmotes.contains(name.string); } - else if ((emote = globalSeventvEmotes.globalEmote(name))) + else if ((emote = globalSeventvEmotes->globalEmote(name))) { flags = MessageElementFlag::SevenTVEmote; zeroWidth = emote.value()->zeroWidth; diff --git a/src/widgets/dialogs/EmotePopup.cpp b/src/widgets/dialogs/EmotePopup.cpp index e83536e24..71a18a0a3 100644 --- a/src/widgets/dialogs/EmotePopup.cpp +++ b/src/widgets/dialogs/EmotePopup.cpp @@ -9,9 +9,11 @@ #include "messages/Message.hpp" #include "messages/MessageBuilder.hpp" #include "messages/MessageElement.hpp" +#include "providers/bttv/BttvEmotes.hpp" +#include "providers/ffz/FfzEmotes.hpp" +#include "providers/seventv/SeventvEmotes.hpp" #include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchChannel.hpp" -#include "providers/twitch/TwitchIrcServer.hpp" #include "singletons/Emotes.hpp" #include "singletons/Settings.hpp" #include "singletons/WindowManager.hpp" @@ -404,19 +406,18 @@ void EmotePopup::loadChannel(ChannelPtr channel) // global if (Settings::instance().enableBTTVGlobalEmotes) { - addEmotes(*globalChannel, *getApp()->twitch->getBttvEmotes().emotes(), + addEmotes(*globalChannel, *getApp()->getBttvEmotes()->emotes(), "BetterTTV", MessageElementFlag::BttvEmote); } if (Settings::instance().enableFFZGlobalEmotes) { - addEmotes(*globalChannel, *getApp()->twitch->getFfzEmotes().emotes(), + addEmotes(*globalChannel, *getApp()->getFfzEmotes()->emotes(), "FrankerFaceZ", MessageElementFlag::FfzEmote); } if (Settings::instance().enableSevenTVGlobalEmotes) { - addEmotes(*globalChannel, - *getApp()->twitch->getSeventvEmotes().globalEmotes(), "7TV", - MessageElementFlag::SevenTVEmote); + addEmotes(*globalChannel, *getApp()->getSeventvEmotes()->globalEmotes(), + "7TV", MessageElementFlag::SevenTVEmote); } // channel @@ -495,11 +496,11 @@ void EmotePopup::filterTwitchEmotes(std::shared_ptr searchChannel, } auto bttvGlobalEmotes = - filterEmoteMap(searchText, getApp()->twitch->getBttvEmotes().emotes()); + filterEmoteMap(searchText, getIApp()->getBttvEmotes()->emotes()); auto ffzGlobalEmotes = - filterEmoteMap(searchText, getApp()->twitch->getFfzEmotes().emotes()); + filterEmoteMap(searchText, getIApp()->getFfzEmotes()->emotes()); auto seventvGlobalEmotes = filterEmoteMap( - searchText, getApp()->twitch->getSeventvEmotes().globalEmotes()); + searchText, getIApp()->getSeventvEmotes()->globalEmotes()); // twitch addTwitchEmoteSets(twitchGlobalEmotes, *searchChannel, *searchChannel, diff --git a/tests/src/InputCompletion.cpp b/tests/src/InputCompletion.cpp index 623d3a799..860035438 100644 --- a/tests/src/InputCompletion.cpp +++ b/tests/src/InputCompletion.cpp @@ -49,9 +49,27 @@ public: return &this->emotes; } + BttvEmotes *getBttvEmotes() override + { + return &this->bttvEmotes; + } + + FfzEmotes *getFfzEmotes() override + { + return &this->ffzEmotes; + } + + SeventvEmotes *getSeventvEmotes() override + { + return &this->seventvEmotes; + } + AccountController accounts; mock::MockTwitchIrcServer twitch; Emotes emotes; + BttvEmotes bttvEmotes; + FfzEmotes ffzEmotes; + SeventvEmotes seventvEmotes; }; } // namespace @@ -154,18 +172,18 @@ private: addEmote(*bttvEmotes, ":-)"); addEmote(*bttvEmotes, "B-)"); addEmote(*bttvEmotes, "Clap"); - this->mockApplication->twitch.bttv.setEmotes(std::move(bttvEmotes)); + this->mockApplication->bttvEmotes.setEmotes(std::move(bttvEmotes)); auto ffzEmotes = std::make_shared(); addEmote(*ffzEmotes, "LilZ"); addEmote(*ffzEmotes, "ManChicken"); addEmote(*ffzEmotes, "CatBag"); - this->mockApplication->twitch.ffz.setEmotes(std::move(ffzEmotes)); + this->mockApplication->ffzEmotes.setEmotes(std::move(ffzEmotes)); auto seventvEmotes = std::make_shared(); addEmote(*seventvEmotes, "Clap"); addEmote(*seventvEmotes, "Clap2"); - this->mockApplication->twitch.seventv.setGlobalEmotes( + this->mockApplication->seventvEmotes.setGlobalEmotes( std::move(seventvEmotes)); } diff --git a/tests/src/TwitchMessageBuilder.cpp b/tests/src/TwitchMessageBuilder.cpp index 37f7cd060..7bd18e90c 100644 --- a/tests/src/TwitchMessageBuilder.cpp +++ b/tests/src/TwitchMessageBuilder.cpp @@ -70,6 +70,21 @@ public: return &this->highlights; } + BttvEmotes *getBttvEmotes() override + { + return &this->bttvEmotes; + } + + FfzEmotes *getFfzEmotes() override + { + return &this->ffzEmotes; + } + + SeventvEmotes *getSeventvEmotes() override + { + return &this->seventvEmotes; + } + AccountController accounts; Emotes emotes; mock::UserDataController userData; @@ -78,6 +93,9 @@ public: FfzBadges ffzBadges; SeventvBadges seventvBadges; HighlightController highlights; + BttvEmotes bttvEmotes; + FfzEmotes ffzEmotes; + SeventvEmotes seventvEmotes; }; } // namespace