From a448af5f574b0a551f4e31c33fedbfe3107a0e57 Mon Sep 17 00:00:00 2001 From: Yoitsu Date: Sun, 11 Apr 2021 02:34:27 +0300 Subject: [PATCH] Implement Helix Search Categories end-point & update /setgame (#2609) Co-authored-by: pajlada --- CHANGELOG.md | 2 +- .../commands/CommandController.cpp | 8 ++--- src/providers/twitch/api/Helix.cpp | 36 +++++++++++++++++++ src/providers/twitch/api/Helix.hpp | 5 +++ src/providers/twitch/api/README.md | 8 +++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a20663931..25d5ba304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - Major: Added clip creation support. You can create clips with `/clip` command, `Alt+X` keybind or `Create a clip` option in split header's context menu. This requires a new authentication scope so re-authentication will be required to use it. (#2271, #2377, #2528) - Major: Added "Channel Filters". See https://wiki.chatterino.com/Filters/ for how they work or how to configure them. (#1748, #2083, #2090, #2200, #2225) - Major: Added Streamer Mode configuration (under `Settings -> General`), where you can select which features of Chatterino should behave differently when you are in Streamer Mode. (#2001, #2316, #2342, #2376) -- Major: Add `/settitle` and `/setgame` commands, originally made for Mm2PL/Dankerino. (#2534) +- Major: Add `/settitle` and `/setgame` commands, originally made for Mm2PL/Dankerino. (#2534, #2609) - Major: Color mentions to match the mentioned users. You can disable this by unchecking "Color @usernames" under `Settings -> General -> Advanced (misc.)`. (#1963, #2284) - Major: Commands `/ignore` and `/unignore` have been renamed to `/block` and `/unblock` in order to keep consistency with Twitch's terms. (#2370) - Major: Added support for bit emotes - the ones you unlock after cheering to streamer. (#2550) diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index 644bb5b39..e6b7a9414 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -687,15 +687,15 @@ void CommandController::initialize(Settings &, Paths &paths) } if (auto twitchChannel = dynamic_cast(channel.get())) { - getHelix()->fetchGames( - QStringList(), {words.mid(1).join(" ")}, + getHelix()->searchGames( + words.mid(1).join(" "), [channel, twitchChannel](std::vector games) { - if (games.size() == 0) + if (games.empty()) { channel->addMessage( makeSystemMessage("Game not found.")); } - else // 0 or 1 games + else // 1 or more games { auto status = twitchChannel->accessStreamStatus(); getHelix()->updateChannel( diff --git a/src/providers/twitch/api/Helix.cpp b/src/providers/twitch/api/Helix.cpp index 28eaf410f..0af299970 100644 --- a/src/providers/twitch/api/Helix.cpp +++ b/src/providers/twitch/api/Helix.cpp @@ -296,6 +296,42 @@ void Helix::fetchGames(QStringList gameIds, QStringList gameNames, .execute(); } +void Helix::searchGames(QString gameName, + ResultCallback> successCallback, + HelixFailureCallback failureCallback) +{ + QUrlQuery urlQuery; + urlQuery.addQueryItem("query", gameName); + + this->makeRequest("search/categories", urlQuery) + .onSuccess([successCallback, failureCallback](auto result) -> Outcome { + auto root = result.parseJson(); + auto data = root.value("data"); + + if (!data.isArray()) + { + failureCallback(); + return Failure; + } + + std::vector games; + + for (const auto &jsonStream : data.toArray()) + { + games.emplace_back(jsonStream.toObject()); + } + + successCallback(games); + + return Success; + }) + .onError([failureCallback](auto /*result*/) { + // TODO: make better xd + failureCallback(); + }) + .execute(); +} + void Helix::getGameById(QString gameId, ResultCallback successCallback, HelixFailureCallback failureCallback) diff --git a/src/providers/twitch/api/Helix.hpp b/src/providers/twitch/api/Helix.hpp index 38fc153d1..ea5c7beed 100644 --- a/src/providers/twitch/api/Helix.hpp +++ b/src/providers/twitch/api/Helix.hpp @@ -252,6 +252,11 @@ public: ResultCallback> successCallback, HelixFailureCallback failureCallback); + // https://dev.twitch.tv/docs/api/reference#search-categories + void searchGames(QString gameName, + ResultCallback> successCallback, + HelixFailureCallback failureCallback); + void getGameById(QString gameId, ResultCallback successCallback, HelixFailureCallback failureCallback); diff --git a/src/providers/twitch/api/README.md b/src/providers/twitch/api/README.md index ddea624ef..779758fe3 100644 --- a/src/providers/twitch/api/README.md +++ b/src/providers/twitch/api/README.md @@ -152,6 +152,14 @@ Requires `user:manage:blocked_users` scope - `widgets/dialogs/UserInfoPopup.cpp` to unblock a user via checkbox in the usercard - `controllers/commands/CommandController.cpp` to unblock a user via "/unblock" command +### Search Categories + +URL: https://dev.twitch.tv/docs/api/reference#search-categories + +- We implement this in `providers/twitch/api/Helix.cpp searchGames` + Used in: + - `controllers/commands/CommandController.cpp` in `/setgame` command to fuzzy search for game titles + ## TMI The TMI api is undocumented.