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](const QJsonObject &root) {
emoteData.emoteSets.clear(); emoteData.emoteSets.clear();
emoteData.emoteCodes.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();
for (QJsonValue emoteValue : it.value().toArray()) {
QJsonObject emoticon = emoteValue.toObject(); QJsonObject emoticon = emoteValue.toObject();
std::string id = QString::number(emoticon["id"].toInt()).toStdString(); QString id = QString::number(emoticon["id"].toInt());
std::string code = emoticon["code"].toString().toStdString(); QString code = emoticon["code"].toString();
emoteData.emoteSets[emoteSetString].push_back({id, code}); emoteSet.emotes.emplace_back(id, code);
emoteData.emoteCodes.push_back(code); emoteData.emoteCodes.push_back(code);
util::EmoteData emote = util::EmoteData emote = this->getEmoteById(id, code);
this->getEmoteById(emoticon["id"].toInt(), emoticon["code"].toString()); emoteData.emotes.insert(code, emote);
emoteData.emotes.insert(emoticon["code"].toString(), emote);
} }
emoteData.emoteSets.emplace_back(std::move(emoteSet));
} }
emoteData.filled = true; 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 TwitchAccountEmoteData {
struct TwitchEmote { struct TwitchEmote {
std::string id; TwitchEmote(const QString &_id, const QString &_code)
std::string code; : id(_id)
, code(_code)
{
}
// i.e. "403921"
QString id;
// i.e. "forsenE"
QString code;
}; };
// emote set struct EmoteSet {
std::map<std::string, std::vector<TwitchEmote>> emoteSets; QString key;
std::vector<TwitchEmote> emotes;
};
std::vector<std::string> emoteCodes; struct TwitchAccountEmoteData {
std::vector<EmoteSet> emoteSets;
std::vector<QString> 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());