diff --git a/src/providers/twitch/TwitchAccount.cpp b/src/providers/twitch/TwitchAccount.cpp index 2f115ab2f..61fef4709 100644 --- a/src/providers/twitch/TwitchAccount.cpp +++ b/src/providers/twitch/TwitchAccount.cpp @@ -106,7 +106,13 @@ void TwitchAccount::loadIgnores() if (userIt == block.MemberEnd()) { continue; } - this->ignores.insert(TwitchUser::fromJSON(userIt->value)); + TwitchUser ignoredUser; + if (!rj::getSafe(userIt->value, ignoredUser)) { + Log("Error parsing twitch user JSON {}", rj::stringify(userIt->value)); + continue; + } + + this->ignores.insert(ignoredUser); } } @@ -155,7 +161,12 @@ void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targe return false; } - auto ignoredUser = TwitchUser::fromJSON(userIt->value); + TwitchUser ignoredUser; + if (!rj::getSafe(userIt->value, ignoredUser)) { + onFinished(IgnoreResult_Failed, + "Bad JSON data while ignoring user (invalid user) " + targetName); + return false; + } { std::lock_guard lock(this->ignoresMutex); diff --git a/src/providers/twitch/TwitchUser.cpp b/src/providers/twitch/TwitchUser.cpp index 0609dc02a..e24e9c781 100644 --- a/src/providers/twitch/TwitchUser.cpp +++ b/src/providers/twitch/TwitchUser.cpp @@ -4,27 +4,4 @@ namespace chatterino { -TwitchUser TwitchUser::fromJSON(const rapidjson::Value &value) -{ - TwitchUser user; - - if (!value.IsObject()) { - throw std::runtime_error("JSON value is not an object"); - } - - if (!rj::getSafe(value, "_id", user.id)) { - throw std::runtime_error("Missing ID key"); - } - - if (!rj::getSafe(value, "name", user.name)) { - throw std::runtime_error("Missing name key"); - } - - if (!rj::getSafe(value, "display_name", user.displayName)) { - throw std::runtime_error("Missing display name key"); - } - - return user; -} - } // namespace chatterino diff --git a/src/providers/twitch/TwitchUser.hpp b/src/providers/twitch/TwitchUser.hpp index aad39efe0..d69c05572 100644 --- a/src/providers/twitch/TwitchUser.hpp +++ b/src/providers/twitch/TwitchUser.hpp @@ -1,7 +1,10 @@ #pragma once +#include "util/RapidjsonHelpers.hpp" + #include #include +#include #include @@ -20,8 +23,6 @@ struct TwitchUser { this->displayName = other.displayName; } - static TwitchUser fromJSON(const rapidjson::Value &value); - bool operator<(const TwitchUser &rhs) const { return this->id < rhs.id; @@ -29,3 +30,45 @@ struct TwitchUser { }; } // namespace chatterino + +namespace pajlada { +namespace Settings { + +template <> +struct Deserialize { + static chatterino::TwitchUser get(const rapidjson::Value &value, bool *error = nullptr) + { + using namespace chatterino; + + TwitchUser user; + + if (!value.IsObject()) { + PAJLADA_REPORT_ERROR(error) + PAJLADA_THROW_EXCEPTION("Deserialized rapidjson::Value is wrong type"); + return user; + } + + if (!rj::getSafe(value, "_id", user.id)) { + PAJLADA_REPORT_ERROR(error) + PAJLADA_THROW_EXCEPTION("Missing ID key"); + return user; + } + + if (!rj::getSafe(value, "name", user.name)) { + PAJLADA_REPORT_ERROR(error) + PAJLADA_THROW_EXCEPTION("Missing name key"); + return user; + } + + if (!rj::getSafe(value, "display_name", user.displayName)) { + PAJLADA_REPORT_ERROR(error) + PAJLADA_THROW_EXCEPTION("Missing display name key"); + return user; + } + + return user; + } +}; + +} // namespace Settings +} // namespace pajlada