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 {
|
||||
|
||||
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<TwitchAccount> &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
|
||||
|
|
|
@ -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<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 TwitchEmote {
|
||||
std::string id;
|
||||
std::string code;
|
||||
};
|
||||
std::vector<EmoteSet> emoteSets;
|
||||
|
||||
// emote set
|
||||
std::map<std::string, std::vector<TwitchEmote>> emoteSets;
|
||||
|
||||
std::vector<std::string> emoteCodes;
|
||||
std::vector<QString> emoteCodes;
|
||||
|
||||
util::EmoteMap emotes;
|
||||
|
||||
bool filled = false;
|
||||
};
|
||||
|
||||
// Key is the user ID
|
||||
std::map<QString, TwitchAccountEmoteData> emotes;
|
||||
|
||||
private:
|
||||
|
@ -43,7 +57,7 @@ private:
|
|||
util::ConcurrentMap<QString, providers::twitch::EmoteValue *> _twitchEmotes;
|
||||
|
||||
// emote id
|
||||
util::ConcurrentMap<long, util::EmoteData> _twitchEmoteFromCache;
|
||||
util::ConcurrentMap<QString, util::EmoteData> _twitchEmoteFromCache;
|
||||
};
|
||||
|
||||
} // namespace twitch
|
||||
|
|
|
@ -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(',');
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue