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

588 lines
18 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/Channel.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 "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/IvrApi.hpp"
#include "providers/irc/IrcMessageBuilder.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"
#include "util/QStringHash.hpp"
2018-06-26 17:20:03 +02:00
#include "util/RapidjsonHelpers.hpp"
2018-02-05 15:11:50 +01:00
namespace chatterino {
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,
[] {});
}
SharedAccessGuard<const std::set<TwitchUser>> TwitchAccount::accessBlocks()
const
{
2021-05-01 13:38:58 +02:00
return this->ignores_.accessConst();
}
SharedAccessGuard<const std::set<QString>> TwitchAccount::accessBlockedUserIds()
const
2021-05-01 13:38:58 +02:00
{
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 and/or OAuth token";
return;
}
// Getting subscription emotes from kraken
getKraken()->getUserEmotes(
this,
[this](KrakenEmoteSets data) {
// 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
}
2019-08-20 21:50:36 +02:00
{
// Clearing emote data
auto emoteData = this->emotes_.access();
emoteData->emoteSets.clear();
emoteData->emotes.clear();
for (auto emoteSetIt = data.emoteSets.begin();
emoteSetIt != data.emoteSets.end(); ++emoteSetIt)
{
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);
continue;
}
KrakenEmote krakenEmote(emoteArrObj.toObject());
auto id = EmoteId{krakenEmote.id};
auto code = EmoteName{
TwitchEmotes::cleanUpEmoteCode(krakenEmote.code)};
emoteSet->emotes.emplace_back(TwitchEmote{id, code});
if (!emoteSet->local)
{
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
}
// Getting userstate emotes from Ivr
this->loadUserstateEmotes();
},
[] {
// kraken request failed
});
2018-08-02 14:23:27 +02:00
}
bool TwitchAccount::setUserstateEmoteSets(QStringList newEmoteSets)
{
newEmoteSets.sort();
if (this->userstateEmoteSets_ == newEmoteSets)
{
// Nothing has changed
return false;
}
this->userstateEmoteSets_ = newEmoteSets;
return true;
}
void TwitchAccount::loadUserstateEmotes()
{
if (this->userstateEmoteSets_.isEmpty())
{
return;
}
QStringList newEmoteSetKeys, krakenEmoteSetKeys;
auto emoteData = this->emotes_.access();
auto userEmoteSets = emoteData->emoteSets;
// get list of already fetched emote sets
for (const auto &userEmoteSet : userEmoteSets)
{
krakenEmoteSetKeys.push_back(userEmoteSet->key);
}
// filter out emote sets from userstate message, which are not in fetched emote set list
for (const auto &emoteSetKey : this->userstateEmoteSets_)
{
if (!krakenEmoteSetKeys.contains(emoteSetKey))
{
newEmoteSetKeys.push_back(emoteSetKey);
}
}
// return if there are no new emote sets
if (newEmoteSetKeys.isEmpty())
{
return;
}
qCDebug(chatterinoTwitch) << QString("Loading %1 emotesets from IVR: %2")
.arg(newEmoteSetKeys.size())
.arg(newEmoteSetKeys.join(", "));
// splitting newEmoteSetKeys to batches of 100, because Ivr API endpoint accepts a maximum of 100 emotesets at once
constexpr int batchSize = 100;
std::vector<QStringList> batches;
int batchCount = (newEmoteSetKeys.size() / batchSize) + 1;
batches.reserve(batchCount);
for (int i = 0; i < batchCount; i++)
{
QStringList batch;
int last = std::min(batchSize, newEmoteSetKeys.size() - batchSize * i);
for (int j = batchSize * i; j < last; j++)
{
batch.push_back(newEmoteSetKeys.at(j));
}
batches.emplace_back(batch);
}
// requesting emotes
for (const auto &batch : batches)
{
getIvr()->getBulkEmoteSets(
batch.join(","),
[this](QJsonArray emoteSetArray) {
auto emoteData = this->emotes_.access();
auto localEmoteData = this->localEmotes_.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->channelName = ivrEmoteSet.login;
for (const auto &emoteObj : ivrEmoteSet.emotes)
{
IvrEmote ivrEmote(emoteObj.toObject());
auto id = EmoteId{ivrEmote.id};
auto code = EmoteName{
TwitchEmotes::cleanUpEmoteCode(ivrEmote.code)};
newUserEmoteSet->emotes.push_back(
TwitchEmote{id, code});
auto emote =
getApp()->emotes->twitch.getOrCreateEmote(id, code);
// Follower emotes can be only used in their origin channel
if (ivrEmote.emoteType == "FOLLOWER")
{
newUserEmoteSet->local = true;
// EmoteMap for target channel wasn't initialized yet, doing it now
if (localEmoteData->find(ivrEmoteSet.channelId) ==
localEmoteData->end())
{
localEmoteData->emplace(ivrEmoteSet.channelId,
EmoteMap());
}
localEmoteData->at(ivrEmoteSet.channelId)
.emplace(code, emote);
}
else
{
emoteData->emotes.emplace(code, emote);
}
}
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
SharedAccessGuard<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();
}
SharedAccessGuard<const std::unordered_map<QString, EmoteMap>>
TwitchAccount::accessLocalEmotes() const
{
return this->localEmotes_.accessConst();
}
// AutoModActions
void TwitchAccount::autoModAllow(const QString msgID, ChannelPtr channel)
2019-01-20 14:47:04 +01:00
{
getHelix()->manageAutoModMessages(
this->getUserId(), msgID, "ALLOW",
[] {
// success
},
[channel](auto error) {
// failure
QString errorMessage("Failed to allow AutoMod message - ");
2019-01-20 16:07:31 +01:00
switch (error)
{
case HelixAutoModMessageError::MessageAlreadyProcessed: {
errorMessage += "message has already been processed.";
}
break;
case HelixAutoModMessageError::UserNotAuthenticated: {
errorMessage += "you need to re-authenticate.";
}
break;
case HelixAutoModMessageError::UserNotAuthorized: {
errorMessage +=
"you don't have permission to perform that action";
}
break;
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
case HelixAutoModMessageError::MessageNotFound: {
errorMessage += "target message not found.";
}
break;
// This would most likely happen if the service is down, or if the JSON payload returned has changed format
case HelixAutoModMessageError::Unknown:
default: {
errorMessage += "an unknown error occured.";
}
break;
}
channel->addMessage(makeSystemMessage(errorMessage));
});
2019-01-20 14:47:04 +01:00
}
void TwitchAccount::autoModDeny(const QString msgID, ChannelPtr channel)
2019-01-20 14:47:04 +01:00
{
getHelix()->manageAutoModMessages(
this->getUserId(), msgID, "DENY",
[] {
// success
},
[channel](auto error) {
// failure
QString errorMessage("Failed to deny AutoMod message - ");
2019-01-20 16:07:31 +01:00
switch (error)
{
case HelixAutoModMessageError::MessageAlreadyProcessed: {
errorMessage += "message has already been processed.";
}
break;
case HelixAutoModMessageError::UserNotAuthenticated: {
errorMessage += "you need to re-authenticate.";
}
break;
case HelixAutoModMessageError::UserNotAuthorized: {
errorMessage +=
"you don't have permission to perform that action";
}
break;
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
case HelixAutoModMessageError::MessageNotFound: {
errorMessage += "target message not found.";
}
break;
// This would most likely happen if the service is down, or if the JSON payload returned has changed format
case HelixAutoModMessageError::Unknown:
default: {
errorMessage += "an unknown error occured.";
}
break;
}
channel->addMessage(makeSystemMessage(errorMessage));
});
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;
}
getHelix()->getEmoteSetData(
emoteSet->key,
[emoteSet](HelixEmoteSetData emoteSetData) {
// Follower emotes can be only used in their origin channel
if (emoteSetData.emoteType == "follower")
{
emoteSet->local = true;
}
if (emoteSetData.ownerId.isEmpty() ||
emoteSetData.setId != emoteSet->key)
2019-08-20 21:50:36 +02:00
{
qCWarning(chatterinoTwitch)
<< QString("Failed to fetch emoteSetData for %1, assuming "
"Twitch is the owner")
.arg(emoteSet->key);
// most (if not all) emotes that fail to load are time limited event emotes owned by Twitch
emoteSet->channelName = "twitch";
emoteSet->text = "Twitch";
return;
2019-08-20 21:50:36 +02:00
}
2018-08-02 14:23:27 +02:00
// emote set 0 = global emotes
if (emoteSetData.ownerId == "0")
{
// emoteSet->channelName = QString();
emoteSet->text = "Twitch Global";
return;
}
getHelix()->getUserById(
emoteSetData.ownerId,
[emoteSet](HelixUser user) {
emoteSet->channelName = user.login;
emoteSet->text = user.displayName;
},
[emoteSetData] {
qCWarning(chatterinoTwitch)
<< "Failed to query user by id:" << emoteSetData.ownerId
<< emoteSetData.setId;
});
},
[emoteSet] {
// fetching emoteset data failed
return;
});
}
2018-02-05 15:11:50 +01:00
} // namespace chatterino