diff --git a/src/providers/twitch/twitchemotes.cpp b/src/providers/twitch/twitchemotes.cpp index d659a3a8b..dfd68a115 100644 --- a/src/providers/twitch/twitchemotes.cpp +++ b/src/providers/twitch/twitchemotes.cpp @@ -12,20 +12,20 @@ namespace twitch { namespace { -QString getEmoteLink(long id, const QString &emoteScale) +QString getEmoteLink(const QString &id, const QString &emoteScale) { QString value = TWITCH_EMOTE_TEMPLATE; value.detach(); - return value.replace("{id}", QString::number(id)).replace("{scale}", emoteScale); + return value.replace("{id}", id).replace("{scale}", emoteScale); } } // namespace // id is used for lookup // emoteName is used for giving a name to the emote in case it doesn't exist -util::EmoteData TwitchEmotes::getEmoteById(long id, const QString &emoteName) +util::EmoteData TwitchEmotes::getEmoteById(const QString &id, const QString &emoteName) { QString _emoteName = emoteName; _emoteName.replace("<", "<"); @@ -75,38 +75,40 @@ void TwitchEmotes::refresh(const std::shared_ptr &user) TwitchAccountEmoteData &emoteData = this->emotes[roomID]; if (emoteData.filled) { - qDebug() << "Already loaded for room id " << roomID; + debug::Log("Emotes are already loaded for room id {}", roomID); return; } QString url("https://api.twitch.tv/kraken/users/" + roomID + "/emotes"); - util::twitch::getAuthorized( - url, clientID, oauthToken, QThread::currentThread(), - [=, &emoteData](const QJsonObject &root) { - emoteData.emoteSets.clear(); - emoteData.emoteCodes.clear(); + auto loadEmotes = [=, &emoteData](const QJsonObject &root) { + emoteData.emoteSets.clear(); + emoteData.emoteCodes.clear(); - auto emoticonSets = root.value("emoticon_sets").toObject(); - for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) { - std::string emoteSetString = it.key().toStdString(); - QJsonArray emoteSetList = it.value().toArray(); + auto emoticonSets = root.value("emoticon_sets").toObject(); + for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) { + EmoteSet emoteSet; - for (QJsonValue emoteValue : emoteSetList) { - QJsonObject emoticon = emoteValue.toObject(); - std::string id = QString::number(emoticon["id"].toInt()).toStdString(); - std::string code = emoticon["code"].toString().toStdString(); - emoteData.emoteSets[emoteSetString].push_back({id, code}); - emoteData.emoteCodes.push_back(code); + emoteSet.key = it.key(); - util::EmoteData emote = - this->getEmoteById(emoticon["id"].toInt(), emoticon["code"].toString()); - emoteData.emotes.insert(emoticon["code"].toString(), emote); - } + for (QJsonValue emoteValue : it.value().toArray()) { + QJsonObject emoticon = emoteValue.toObject(); + QString id = QString::number(emoticon["id"].toInt()); + QString code = emoticon["code"].toString(); + emoteSet.emotes.emplace_back(id, code); + emoteData.emoteCodes.push_back(code); + + util::EmoteData emote = this->getEmoteById(id, code); + emoteData.emotes.insert(code, emote); } - emoteData.filled = true; - }); + emoteData.emoteSets.emplace_back(std::move(emoteSet)); + } + + emoteData.filled = true; + }; + + util::twitch::getAuthorized(url, clientID, oauthToken, QThread::currentThread(), loadEmotes); } } // namespace twitch diff --git a/src/providers/twitch/twitchemotes.hpp b/src/providers/twitch/twitchemotes.hpp index b69044508..f0aab695d 100644 --- a/src/providers/twitch/twitchemotes.hpp +++ b/src/providers/twitch/twitchemotes.hpp @@ -15,27 +15,41 @@ namespace twitch { class TwitchEmotes { public: - util::EmoteData getEmoteById(long int id, const QString &emoteName); + util::EmoteData getEmoteById(const QString &id, const QString &emoteName); /// Twitch emotes void refresh(const std::shared_ptr &user); + struct TwitchEmote { + TwitchEmote(const QString &_id, const QString &_code) + : id(_id) + , code(_code) + { + } + + // i.e. "403921" + QString id; + + // i.e. "forsenE" + QString code; + }; + + struct EmoteSet { + QString key; + std::vector emotes; + }; + struct TwitchAccountEmoteData { - struct TwitchEmote { - std::string id; - std::string code; - }; + std::vector emoteSets; - // emote set - std::map> emoteSets; - - std::vector emoteCodes; + std::vector emoteCodes; util::EmoteMap emotes; bool filled = false; }; + // Key is the user ID std::map emotes; private: @@ -43,7 +57,7 @@ private: util::ConcurrentMap _twitchEmotes; // emote id - util::ConcurrentMap _twitchEmoteFromCache; + util::ConcurrentMap _twitchEmoteFromCache; }; } // namespace twitch diff --git a/src/providers/twitch/twitchmessagebuilder.cpp b/src/providers/twitch/twitchmessagebuilder.cpp index 10563ccb7..3a29b71ff 100644 --- a/src/providers/twitch/twitchmessagebuilder.cpp +++ b/src/providers/twitch/twitchmessagebuilder.cpp @@ -498,7 +498,7 @@ void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcMessage *ircMessa return; } - long int id = std::stol(parameters.at(0).toStdString(), nullptr, 10); + const auto &id = parameters.at(0); QStringList occurences = parameters.at(1).split(','); diff --git a/src/widgets/emotepopup.cpp b/src/widgets/emotepopup.cpp index ae111c758..53197be1d 100644 --- a/src/widgets/emotepopup.cpp +++ b/src/widgets/emotepopup.cpp @@ -102,13 +102,11 @@ void EmotePopup::loadChannel(ChannelPtr _channel) builder2.getMessage()->flags |= Message::Centered; builder2.getMessage()->flags |= Message::DisableCompactEmotes; - for (const auto &emote : set.second) { + for (const auto &emote : set.emotes) { [&](const QString &key, const util::EmoteData &value) { builder2.append((new EmoteElement(value, MessageElement::Flags::AlwaysShow)) ->setLink(Link(Link::InsertText, key))); - }(QString::fromStdString(emote.code), - app->emotes->twitch.getEmoteById(QString::fromStdString(emote.id).toLong(), - QString::fromStdString(emote.code))); + }(emote.code, app->emotes->twitch.getEmoteById(emote.id, emote.code)); } emoteChannel->addMessage(builder2.getMessage());