-
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add setting for showing pronouns in user info popup (#5442)
This uses https://pr.alejo.io/ Co-authored-by: pajlada <[email protected]>
- Loading branch information
Showing
18 changed files
with
402 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#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 <string> | ||
#include <unordered_map> | ||
|
||
namespace chatterino::pronouns { | ||
|
||
void Pronouns::fetch(const QString &username, | ||
const std::function<void(UserPronouns)> &callbackSuccess, | ||
const std::function<void()> &callbackFail) | ||
{ | ||
// Only fetch pronouns if we haven't fetched before. | ||
{ | ||
std::shared_lock lock(this->mutex); | ||
|
||
auto iter = this->saved.find(username); | ||
if (iter != this->saved.end()) | ||
{ | ||
callbackSuccess(iter->second); | ||
return; | ||
} | ||
} // unlock mutex | ||
|
||
qCDebug(chatterinoPronouns) | ||
<< "Fetching pronouns from alejo.io for " << username; | ||
|
||
alejoApi.fetch(username, [this, callbackSuccess, callbackFail, | ||
username](std::optional<UserPronouns> result) { | ||
if (result.has_value()) | ||
{ | ||
{ | ||
std::unique_lock lock(this->mutex); | ||
this->saved[username] = *result; | ||
} // unlock mutex | ||
qCDebug(chatterinoPronouns) | ||
<< "Adding pronouns " << result->format() << " for user " | ||
<< username; | ||
callbackSuccess(*result); | ||
} | ||
else | ||
{ | ||
callbackFail(); | ||
} | ||
}); | ||
} | ||
|
||
std::optional<UserPronouns> Pronouns::getForUsername(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,31 @@ | ||
#pragma once | ||
|
||
#include "providers/pronouns/alejo/PronounsAlejoApi.hpp" | ||
#include "providers/pronouns/UserPronouns.hpp" | ||
|
||
#include <optional> | ||
#include <shared_mutex> | ||
#include <unordered_map> | ||
|
||
namespace chatterino::pronouns { | ||
|
||
class Pronouns | ||
{ | ||
public: | ||
Pronouns() = default; | ||
|
||
void fetch(const QString &username, | ||
const std::function<void(UserPronouns)> &callbackSuccess, | ||
const std::function<void()> &callbackFail); | ||
|
||
// Retrieve cached pronouns for user. | ||
std::optional<UserPronouns> getForUsername(const QString &username); | ||
|
||
private: | ||
// 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.