mirror-chatterino2/src/util/completionmodel.cpp

138 lines
4.5 KiB
C++
Raw Normal View History

#include "util/completionmodel.hpp"
2017-12-31 00:50:07 +01:00
#include "application.hpp"
2017-12-31 00:50:07 +01:00
#include "common.hpp"
#include "debug/log.hpp"
#include "singletons/emotemanager.hpp"
#include <QtAlgorithms>
#include <utility>
2017-12-31 00:50:07 +01:00
namespace chatterino {
2017-12-31 00:50:07 +01:00
CompletionModel::CompletionModel(const QString &_channelName)
: channelName(_channelName)
{
}
void CompletionModel::refresh()
{
debug::Log("[CompletionModel:{}] Refreshing...]", this->channelName);
2017-12-31 00:50:07 +01:00
auto app = getApp();
2017-12-31 00:50:07 +01:00
// User-specific: Twitch Emotes
// TODO: Fix this so it properly updates with the proper api. oauth token needs proper scope
for (const auto &m : app->emotes->twitchAccountEmotes) {
2017-12-31 00:50:07 +01:00
for (const auto &emoteName : m.second.emoteCodes) {
// XXX: No way to discern between a twitch global emote and sub emote right now
this->addString(emoteName, TaggedString::Type::TwitchGlobalEmote);
2017-12-31 00:50:07 +01:00
}
}
// Global: BTTV Global Emotes
std::vector<std::string> &bttvGlobalEmoteCodes = app->emotes->bttvGlobalEmoteCodes;
2017-12-31 00:50:07 +01:00
for (const auto &m : bttvGlobalEmoteCodes) {
this->addString(m, TaggedString::Type::BTTVGlobalEmote);
2017-12-31 00:50:07 +01:00
}
// Global: FFZ Global Emotes
std::vector<std::string> &ffzGlobalEmoteCodes = app->emotes->ffzGlobalEmoteCodes;
2017-12-31 00:50:07 +01:00
for (const auto &m : ffzGlobalEmoteCodes) {
this->addString(m, TaggedString::Type::FFZGlobalEmote);
2017-12-31 00:50:07 +01:00
}
// Channel-specific: BTTV Channel Emotes
std::vector<std::string> &bttvChannelEmoteCodes =
app->emotes->bttvChannelEmoteCodes[this->channelName.toStdString()];
2017-12-31 00:50:07 +01:00
for (const auto &m : bttvChannelEmoteCodes) {
this->addString(m, TaggedString::Type::BTTVChannelEmote);
2017-12-31 00:50:07 +01:00
}
// Channel-specific: FFZ Channel Emotes
std::vector<std::string> &ffzChannelEmoteCodes =
app->emotes->ffzChannelEmoteCodes[this->channelName.toStdString()];
2017-12-31 00:50:07 +01:00
for (const auto &m : ffzChannelEmoteCodes) {
this->addString(m, TaggedString::Type::FFZChannelEmote);
2017-12-31 00:50:07 +01:00
}
// Global: Emojis
const auto &emojiShortCodes = app->emotes->emojiShortCodes;
2017-12-31 00:50:07 +01:00
for (const auto &m : emojiShortCodes) {
this->addString(":" + m + ":", TaggedString::Type::Emoji);
2017-12-31 00:50:07 +01:00
}
// Channel-specific: Usernames
2018-02-05 15:11:50 +01:00
// fourtf: only works with twitch chat
// auto c = singletons::ChannelManager::getInstance().getTwitchChannel(this->channelName);
// auto usernames = c->getUsernamesForCompletions();
// for (const auto &name : usernames) {
// assert(!name.displayName.isEmpty());
// this->addString(name.displayName);
// this->addString('@' + name.displayName);
2017-12-31 00:50:07 +01:00
2018-02-05 15:11:50 +01:00
// if (!name.localizedName.isEmpty()) {
// this->addString(name.localizedName);
// this->addString('@' + name.localizedName);
// }
// }
2017-12-31 00:50:07 +01:00
}
void CompletionModel::addString(const std::string &str, TaggedString::Type type)
2017-12-31 00:50:07 +01:00
{
2018-03-30 13:50:43 +02:00
this->addString(qS(str), type);
2017-12-31 00:50:07 +01:00
}
void CompletionModel::addString(const QString &str, TaggedString::Type type)
2017-12-31 00:50:07 +01:00
{
2018-03-30 13:50:43 +02:00
std::lock_guard<std::mutex> lock(this->emotesMutex);
2017-12-31 00:50:07 +01:00
// Always add a space at the end of completions
this->emotes.insert({str + " ", type});
}
2018-05-14 17:28:00 +02:00
void CompletionModel::addUser(const QString &username)
{
2018-05-14 17:28:00 +02:00
auto add = [this](const QString &str) {
auto ts = this->createUser(str + " ");
// Always add a space at the end of completions
std::pair<std::set<TaggedString>::iterator, bool> p = this->emotes.insert(ts);
if (!p.second) {
// No inseration was made, figure out if we need to replace the username.
if (p.first->str > ts.str) {
// Replace lowercase version of name with mixed-case version
this->emotes.erase(p.first);
auto result2 = this->emotes.insert(ts);
assert(result2.second);
} else {
p.first->timeAdded = std::chrono::steady_clock::now();
}
}
2018-05-14 17:28:00 +02:00
};
add(username);
add("@" + username);
2017-12-31 00:50:07 +01:00
}
2018-03-30 13:50:43 +02:00
void CompletionModel::ClearExpiredStrings()
{
std::lock_guard<std::mutex> lock(this->emotesMutex);
auto now = std::chrono::steady_clock::now();
for (auto it = this->emotes.begin(); it != this->emotes.end();) {
const auto &taggedString = *it;
if (taggedString.HasExpired(now)) {
// debug::Log("String {} expired", taggedString.str);
it = this->emotes.erase(it);
} else {
++it;
}
}
}
2018-02-05 15:11:50 +01:00
} // namespace chatterino