Skip to content

Commit

Permalink
Ifdef for pronouns
Browse files Browse the repository at this point in the history
  • Loading branch information
devJimmyboy committed Feb 11, 2024
1 parent 4e15eb1 commit 3417f93
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${SOURCE_FILES})
# Add autogenerated files
list(APPEND SOURCE_FILES ${RES_AUTOGEN_FILES})

add_library(${LIBRARY_PROJECT} OBJECT ${SOURCE_FILES})
add_library(${LIBRARY_PROJECT} OBJECT ${SOURCE_FILES} "providers/pronouns/Api.cpp")

if(CHATTERINO_PLUGINS)
target_compile_definitions(${LIBRARY_PROJECT}
Expand Down Expand Up @@ -819,7 +819,7 @@ if (BUILD_APP)
if (APPLE)
add_executable(${EXECUTABLE_PROJECT} ${MACOS_BUNDLE_ICON_FILE} main.cpp)
else()
add_executable(${EXECUTABLE_PROJECT} main.cpp)
add_executable(${EXECUTABLE_PROJECT} main.cpp "providers/pronouns/Api.cpp")
endif()

if (CHATTERINO_WITH_AVIF_PLUGIN)
Expand Down Expand Up @@ -935,7 +935,7 @@ set_target_properties(${LIBRARY_PROJECT}
# To avoid recompilations because of changing preprocessor definitions,
# this is its own project.
set(VERSION_SOURCE_FILES common/Version.cpp common/Version.hpp)
add_library(${VERSION_PROJECT} STATIC ${VERSION_SOURCE_FILES})
add_library(${VERSION_PROJECT} STATIC ${VERSION_SOURCE_FILES} "providers/pronouns/Api.cpp")

# source group for IDEs
source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${VERSION_SOURCE_FILES})
Expand Down
43 changes: 43 additions & 0 deletions src/providers/pronouns/Api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifdef CHATTERINO_HAVE_PLUGINS
#include "Api.hpp"

#include "common/NetworkRequest.hpp"
#include "common/NetworkResult.hpp"
#include "common/Outcome.hpp"

#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>

namespace chatterino::pronouns {

void fetchPronounsForUser(const QString &userName,
ResultCallback onLoaded, ErrorCallback onError)
{

NetworkRequest("https://pronouns.alejo.io/api/users/" + userName)
.timeout(5 * 1000)
.header("Accept", "application/json")
.onSuccess([onLoaded, onError](auto result) -> Outcome {
auto data = result.parseJsonArray();
QString pronoun("None");

if (!data.isEmpty())
{
pronoun = data[0].toObject().value("pronoun_id").toString();
}

onLoaded(pronoun);

return Success;
})
.onError([onError](auto /* result */) {
// TODO: make better xd

onError();
})
.execute();
}

} // namespace chatterino::pronouns
#endif
28 changes: 28 additions & 0 deletions src/providers/pronouns/Api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#ifdef CHATTERINO_HAVE_PLUGINS
#include <QString>


#include <functional>
#include <memory>
#include <vector>

namespace chatterino::pronouns {

using ResultCallback = std::function<void(const QString &)>;
using ErrorCallback = std::function<void()>;

/**
* @brief Loads pronouns for user provided
*
* @param userName Name of Twitch User
* @param onLoaded Callback taking the pronouns of the user
* @param onError Callback called when the network request fails
*/
void fetchPronounsForUser(const QString &userName,
ResultCallback onLoaded, ErrorCallback onError);


} // namespace chatterino::recentmessages
#endif
26 changes: 25 additions & 1 deletion src/widgets/dialogs/UserInfoPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/IvrApi.hpp"
#ifdef CHATTERINO_HAVE_PLUGINS
#include "providers/pronouns/Api.hpp"
#endif
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/ChannelPointReward.hpp"
#include "providers/twitch/TwitchAccount.hpp"
Expand Down Expand Up @@ -44,6 +47,7 @@
const QString TEXT_FOLLOWERS("Followers: %1");
const QString TEXT_CREATED("Created: %1");
const QString TEXT_LIVE("Status: %1");
const QString TEXT_PRONOUN("Pronouns: %1");
const QString TEXT_TITLE("%1's Usercard - #%2");
#define TEXT_USER_ID "ID: "
#define TEXT_UNAVAILABLE "(not available)"
Expand Down Expand Up @@ -386,6 +390,10 @@ UserInfoPopup::UserInfoPopup(bool closeAutomatically, Split *split)
.assign(&this->ui_.createdDateLabel);
vbox.emplace<Label>(TEXT_LIVE.arg(""))
.assign(&this->ui_.liveLabel);
#ifdef CHATTERINO_HAVE_PLUGINS
vbox.emplace<Label>(TEXT_PRONOUN.arg(""))
.assign(&this->ui_.pronounLabel);
#endif

vbox.emplace<Label>("").assign(&this->ui_.followageLabel);
vbox.emplace<Label>("").assign(&this->ui_.subageLabel);
Expand Down Expand Up @@ -822,7 +830,9 @@ void UserInfoPopup::updateUserData()
TEXT_FOLLOWERS.arg(TEXT_UNAVAILABLE));
this->ui_.createdDateLabel->setText(TEXT_CREATED.arg(TEXT_UNAVAILABLE));
this->ui_.liveLabel->setText(TEXT_LIVE.arg(TEXT_UNAVAILABLE));

#ifdef CHATTERINO_HAVE_PLUGINS
this->ui_.pronounLabel->setText(TEXT_PRONOUN.arg(TEXT_UNAVAILABLE));
#endif
this->ui_.nameLabel->setText(this->userName_);

this->ui_.userIDLabel->setText(QString("ID ") +
Expand Down Expand Up @@ -879,6 +889,20 @@ void UserInfoPopup::updateUserData()
{
this->loadAvatar(user);
}
#ifdef CHATTERINO_HAVE_PLUGINS
chatterino::pronouns::fetchPronounsForUser(
user.login,
[this, hack](const auto &pronouns) {
if (!hack.lock())
{
return;
}
this->ui_.pronounLabel->setText(TEXT_PRONOUN.arg(pronouns));
},
[]() {
qCWarning(chatterinoTwitch) << "Error getting pronouns";
});
#endif

getHelix()->getChannelFollowers(
user.id,
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/dialogs/UserInfoPopup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class UserInfoPopup final : public DraggablePopup
Label *followageLabel = nullptr;
Label *subageLabel = nullptr;
Label *liveLabel = nullptr;

#ifdef CHATTERINO_HAVE_PLUGINS
Label *pronounLabel = nullptr;
#endif
QCheckBox *block = nullptr;
QCheckBox *ignoreHighlights = nullptr;

Expand Down

0 comments on commit 3417f93

Please sign in to comment.