add replaced emotes into twitchEmotes

This commit is contained in:
hemirt 2018-08-19 00:25:58 +02:00
parent a63454f00d
commit e8e059f847
2 changed files with 71 additions and 8 deletions

View file

@ -42,10 +42,15 @@ public:
QRegularExpression::UseUnicodePropertiesOption); QRegularExpression::UseUnicodePropertiesOption);
} }
/*const auto &accvec = getApp()->accounts->twitch.accounts.getVector(); const auto &accvec = getApp()->accounts->twitch.accounts.getVector();
for (const auto &acc : accvec) { for (const auto &acc : accvec) {
// const auto &accemotes = *acc->accessEmotes();
}*/ for (const auto &emote : accemotes.emotes) {
if (this->replace_.contains(emote.first.string, Qt::CaseSensitive)) {
this->emotes_.emplace(emote.first, emote.second);
}
}
}
} }
const QString &getPattern() const const QString &getPattern() const
@ -95,6 +100,16 @@ public:
return this->isCaseSensitive_ ? Qt::CaseSensitive : Qt::CaseInsensitive; return this->isCaseSensitive_ ? Qt::CaseSensitive : Qt::CaseInsensitive;
} }
const std::unordered_map<EmoteName, EmotePtr> &getEmotes() const
{
return this->emotes_;
}
bool containsEmote() const
{
return !this->emotes_.empty();
}
private: private:
QString pattern_; QString pattern_;
bool isRegex_; bool isRegex_;
@ -102,7 +117,7 @@ private:
bool isBlock_; bool isBlock_;
QString replace_; QString replace_;
bool isCaseSensitive_; bool isCaseSensitive_;
std::map<QString, EmotePtr> emotes; std::unordered_map<EmoteName, EmotePtr> emotes_;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -167,7 +167,11 @@ MessagePtr TwitchMessageBuilder::build()
twitchEmotes.begin(), twitchEmotes.end(), [&pos, &len](const auto &item) { twitchEmotes.begin(), twitchEmotes.end(), [&pos, &len](const auto &item) {
return ((std::get<0>(item) >= pos) && std::get<0>(item) < (pos + len)); return ((std::get<0>(item) >= pos) && std::get<0>(item) < (pos + len));
}); });
for (; it != twitchEmotes.end(); ++it) {
if (std::get<1>(*it) == nullptr) {
log("remem nullptr {}", std::get<2>(*it).string);
}
}
std::vector<std::tuple<int, EmotePtr, EmoteName>> v(it, twitchEmotes.end()); std::vector<std::tuple<int, EmotePtr, EmoteName>> v(it, twitchEmotes.end());
twitchEmotes.erase(it, twitchEmotes.end()); twitchEmotes.erase(it, twitchEmotes.end());
return v; return v;
@ -182,6 +186,27 @@ MessagePtr TwitchMessageBuilder::build()
} }
}; };
auto addReplEmotes = [&twitchEmotes](const IgnorePhrase &phrase, const QString &midrepl,
int startIndex) mutable {
if (!phrase.containsEmote()) {
return;
}
QStringList words = midrepl.split(' ');
int pos = 0;
for (const auto &word : words) {
for (const auto &emote : phrase.getEmotes()) {
if (word == emote.first.string) {
if (emote.second == nullptr) {
log("emote null {}", emote.first.string);
}
twitchEmotes.push_back(std::tuple<int, EmotePtr, EmoteName>{
startIndex + pos, emote.second, emote.first});
}
}
pos += word.length() + 1;
}
};
for (const auto &phrase : phrases) { for (const auto &phrase : phrases) {
if (phrase.isBlock()) { if (phrase.isBlock()) {
continue; continue;
@ -204,6 +229,9 @@ MessagePtr TwitchMessageBuilder::build()
// mb in IgnoredPhrase ?? // mb in IgnoredPhrase ??
for (auto &tup : vret) { for (auto &tup : vret) {
if (std::get<1>(tup) == nullptr) {
log("v nullptr {}", std::get<2>(tup).string);
}
int index = 0; int index = 0;
const auto &emote = std::get<2>(tup); const auto &emote = std::get<2>(tup);
while ((index = mid.indexOf(emote.string, index)) != -1) { while ((index = mid.indexOf(emote.string, index)) != -1) {
@ -212,6 +240,9 @@ MessagePtr TwitchMessageBuilder::build()
twitchEmotes.push_back(tup); twitchEmotes.push_back(tup);
} }
} }
addReplEmotes(phrase, mid, from);
this->originalMessage_.replace(from, len, mid); this->originalMessage_.replace(from, len, mid);
int midsize = mid.size(); int midsize = mid.size();
from += midsize; from += midsize;
@ -234,6 +265,9 @@ MessagePtr TwitchMessageBuilder::build()
// mb in IgnoredPhrase ?? // mb in IgnoredPhrase ??
for (auto &tup : vret) { for (auto &tup : vret) {
if (std::get<1>(tup) == nullptr) {
log("v nullptr {}", std::get<2>(tup).string);
}
int index = 0; int index = 0;
const auto &emote = std::get<2>(tup); const auto &emote = std::get<2>(tup);
while ((index = replace.indexOf(emote.string, index)) != -1) { while ((index = replace.indexOf(emote.string, index)) != -1) {
@ -242,6 +276,9 @@ MessagePtr TwitchMessageBuilder::build()
twitchEmotes.push_back(tup); twitchEmotes.push_back(tup);
} }
} }
addReplEmotes(phrase, replace, from);
this->originalMessage_.replace(from, len, replace); this->originalMessage_.replace(from, len, replace);
int replacesize = replace.size(); int replacesize = replace.size();
from += replacesize; from += replacesize;
@ -252,6 +289,11 @@ MessagePtr TwitchMessageBuilder::build()
std::sort(twitchEmotes.begin(), twitchEmotes.end(), std::sort(twitchEmotes.begin(), twitchEmotes.end(),
[](const auto &a, const auto &b) { return std::get<0>(a) < std::get<0>(b); }); [](const auto &a, const auto &b) { return std::get<0>(a) < std::get<0>(b); });
twitchEmotes.erase(std::unique(twitchEmotes.begin(), twitchEmotes.end(),
[](const auto &first, const auto &second) {
return std::get<0>(first) == std::get<0>(second);
}),
twitchEmotes.end());
auto currentTwitchEmote = twitchEmotes.begin(); auto currentTwitchEmote = twitchEmotes.begin();
// words // words
@ -274,6 +316,9 @@ void TwitchMessageBuilder::addWords(
// check if it's a twitch emote twitch emote // check if it's a twitch emote twitch emote
if (currentTwitchEmote != twitchEmotes.end() && std::get<0>(*currentTwitchEmote) == i) { if (currentTwitchEmote != twitchEmotes.end() && std::get<0>(*currentTwitchEmote) == i) {
auto emoteImage = std::get<1>(*currentTwitchEmote); auto emoteImage = std::get<1>(*currentTwitchEmote);
if (emoteImage == nullptr) {
log("emoteImage nullptr {}", std::get<2>(*currentTwitchEmote).string);
}
this->emplace<EmoteElement>(emoteImage, MessageElementFlag::TwitchEmote); this->emplace<EmoteElement>(emoteImage, MessageElementFlag::TwitchEmote);
i += word.length() + 1; i += word.length() + 1;
@ -689,9 +734,12 @@ void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcMessage *ircMessa
} }
auto name = EmoteName{this->originalMessage_.mid(start, end - start + 1)}; auto name = EmoteName{this->originalMessage_.mid(start, end - start + 1)};
auto tup = std::tuple<int, EmotePtr, EmoteName>{
vec.push_back(std::tuple<int, EmotePtr, EmoteName>{ start, app->emotes->twitch.getOrCreateEmote(id, name), name};
start, app->emotes->twitch.getOrCreateEmote(id, name), name}); if (std::get<1>(tup) == nullptr) {
log("nullptr {}", std::get<2>(tup).string);
}
vec.push_back(std::move(tup));
} }
} }