mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
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:
parent
3bc7e2da8a
commit
93fe7adce7
|
@ -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("<", "<");
|
_emoteName.replace("<", "<");
|
||||||
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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(',');
|
||||||
|
|
||||||
|
|
|
@ -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());
|
||||||
|
|
Loading…
Reference in a new issue