some concurrency additions (#2698)

This commit is contained in:
fourtf 2021-05-01 13:38:58 +02:00 committed by GitHub
parent 345bcdb963
commit 8b3f301c50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 84 additions and 54 deletions

View file

@ -4,14 +4,11 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <QThread> #include <QThread>
#include <QUrl>
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp" #include "common/Outcome.hpp"
#include "messages/Emote.hpp" #include "messages/Emote.hpp"
#include <QUrl>
#include <map>
namespace chatterino { namespace chatterino {
void ChatterinoBadges::initialize(Settings &settings, Paths &paths) void ChatterinoBadges::initialize(Settings &settings, Paths &paths)
{ {
@ -24,6 +21,8 @@ ChatterinoBadges::ChatterinoBadges()
boost::optional<EmotePtr> ChatterinoBadges::getBadge(const UserId &id) boost::optional<EmotePtr> ChatterinoBadges::getBadge(const UserId &id)
{ {
std::shared_lock lock(this->mutex_);
auto it = badgeMap.find(id.string); auto it = badgeMap.find(id.string);
if (it != badgeMap.end()) if (it != badgeMap.end())
{ {
@ -37,8 +36,12 @@ void ChatterinoBadges::loadChatterinoBadges()
static QUrl url("https://api.chatterino.com/badges"); static QUrl url("https://api.chatterino.com/badges");
NetworkRequest(url) NetworkRequest(url)
.concurrent()
.onSuccess([this](auto result) -> Outcome { .onSuccess([this](auto result) -> Outcome {
auto jsonRoot = result.parseJson(); auto jsonRoot = result.parseJson();
std::unique_lock lock(this->mutex_);
int index = 0; int index = 0;
for (const auto &jsonBadge_ : jsonRoot.value("badges").toArray()) for (const auto &jsonBadge_ : jsonRoot.value("badges").toArray())
{ {

View file

@ -2,11 +2,11 @@
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <common/Singleton.hpp> #include <common/Singleton.hpp>
#include <shared_mutex>
#include "common/Aliases.hpp" #include <unordered_map>
#include <map>
#include <vector> #include <vector>
#include "common/Aliases.hpp"
#include "util/QStringHash.hpp"
namespace chatterino { namespace chatterino {
@ -23,7 +23,10 @@ public:
private: private:
void loadChatterinoBadges(); void loadChatterinoBadges();
std::map<QString, int> badgeMap;
std::shared_mutex mutex_;
std::unordered_map<QString, int> badgeMap;
std::vector<EmotePtr> emotes; std::vector<EmotePtr> emotes;
}; };

View file

@ -4,14 +4,13 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <QThread> #include <QThread>
#include <QUrl>
#include <map>
#include <shared_mutex>
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp" #include "common/Outcome.hpp"
#include "messages/Emote.hpp" #include "messages/Emote.hpp"
#include <QUrl>
#include <map>
namespace chatterino { namespace chatterino {
void FfzBadges::initialize(Settings &settings, Paths &paths) void FfzBadges::initialize(Settings &settings, Paths &paths)
@ -21,6 +20,8 @@ void FfzBadges::initialize(Settings &settings, Paths &paths)
boost::optional<EmotePtr> FfzBadges::getBadge(const UserId &id) boost::optional<EmotePtr> FfzBadges::getBadge(const UserId &id)
{ {
std::shared_lock lock(this->mutex_);
auto it = this->badgeMap.find(id.string); auto it = this->badgeMap.find(id.string);
if (it != this->badgeMap.end()) if (it != this->badgeMap.end())
{ {
@ -30,6 +31,8 @@ boost::optional<EmotePtr> FfzBadges::getBadge(const UserId &id)
} }
boost::optional<QColor> FfzBadges::getBadgeColor(const UserId &id) boost::optional<QColor> FfzBadges::getBadgeColor(const UserId &id)
{ {
std::shared_lock lock(this->mutex_);
auto badgeIt = this->badgeMap.find(id.string); auto badgeIt = this->badgeMap.find(id.string);
if (badgeIt != this->badgeMap.end()) if (badgeIt != this->badgeMap.end())
{ {
@ -49,6 +52,8 @@ void FfzBadges::loadFfzBadges()
NetworkRequest(url) NetworkRequest(url)
.onSuccess([this](auto result) -> Outcome { .onSuccess([this](auto result) -> Outcome {
std::unique_lock lock(this->mutex_);
auto jsonRoot = result.parseJson(); auto jsonRoot = result.parseJson();
int index = 0; int index = 0;
for (const auto &jsonBadge_ : jsonRoot.value("badges").toArray()) for (const auto &jsonBadge_ : jsonRoot.value("badges").toArray())

View file

@ -4,8 +4,10 @@
#include <common/Singleton.hpp> #include <common/Singleton.hpp>
#include "common/Aliases.hpp" #include "common/Aliases.hpp"
#include "util/QStringHash.hpp"
#include <map> #include <map>
#include <shared_mutex>
#include <vector> #include <vector>
namespace chatterino { namespace chatterino {
@ -24,9 +26,12 @@ public:
private: private:
void loadFfzBadges(); void loadFfzBadges();
std::map<QString, int> badgeMap;
std::shared_mutex mutex_;
std::unordered_map<QString, int> badgeMap;
std::vector<EmotePtr> badges; std::vector<EmotePtr> badges;
std::map<int, QColor> colorMap; std::unordered_map<int, QColor> colorMap;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -101,14 +101,17 @@ void TwitchAccount::loadBlocks()
getHelix()->loadBlocks( getHelix()->loadBlocks(
getApp()->accounts->twitch.getCurrent()->userId_, getApp()->accounts->twitch.getCurrent()->userId_,
[this](std::vector<HelixBlock> blocks) { [this](std::vector<HelixBlock> blocks) {
std::lock_guard<std::mutex> lock(this->ignoresMutex_); auto ignores = this->ignores_.access();
this->ignores_.clear(); auto userIds = this->ignoresUserIds_.access();
ignores->clear();
userIds->clear();
for (const HelixBlock &block : blocks) for (const HelixBlock &block : blocks)
{ {
TwitchUser blockedUser; TwitchUser blockedUser;
blockedUser.fromHelixBlock(block); blockedUser.fromHelixBlock(block);
this->ignores_.insert(blockedUser); ignores->insert(blockedUser);
userIds->insert(blockedUser.id);
} }
}, },
[] { [] {
@ -125,9 +128,11 @@ void TwitchAccount::blockUser(QString userId, std::function<void()> onSuccess,
TwitchUser blockedUser; TwitchUser blockedUser;
blockedUser.id = userId; blockedUser.id = userId;
{ {
std::lock_guard<std::mutex> lock(this->ignoresMutex_); auto ignores = this->ignores_.access();
auto userIds = this->ignoresUserIds_.access();
this->ignores_.insert(blockedUser); ignores->insert(blockedUser);
userIds->insert(blockedUser.id);
} }
onSuccess(); onSuccess();
}, },
@ -143,9 +148,11 @@ void TwitchAccount::unblockUser(QString userId, std::function<void()> onSuccess,
TwitchUser ignoredUser; TwitchUser ignoredUser;
ignoredUser.id = userId; ignoredUser.id = userId;
{ {
std::lock_guard<std::mutex> lock(this->ignoresMutex_); auto ignores = this->ignores_.access();
auto userIds = this->ignoresUserIds_.access();
this->ignores_.erase(ignoredUser); ignores->erase(ignoredUser);
userIds->erase(ignoredUser.id);
} }
onSuccess(); onSuccess();
}, },
@ -169,11 +176,14 @@ void TwitchAccount::checkFollow(const QString targetUserID,
[] {}); [] {});
} }
std::set<TwitchUser> TwitchAccount::getBlocks() const AccessGuard<const std::set<TwitchUser>> TwitchAccount::accessBlocks() const
{ {
std::lock_guard<std::mutex> lock(this->ignoresMutex_); return this->ignores_.accessConst();
}
return this->ignores_; AccessGuard<const std::set<QString>> TwitchAccount::accessBlockedUserIds() const
{
return this->ignoresUserIds_.accessConst();
} }
void TwitchAccount::loadEmotes() void TwitchAccount::loadEmotes()

View file

@ -106,7 +106,8 @@ public:
void checkFollow(const QString targetUserID, void checkFollow(const QString targetUserID,
std::function<void(FollowResult)> onFinished); std::function<void(FollowResult)> onFinished);
std::set<TwitchUser> getBlocks() const; AccessGuard<const std::set<QString>> accessBlockedUserIds() const;
AccessGuard<const std::set<TwitchUser>> accessBlocks() const;
void loadEmotes(); void loadEmotes();
void loadUserstateEmotes(QStringList emoteSetKeys); void loadUserstateEmotes(QStringList emoteSetKeys);
@ -128,7 +129,8 @@ private:
mutable std::mutex ignoresMutex_; mutable std::mutex ignoresMutex_;
QElapsedTimer userstateEmotesTimer_; QElapsedTimer userstateEmotesTimer_;
std::set<TwitchUser> ignores_; UniqueAccess<std::set<TwitchUser>> ignores_;
UniqueAccess<std::set<QString>> ignoresUserIds_;
// std::map<UserId, TwitchAccountEmoteData> emotes; // std::map<UserId, TwitchAccountEmoteData> emotes;
UniqueAccess<TwitchAccountEmoteData> emotes_; UniqueAccess<TwitchAccountEmoteData> emotes_;

View file

@ -720,6 +720,7 @@ void TwitchChannel::loadRecentMessages()
.arg(getSettings()->twitchMessageHistoryLimit); .arg(getSettings()->twitchMessageHistoryLimit);
NetworkRequest(url) NetworkRequest(url)
.concurrent()
.onSuccess([weak = weakOf<Channel>(this)](auto result) -> Outcome { .onSuccess([weak = weakOf<Channel>(this)](auto result) -> Outcome {
auto shared = weak.lock(); auto shared = weak.lock();
if (!shared) if (!shared)

View file

@ -143,28 +143,28 @@ bool TwitchMessageBuilder::isIgnored() const
{ {
auto sourceUserID = this->tags.value("user-id").toString(); auto sourceUserID = this->tags.value("user-id").toString();
for (const auto &user : app->accounts->twitch.getCurrent()->getBlocks()) auto blocks =
{ app->accounts->twitch.getCurrent()->accessBlockedUserIds();
if (sourceUserID == user.id)
{
switch (static_cast<ShowIgnoredUsersMessages>(
getSettings()->showBlockedUsersMessages.getValue()))
{
case ShowIgnoredUsersMessages::IfModerator:
if (this->channel->isMod() ||
this->channel->isBroadcaster())
return false;
break;
case ShowIgnoredUsersMessages::IfBroadcaster:
if (this->channel->isBroadcaster())
return false;
break;
case ShowIgnoredUsersMessages::Never:
break;
}
return true; if (auto it = blocks->find(sourceUserID); it != blocks->end())
{
switch (static_cast<ShowIgnoredUsersMessages>(
getSettings()->showBlockedUsersMessages.getValue()))
{
case ShowIgnoredUsersMessages::IfModerator:
if (this->channel->isMod() ||
this->channel->isBroadcaster())
return false;
break;
case ShowIgnoredUsersMessages::IfBroadcaster:
if (this->channel->isBroadcaster())
return false;
break;
case ShowIgnoredUsersMessages::Never:
break;
} }
return true;
} }
} }

View file

@ -697,13 +697,11 @@ void UserInfoPopup::updateUserData()
// get ignore state // get ignore state
bool isIgnoring = false; bool isIgnoring = false;
for (const auto &blockedUser : currentUser->getBlocks())
if (auto blocks = currentUser->accessBlockedUserIds();
blocks->find(user.id) != blocks->end())
{ {
if (user.id == blockedUser.id) isIgnoring = true;
{
isIgnoring = true;
break;
}
} }
// get ignoreHighlights state // get ignoreHighlights state

View file

@ -123,7 +123,10 @@ void IgnoresPage::onShow()
} }
QStringList users; QStringList users;
for (const auto &blockedUser : user->getBlocks())
auto blocks = app->accounts->twitch.getCurrent()->accessBlocks();
for (const auto &blockedUser : *blocks)
{ {
users << blockedUser.name; users << blockedUser.name;
} }