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

View file

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

View file

@ -4,14 +4,13 @@
#include <QJsonObject>
#include <QJsonValue>
#include <QThread>
#include <QUrl>
#include <map>
#include <shared_mutex>
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "messages/Emote.hpp"
#include <QUrl>
#include <map>
namespace chatterino {
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)
{
std::shared_lock lock(this->mutex_);
auto it = this->badgeMap.find(id.string);
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)
{
std::shared_lock lock(this->mutex_);
auto badgeIt = this->badgeMap.find(id.string);
if (badgeIt != this->badgeMap.end())
{
@ -49,6 +52,8 @@ void FfzBadges::loadFfzBadges()
NetworkRequest(url)
.onSuccess([this](auto result) -> Outcome {
std::unique_lock lock(this->mutex_);
auto jsonRoot = result.parseJson();
int index = 0;
for (const auto &jsonBadge_ : jsonRoot.value("badges").toArray())

View file

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

View file

@ -101,14 +101,17 @@ void TwitchAccount::loadBlocks()
getHelix()->loadBlocks(
getApp()->accounts->twitch.getCurrent()->userId_,
[this](std::vector<HelixBlock> blocks) {
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
this->ignores_.clear();
auto ignores = this->ignores_.access();
auto userIds = this->ignoresUserIds_.access();
ignores->clear();
userIds->clear();
for (const HelixBlock &block : blocks)
{
TwitchUser blockedUser;
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;
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();
},
@ -143,9 +148,11 @@ void TwitchAccount::unblockUser(QString userId, std::function<void()> onSuccess,
TwitchUser ignoredUser;
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();
},
@ -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()

View file

@ -106,7 +106,8 @@ public:
void checkFollow(const QString targetUserID,
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 loadUserstateEmotes(QStringList emoteSetKeys);
@ -128,7 +129,8 @@ private:
mutable std::mutex ignoresMutex_;
QElapsedTimer userstateEmotesTimer_;
std::set<TwitchUser> ignores_;
UniqueAccess<std::set<TwitchUser>> ignores_;
UniqueAccess<std::set<QString>> ignoresUserIds_;
// std::map<UserId, TwitchAccountEmoteData> emotes;
UniqueAccess<TwitchAccountEmoteData> emotes_;

View file

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

View file

@ -143,28 +143,28 @@ bool TwitchMessageBuilder::isIgnored() const
{
auto sourceUserID = this->tags.value("user-id").toString();
for (const auto &user : app->accounts->twitch.getCurrent()->getBlocks())
{
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;
}
auto blocks =
app->accounts->twitch.getCurrent()->accessBlockedUserIds();
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
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;
break;
}
isIgnoring = true;
}
// get ignoreHighlights state

View file

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