Fixed cheers not showing up in chat Fixes #1031

This commit is contained in:
apa420
2019-09-08 12:45:25 +02:00
parent e4e863bae5
commit 48fcc1a1a1
5 changed files with 114 additions and 73 deletions
+55 -13
View File
@@ -739,22 +739,22 @@ void TwitchChannel::refreshBadges()
void TwitchChannel::refreshCheerEmotes()
{
/*auto url = Url{"https://api.twitch.tv/kraken/bits/actions?channel_id=" +
this->getRoomId()};
auto request = NetworkRequest::twitchRequest(url.string);
request.setCaller(QThread::currentThread());
request.onSuccess(
[this, weak = weakOf<Channel>(this)](auto result) -> Outcome {
QString url("https://api.twitch.tv/kraken/bits/actions?channel_id=" +
this->roomId());
NetworkRequest::twitchRequest(url)
.onSuccess([this,
weak = weakOf<Channel>(this)](auto result) -> Outcome {
auto cheerEmoteSets = ParseCheermoteSets(result.parseRapidJson());
std::vector<CheerEmoteSet> emoteSets;
for (auto &set : cheerEmoteSets) {
for (auto &set : cheerEmoteSets)
{
auto cheerEmoteSet = CheerEmoteSet();
cheerEmoteSet.regex = QRegularExpression(
"^" + set.prefix.toLower() + "([1-9][0-9]*)$");
for (auto &tier : set.tiers) {
for (auto &tier : set.tiers)
{
CheerEmote cheerEmote;
cheerEmote.color = QColor(tier.color);
@@ -795,10 +795,8 @@ void TwitchChannel::refreshCheerEmotes()
*this->cheerEmoteSets_.access() = std::move(emoteSets);
return Success;
});
request.execute();
*/
})
.execute();
}
boost::optional<EmotePtr> TwitchChannel::twitchBadge(
@@ -825,4 +823,48 @@ boost::optional<EmotePtr> TwitchChannel::ffzCustomModBadge() const
return boost::none;
}
boost::optional<std::tuple<boost::optional<EmotePtr>, boost::optional<EmotePtr>,
boost::optional<QColor>>>
TwitchChannel::cheerEmote(const QString &string)
{
auto sets = this->cheerEmoteSets_.access();
for (const auto &set : *sets)
{
auto match = set.regex.match(string.toLower());
if (!match.hasMatch())
{
continue;
}
QString amount = match.captured(1);
bool ok = false;
int bitAmount = amount.toInt(&ok);
if (!ok)
{
log("Error parsing bit amount in cheerEmote");
}
for (const auto &emote : set.cheerEmotes)
{
if (amount >= emote.minBits)
{
using OPEP = boost::optional<EmotePtr>;
std::tuple<OPEP, OPEP, boost::optional<QColor>> retval;
if (emote.staticEmote)
{
std::get<0>(retval) = emote.staticEmote;
}
if (emote.animatedEmote)
{
std::get<1>(retval) = emote.animatedEmote;
}
if (emote.color != QColor())
{
std::get<2>(retval) = emote.color;
}
return retval;
}
}
}
return boost::none;
}
} // namespace chatterino
+6
View File
@@ -92,6 +92,12 @@ public:
boost::optional<EmotePtr> twitchBadge(const QString &set,
const QString &version) const;
// Cheers
boost::optional<
std::tuple<boost::optional<EmotePtr>, boost::optional<EmotePtr>,
boost::optional<QColor>>>
cheerEmote(const QString &string);
// Signals
pajlada::Signals::NoArgSignal roomIdChanged;
pajlada::Signals::NoArgSignal userStateChanged;
+24 -51
View File
@@ -274,7 +274,7 @@ MessagePtr TwitchMessageBuilder::build()
if (iterator != this->tags.end())
{
this->hasBits_ = true;
// bits = iterator.value().toString();
this->bits = iterator.value().toString();
}
// twitch emotes
@@ -1285,56 +1285,29 @@ void TwitchMessageBuilder::appendChatterinoBadges()
}
}
Outcome TwitchMessageBuilder::tryParseCheermote(const QString & /*string*/)
Outcome TwitchMessageBuilder::tryParseCheermote(const QString &string)
{
// auto app = getApp();
//// Try to parse custom cheermotes
// const auto &channelResources = app->resources->channels[this->roomID_];
// if (channelResources.loaded) {
// for (const auto &cheermoteSet : channelResources.cheermoteSets) {
// auto match = cheermoteSet.regex.match(string);
// if (!match.hasMatch()) {
// continue;
// }
// QString amount = match.captured(1);
// bool ok = false;
// int numBits = amount.toInt(&ok);
// if (!ok) {
// Log("Error parsing bit amount in tryParseCheermote");
// return Failure;
// }
// auto savedIt = cheermoteSet.cheermotes.end();
// // Fetch cheermote that matches our numBits
// for (auto it = cheermoteSet.cheermotes.begin(); it !=
// cheermoteSet.cheermotes.end();
// ++it) {
// if (numBits >= it->minBits) {
// savedIt = it;
// } else {
// break;
// }
// }
// if (savedIt == cheermoteSet.cheermotes.end()) {
// Log("Error getting a cheermote from a cheermote set for the
// bit amount {}",
// numBits);
// return Failure;
// }
// const auto &cheermote = *savedIt;
// this->emplace<EmoteElement>(cheermote.animatedEmote,
// MessageElementFlag::BitsAnimated);
// this->emplace<TextElement>(amount, MessageElementFlag::Text,
// cheermote.color);
// return Success;
// }
//}
return Failure;
auto cheerOpt = this->twitchChannel->cheerEmote(string);
if (!cheerOpt)
{
return Failure;
}
auto &cheerPairAndColor = *cheerOpt;
if (std::get<0>(cheerPairAndColor))
{
this->emplace<EmoteElement>(*std::get<0>(cheerPairAndColor),
MessageElementFlag::BitsStatic);
}
if (std::get<1>(cheerPairAndColor))
{
this->emplace<EmoteElement>(*std::get<1>(cheerPairAndColor),
MessageElementFlag::BitsAnimated);
}
if (std::get<2>(cheerPairAndColor))
{
this->emplace<TextElement>(this->bits, MessageElementFlag::BitsAmount,
*std::get<2>(cheerPairAndColor));
}
return Success;
}
} // namespace chatterino
@@ -78,6 +78,7 @@ private:
QString roomID_;
bool hasBits_ = false;
QString bits;
bool historicalMessage_ = false;
QString userId_;