Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to the new Get Channel Followers Helix endpoint, fixing follower count not showing up in usercards #4809

Merged
merged 2 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

- Minor: Migrate to the new Get Channel Followers Helix endpoint, fixing follower count not showing up in usercards. (#4809)
- Bugfix: Fixed a performance issue when displaying replies to certain messages. (#4807)
- Bugfix: Fixed a data race when disconnecting from Twitch PubSub. (#4771)
- Bugfix: Fixed `/shoutout` command not working with usernames starting with @'s (e.g. `/shoutout @forsen`). (#4800)
Expand Down
17 changes: 6 additions & 11 deletions mocks/include/mocks/Helix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,12 @@ class Helix : public IHelix
HelixFailureCallback failureCallback),
(override));

MOCK_METHOD(void, fetchUsersFollows,
(QString fromId, QString toId,
ResultCallback<HelixUsersFollowsResponse> successCallback,
HelixFailureCallback failureCallback),
(override));

MOCK_METHOD(void, getUserFollowers,
(QString userId,
ResultCallback<HelixUsersFollowsResponse> successCallback,
HelixFailureCallback failureCallback),
(override));
MOCK_METHOD(
Felanbird marked this conversation as resolved.
Show resolved Hide resolved
void, getChannelFollowers,
(QString broadcasterID,
ResultCallback<HelixGetChannelFollowersResponse> successCallback,
std::function<void(QString)> failureCallback),
(override));

MOCK_METHOD(void, fetchStreams,
(QStringList userIds, QStringList userLogins,
Expand Down
49 changes: 20 additions & 29 deletions src/providers/twitch/api/Helix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,52 +126,43 @@ void Helix::getUserById(QString userId,
failureCallback);
}

void Helix::fetchUsersFollows(
QString fromId, QString toId,
ResultCallback<HelixUsersFollowsResponse> successCallback,
HelixFailureCallback failureCallback)
void Helix::getChannelFollowers(
QString broadcasterID,
ResultCallback<HelixGetChannelFollowersResponse> successCallback,
std::function<void(QString)> failureCallback)
{
assert(!fromId.isEmpty() || !toId.isEmpty());
assert(!broadcasterID.isEmpty());

QUrlQuery urlQuery;

if (!fromId.isEmpty())
{
urlQuery.addQueryItem("from_id", fromId);
}

if (!toId.isEmpty())
{
urlQuery.addQueryItem("to_id", toId);
}
urlQuery.addQueryItem("broadcaster_id", broadcasterID);

// TODO: set on success and on error
this->makeGet("users/follows", urlQuery)
this->makeGet("channels/followers", urlQuery)
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto root = result.parseJson();
if (root.empty())
{
failureCallback();
failureCallback("Bad JSON response");
return Failure;
}
successCallback(HelixUsersFollowsResponse(root));
successCallback(HelixGetChannelFollowersResponse(root));
return Success;
})
.onError([failureCallback](auto /*result*/) {
// TODO: make better xd
failureCallback();
.onError([failureCallback](auto result) {
auto root = result.parseJson();
if (root.empty())
{
failureCallback("Unknown error");
return;
}

// Forward "message" from Twitch
HelixError error(root);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'error' of type 'chatterino::HelixError' can be declared 'const' [misc-const-correctness]

Suggested change
HelixError error(root);
HelixError const error(root);

failureCallback(error.message);
})
.execute();
}

void Helix::getUserFollowers(
QString userId, ResultCallback<HelixUsersFollowsResponse> successCallback,
HelixFailureCallback failureCallback)
{
this->fetchUsersFollows("", std::move(userId), std::move(successCallback),
std::move(failureCallback));
}

void Helix::fetchStreams(
QStringList userIds, QStringList userLogins,
ResultCallback<std::vector<HelixStream>> successCallback,
Expand Down
84 changes: 29 additions & 55 deletions src/providers/twitch/api/Helix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,12 @@ struct HelixUser {
}
};

struct HelixUsersFollowsRecord {
QString fromId;
QString fromName;
QString toId;
QString toName;
QString followedAt; // date time object

HelixUsersFollowsRecord()
: fromId("")
, fromName("")
, toId("")
, toName("")
, followedAt("")
{
}

explicit HelixUsersFollowsRecord(QJsonObject jsonObject)
: fromId(jsonObject.value("from_id").toString())
, fromName(jsonObject.value("from_name").toString())
, toId(jsonObject.value("to_id").toString())
, toName(jsonObject.value("to_name").toString())
, followedAt(jsonObject.value("followed_at").toString())
{
}
};

struct HelixUsersFollowsResponse {
struct HelixGetChannelFollowersResponse {
int total;
std::vector<HelixUsersFollowsRecord> data;
explicit HelixUsersFollowsResponse(QJsonObject jsonObject)

explicit HelixGetChannelFollowersResponse(const QJsonObject &jsonObject)
: total(jsonObject.value("total").toInt())
{
const auto &jsonData = jsonObject.value("data").toArray();
std::transform(jsonData.begin(), jsonData.end(),
std::back_inserter(this->data),
[](const QJsonValue &record) {
return HelixUsersFollowsRecord(record.toObject());
});
}
};

Expand Down Expand Up @@ -720,6 +688,22 @@ enum class HelixGetGlobalBadgesError {
Forwarded,
};

struct HelixError {
/// Text version of the HTTP error that happened (e.g. Bad Request)
QString error;
/// Number version of the HTTP error that happened (e.g. 400)
int status;
/// The error message string
QString message;

explicit HelixError(const QJsonObject &json)
: error(json["error"].toString())
, status(json["status"].toInt())
, message(json["message"].toString())
{
}
};

using HelixGetChannelBadgesError = HelixGetGlobalBadgesError;

class IHelix
Expand All @@ -740,16 +724,11 @@ class IHelix
ResultCallback<HelixUser> successCallback,
HelixFailureCallback failureCallback) = 0;

// https://dev.twitch.tv/docs/api/reference#get-users-follows
virtual void fetchUsersFollows(
QString fromId, QString toId,
ResultCallback<HelixUsersFollowsResponse> successCallback,
HelixFailureCallback failureCallback) = 0;

virtual void getUserFollowers(
QString userId,
ResultCallback<HelixUsersFollowsResponse> successCallback,
HelixFailureCallback failureCallback) = 0;
// https://dev.twitch.tv/docs/api/reference/#get-channel-followers
virtual void getChannelFollowers(
QString broadcasterID,
ResultCallback<HelixGetChannelFollowersResponse> successCallback,
std::function<void(QString)> failureCallback) = 0;

// https://dev.twitch.tv/docs/api/reference#get-streams
virtual void fetchStreams(
Expand Down Expand Up @@ -1064,16 +1043,11 @@ class Helix final : public IHelix
void getUserById(QString userId, ResultCallback<HelixUser> successCallback,
HelixFailureCallback failureCallback) final;

// https://dev.twitch.tv/docs/api/reference#get-users-follows
void fetchUsersFollows(
QString fromId, QString toId,
ResultCallback<HelixUsersFollowsResponse> successCallback,
HelixFailureCallback failureCallback) final;

void getUserFollowers(
QString userId,
ResultCallback<HelixUsersFollowsResponse> successCallback,
HelixFailureCallback failureCallback) final;
// https://dev.twitch.tv/docs/api/reference/#get-channel-followers
void getChannelFollowers(
QString broadcasterID,
ResultCallback<HelixGetChannelFollowersResponse> successCallback,
std::function<void(QString)> failureCallback) final;

// https://dev.twitch.tv/docs/api/reference#get-streams
void fetchStreams(QStringList userIds, QStringList userLogins,
Expand Down
7 changes: 4 additions & 3 deletions src/widgets/dialogs/UserInfoPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ void UserInfoPopup::updateUserData()
this->loadAvatar(user.profileImageUrl);
}

getHelix()->getUserFollowers(
getHelix()->getChannelFollowers(
user.id,
[this, hack](const auto &followers) {
if (!hack.lock())
Expand All @@ -827,8 +827,9 @@ void UserInfoPopup::updateUserData()
this->ui_.followerCountLabel->setText(
TEXT_FOLLOWERS.arg(localizeNumbers(followers.total)));
},
[] {
// on failure
[](const auto &errorMessage) {
qCWarning(chatterinoTwitch)
<< "Error getting followers:" << errorMessage;
});

// get ignore state
Expand Down