diff --git a/lib/lrucache/lrucache/lrucache.hpp b/lib/lrucache/lrucache/lrucache.hpp index 183bc3b02..ef7d031db 100644 --- a/lib/lrucache/lrucache/lrucache.hpp +++ b/lib/lrucache/lrucache/lrucache.hpp @@ -71,17 +71,6 @@ public: } } - void remove(const key_t &key) - { - auto it = _cache_items_map.find(key); - if (it == _cache_items_map.end()) - { - throw std::range_error("There is no such key in cache"); - } - _cache_items_list.erase(it->second); - _cache_items_map.erase(it); - } - const value_t &get(const key_t &key) { auto it = _cache_items_map.find(key); diff --git a/mocks/include/mocks/BaseApplication.hpp b/mocks/include/mocks/BaseApplication.hpp index 2ba9f949c..619203afc 100644 --- a/mocks/include/mocks/BaseApplication.hpp +++ b/mocks/include/mocks/BaseApplication.hpp @@ -3,6 +3,7 @@ #include "common/Args.hpp" #include "mocks/DisabledStreamerMode.hpp" #include "mocks/EmptyApplication.hpp" +#include "mocks/TwitchUsers.hpp" #include "providers/bttv/BttvLiveUpdates.hpp" #include "singletons/Fonts.hpp" #include "singletons/Settings.hpp" @@ -55,6 +56,11 @@ public: return &this->fonts; } + ITwitchUsers *getTwitchUsers() override + { + return &this->twitchUsers; + } + BttvLiveUpdates *getBttvLiveUpdates() override { return nullptr; @@ -71,6 +77,7 @@ public: DisabledStreamerMode streamerMode; Theme theme; Fonts fonts; + TwitchUsers twitchUsers; }; } // namespace chatterino::mock diff --git a/mocks/include/mocks/TwitchIrcServer.hpp b/mocks/include/mocks/TwitchIrcServer.hpp index 6b305e5ba..d218192b3 100644 --- a/mocks/include/mocks/TwitchIrcServer.hpp +++ b/mocks/include/mocks/TwitchIrcServer.hpp @@ -26,7 +26,6 @@ public: , mentionsChannel(std::shared_ptr(new MockChannel("forsen3"))) , liveChannel(std::shared_ptr(new MockChannel("forsen"))) , automodChannel(std::shared_ptr(new MockChannel("forsen2"))) - , channelNamesById_(1) { } @@ -50,18 +49,6 @@ public: return {}; } - std::optional getOrPopulateChannelCache( - const QString &channelId) override - { - if (channelId == "11148817") - return "pajlada"; - if (channelId == "141981764") - return "twitchdev"; - if (channelId == "1025594235") - return "shared_chat_test_01"; - return {}; - } - void addFakeMessage(const QString &data) override { } @@ -161,7 +148,6 @@ public: ChannelPtr mentionsChannel; ChannelPtr liveChannel; ChannelPtr automodChannel; - UniqueAccess> channelNamesById_; QString lastUserThatWhisperedMe{"forsen"}; std::unordered_map> mockChannels; diff --git a/mocks/include/mocks/TwitchUsers.hpp b/mocks/include/mocks/TwitchUsers.hpp new file mode 100644 index 000000000..14f6bf7e6 --- /dev/null +++ b/mocks/include/mocks/TwitchUsers.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "providers/twitch/TwitchUser.hpp" +#include "providers/twitch/TwitchUsers.hpp" + +namespace chatterino::mock { + +class TwitchUsers : public ITwitchUsers +{ +public: + TwitchUsers() = default; + + std::shared_ptr resolveID(const UserId &id) + { + TwitchUser u = { + .id = id.string, + .name = {}, + .displayName = {}, + }; + return std::make_shared(u); + } +}; + +} // namespace chatterino::mock diff --git a/src/messages/MessageBuilder.cpp b/src/messages/MessageBuilder.cpp index 87d4a1a2b..49b6399cd 100644 --- a/src/messages/MessageBuilder.cpp +++ b/src/messages/MessageBuilder.cpp @@ -32,6 +32,7 @@ #include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchIrc.hpp" #include "providers/twitch/TwitchIrcServer.hpp" +#include "providers/twitch/TwitchUsers.hpp" #include "singletons/Emotes.hpp" #include "singletons/Resources.hpp" #include "singletons/Settings.hpp" @@ -2765,22 +2766,23 @@ void MessageBuilder::appendTwitchBadges(const QVariantMap &tags, if (this->message().flags.has(MessageFlag::SharedMessage)) { const QString sourceId = tags["source-room-id"].toString(); - std::optional sourceName; - if (twitchChannel->roomId() == sourceId) + QString sourceName; + if (sourceId.isEmpty()) + { + sourceName = ""; + } + else if (twitchChannel->roomId() == sourceId) { sourceName = twitchChannel->getName(); } else { sourceName = - getApp()->getTwitch()->getOrPopulateChannelCache(sourceId); + getApp()->getTwitchUsers()->resolveID({sourceId})->displayName; } - if (sourceName.has_value()) - { - this->emplace(makeSharedChatBadge(sourceName.value()), - MessageElementFlag::BadgeSharedChannel); - } + this->emplace(makeSharedChatBadge(sourceName), + MessageElementFlag::BadgeSharedChannel); } auto badgeInfos = parseBadgeInfoTag(tags); diff --git a/src/providers/twitch/TwitchIrcServer.cpp b/src/providers/twitch/TwitchIrcServer.cpp index 16a48f1b7..9ca93f6fd 100644 --- a/src/providers/twitch/TwitchIrcServer.cpp +++ b/src/providers/twitch/TwitchIrcServer.cpp @@ -153,7 +153,6 @@ TwitchIrcServer::TwitchIrcServer() , liveChannel(new Channel("/live", Channel::Type::TwitchLive)) , automodChannel(new Channel("/automod", Channel::Type::TwitchAutomod)) , watchingChannel(Channel::getEmpty(), Channel::Type::TwitchWatching) - , channelNamesById_(512) { // Initialize the connections // XXX: don't create write connection if there is no separate write connection. @@ -1129,38 +1128,6 @@ std::shared_ptr TwitchIrcServer::getChannelOrEmptyByID( return Channel::getEmpty(); } -std::optional TwitchIrcServer::getOrPopulateChannelCache( - const QString &channelId) -{ - { - const auto cache = this->channelNamesById_.access(); - if (cache->exists(channelId)) - { - return cache->get(channelId); - } - - // prevent multiple helix requests for single user - cache->put(channelId, ""); - } - - getHelix()->getUserById( - channelId, - [this](const HelixUser &user) { - const auto cache = this->channelNamesById_.access(); - cache->put(user.id, user.displayName); - }, - [this, channelId] { - const auto cache = this->channelNamesById_.access(); - if (cache->exists(channelId) && cache->get(channelId).isEmpty()) - { - // invalidate cache so another helix request can be attempted - cache->remove(channelId); - } - }); - - return {}; -} - QString TwitchIrcServer::cleanChannelName(const QString &dirtyChannelName) { if (dirtyChannelName.startsWith('#')) diff --git a/src/providers/twitch/TwitchIrcServer.hpp b/src/providers/twitch/TwitchIrcServer.hpp index 757f9448c..fc3b888ef 100644 --- a/src/providers/twitch/TwitchIrcServer.hpp +++ b/src/providers/twitch/TwitchIrcServer.hpp @@ -3,12 +3,10 @@ #include "common/Atomic.hpp" #include "common/Channel.hpp" #include "common/Common.hpp" -#include "common/UniqueAccess.hpp" #include "providers/irc/IrcConnection2.hpp" #include "util/RatelimitBucket.hpp" #include -#include #include #include @@ -45,9 +43,6 @@ public: virtual ChannelPtr getOrAddChannel(const QString &dirtyChannelName) = 0; virtual ChannelPtr getChannelOrEmpty(const QString &dirtyChannelName) = 0; - virtual std::optional getOrPopulateChannelCache( - const QString &channelId) = 0; - virtual void addFakeMessage(const QString &data) = 0; virtual void addGlobalSystemMessage(const QString &messageText) = 0; @@ -100,14 +95,6 @@ public: std::shared_ptr getChannelOrEmptyByID( const QString &channelID) override; - /** - * Obtains the channel display name associated with the passed ID, - * so that Shared Chat messages can provide source channel context. - * Can yield an empty string if a helix request is already in-flight. - */ - std::optional getOrPopulateChannelCache( - const QString &channelId) override; - void reloadAllBTTVChannelEmotes(); void reloadAllFFZChannelEmotes(); void reloadAllSevenTVChannelEmotes(); @@ -203,9 +190,6 @@ private: // https://dev.twitch.tv/docs/irc/guide#rate-limits QObjectPtr joinBucket_; - // cached channel id => name for resolving Shared Chat members - UniqueAccess> channelNamesById_; - QTimer reconnectTimer_; int falloffCounter_ = 1; diff --git a/tests/snapshots/IrcMessageHandler/shared-chat-announcement.json b/tests/snapshots/IrcMessageHandler/shared-chat-announcement.json index e038fce57..7e3ad232a 100644 --- a/tests/snapshots/IrcMessageHandler/shared-chat-announcement.json +++ b/tests/snapshots/IrcMessageHandler/shared-chat-announcement.json @@ -70,15 +70,15 @@ "images": { "1x": "" }, - "name": "SharedChat_shared_chat_test_01", - "tooltip": "Shared Message from shared_chat_test_01" + "name": "SharedChat_", + "tooltip": "Shared Message" }, "flags": "BadgeSharedChannel", "link": { "type": "None", "value": "" }, - "tooltip": "Shared Message from shared_chat_test_01", + "tooltip": "Shared Message", "trailingSpace": true, "type": "BadgeElement" }, diff --git a/tests/snapshots/IrcMessageHandler/shared-chat-unknown.json b/tests/snapshots/IrcMessageHandler/shared-chat-unknown.json index 6eaf5e5be..a99e1b1e9 100644 --- a/tests/snapshots/IrcMessageHandler/shared-chat-unknown.json +++ b/tests/snapshots/IrcMessageHandler/shared-chat-unknown.json @@ -70,15 +70,15 @@ "images": { "1x": "" }, - "name": "SharedChat_shared_chat_test_01", - "tooltip": "Shared Message from shared_chat_test_01" + "name": "SharedChat_", + "tooltip": "Shared Message" }, "flags": "BadgeSharedChannel", "link": { "type": "None", "value": "" }, - "tooltip": "Shared Message from shared_chat_test_01", + "tooltip": "Shared Message", "trailingSpace": true, "type": "BadgeElement" },