From 6bb96f128ba116a7a052be4beee576fb80bf9e09 Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 22 May 2022 12:11:33 +0200 Subject: [PATCH] Make Helix an interface (#3756) --- src/providers/twitch/api/Helix.cpp | 13 ++- src/providers/twitch/api/Helix.hpp | 175 ++++++++++++++++++++++++----- 2 files changed, 159 insertions(+), 29 deletions(-) diff --git a/src/providers/twitch/api/Helix.cpp b/src/providers/twitch/api/Helix.cpp index 7895995d2..ee47489a0 100644 --- a/src/providers/twitch/api/Helix.cpp +++ b/src/providers/twitch/api/Helix.cpp @@ -7,7 +7,7 @@ namespace chatterino { -static Helix *instance = nullptr; +static IHelix *instance = nullptr; void Helix::fetchUsers(QStringList userIds, QStringList userLogins, ResultCallback> successCallback, @@ -808,10 +808,17 @@ void Helix::initialize() { assert(instance == nullptr); - instance = new Helix(); + initializeHelix(new Helix()); } -Helix *getHelix() +void initializeHelix(IHelix *_instance) +{ + assert(_instance != nullptr); + + instance = _instance; +} + +IHelix *getHelix() { assert(instance != nullptr); diff --git a/src/providers/twitch/api/Helix.hpp b/src/providers/twitch/api/Helix.hpp index 57809c683..3e0c3e54d 100644 --- a/src/providers/twitch/api/Helix.hpp +++ b/src/providers/twitch/api/Helix.hpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -323,117 +322,237 @@ enum class HelixAutoModMessageError { MessageNotFound, }; -class Helix final : boost::noncopyable +class IHelix +{ +public: + // https://dev.twitch.tv/docs/api/reference#get-users + virtual void fetchUsers( + QStringList userIds, QStringList userLogins, + ResultCallback> successCallback, + HelixFailureCallback failureCallback) = 0; + virtual void getUserByName(QString userName, + ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + virtual void getUserById(QString userId, + ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#get-users-follows + virtual void fetchUsersFollows( + QString fromId, QString toId, + ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + + virtual void getUserFollowers( + QString userId, + ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#get-streams + virtual void fetchStreams( + QStringList userIds, QStringList userLogins, + ResultCallback> successCallback, + HelixFailureCallback failureCallback) = 0; + + virtual void getStreamById( + QString userId, ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + + virtual void getStreamByName( + QString userName, ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#get-games + virtual void fetchGames( + QStringList gameIds, QStringList gameNames, + ResultCallback> successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#search-categories + virtual void searchGames( + QString gameName, + ResultCallback> successCallback, + HelixFailureCallback failureCallback) = 0; + + virtual void getGameById(QString gameId, + ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#create-clip + virtual void createClip(QString channelId, + ResultCallback successCallback, + std::function failureCallback, + std::function finallyCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#get-channel-information + virtual void getChannel(QString broadcasterId, + ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference/#create-stream-marker + virtual void createStreamMarker( + QString broadcasterId, QString description, + ResultCallback successCallback, + std::function failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#get-user-block-list + virtual void loadBlocks( + QString userId, ResultCallback> successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#block-user + virtual void blockUser(QString targetUserId, + std::function successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#unblock-user + virtual void unblockUser(QString targetUserId, + std::function successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#modify-channel-information + virtual void updateChannel( + QString broadcasterId, QString gameId, QString language, QString title, + std::function successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#manage-held-automod-messages + virtual void manageAutoModMessages( + QString userID, QString msgID, QString action, + std::function successCallback, + std::function failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference/#get-cheermotes + virtual void getCheermotes( + QString broadcasterId, + ResultCallback> successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#get-emote-sets + virtual void getEmoteSetData( + QString emoteSetId, ResultCallback successCallback, + HelixFailureCallback failureCallback) = 0; + + // https://dev.twitch.tv/docs/api/reference#get-channel-emotes + virtual void getChannelEmotes( + QString broadcasterId, + ResultCallback> successCallback, + HelixFailureCallback failureCallback) = 0; + + virtual void update(QString clientId, QString oauthToken) = 0; +}; + +class Helix final : public IHelix { public: // https://dev.twitch.tv/docs/api/reference#get-users void fetchUsers(QStringList userIds, QStringList userLogins, ResultCallback> successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; void getUserByName(QString userName, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; void getUserById(QString userId, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#get-users-follows void fetchUsersFollows( QString fromId, QString toId, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; void getUserFollowers( QString userId, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#get-streams void fetchStreams(QStringList userIds, QStringList userLogins, ResultCallback> successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; void getStreamById(QString userId, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; void getStreamByName(QString userName, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#get-games void fetchGames(QStringList gameIds, QStringList gameNames, ResultCallback> successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#search-categories void searchGames(QString gameName, ResultCallback> successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; void getGameById(QString gameId, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#create-clip void createClip(QString channelId, ResultCallback successCallback, std::function failureCallback, - std::function finallyCallback); + std::function finallyCallback) final; // https://dev.twitch.tv/docs/api/reference#get-channel-information void getChannel(QString broadcasterId, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference/#create-stream-marker void createStreamMarker( QString broadcasterId, QString description, ResultCallback successCallback, - std::function failureCallback); + std::function failureCallback) final; // https://dev.twitch.tv/docs/api/reference#get-user-block-list void loadBlocks(QString userId, ResultCallback> successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#block-user void blockUser(QString targetUserId, std::function successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#unblock-user void unblockUser(QString targetUserId, std::function successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#modify-channel-information void updateChannel(QString broadcasterId, QString gameId, QString language, QString title, std::function successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#manage-held-automod-messages void manageAutoModMessages( QString userID, QString msgID, QString action, std::function successCallback, - std::function failureCallback); + std::function failureCallback) final; // https://dev.twitch.tv/docs/api/reference/#get-cheermotes void getCheermotes( QString broadcasterId, ResultCallback> successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#get-emote-sets void getEmoteSetData(QString emoteSetId, ResultCallback successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; // https://dev.twitch.tv/docs/api/reference#get-channel-emotes void getChannelEmotes( QString broadcasterId, ResultCallback> successCallback, - HelixFailureCallback failureCallback); + HelixFailureCallback failureCallback) final; - void update(QString clientId, QString oauthToken); + void update(QString clientId, QString oauthToken) final; static void initialize(); @@ -444,6 +563,10 @@ private: QString oauthToken; }; -Helix *getHelix(); +// initializeHelix sets the helix instance to _instance +// from a normal application, this should never be called, and will instead be handled by calling Helix::initialize() +void initializeHelix(IHelix *_instance); + +IHelix *getHelix(); } // namespace chatterino