Implement emoji sending using shortcodes 😎 🅱️

This commit is contained in:
Rasmus Karlsson 2017-07-31 22:15:02 +02:00
parent 65e18d4833
commit b5407c94aa
3 changed files with 41 additions and 1 deletions

View file

@ -118,7 +118,11 @@ void Channel::reloadChannelEmotes()
void Channel::sendMessage(const QString &message)
{
qDebug() << "Channel send message: " << message;
this->ircManager.sendMessage(name, message);
// Do last message processing
QString parsedMessage = this->emoteManager.replaceShortCodes(message);
this->ircManager.sendMessage(name, parsedMessage);
}
} // namespace chatterino

View file

@ -24,6 +24,7 @@ namespace chatterino {
EmoteManager::EmoteManager(WindowManager &_windowManager, Resources &_resources)
: windowManager(_windowManager)
, resources(_resources)
, findShortCodesRegex(":([-+\\w]+):")
{
// Note: Do not use this->resources in ctor
pajlada::Settings::Setting<std::string> roomID(
@ -294,6 +295,36 @@ void EmoteManager::parseEmojis(std::vector<std::tuple<EmoteData, QString>> &pars
}
}
QString EmoteManager::replaceShortCodes(const QString &text)
{
QString ret(text);
auto it = this->findShortCodesRegex.globalMatch(text);
int32_t offset = 0;
while (it.hasNext()) {
auto match = it.next();
auto capturedString = match.captured();
QString matchString = capturedString.toLower().mid(1, capturedString.size() - 2);
auto emojiIt = this->emojiShortCodeToEmoji.constFind(matchString);
if (emojiIt == this->emojiShortCodeToEmoji.constEnd()) {
continue;
}
auto emojiData = emojiIt.value();
ret.replace(offset + match.capturedStart(), match.capturedLength(), emojiData.value);
offset += emojiData.value.size() - match.capturedLength();
}
return ret;
}
void EmoteManager::refreshTwitchEmotes(const std::string &roomID)
{
std::string oauthClient =

View file

@ -10,6 +10,7 @@
#include <QMap>
#include <QMutex>
#include <QRegularExpression>
#include <QString>
#include <QTimer>
#include <boost/signals2.hpp>
@ -75,6 +76,8 @@ private:
Resources &resources;
/// Emojis
QRegularExpression findShortCodesRegex;
// shortCodeToEmoji maps strings like "sunglasses" to its emoji
QMap<QString, EmojiData> emojiShortCodeToEmoji;
@ -89,6 +92,8 @@ private:
public:
void parseEmojis(std::vector<std::tuple<EmoteData, QString>> &parsedWords, const QString &text);
QString replaceShortCodes(const QString &text);
/// Twitch emotes
void refreshTwitchEmotes(const std::string &roomID);