mirror-chatterino2/src/providers/twitch/TwitchEmotes.cpp
pajlada 18cb4bd6e5
Massage includes some more (#4294)
* Add `<functional>` include to QStringHash.hpp

This ensures the base `std::hash` template is declared before this
specialization

* Add missing includes to `src/providers/twitch/TwitchAccountManager.hpp`

* Move explicit HelixChatters constructor to the source file

* Remove unused includes & add used includes to NicknamesModel.hpp

* NicknamesModel.hpp: Remove `virtual` when `override` is used

* Add missing QStringHash include to TwitchEmotes.cpp

* Add missing includes to various files

* Print Qt version in cmake step

Technically unrelated, but I'm sneaking it in

* Add changelog entry
2023-01-08 12:07:06 +00:00

68 lines
2 KiB
C++

#include "providers/twitch/TwitchEmotes.hpp"
#include "common/QLogging.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
#include "util/QStringHash.hpp"
namespace chatterino {
QString TwitchEmotes::cleanUpEmoteCode(const QString &dirtyEmoteCode)
{
auto cleanCode = dirtyEmoteCode;
cleanCode.detach();
static QMap<QString, QString> emoteNameReplacements{
{"[oO](_|\\.)[oO]", "O_o"}, {"\\&gt\\;\\(", "&gt;("},
{"\\&lt\\;3", "&lt;3"}, {"\\:-?(o|O)", ":O"},
{"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("},
{"\\:-?\\)", ":)"}, {"\\:-?D", ":D"},
{"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
{"R-?\\)", "R)"}, {"B-?\\)", "B)"},
};
auto it = emoteNameReplacements.find(dirtyEmoteCode);
if (it != emoteNameReplacements.end())
{
cleanCode = it.value();
}
return cleanCode;
}
// id is used for lookup
// emoteName is used for giving a name to the emote in case it doesn't exist
EmotePtr TwitchEmotes::getOrCreateEmote(const EmoteId &id,
const EmoteName &name_)
{
auto name = TwitchEmotes::cleanUpEmoteCode(name_.string);
// search in cache or create new emote
auto cache = this->twitchEmotesCache_.access();
auto shared = (*cache)[id].lock();
if (!shared)
{
(*cache)[id] = shared = std::make_shared<Emote>(Emote{
EmoteName{name},
ImageSet{
Image::fromUrl(getEmoteLink(id, "1.0"), 1),
Image::fromUrl(getEmoteLink(id, "2.0"), 0.5),
Image::fromUrl(getEmoteLink(id, "3.0"), 0.25),
},
Tooltip{name.toHtmlEscaped() + "<br>Twitch Emote"},
});
}
return shared;
}
Url TwitchEmotes::getEmoteLink(const EmoteId &id, const QString &emoteScale)
{
return {QString(TWITCH_EMOTE_TEMPLATE)
.replace("{id}", id.string)
.replace("{scale}", emoteScale)};
}
} // namespace chatterino