forked from Chatterino/chatterino2
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into chatterino7
- Loading branch information
Showing
30 changed files
with
628 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "providers/pronouns/Pronouns.hpp" | ||
|
||
#include "Application.hpp" | ||
#include "common/QLogging.hpp" | ||
#include "providers/pronouns/alejo/PronounsAlejoApi.hpp" | ||
#include "providers/pronouns/UserPronouns.hpp" | ||
|
||
#include <mutex> | ||
#include <unordered_map> | ||
|
||
namespace { | ||
|
||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) | ||
const auto &LOG = chatterinoPronouns; | ||
|
||
} // namespace | ||
|
||
namespace chatterino::pronouns { | ||
|
||
void Pronouns::getUserPronoun( | ||
const QString &username, | ||
const std::function<void(UserPronouns)> &callbackSuccess, | ||
const std::function<void()> &callbackFail) | ||
{ | ||
// Only fetch pronouns if we haven't fetched before. | ||
auto cachedPronoun = this->getCachedUserPronoun(username); | ||
if (cachedPronoun.has_value()) | ||
{ | ||
callbackSuccess(*cachedPronoun); | ||
return; | ||
} | ||
|
||
this->alejoApi.fetch(username, [this, callbackSuccess, callbackFail, | ||
username](const auto &oUserPronoun) { | ||
if (!oUserPronoun.has_value()) | ||
{ | ||
callbackFail(); | ||
return; | ||
} | ||
|
||
const auto &userPronoun = *oUserPronoun; | ||
|
||
qCDebug(LOG) << "Caching pronoun" << userPronoun.format() << "for user" | ||
<< username; | ||
{ | ||
std::unique_lock lock(this->mutex); | ||
this->saved[username] = userPronoun; | ||
} | ||
|
||
callbackSuccess(userPronoun); | ||
}); | ||
} | ||
|
||
std::optional<UserPronouns> Pronouns::getCachedUserPronoun( | ||
const QString &username) | ||
{ | ||
std::shared_lock lock(this->mutex); | ||
auto it = this->saved.find(username); | ||
if (it != this->saved.end()) | ||
{ | ||
return {it->second}; | ||
} | ||
return {}; | ||
} | ||
|
||
} // namespace chatterino::pronouns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include "providers/pronouns/alejo/PronounsAlejoApi.hpp" | ||
#include "providers/pronouns/UserPronouns.hpp" | ||
|
||
#include <functional> | ||
#include <optional> | ||
#include <shared_mutex> | ||
#include <unordered_map> | ||
|
||
namespace chatterino::pronouns { | ||
|
||
class Pronouns | ||
{ | ||
public: | ||
void getUserPronoun( | ||
const QString &username, | ||
const std::function<void(UserPronouns)> &callbackSuccess, | ||
const std::function<void()> &callbackFail); | ||
|
||
private: | ||
// Retrieve cached pronouns for user. | ||
std::optional<UserPronouns> getCachedUserPronoun(const QString &username); | ||
|
||
// mutex for editing the saved map. | ||
std::shared_mutex mutex; | ||
// Login name -> Pronouns | ||
std::unordered_map<QString, UserPronouns> saved; | ||
AlejoApi alejoApi; | ||
}; | ||
|
||
} // namespace chatterino::pronouns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "providers/pronouns/UserPronouns.hpp" | ||
|
||
#include <QString> | ||
|
||
#include <optional> | ||
|
||
namespace chatterino::pronouns { | ||
|
||
UserPronouns::UserPronouns(QString pronouns) | ||
: representation{!pronouns.isEmpty() ? std::move(pronouns) : QString()} | ||
{ | ||
} | ||
|
||
bool UserPronouns::isUnspecified() const | ||
{ | ||
return this->representation.isEmpty(); | ||
} | ||
|
||
UserPronouns::operator bool() const | ||
{ | ||
return !isUnspecified(); | ||
} | ||
|
||
} // namespace chatterino::pronouns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <QString> | ||
|
||
namespace chatterino::pronouns { | ||
|
||
class UserPronouns | ||
{ | ||
public: | ||
UserPronouns() = default; | ||
UserPronouns(QString pronouns); | ||
|
||
QString format() const | ||
{ | ||
if (isUnspecified()) | ||
{ | ||
return "unspecified"; | ||
} | ||
return this->representation; | ||
} | ||
|
||
bool isUnspecified() const; | ||
|
||
/// True, iff the pronouns are not unspecified. | ||
operator bool() const; | ||
|
||
private: | ||
QString representation; | ||
}; | ||
|
||
} // namespace chatterino::pronouns |
Oops, something went wrong.