Fix Twitch token status not reflecting reality when cached user ID empty

This commit is contained in:
WarmUpTill
2026-09-25 19:43:24 +02:00
parent abc8c3708f
commit b104f98d02
2 changed files with 24 additions and 10 deletions

View File

@@ -233,7 +233,8 @@ void TwitchToken::Load(obs_data_t *obj)
{ {
Item::Load(obj); Item::Load(obj);
_token = obs_data_get_string(obj, "token"); _token = obs_data_get_string(obj, "token");
if (obs_data_has_user_value(obj, "userID")) { if (obs_data_has_user_value(obj, "userID") &&
*obs_data_get_string(obj, "userID") != '\0') {
_userID = obs_data_get_string(obj, "userID"); _userID = obs_data_get_string(obj, "userID");
} else { } else {
_userID = {}; _userID = {};
@@ -259,7 +260,7 @@ void TwitchToken::Save(obs_data_t *obj) const
{ {
Item::Save(obj); Item::Save(obj);
obs_data_set_string(obj, "token", _token.c_str()); obs_data_set_string(obj, "token", _token.c_str());
if (_userID) { if (_userID && !_userID->empty()) {
obs_data_set_string(obj, "userID", _userID->c_str()); obs_data_set_string(obj, "userID", _userID->c_str());
} }
obs_data_set_bool(obj, "validateEventSubTimestamps", obs_data_set_bool(obj, "validateEventSubTimestamps",
@@ -314,7 +315,7 @@ bool TwitchToken::AnyOptionIsEnabled(
return false; return false;
} }
void TwitchToken::SetToken(const std::string &value) bool TwitchToken::SetToken(const std::string &value)
{ {
_token = value; _token = value;
auto res = auto res =
@@ -322,21 +323,34 @@ void TwitchToken::SetToken(const std::string &value)
if (res.status != 200) { if (res.status != 200) {
blog(LOG_WARNING, "failed to get Twitch user id from token!"); blog(LOG_WARNING, "failed to get Twitch user id from token!");
_userID = {}; _userID = {};
return; return false;
} }
bool gotUserID = false;
OBSDataArrayAutoRelease array = obs_data_get_array(res.data, "data"); OBSDataArrayAutoRelease array = obs_data_get_array(res.data, "data");
size_t count = obs_data_array_count(array); size_t count = obs_data_array_count(array);
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
OBSDataAutoRelease arrayObj = obs_data_array_item(array, i); OBSDataAutoRelease arrayObj = obs_data_array_item(array, i);
_userID = obs_data_get_string(arrayObj, "id"); std::string id = obs_data_get_string(arrayObj, "id");
if (id.empty()) {
blog(LOG_WARNING,
"Twitch /helix/users response did not contain a user id!");
continue;
}
_userID = id;
_name = obs_data_get_string(arrayObj, "display_name"); _name = obs_data_get_string(arrayObj, "display_name");
gotUserID = true;
}
if (!gotUserID) {
return false;
} }
// Trigger resubscribes with new token // Trigger resubscribes with new token
if (_eventSub) { if (_eventSub) {
_eventSub->ClearActiveSubscriptions(); _eventSub->ClearActiveSubscriptions();
} }
return true;
} }
std::optional<std::string> TwitchToken::GetToken() const std::optional<std::string> TwitchToken::GetToken() const
@@ -392,7 +406,7 @@ bool TwitchToken::IsValid(bool forceUpdate) const
return false; return false;
} }
if (_userID && _userID != id) { if (_userID && !_userID->empty() && _userID != id) {
blog(LOG_INFO, blog(LOG_INFO,
"Twitch token %s does not match expected user (got %s, expected %s)!", "Twitch token %s does not match expected user (got %s, expected %s)!",
_name.c_str(), id, _userID->c_str()); _name.c_str(), id, _userID->c_str());
@@ -400,7 +414,7 @@ bool TwitchToken::IsValid(bool forceUpdate) const
return false; return false;
} }
if (!_userID) { if (!_userID || _userID->empty()) {
_userID = id; _userID = id;
} }
@@ -827,9 +841,9 @@ void TwitchTokenSettingsDialog::GotToken(const std::optional<QString> &value)
return; return;
} }
_currentToken.SetToken(value.value().toStdString()); bool success = _currentToken.SetToken(value.value().toStdString());
auto name = QString::fromStdString(_currentToken._name); auto name = QString::fromStdString(_currentToken._name);
if (name.isEmpty()) { if (!success || name.isEmpty()) {
_tokenStatus->setText(obs_module_text( _tokenStatus->setText(obs_module_text(
"AdvSceneSwitcher.twitchToken.request.fail")); "AdvSceneSwitcher.twitchToken.request.fail"));
_name->setText(""); _name->setText("");

View File

@@ -48,7 +48,7 @@ public:
bool OptionIsActive(const TokenOption &option) const; bool OptionIsActive(const TokenOption &option) const;
bool OptionIsEnabled(const TokenOption &option) const; bool OptionIsEnabled(const TokenOption &option) const;
bool AnyOptionIsEnabled(const std::vector<TokenOption> &options) const; bool AnyOptionIsEnabled(const std::vector<TokenOption> &options) const;
void SetToken(const std::string &); bool SetToken(const std::string &);
bool IsEmpty() const { return _token.empty(); } bool IsEmpty() const { return _token.empty(); }
std::optional<std::string> GetToken() const; std::optional<std::string> GetToken() const;
std::optional<std::string> GetUserID() const { return _userID; } std::optional<std::string> GetUserID() const { return _userID; }