Make TwitchEmotes class conform to QString standard

Make twitch emotes only be identified by a string ID, instead of sometimes by a string and sometimes by an int

Make the EmoteSet a struct instead of just a vector of emotes. This will be handy when we later fill in the emote sets name and other info (i.e. whether it's a subscription benifit or not)
This commit is contained in:
Rasmus Karlsson 2018-06-07 13:01:06 +02:00
parent 3bc7e2da8a
commit 93fe7adce7
4 changed files with 54 additions and 40 deletions

View file

@ -12,20 +12,20 @@ namespace twitch {
namespace { namespace {
QString getEmoteLink(long id, const QString &emoteScale) QString getEmoteLink(const QString &id, const QString &emoteScale)
{ {
QString value = TWITCH_EMOTE_TEMPLATE; QString value = TWITCH_EMOTE_TEMPLATE;
value.detach(); value.detach();
return value.replace("{id}", QString::number(id)).replace("{scale}", emoteScale); return value.replace("{id}", id).replace("{scale}", emoteScale);
} }
} // namespace } // namespace
// id is used for lookup // id is used for lookup
// emoteName is used for giving a name to the emote in case it doesn't exist // 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; QString _emoteName = emoteName;
_emoteName.replace("<", "&lt;"); _emoteName.replace("<", "&lt;");
@ -75,38 +75,40 @@ void TwitchEmotes::refresh(const std::shared_ptr<TwitchAccount> &user)
TwitchAccountEmoteData &emoteData = this->emotes[roomID]; TwitchAccountEmoteData &emoteData = this->emotes[roomID];
if (emoteData.filled) { if (emoteData.filled) {
qDebug() << "Already loaded for room id " << roomID; debug::Log("Emotes are already loaded for room id {}", roomID);
return; return;
} }
QString url("https://api.twitch.tv/kraken/users/" + roomID + "/emotes"); QString url("https://api.twitch.tv/kraken/users/" + roomID + "/emotes");
util::twitch::getAuthorized( auto loadEmotes = [=, &emoteData](const QJsonObject &root) {
url, clientID, oauthToken, QThread::currentThread(), emoteData.emoteSets.clear();
[=, &emoteData](const QJsonObject &root) { emoteData.emoteCodes.clear();
emoteData.emoteSets.clear();
emoteData.emoteCodes.clear();
auto emoticonSets = root.value("emoticon_sets").toObject(); auto emoticonSets = root.value("emoticon_sets").toObject();
for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) { for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) {
std::string emoteSetString = it.key().toStdString(); EmoteSet emoteSet;
QJsonArray emoteSetList = it.value().toArray();
for (QJsonValue emoteValue : emoteSetList) { emoteSet.key = it.key();
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);
util::EmoteData emote = for (QJsonValue emoteValue : it.value().toArray()) {
this->getEmoteById(emoticon["id"].toInt(), emoticon["code"].toString()); QJsonObject emoticon = emoteValue.toObject();
emoteData.emotes.insert(emoticon["code"].toString(), emote); 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 } // namespace twitch

View file

@ -15,27 +15,41 @@ namespace twitch {
class TwitchEmotes class TwitchEmotes
{ {
public: public:
util::EmoteData getEmoteById(long int id, const QString &emoteName); util::EmoteData getEmoteById(const QString &id, const QString &emoteName);
/// Twitch emotes /// Twitch emotes
void refresh(const std::shared_ptr<providers::twitch::TwitchAccount> &user); void refresh(const std::shared_ptr<providers::twitch::TwitchAccount> &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<TwitchEmote> emotes;
};
struct TwitchAccountEmoteData { struct TwitchAccountEmoteData {
struct TwitchEmote { std::vector<EmoteSet> emoteSets;
std::string id;
std::string code;
};
// emote set std::vector<QString> emoteCodes;
std::map<std::string, std::vector<TwitchEmote>> emoteSets;
std::vector<std::string> emoteCodes;
util::EmoteMap emotes; util::EmoteMap emotes;
bool filled = false; bool filled = false;
}; };
// Key is the user ID
std::map<QString, TwitchAccountEmoteData> emotes; std::map<QString, TwitchAccountEmoteData> emotes;
private: private:
@ -43,7 +57,7 @@ private:
util::ConcurrentMap<QString, providers::twitch::EmoteValue *> _twitchEmotes; util::ConcurrentMap<QString, providers::twitch::EmoteValue *> _twitchEmotes;
// emote id // emote id
util::ConcurrentMap<long, util::EmoteData> _twitchEmoteFromCache; util::ConcurrentMap<QString, util::EmoteData> _twitchEmoteFromCache;
}; };
} // namespace twitch } // namespace twitch

View file

@ -498,7 +498,7 @@ void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcMessage *ircMessa
return; return;
} }
long int id = std::stol(parameters.at(0).toStdString(), nullptr, 10); const auto &id = parameters.at(0);
QStringList occurences = parameters.at(1).split(','); QStringList occurences = parameters.at(1).split(',');

View file

@ -102,13 +102,11 @@ void EmotePopup::loadChannel(ChannelPtr _channel)
builder2.getMessage()->flags |= Message::Centered; builder2.getMessage()->flags |= Message::Centered;
builder2.getMessage()->flags |= Message::DisableCompactEmotes; builder2.getMessage()->flags |= Message::DisableCompactEmotes;
for (const auto &emote : set.second) { for (const auto &emote : set.emotes) {
[&](const QString &key, const util::EmoteData &value) { [&](const QString &key, const util::EmoteData &value) {
builder2.append((new EmoteElement(value, MessageElement::Flags::AlwaysShow)) builder2.append((new EmoteElement(value, MessageElement::Flags::AlwaysShow))
->setLink(Link(Link::InsertText, key))); ->setLink(Link(Link::InsertText, key)));
}(QString::fromStdString(emote.code), }(emote.code, app->emotes->twitch.getEmoteById(emote.id, emote.code));
app->emotes->twitch.getEmoteById(QString::fromStdString(emote.id).toLong(),
QString::fromStdString(emote.code)));
} }
emoteChannel->addMessage(builder2.getMessage()); emoteChannel->addMessage(builder2.getMessage());