mirror-chatterino2/src/providers/twitch/TwitchAccount.cpp

445 lines
13 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchAccount.hpp"
2018-08-02 14:23:27 +02:00
#include <QThread>
#include "Application.hpp"
#include "common/Env.hpp"
2018-06-26 15:33:51 +02:00
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "common/QLogging.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/IvrApi.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "providers/twitch/TwitchUser.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "providers/twitch/api/Kraken.hpp"
2018-08-02 14:23:27 +02:00
#include "singletons/Emotes.hpp"
2018-06-26 17:20:03 +02:00
#include "util/RapidjsonHelpers.hpp"
2018-02-05 15:11:50 +01:00
namespace chatterino {
namespace {
constexpr int USERSTATE_EMOTES_REFRESH_PERIOD = 10 * 60 * 1000;
} // namespace
2018-07-06 19:23:47 +02:00
TwitchAccount::TwitchAccount(const QString &username, const QString &oauthToken,
const QString &oauthClient, const QString &userID)
2018-06-26 17:06:17 +02:00
: Account(ProviderId::Twitch)
2018-07-06 19:23:47 +02:00
, oauthClient_(oauthClient)
, oauthToken_(oauthToken)
, userName_(username)
, userId_(userID)
, isAnon_(username == ANONYMOUS_USERNAME)
2018-02-05 15:11:50 +01:00
{
}
2018-05-06 12:52:47 +02:00
QString TwitchAccount::toString() const
{
return this->getUserName();
}
2018-02-05 15:11:50 +01:00
const QString &TwitchAccount::getUserName() const
{
2018-07-06 19:23:47 +02:00
return this->userName_;
2018-02-05 15:11:50 +01:00
}
const QString &TwitchAccount::getOAuthClient() const
{
2018-07-06 19:23:47 +02:00
return this->oauthClient_;
2018-02-05 15:11:50 +01:00
}
const QString &TwitchAccount::getOAuthToken() const
{
2018-07-06 19:23:47 +02:00
return this->oauthToken_;
2018-02-05 15:11:50 +01:00
}
const QString &TwitchAccount::getUserId() const
{
2018-07-06 19:23:47 +02:00
return this->userId_;
2018-02-05 15:11:50 +01:00
}
QColor TwitchAccount::color()
{
return this->color_.get();
}
void TwitchAccount::setColor(QColor color)
{
this->color_.set(std::move(color));
}
2018-02-05 15:11:50 +01:00
bool TwitchAccount::setOAuthClient(const QString &newClientID)
{
2018-10-21 13:43:02 +02:00
if (this->oauthClient_.compare(newClientID) == 0)
{
2018-02-05 15:11:50 +01:00
return false;
}
2018-07-06 19:23:47 +02:00
this->oauthClient_ = newClientID;
2018-02-05 15:11:50 +01:00
return true;
}
bool TwitchAccount::setOAuthToken(const QString &newOAuthToken)
{
2018-10-21 13:43:02 +02:00
if (this->oauthToken_.compare(newOAuthToken) == 0)
{
2018-02-05 15:11:50 +01:00
return false;
}
2018-07-06 19:23:47 +02:00
this->oauthToken_ = newOAuthToken;
2018-02-05 15:11:50 +01:00
return true;
}
bool TwitchAccount::isAnon() const
{
2018-07-06 19:23:47 +02:00
return this->isAnon_;
2018-02-05 15:11:50 +01:00
}
void TwitchAccount::loadBlocks()
{
getHelix()->loadBlocks(
getApp()->accounts->twitch.getCurrent()->userId_,
[this](std::vector<HelixBlock> blocks) {
2021-05-01 13:38:58 +02:00
auto ignores = this->ignores_.access();
auto userIds = this->ignoresUserIds_.access();
ignores->clear();
userIds->clear();
for (const HelixBlock &block : blocks)
2018-10-21 13:43:02 +02:00
{
TwitchUser blockedUser;
blockedUser.fromHelixBlock(block);
2021-05-01 13:38:58 +02:00
ignores->insert(blockedUser);
userIds->insert(blockedUser.id);
}
},
[] {
qDebug() << "Fetching blocks failed!";
});
}
void TwitchAccount::blockUser(QString userId, std::function<void()> onSuccess,
std::function<void()> onFailure)
{
getHelix()->blockUser(
userId,
[this, userId, onSuccess] {
TwitchUser blockedUser;
blockedUser.id = userId;
2019-08-20 21:50:36 +02:00
{
2021-05-01 13:38:58 +02:00
auto ignores = this->ignores_.access();
auto userIds = this->ignoresUserIds_.access();
2021-05-01 13:38:58 +02:00
ignores->insert(blockedUser);
userIds->insert(blockedUser.id);
2019-08-20 21:50:36 +02:00
}
onSuccess();
},
std::move(onFailure));
}
void TwitchAccount::unblockUser(QString userId, std::function<void()> onSuccess,
std::function<void()> onFailure)
{
getHelix()->unblockUser(
userId,
[this, userId, onSuccess] {
2019-08-20 21:50:36 +02:00
TwitchUser ignoredUser;
ignoredUser.id = userId;
2019-08-20 21:50:36 +02:00
{
2021-05-01 13:38:58 +02:00
auto ignores = this->ignores_.access();
auto userIds = this->ignoresUserIds_.access();
2021-05-01 13:38:58 +02:00
ignores->erase(ignoredUser);
userIds->erase(ignoredUser.id);
2019-08-20 21:50:36 +02:00
}
onSuccess();
},
std::move(onFailure));
}
void TwitchAccount::checkFollow(const QString targetUserID,
std::function<void(FollowResult)> onFinished)
{
const auto onResponse = [onFinished](bool following, const auto &record) {
if (!following)
{
onFinished(FollowResult_NotFollowing);
return;
}
onFinished(FollowResult_Following);
};
Improvements to Message Search (#1237) * Ran clang-format * Implement user-specific search in message history This functionality was originally requested in #1236. This commit changes the SearchPopup::performSearch method so that only messages from specific users can be shown. In order to filter for a specific user, enter their username with a leading '@' in the search popup. You can also add an additional search phrase which will also be considered in the search. * Naive implementation for "from:" tags Rebase later? * Cleverer (?) version using Predicates Commit adds two POC predicates: one for the author of messages, and one for substring search in messages. Problems/TODOs: * Best way to register new predicates? * Clean up tags (e.g. "from:") or not? * Test combinations of different predicates * Add a predicate to check for links in messages * Remove a dumb TODO * Rewrite SearchPopup::performSearch to be cleaner * Ran clang-format on all files * Remove TODO I missed earlier * Forgot to run clang-format peepoSadDank * Re-use {}-initialization Was accidentally removed when fixing earlier merge conflict. * Does this fix line endings? No diffs are shown locally, hopefully Git doesn't lie to me. * Rename "predicates" directory to "search" Resolving one conversation in the review of #1237. * Use LinkParser in LinkPredicate Resolving a conversation in the review of #1237. * Predicates: Use unique_ptr instead of shared_ptr Resolves a conversation in the review of #1237. * Refactor of SearchPopup and AuthorPredicate Resolving some points from the review in #1237. * Moved parsing of comma-seperated values into AuthorPredicate constructor. * Rewrite SearchPopup::parsePredicates as suggested. * Deleted now redundant methods in SearchPopup. * MessagePredicate::appliesTo now takes a Message& ... instead of a MessagePtr. This resolves a conversation in the review of #1237. * Run clang-format on two files I missed * AuthorPredicate: Check for displayName & loginName Resolving conversation on #1237.
2019-09-09 15:21:49 +02:00
getHelix()->getUserFollow(this->getUserId(), targetUserID, onResponse,
[] {});
}
2021-05-01 13:38:58 +02:00
AccessGuard<const std::set<TwitchUser>> TwitchAccount::accessBlocks() const
{
2021-05-01 13:38:58 +02:00
return this->ignores_.accessConst();
}
2021-05-01 13:38:58 +02:00
AccessGuard<const std::set<QString>> TwitchAccount::accessBlockedUserIds() const
{
return this->ignoresUserIds_.accessConst();
}
2018-08-02 14:23:27 +02:00
void TwitchAccount::loadEmotes()
{
qCDebug(chatterinoTwitch)
<< "Loading Twitch emotes for user" << this->getUserName();
if (this->getOAuthClient().isEmpty() || this->getOAuthToken().isEmpty())
2018-10-21 13:43:02 +02:00
{
qCDebug(chatterinoTwitch) << "Missing Client ID or OAuth token";
return;
}
getKraken()->getUserEmotes(
this,
[this](KrakenEmoteSets data) {
// clear emote data
auto emoteData = this->emotes_.access();
emoteData->emoteSets.clear();
emoteData->allEmoteNames.clear();
Improvements to Message Search (#1237) * Ran clang-format * Implement user-specific search in message history This functionality was originally requested in #1236. This commit changes the SearchPopup::performSearch method so that only messages from specific users can be shown. In order to filter for a specific user, enter their username with a leading '@' in the search popup. You can also add an additional search phrase which will also be considered in the search. * Naive implementation for "from:" tags Rebase later? * Cleverer (?) version using Predicates Commit adds two POC predicates: one for the author of messages, and one for substring search in messages. Problems/TODOs: * Best way to register new predicates? * Clean up tags (e.g. "from:") or not? * Test combinations of different predicates * Add a predicate to check for links in messages * Remove a dumb TODO * Rewrite SearchPopup::performSearch to be cleaner * Ran clang-format on all files * Remove TODO I missed earlier * Forgot to run clang-format peepoSadDank * Re-use {}-initialization Was accidentally removed when fixing earlier merge conflict. * Does this fix line endings? No diffs are shown locally, hopefully Git doesn't lie to me. * Rename "predicates" directory to "search" Resolving one conversation in the review of #1237. * Use LinkParser in LinkPredicate Resolving a conversation in the review of #1237. * Predicates: Use unique_ptr instead of shared_ptr Resolves a conversation in the review of #1237. * Refactor of SearchPopup and AuthorPredicate Resolving some points from the review in #1237. * Moved parsing of comma-seperated values into AuthorPredicate constructor. * Rewrite SearchPopup::parsePredicates as suggested. * Deleted now redundant methods in SearchPopup. * MessagePredicate::appliesTo now takes a Message& ... instead of a MessagePtr. This resolves a conversation in the review of #1237. * Run clang-format on two files I missed * AuthorPredicate: Check for displayName & loginName Resolving conversation on #1237.
2019-09-09 15:21:49 +02:00
// no emotes available
if (data.emoteSets.isEmpty())
2019-08-20 21:50:36 +02:00
{
qCWarning(chatterinoTwitch)
<< "\"emoticon_sets\" either empty or not present in "
"Kraken::getUserEmotes response";
return;
2019-08-20 21:50:36 +02:00
}
for (auto emoteSetIt = data.emoteSets.begin();
emoteSetIt != data.emoteSets.end(); ++emoteSetIt)
2019-08-20 21:50:36 +02:00
{
auto emoteSet = std::make_shared<EmoteSet>();
emoteSet->key = emoteSetIt.key();
this->loadEmoteSetData(emoteSet);
for (const auto emoteArrObj : emoteSetIt.value().toArray())
{
if (!emoteArrObj.isObject())
{
qCWarning(chatterinoTwitch)
<< QString("Emote value from set %1 was invalid")
.arg(emoteSet->key);
}
KrakenEmote krakenEmote(emoteArrObj.toObject());
auto id = EmoteId{krakenEmote.id};
auto code = EmoteName{krakenEmote.code};
auto cleanCode =
EmoteName{TwitchEmotes::cleanUpEmoteCode(code)};
emoteSet->emotes.emplace_back(TwitchEmote{id, cleanCode});
emoteData->allEmoteNames.push_back(cleanCode);
auto emote =
getApp()->emotes->twitch.getOrCreateEmote(id, code);
emoteData->emotes.emplace(code, emote);
}
std::sort(emoteSet->emotes.begin(), emoteSet->emotes.end(),
[](const TwitchEmote &l, const TwitchEmote &r) {
return l.name.string < r.name.string;
});
emoteData->emoteSets.emplace_back(emoteSet);
2019-08-20 21:50:36 +02:00
}
},
[] {
// request failed
});
2018-08-02 14:23:27 +02:00
}
void TwitchAccount::loadUserstateEmotes(QStringList emoteSetKeys)
{
// do not attempt to load emotes too often
if (!this->userstateEmotesTimer_.isValid())
{
this->userstateEmotesTimer_.start();
}
else if (this->userstateEmotesTimer_.elapsed() <
USERSTATE_EMOTES_REFRESH_PERIOD)
{
return;
}
this->userstateEmotesTimer_.restart();
auto emoteData = this->emotes_.access();
auto userEmoteSets = emoteData->emoteSets;
QStringList newEmoteSetKeys, currentEmoteSetKeys;
// get list of already fetched emote sets
for (const auto &userEmoteSet : userEmoteSets)
{
currentEmoteSetKeys.push_back(userEmoteSet->key);
}
// filter out emote sets from userstate message, which are not in fetched emote set list
for (const auto &emoteSetKey : emoteSetKeys)
{
if (!currentEmoteSetKeys.contains(emoteSetKey))
{
newEmoteSetKeys.push_back(emoteSetKey);
}
}
// return if there are no new emote sets
if (newEmoteSetKeys.isEmpty())
{
return;
}
getIvr()->getBulkEmoteSets(
newEmoteSetKeys.join(","),
[this](QJsonArray emoteSetArray) {
auto emoteData = this->emotes_.access();
for (auto emoteSet : emoteSetArray)
{
auto newUserEmoteSet = std::make_shared<EmoteSet>();
IvrEmoteSet ivrEmoteSet(emoteSet.toObject());
newUserEmoteSet->key = ivrEmoteSet.setId;
auto name = ivrEmoteSet.login;
name.detach();
name[0] = name[0].toUpper();
newUserEmoteSet->text = name;
newUserEmoteSet->type = QString();
newUserEmoteSet->channelName = ivrEmoteSet.login;
for (const auto &emote : ivrEmoteSet.emotes)
{
IvrEmote ivrEmote(emote.toObject());
auto id = EmoteId{ivrEmote.id};
auto code = EmoteName{ivrEmote.code};
auto cleanCode =
EmoteName{TwitchEmotes::cleanUpEmoteCode(code)};
newUserEmoteSet->emotes.emplace_back(
TwitchEmote{id, cleanCode});
emoteData->allEmoteNames.push_back(cleanCode);
auto twitchEmote =
getApp()->emotes->twitch.getOrCreateEmote(id, code);
emoteData->emotes.emplace(code, twitchEmote);
}
std::sort(newUserEmoteSet->emotes.begin(),
newUserEmoteSet->emotes.end(),
[](const TwitchEmote &l, const TwitchEmote &r) {
return l.name.string < r.name.string;
});
emoteData->emoteSets.emplace_back(newUserEmoteSet);
}
},
[] {
// fetching emotes failed, ivr API might be down
});
}
2018-08-02 14:23:27 +02:00
2018-08-06 21:17:03 +02:00
AccessGuard<const TwitchAccount::TwitchAccountEmoteData>
2018-08-15 22:46:20 +02:00
TwitchAccount::accessEmotes() const
2018-08-02 14:23:27 +02:00
{
return this->emotes_.accessConst();
}
// AutoModActions
2019-01-20 16:07:31 +01:00
void TwitchAccount::autoModAllow(const QString msgID)
2019-01-20 14:47:04 +01:00
{
2019-01-20 16:07:31 +01:00
QString url("https://api.twitch.tv/kraken/chat/twitchbot/approve");
auto qba = (QString("{\"msg_id\":\"") + msgID + "\"}").toUtf8();
2019-08-20 21:50:36 +02:00
NetworkRequest(url, NetworkRequestType::Post)
.header("Content-Type", "application/json")
.header("Content-Length", QByteArray::number(qba.size()))
.payload(qba)
Improvements to Message Search (#1237) * Ran clang-format * Implement user-specific search in message history This functionality was originally requested in #1236. This commit changes the SearchPopup::performSearch method so that only messages from specific users can be shown. In order to filter for a specific user, enter their username with a leading '@' in the search popup. You can also add an additional search phrase which will also be considered in the search. * Naive implementation for "from:" tags Rebase later? * Cleverer (?) version using Predicates Commit adds two POC predicates: one for the author of messages, and one for substring search in messages. Problems/TODOs: * Best way to register new predicates? * Clean up tags (e.g. "from:") or not? * Test combinations of different predicates * Add a predicate to check for links in messages * Remove a dumb TODO * Rewrite SearchPopup::performSearch to be cleaner * Ran clang-format on all files * Remove TODO I missed earlier * Forgot to run clang-format peepoSadDank * Re-use {}-initialization Was accidentally removed when fixing earlier merge conflict. * Does this fix line endings? No diffs are shown locally, hopefully Git doesn't lie to me. * Rename "predicates" directory to "search" Resolving one conversation in the review of #1237. * Use LinkParser in LinkPredicate Resolving a conversation in the review of #1237. * Predicates: Use unique_ptr instead of shared_ptr Resolves a conversation in the review of #1237. * Refactor of SearchPopup and AuthorPredicate Resolving some points from the review in #1237. * Moved parsing of comma-seperated values into AuthorPredicate constructor. * Rewrite SearchPopup::parsePredicates as suggested. * Deleted now redundant methods in SearchPopup. * MessagePredicate::appliesTo now takes a Message& ... instead of a MessagePtr. This resolves a conversation in the review of #1237. * Run clang-format on two files I missed * AuthorPredicate: Check for displayName & loginName Resolving conversation on #1237.
2019-09-09 15:21:49 +02:00
2019-08-20 21:50:36 +02:00
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([=](NetworkResult result) {
qCWarning(chatterinoTwitch)
<< "[TwitchAccounts::autoModAllow] Error" << result.status();
2019-08-20 21:50:36 +02:00
})
.execute();
2019-01-20 14:47:04 +01:00
}
2019-01-20 16:07:31 +01:00
void TwitchAccount::autoModDeny(const QString msgID)
2019-01-20 14:47:04 +01:00
{
2019-01-20 16:07:31 +01:00
QString url("https://api.twitch.tv/kraken/chat/twitchbot/deny");
auto qba = (QString("{\"msg_id\":\"") + msgID + "\"}").toUtf8();
2019-08-20 21:50:36 +02:00
NetworkRequest(url, NetworkRequestType::Post)
.header("Content-Type", "application/json")
.header("Content-Length", QByteArray::number(qba.size()))
.payload(qba)
Improvements to Message Search (#1237) * Ran clang-format * Implement user-specific search in message history This functionality was originally requested in #1236. This commit changes the SearchPopup::performSearch method so that only messages from specific users can be shown. In order to filter for a specific user, enter their username with a leading '@' in the search popup. You can also add an additional search phrase which will also be considered in the search. * Naive implementation for "from:" tags Rebase later? * Cleverer (?) version using Predicates Commit adds two POC predicates: one for the author of messages, and one for substring search in messages. Problems/TODOs: * Best way to register new predicates? * Clean up tags (e.g. "from:") or not? * Test combinations of different predicates * Add a predicate to check for links in messages * Remove a dumb TODO * Rewrite SearchPopup::performSearch to be cleaner * Ran clang-format on all files * Remove TODO I missed earlier * Forgot to run clang-format peepoSadDank * Re-use {}-initialization Was accidentally removed when fixing earlier merge conflict. * Does this fix line endings? No diffs are shown locally, hopefully Git doesn't lie to me. * Rename "predicates" directory to "search" Resolving one conversation in the review of #1237. * Use LinkParser in LinkPredicate Resolving a conversation in the review of #1237. * Predicates: Use unique_ptr instead of shared_ptr Resolves a conversation in the review of #1237. * Refactor of SearchPopup and AuthorPredicate Resolving some points from the review in #1237. * Moved parsing of comma-seperated values into AuthorPredicate constructor. * Rewrite SearchPopup::parsePredicates as suggested. * Deleted now redundant methods in SearchPopup. * MessagePredicate::appliesTo now takes a Message& ... instead of a MessagePtr. This resolves a conversation in the review of #1237. * Run clang-format on two files I missed * AuthorPredicate: Check for displayName & loginName Resolving conversation on #1237.
2019-09-09 15:21:49 +02:00
2019-08-20 21:50:36 +02:00
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
.onError([=](NetworkResult result) {
qCWarning(chatterinoTwitch)
<< "[TwitchAccounts::autoModDeny] Error" << result.status();
2019-08-20 21:50:36 +02:00
})
.execute();
2019-01-20 14:47:04 +01:00
}
2018-08-02 14:23:27 +02:00
void TwitchAccount::loadEmoteSetData(std::shared_ptr<EmoteSet> emoteSet)
{
2018-10-21 13:43:02 +02:00
if (!emoteSet)
{
qCWarning(chatterinoTwitch) << "null emote set sent";
2018-08-02 14:23:27 +02:00
return;
}
auto staticSetIt = this->staticEmoteSets.find(emoteSet->key);
2018-10-21 13:43:02 +02:00
if (staticSetIt != this->staticEmoteSets.end())
{
2018-08-02 14:23:27 +02:00
const auto &staticSet = staticSetIt->second;
emoteSet->channelName = staticSet.channelName;
emoteSet->text = staticSet.text;
return;
}
2019-08-20 21:50:36 +02:00
NetworkRequest(Env::get().twitchEmoteSetResolverUrl.arg(emoteSet->key))
.cache()
.onSuccess([emoteSet](NetworkResult result) -> Outcome {
auto rootOld = result.parseRapidJson();
auto root = result.parseJson();
if (root.isEmpty())
2019-08-20 21:50:36 +02:00
{
return Failure;
}
2018-08-02 14:23:27 +02:00
TwitchEmoteSetResolverResponse response(root);
2018-08-02 14:23:27 +02:00
auto name = response.channelName;
2019-08-20 21:50:36 +02:00
name.detach();
name[0] = name[0].toUpper();
2018-08-02 14:23:27 +02:00
2019-08-20 21:50:36 +02:00
emoteSet->text = name;
emoteSet->type = response.type;
emoteSet->channelName = response.channelName;
2018-08-02 14:23:27 +02:00
qCDebug(chatterinoTwitch)
<< QString("Loaded twitch emote set data for %1")
.arg(emoteSet->key);
2018-08-02 14:23:27 +02:00
2019-08-20 21:50:36 +02:00
return Success;
})
.onError([](NetworkResult result) {
qCWarning(chatterinoTwitch)
<< QString("Error code %1 while loading emote set data")
.arg(result.status());
})
2019-08-20 21:50:36 +02:00
.execute();
}
2018-02-05 15:11:50 +01:00
} // namespace chatterino