2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/TwitchAccount.hpp"
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
#include "Application.hpp"
|
2021-05-14 13:14:43 +02:00
|
|
|
#include "common/Channel.hpp"
|
2019-05-26 21:44:13 +02:00
|
|
|
#include "common/Env.hpp"
|
2018-06-26 15:33:51 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Outcome.hpp"
|
2020-11-21 16:20:10 +01:00
|
|
|
#include "common/QLogging.hpp"
|
2021-02-14 14:01:13 +01:00
|
|
|
#include "controllers/accounts/AccountController.hpp"
|
2021-05-14 13:14:43 +02:00
|
|
|
#include "messages/Message.hpp"
|
|
|
|
#include "messages/MessageBuilder.hpp"
|
2021-03-21 15:42:45 +01:00
|
|
|
#include "providers/IvrApi.hpp"
|
2021-05-14 13:14:43 +02:00
|
|
|
#include "providers/irc/IrcMessageBuilder.hpp"
|
2018-06-26 17:25:24 +02:00
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
2021-02-14 14:01:13 +01:00
|
|
|
#include "providers/twitch/TwitchUser.hpp"
|
2020-03-14 12:13:57 +01:00
|
|
|
#include "providers/twitch/api/Helix.hpp"
|
2021-03-21 15:19:49 +01:00
|
|
|
#include "providers/twitch/api/Kraken.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "singletons/Emotes.hpp"
|
2021-07-11 11:12:49 +02:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2018-08-11 17:35:46 +02:00
|
|
|
QColor TwitchAccount::color()
|
|
|
|
{
|
|
|
|
return this->color_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchAccount::setColor(QColor color)
|
|
|
|
{
|
2021-04-10 14:34:40 +02:00
|
|
|
this->color_.set(std::move(color));
|
2018-08-11 17:35:46 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2018-03-31 13:44:15 +02:00
|
|
|
|
2021-02-14 14:01:13 +01:00
|
|
|
void TwitchAccount::loadBlocks()
|
2018-05-12 20:34:13 +02:00
|
|
|
{
|
2021-02-14 14:01:13 +01:00
|
|
|
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();
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2021-02-14 14:01:13 +01:00
|
|
|
for (const HelixBlock &block : blocks)
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2021-02-14 14:01:13 +01:00
|
|
|
TwitchUser blockedUser;
|
|
|
|
blockedUser.fromHelixBlock(block);
|
2021-05-01 13:38:58 +02:00
|
|
|
ignores->insert(blockedUser);
|
|
|
|
userIds->insert(blockedUser.id);
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
2021-02-14 14:01:13 +01:00
|
|
|
},
|
|
|
|
[] {
|
|
|
|
qDebug() << "Fetching blocks failed!";
|
|
|
|
});
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2021-02-14 14:01:13 +01:00
|
|
|
void TwitchAccount::blockUser(QString userId, std::function<void()> onSuccess,
|
|
|
|
std::function<void()> onFailure)
|
2018-05-13 17:53:24 +02:00
|
|
|
{
|
2021-02-14 14:01:13 +01:00
|
|
|
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();
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2021-05-01 13:38:58 +02:00
|
|
|
ignores->insert(blockedUser);
|
|
|
|
userIds->insert(blockedUser.id);
|
2019-08-20 21:50:36 +02:00
|
|
|
}
|
2021-02-14 14:01:13 +01:00
|
|
|
onSuccess();
|
|
|
|
},
|
2021-04-10 14:34:40 +02:00
|
|
|
std::move(onFailure));
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2021-02-14 14:01:13 +01:00
|
|
|
void TwitchAccount::unblockUser(QString userId, std::function<void()> onSuccess,
|
|
|
|
std::function<void()> onFailure)
|
2018-05-13 17:53:24 +02:00
|
|
|
{
|
2021-02-14 14:01:13 +01:00
|
|
|
getHelix()->unblockUser(
|
|
|
|
userId,
|
|
|
|
[this, userId, onSuccess] {
|
2019-08-20 21:50:36 +02:00
|
|
|
TwitchUser ignoredUser;
|
2021-02-14 14:01:13 +01:00
|
|
|
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();
|
2018-05-13 17:53:24 +02:00
|
|
|
|
2021-05-01 13:38:58 +02:00
|
|
|
ignores->erase(ignoredUser);
|
|
|
|
userIds->erase(ignoredUser.id);
|
2019-08-20 21:50:36 +02:00
|
|
|
}
|
2021-02-14 14:01:13 +01:00
|
|
|
onSuccess();
|
|
|
|
},
|
2021-04-10 14:34:40 +02:00
|
|
|
std::move(onFailure));
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchAccount::checkFollow(const QString targetUserID,
|
|
|
|
std::function<void(FollowResult)> onFinished)
|
|
|
|
{
|
2020-03-14 12:13:57 +01:00
|
|
|
const auto onResponse = [onFinished](bool following, const auto &record) {
|
|
|
|
if (!following)
|
|
|
|
{
|
|
|
|
onFinished(FollowResult_NotFollowing);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onFinished(FollowResult_Following);
|
|
|
|
};
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2020-03-14 12:13:57 +01:00
|
|
|
getHelix()->getUserFollow(this->getUserId(), targetUserID, onResponse,
|
|
|
|
[] {});
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 17:19:41 +02:00
|
|
|
SharedAccessGuard<const std::set<TwitchUser>> TwitchAccount::accessBlocks()
|
|
|
|
const
|
2018-05-12 20:34:13 +02:00
|
|
|
{
|
2021-05-01 13:38:58 +02:00
|
|
|
return this->ignores_.accessConst();
|
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2021-05-01 17:19:41 +02:00
|
|
|
SharedAccessGuard<const std::set<QString>> TwitchAccount::accessBlockedUserIds()
|
|
|
|
const
|
2021-05-01 13:38:58 +02:00
|
|
|
{
|
|
|
|
return this->ignoresUserIds_.accessConst();
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
void TwitchAccount::loadEmotes()
|
2018-06-27 02:16:30 +02:00
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoTwitch)
|
|
|
|
<< "Loading Twitch emotes for user" << this->getUserName();
|
2018-06-27 02:16:30 +02:00
|
|
|
|
2021-03-21 15:19:49 +01:00
|
|
|
if (this->getOAuthClient().isEmpty() || this->getOAuthToken().isEmpty())
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2021-06-20 00:11:06 +02:00
|
|
|
qCDebug(chatterinoTwitch) << "Missing Client ID and/or OAuth token";
|
2018-06-27 02:16:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-20 00:11:06 +02:00
|
|
|
// Getting subscription emotes from kraken
|
2021-03-21 15:19:49 +01:00
|
|
|
getKraken()->getUserEmotes(
|
|
|
|
this,
|
|
|
|
[this](KrakenEmoteSets data) {
|
|
|
|
// no emotes available
|
|
|
|
if (data.emoteSets.isEmpty())
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
2021-03-21 15:19:49 +01:00
|
|
|
qCWarning(chatterinoTwitch)
|
|
|
|
<< "\"emoticon_sets\" either empty or not present in "
|
|
|
|
"Kraken::getUserEmotes response";
|
|
|
|
return;
|
2019-08-20 21:50:36 +02:00
|
|
|
}
|
2021-03-21 15:19:49 +01:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
2021-06-20 00:11:06 +02:00
|
|
|
// Clearing emote data
|
|
|
|
auto emoteData = this->emotes_.access();
|
|
|
|
emoteData->emoteSets.clear();
|
2021-07-11 11:12:49 +02:00
|
|
|
emoteData->emotes.clear();
|
2021-03-21 15:19:49 +01:00
|
|
|
|
2021-06-20 00:11:06 +02:00
|
|
|
for (auto emoteSetIt = data.emoteSets.begin();
|
|
|
|
emoteSetIt != data.emoteSets.end(); ++emoteSetIt)
|
2021-03-21 15:19:49 +01:00
|
|
|
{
|
2021-06-20 00:11:06 +02:00
|
|
|
auto emoteSet = std::make_shared<EmoteSet>();
|
|
|
|
|
|
|
|
emoteSet->key = emoteSetIt.key();
|
|
|
|
this->loadEmoteSetData(emoteSet);
|
|
|
|
|
|
|
|
for (const auto emoteArrObj : emoteSetIt.value().toArray())
|
2021-03-21 15:19:49 +01:00
|
|
|
{
|
2021-06-20 00:11:06 +02:00
|
|
|
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};
|
2021-07-17 12:35:43 +02:00
|
|
|
auto code = EmoteName{
|
|
|
|
TwitchEmotes::cleanUpEmoteCode(krakenEmote.code)};
|
2021-03-21 15:19:49 +01:00
|
|
|
|
2021-07-17 12:35:43 +02:00
|
|
|
emoteSet->emotes.emplace_back(TwitchEmote{id, code});
|
2021-03-21 15:19:49 +01:00
|
|
|
|
2021-07-11 11:12:49 +02:00
|
|
|
if (!emoteSet->local)
|
|
|
|
{
|
|
|
|
auto emote =
|
|
|
|
getApp()->emotes->twitch.getOrCreateEmote(id,
|
|
|
|
code);
|
|
|
|
emoteData->emotes.emplace(code, emote);
|
|
|
|
}
|
2021-06-20 00:11:06 +02:00
|
|
|
}
|
2021-03-21 15:19:49 +01:00
|
|
|
|
2021-06-20 00:11:06 +02:00
|
|
|
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);
|
2021-03-21 15:19:49 +01:00
|
|
|
}
|
2019-08-20 21:50:36 +02:00
|
|
|
}
|
2021-06-20 00:11:06 +02:00
|
|
|
// Getting userstate emotes from Ivr
|
|
|
|
this->loadUserstateEmotes();
|
2021-03-21 15:19:49 +01:00
|
|
|
},
|
|
|
|
[] {
|
2021-06-20 00:11:06 +02:00
|
|
|
// kraken request failed
|
2021-03-21 15:19:49 +01:00
|
|
|
});
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-20 00:11:06 +02:00
|
|
|
bool TwitchAccount::setUserstateEmoteSets(QStringList newEmoteSets)
|
2021-03-21 15:42:45 +01:00
|
|
|
{
|
2021-06-20 00:11:06 +02:00
|
|
|
newEmoteSets.sort();
|
|
|
|
|
|
|
|
if (this->userstateEmoteSets_ == newEmoteSets)
|
2021-03-21 15:42:45 +01:00
|
|
|
{
|
2021-06-20 00:11:06 +02:00
|
|
|
// Nothing has changed
|
|
|
|
return false;
|
2021-03-21 15:42:45 +01:00
|
|
|
}
|
2021-06-20 00:11:06 +02:00
|
|
|
|
|
|
|
this->userstateEmoteSets_ = newEmoteSets;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchAccount::loadUserstateEmotes()
|
|
|
|
{
|
|
|
|
if (this->userstateEmoteSets_.isEmpty())
|
2021-03-21 15:42:45 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-06-20 00:11:06 +02:00
|
|
|
|
|
|
|
QStringList newEmoteSetKeys, krakenEmoteSetKeys;
|
2021-03-21 15:42:45 +01:00
|
|
|
|
|
|
|
auto emoteData = this->emotes_.access();
|
|
|
|
auto userEmoteSets = emoteData->emoteSets;
|
|
|
|
|
|
|
|
// get list of already fetched emote sets
|
|
|
|
for (const auto &userEmoteSet : userEmoteSets)
|
|
|
|
{
|
2021-06-20 00:11:06 +02:00
|
|
|
krakenEmoteSetKeys.push_back(userEmoteSet->key);
|
2021-03-21 15:42:45 +01:00
|
|
|
}
|
2021-06-06 19:27:45 +02:00
|
|
|
|
2021-03-21 15:42:45 +01:00
|
|
|
// filter out emote sets from userstate message, which are not in fetched emote set list
|
2021-06-20 00:11:06 +02:00
|
|
|
for (const auto &emoteSetKey : this->userstateEmoteSets_)
|
2021-03-21 15:42:45 +01:00
|
|
|
{
|
2021-06-20 00:11:06 +02:00
|
|
|
if (!krakenEmoteSetKeys.contains(emoteSetKey))
|
2021-03-21 15:42:45 +01:00
|
|
|
{
|
|
|
|
newEmoteSetKeys.push_back(emoteSetKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return if there are no new emote sets
|
|
|
|
if (newEmoteSetKeys.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-06-20 00:11:06 +02:00
|
|
|
qCDebug(chatterinoTwitch) << QString("Loading %1 emotesets from IVR: %2")
|
|
|
|
.arg(newEmoteSetKeys.size())
|
|
|
|
.arg(newEmoteSetKeys.join(", "));
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-06 19:27:45 +02:00
|
|
|
// splitting newEmoteSetKeys to batches of 100, because Ivr API endpoint accepts a maximum of 100 emotesets at once
|
|
|
|
constexpr int batchSize = 100;
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-06 19:27:45 +02:00
|
|
|
std::vector<QStringList> batches;
|
2021-06-06 21:59:53 +02:00
|
|
|
int batchCount = (newEmoteSetKeys.size() / batchSize) + 1;
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-06 21:59:53 +02:00
|
|
|
batches.reserve(batchCount);
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-06 21:59:53 +02:00
|
|
|
for (int i = 0; i < batchCount; i++)
|
2021-06-06 19:27:45 +02:00
|
|
|
{
|
|
|
|
QStringList batch;
|
2021-06-06 21:59:53 +02:00
|
|
|
|
|
|
|
int last = std::min(batchSize, newEmoteSetKeys.size() - batchSize * i);
|
|
|
|
for (int j = batchSize * i; j < last; j++)
|
2021-06-06 19:27:45 +02:00
|
|
|
{
|
|
|
|
batch.push_back(newEmoteSetKeys.at(j));
|
|
|
|
}
|
|
|
|
batches.emplace_back(batch);
|
|
|
|
}
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-20 00:11:06 +02:00
|
|
|
// requesting emotes
|
2021-06-06 19:27:45 +02:00
|
|
|
for (const auto &batch : batches)
|
|
|
|
{
|
|
|
|
getIvr()->getBulkEmoteSets(
|
|
|
|
batch.join(","),
|
|
|
|
[this](QJsonArray emoteSetArray) {
|
|
|
|
auto emoteData = this->emotes_.access();
|
2021-07-11 11:12:49 +02:00
|
|
|
auto localEmoteData = this->localEmotes_.access();
|
2021-06-06 19:27:45 +02:00
|
|
|
for (auto emoteSet : emoteSetArray)
|
2021-03-21 15:42:45 +01:00
|
|
|
{
|
2021-06-06 19:27:45 +02:00
|
|
|
auto newUserEmoteSet = std::make_shared<EmoteSet>();
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-06 19:27:45 +02:00
|
|
|
IvrEmoteSet ivrEmoteSet(emoteSet.toObject());
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-06 19:27:45 +02:00
|
|
|
newUserEmoteSet->key = ivrEmoteSet.setId;
|
2021-03-21 15:42:45 +01:00
|
|
|
|
2021-06-06 19:27:45 +02:00
|
|
|
auto name = ivrEmoteSet.login;
|
|
|
|
name.detach();
|
|
|
|
name[0] = name[0].toUpper();
|
|
|
|
|
|
|
|
newUserEmoteSet->text = name;
|
|
|
|
newUserEmoteSet->channelName = ivrEmoteSet.login;
|
|
|
|
|
2021-07-11 11:12:49 +02:00
|
|
|
for (const auto &emoteObj : ivrEmoteSet.emotes)
|
2021-06-06 19:27:45 +02:00
|
|
|
{
|
2021-07-11 11:12:49 +02:00
|
|
|
IvrEmote ivrEmote(emoteObj.toObject());
|
2021-06-06 19:27:45 +02:00
|
|
|
|
|
|
|
auto id = EmoteId{ivrEmote.id};
|
2021-07-17 12:35:43 +02:00
|
|
|
auto code = EmoteName{
|
|
|
|
TwitchEmotes::cleanUpEmoteCode(ivrEmote.code)};
|
|
|
|
|
2021-06-06 19:27:45 +02:00
|
|
|
newUserEmoteSet->emotes.push_back(
|
2021-07-17 12:35:43 +02:00
|
|
|
TwitchEmote{id, code});
|
2021-06-06 19:27:45 +02:00
|
|
|
|
2021-07-11 11:12:49 +02:00
|
|
|
auto emote =
|
2021-06-06 19:27:45 +02:00
|
|
|
getApp()->emotes->twitch.getOrCreateEmote(id, code);
|
2021-07-11 11:12:49 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2021-06-06 19:27:45 +02:00
|
|
|
}
|
|
|
|
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);
|
2021-03-21 15:42:45 +01:00
|
|
|
}
|
2021-06-06 19:27:45 +02:00
|
|
|
},
|
|
|
|
[] {
|
|
|
|
// fetching emotes failed, ivr API might be down
|
|
|
|
});
|
|
|
|
};
|
2021-03-21 15:42:45 +01:00
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2021-05-01 17:19:41 +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();
|
|
|
|
}
|
|
|
|
|
2021-07-11 11:12:49 +02:00
|
|
|
SharedAccessGuard<const std::unordered_map<QString, EmoteMap>>
|
|
|
|
TwitchAccount::accessLocalEmotes() const
|
|
|
|
{
|
|
|
|
return this->localEmotes_.accessConst();
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:45:59 +01:00
|
|
|
// AutoModActions
|
2021-05-14 13:14:43 +02:00
|
|
|
void TwitchAccount::autoModAllow(const QString msgID, ChannelPtr channel)
|
2019-01-20 14:47:04 +01:00
|
|
|
{
|
2021-05-14 13:14:43 +02: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
|
|
|
|
2021-05-14 13:14:43 +02:00
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case HelixAutoModMessageError::MessageAlreadyProcessed: {
|
|
|
|
errorMessage += "message has already been processed.";
|
|
|
|
}
|
|
|
|
break;
|
2019-01-20 17:03:45 +01:00
|
|
|
|
2021-05-14 13:14:43 +02:00
|
|
|
case HelixAutoModMessageError::UserNotAuthenticated: {
|
|
|
|
errorMessage += "you need to re-authenticate.";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HelixAutoModMessageError::UserNotAuthorized: {
|
|
|
|
errorMessage +=
|
|
|
|
"you don't have permission to perform that action";
|
|
|
|
}
|
|
|
|
break;
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2021-05-14 13:14:43 +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
|
|
|
}
|
|
|
|
|
2021-05-14 13:14:43 +02:00
|
|
|
void TwitchAccount::autoModDeny(const QString msgID, ChannelPtr channel)
|
2019-01-20 14:47:04 +01:00
|
|
|
{
|
2021-05-14 13:14:43 +02: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
|
|
|
|
2021-05-14 13:14:43 +02:00
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case HelixAutoModMessageError::MessageAlreadyProcessed: {
|
|
|
|
errorMessage += "message has already been processed.";
|
|
|
|
}
|
|
|
|
break;
|
2019-01-20 17:03:45 +01:00
|
|
|
|
2021-05-14 13:14:43 +02:00
|
|
|
case HelixAutoModMessageError::UserNotAuthenticated: {
|
|
|
|
errorMessage += "you need to re-authenticate.";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HelixAutoModMessageError::UserNotAuthorized: {
|
|
|
|
errorMessage +=
|
|
|
|
"you don't have permission to perform that action";
|
|
|
|
}
|
|
|
|
break;
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2021-05-14 13:14:43 +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
|
|
|
}
|
2019-01-20 14:45:59 +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)
|
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-24 23:23:31 +02:00
|
|
|
getHelix()->getEmoteSetData(
|
|
|
|
emoteSet->key,
|
|
|
|
[emoteSet](HelixEmoteSetData emoteSetData) {
|
2021-07-11 11:12:49 +02:00
|
|
|
// Follower emotes can be only used in their origin channel
|
|
|
|
if (emoteSetData.emoteType == "follower")
|
|
|
|
{
|
|
|
|
emoteSet->local = true;
|
|
|
|
}
|
|
|
|
|
2021-06-24 23:23:31 +02:00
|
|
|
if (emoteSetData.ownerId.isEmpty() ||
|
|
|
|
emoteSetData.setId != emoteSet->key)
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
2021-06-24 23:23:31 +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
|
|
|
|
2021-06-24 23:23:31 +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-06-27 02:16:30 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
} // namespace chatterino
|