Skip to content

Commit

Permalink
feat: add setting for showing pronouns in user info popup (#5442)
Browse files Browse the repository at this point in the history
This uses https://pr.alejo.io/

Co-authored-by: pajlada <[email protected]>
  • Loading branch information
DeltaTimo and pajlada committed Sep 8, 2024
1 parent aae1288 commit 9375bce
Show file tree
Hide file tree
Showing 18 changed files with 402 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unversioned

- Major: Add option to show pronouns in user card. (#5442)
- Major: Release plugins alpha. (#5288)
- Major: Improve high-DPI support on Windows. (#4868, #5391)
- Minor: Removed the Ctrl+Shift+L hotkey for toggling the "live only" tab visibility state. (#5530)
Expand Down
7 changes: 7 additions & 0 deletions benchmarks/src/RecentMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "providers/chatterino/ChatterinoBadges.hpp"
#include "providers/ffz/FfzBadges.hpp"
#include "providers/ffz/FfzEmotes.hpp"
#include "providers/pronouns/Pronouns.hpp"
#include "providers/recentmessages/Impl.hpp"
#include "providers/seventv/SeventvBadges.hpp"
#include "providers/seventv/SeventvEmotes.hpp"
Expand Down Expand Up @@ -110,6 +111,11 @@ class MockApplication : public mock::BaseApplication
return &this->linkResolver;
}

pronouns::Pronouns *getPronouns() override
{
return &this->pronouns;
}

AccountController accounts;
Emotes emotes;
mock::UserDataController userData;
Expand All @@ -124,6 +130,7 @@ class MockApplication : public mock::BaseApplication
FfzEmotes ffzEmotes;
SeventvEmotes seventvEmotes;
DisabledStreamerMode streamerMode;
pronouns::Pronouns pronouns;
};

std::optional<QJsonDocument> tryReadJsonFile(const QString &path)
Expand Down
7 changes: 7 additions & 0 deletions mocks/include/mocks/EmptyApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ class EmptyApplication : public IApplication
return nullptr;
}

pronouns::Pronouns *getPronouns() override
{
assert(false && "EmptyApplication::getPronouns was called without "
"being initialized");
return nullptr;
}

QTemporaryDir settingsDir;
Paths paths_;
Args args_;
Expand Down
10 changes: 10 additions & 0 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "providers/bttv/BttvEmotes.hpp"
#include "providers/ffz/FfzEmotes.hpp"
#include "providers/links/LinkResolver.hpp"
#include "providers/pronouns/Pronouns.hpp"
#include "providers/seventv/SeventvAPI.hpp"
#include "providers/seventv/SeventvEmotes.hpp"
#include "providers/twitch/TwitchBadges.hpp"
Expand Down Expand Up @@ -178,6 +179,7 @@ Application::Application(Settings &_settings, const Paths &paths,
, linkResolver(new LinkResolver)
, streamerMode(new StreamerMode)
, twitchUsers(new TwitchUsers)
, pronouns(std::make_shared<pronouns::Pronouns>())
#ifdef CHATTERINO_HAVE_PLUGINS
, plugins(new PluginController(paths))
#endif
Expand Down Expand Up @@ -565,6 +567,14 @@ SeventvEventAPI *Application::getSeventvEventAPI()
return this->seventvEventAPI.get();
}

pronouns::Pronouns *Application::getPronouns()
{
// pronouns::Pronouns handles its own locks, so we don't need to assert that this is called in the GUI thread
assert(this->pronouns);

return this->pronouns.get();
}

void Application::save()
{
this->commands->save();
Expand Down
6 changes: 6 additions & 0 deletions src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class SeventvEventAPI;
class ILinkResolver;
class IStreamerMode;
class ITwitchUsers;
namespace pronouns {
class Pronouns;
} // namespace pronouns

class IApplication
{
Expand Down Expand Up @@ -105,6 +108,7 @@ class IApplication
virtual ILinkResolver *getLinkResolver() = 0;
virtual IStreamerMode *getStreamerMode() = 0;
virtual ITwitchUsers *getTwitchUsers() = 0;
virtual pronouns::Pronouns *getPronouns() = 0;
};

class Application : public IApplication
Expand Down Expand Up @@ -169,6 +173,7 @@ class Application : public IApplication
std::unique_ptr<ILinkResolver> linkResolver;
std::unique_ptr<IStreamerMode> streamerMode;
std::unique_ptr<ITwitchUsers> twitchUsers;
std::shared_ptr<pronouns::Pronouns> pronouns;
#ifdef CHATTERINO_HAVE_PLUGINS
std::unique_ptr<PluginController> plugins;
#endif
Expand Down Expand Up @@ -215,6 +220,7 @@ class Application : public IApplication
FfzEmotes *getFfzEmotes() override;
SeventvEmotes *getSeventvEmotes() override;
SeventvEventAPI *getSeventvEventAPI() override;
pronouns::Pronouns *getPronouns() override;

ILinkResolver *getLinkResolver() override;
IStreamerMode *getStreamerMode() override;
Expand Down
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ set(SOURCE_FILES
providers/liveupdates/BasicPubSubManager.hpp
providers/liveupdates/BasicPubSubWebsocket.hpp

providers/pronouns/Pronouns.cpp
providers/pronouns/Pronouns.hpp
providers/pronouns/UserPronouns.cpp
providers/pronouns/UserPronouns.hpp
providers/pronouns/alejo/PronounsAlejoApi.cpp
providers/pronouns/alejo/PronounsAlejoApi.hpp

providers/recentmessages/Api.cpp
providers/recentmessages/Api.hpp
providers/recentmessages/Impl.cpp
Expand Down
1 change: 1 addition & 0 deletions src/common/QLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Q_LOGGING_CATEGORY(chatterinoNotification, "chatterino.notification",
logThreshold);
Q_LOGGING_CATEGORY(chatterinoImageuploader, "chatterino.imageuploader",
logThreshold);
Q_LOGGING_CATEGORY(chatterinoPronouns, "chatterino.pronouns", logThreshold);
Q_LOGGING_CATEGORY(chatterinoPubSub, "chatterino.pubsub", logThreshold);
Q_LOGGING_CATEGORY(chatterinoRecentMessages, "chatterino.recentmessages",
logThreshold);
Expand Down
1 change: 1 addition & 0 deletions src/common/QLogging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Q_DECLARE_LOGGING_CATEGORY(chatterinoMessage);
Q_DECLARE_LOGGING_CATEGORY(chatterinoNativeMessage);
Q_DECLARE_LOGGING_CATEGORY(chatterinoNetwork);
Q_DECLARE_LOGGING_CATEGORY(chatterinoNotification);
Q_DECLARE_LOGGING_CATEGORY(chatterinoPronouns);
Q_DECLARE_LOGGING_CATEGORY(chatterinoPubSub);
Q_DECLARE_LOGGING_CATEGORY(chatterinoRecentMessages);
Q_DECLARE_LOGGING_CATEGORY(chatterinoSettings);
Expand Down
64 changes: 64 additions & 0 deletions src/providers/pronouns/Pronouns.cpp
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
31 changes: 31 additions & 0 deletions src/providers/pronouns/Pronouns.hpp
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
24 changes: 24 additions & 0 deletions src/providers/pronouns/UserPronouns.cpp
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
31 changes: 31 additions & 0 deletions src/providers/pronouns/UserPronouns.hpp
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
Loading

0 comments on commit 9375bce

Please sign in to comment.