From 857705668e65bceeb9833ff87f61b3ac11245bda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= Date: Thu, 21 Jan 2021 13:48:48 +0100 Subject: [PATCH] Migrated getChannel to Helix (#2381) Co-authored-by: Rasmus Karlsson --- .github/workflows/build.yml | 4 ++-- .github/workflows/test.yml | 2 +- CHANGELOG.md | 1 + src/providers/twitch/TwitchChannel.cpp | 33 +++++++++++++------------- src/providers/twitch/api/Helix.cpp | 29 ++++++++++++++++++++++ src/providers/twitch/api/Helix.hpp | 24 +++++++++++++++++++ src/providers/twitch/api/Kraken.cpp | 21 ---------------- src/providers/twitch/api/Kraken.hpp | 5 ---- src/providers/twitch/api/README.md | 16 ++++++------- 9 files changed, 81 insertions(+), 54 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5a7a4114c..086f5cc23 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -43,7 +43,7 @@ jobs: uses: jurplel/install-qt-action@v2 if: ${{ matrix.qt-version == 'default' }} with: - # mirror: 'http://mirrors.ocf.berkeley.edu/qt/' + mirror: 'http://mirrors.ocf.berkeley.edu/qt/' cached: ${{ steps.cache-qt.outputs.cache-hit }} extra: --external 7z @@ -51,7 +51,7 @@ jobs: uses: jurplel/install-qt-action@v2 if: ${{ matrix.qt-version != 'default' }} with: - # mirror: 'http://mirrors.ocf.berkeley.edu/qt/' + mirror: 'http://mirrors.ocf.berkeley.edu/qt/' cached: ${{ steps.cache-qt.outputs.cache-hit }} extra: --external 7z version: ${{ matrix.qt-version }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1a81ab415..a8a2ac27b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,7 +35,7 @@ jobs: - name: Install Qt uses: jurplel/install-qt-action@v2 with: - # mirror: 'http://mirrors.ocf.berkeley.edu/qt/' + mirror: 'http://mirrors.ocf.berkeley.edu/qt/' cached: ${{ steps.cache-qt.outputs.cache-hit }} extra: --external 7z diff --git a/CHANGELOG.md b/CHANGELOG.md index 33a6a5500..8270b08c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,7 @@ - Dev: Updated minimum required Qt framework version to 5.12. (#2210) - Dev: Migrated `Kraken::getUser` to Helix (#2260) - Dev: Migrated `TwitchAccount::(un)followUser` from Kraken to Helix and moved it to `Helix::(un)followUser`. (#2306) +- Dev: Migrated `Kraken::getChannel` to Helix. (#2381) - Dev: Build in CI with multiple Qt versions (#2349) ## 2.2.2 diff --git a/src/providers/twitch/TwitchChannel.cpp b/src/providers/twitch/TwitchChannel.cpp index 1747562c7..deeb71da1 100644 --- a/src/providers/twitch/TwitchChannel.cpp +++ b/src/providers/twitch/TwitchChannel.cpp @@ -599,25 +599,26 @@ void TwitchChannel::refreshTitle() } this->titleRefreshedTime_ = QTime::currentTime(); - const auto onSuccess = [this, - weak = weakOf(this)](const auto &channel) { - ChannelPtr shared = weak.lock(); - if (!shared) - { - return; - } + getHelix()->getChannel( + roomID, + [this, weak = weakOf(this)](HelixChannel channel) { + ChannelPtr shared = weak.lock(); - { - auto status = this->streamStatus_.access(); - status->title = channel.status; - } + if (!shared) + { + return; + } - this->liveStatusChanged.invoke(); - }; + { + auto status = this->streamStatus_.access(); + status->title = channel.title; + } - const auto onFailure = [] {}; - - getKraken()->getChannel(roomID, onSuccess, onFailure); + this->liveStatusChanged.invoke(); + }, + [] { + // failure + }); } void TwitchChannel::refreshLiveStatus() diff --git a/src/providers/twitch/api/Helix.cpp b/src/providers/twitch/api/Helix.cpp index 4272b6f2f..51c2c27d3 100644 --- a/src/providers/twitch/api/Helix.cpp +++ b/src/providers/twitch/api/Helix.cpp @@ -413,6 +413,35 @@ void Helix::createClip(QString channelId, .execute(); } +void Helix::getChannel(QString broadcasterId, + ResultCallback successCallback, + HelixFailureCallback failureCallback) +{ + QUrlQuery urlQuery; + urlQuery.addQueryItem("broadcaster_id", broadcasterId); + + this->makeRequest("channels", urlQuery) + .onSuccess([successCallback, failureCallback](auto result) -> Outcome { + auto root = result.parseJson(); + auto data = root.value("data"); + + if (!data.isArray()) + { + failureCallback(); + return Failure; + } + + HelixChannel channel(data.toArray()[0].toObject()); + + successCallback(channel); + return Success; + }) + .onError([failureCallback](auto /*result*/) { + failureCallback(); + }) + .execute(); +} + NetworkRequest Helix::makeRequest(QString url, QUrlQuery urlQuery) { assert(!url.startsWith("/")); diff --git a/src/providers/twitch/api/Helix.hpp b/src/providers/twitch/api/Helix.hpp index 581843f96..14a971ba7 100644 --- a/src/providers/twitch/api/Helix.hpp +++ b/src/providers/twitch/api/Helix.hpp @@ -146,6 +146,25 @@ struct HelixClip { } }; +struct HelixChannel { + QString userId; + QString name; + QString language; + QString gameId; + QString gameName; + QString title; + + explicit HelixChannel(QJsonObject jsonObject) + : userId(jsonObject.value("broadcaster_id").toString()) + , name(jsonObject.value("broadcaster_name").toString()) + , language(jsonObject.value("broadcaster_language").toString()) + , gameId(jsonObject.value("game_id").toString()) + , gameName(jsonObject.value("game_name").toString()) + , title(jsonObject.value("title").toString()) + { + } +}; + enum class HelixClipError { Unknown, ClipsDisabled, @@ -218,6 +237,11 @@ public: std::function failureCallback, std::function finallyCallback); + // https://dev.twitch.tv/docs/api/reference#get-channel-information + void getChannel(QString broadcasterId, + ResultCallback successCallback, + HelixFailureCallback failureCallback); + void update(QString clientId, QString oauthToken); static void initialize(); diff --git a/src/providers/twitch/api/Kraken.cpp b/src/providers/twitch/api/Kraken.cpp index edd8682dc..2f64dd0d4 100644 --- a/src/providers/twitch/api/Kraken.cpp +++ b/src/providers/twitch/api/Kraken.cpp @@ -8,27 +8,6 @@ namespace chatterino { static Kraken *instance = nullptr; -void Kraken::getChannel(QString userId, - ResultCallback successCallback, - KrakenFailureCallback failureCallback) -{ - assert(!userId.isEmpty()); - - this->makeRequest("channels/" + userId, {}) - .onSuccess([successCallback, failureCallback](auto result) -> Outcome { - auto root = result.parseJson(); - - successCallback(root); - - return Success; - }) - .onError([failureCallback](auto result) { - // TODO: make better xd - failureCallback(); - }) - .execute(); -} - NetworkRequest Kraken::makeRequest(QString url, QUrlQuery urlQuery) { assert(!url.startsWith("/")); diff --git a/src/providers/twitch/api/Kraken.hpp b/src/providers/twitch/api/Kraken.hpp index 0f6c7e2f9..42e6a7a4d 100644 --- a/src/providers/twitch/api/Kraken.hpp +++ b/src/providers/twitch/api/Kraken.hpp @@ -26,11 +26,6 @@ struct KrakenChannel { class Kraken final : boost::noncopyable { public: - // https://dev.twitch.tv/docs/v5/reference/users#follow-channel - void getChannel(QString userId, - ResultCallback resultCallback, - KrakenFailureCallback failureCallback); - void update(QString clientId, QString oauthToken); static void initialize(); diff --git a/src/providers/twitch/api/README.md b/src/providers/twitch/api/README.md index 377032245..4bec12259 100644 --- a/src/providers/twitch/api/README.md +++ b/src/providers/twitch/api/README.md @@ -4,15 +4,6 @@ this folder describes what sort of API requests we do, what permissions are requ ## Kraken (V5) We use a bunch of Kraken (V5) in Chatterino2. -### Get Channel -URL: https://dev.twitch.tv/docs/v5/reference/channels#get-channel - -Migration path: **Unknown** - - * We implement this in `providers/twitch/api/Kraken.cpp getChannel` - Used in: - * `TwitchChannel::refreshTitle` to check the current stream title/game of offline channels - ### Get Cheermotes URL: https://dev.twitch.tv/docs/v5/reference/bits#get-cheermotes @@ -115,6 +106,13 @@ Requires `clips:edit` scope Used in: * `TwitchChannel` to create a clip of a live broadcast +### Get Channel +URL: https://dev.twitch.tv/docs/api/reference#get-channel-information + + * We implement this in `providers/twitch/api/Helix.cpp getChannel` + Used in: + * `TwitchChannel` to refresh stream title + ## TMI The TMI api is undocumented.