2018-03-24 12:13:09 +01:00
|
|
|
#include "util/completionmodel.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-04-27 22:11:19 +02:00
|
|
|
#include "application.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "debug/log.hpp"
|
|
|
|
#include "singletons/emotemanager.hpp"
|
|
|
|
|
2018-03-24 11:12:24 +01:00
|
|
|
#include <QtAlgorithms>
|
|
|
|
|
2018-03-30 12:06:02 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
namespace chatterino {
|
2018-03-24 12:02:07 +01:00
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
CompletionModel::CompletionModel(const QString &_channelName)
|
|
|
|
: channelName(_channelName)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompletionModel::refresh()
|
|
|
|
{
|
2018-03-05 22:49:19 +01:00
|
|
|
debug::Log("[CompletionModel:{}] Refreshing...]", this->channelName);
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-04-27 22:11:19 +02: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
|
2018-06-05 17:13:29 +02:00
|
|
|
for (const auto &m : app->emotes->twitch.emotes) {
|
2017-12-31 00:50:07 +01:00
|
|
|
for (const auto &emoteName : m.second.emoteCodes) {
|
2018-03-30 12:06:02 +02:00
|
|
|
// 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
|
2018-06-07 13:09:08 +02:00
|
|
|
std::vector<QString> &bttvGlobalEmoteCodes = app->emotes->bttv.globalEmoteCodes;
|
2017-12-31 00:50:07 +01:00
|
|
|
for (const auto &m : bttvGlobalEmoteCodes) {
|
2018-03-30 12:06:02 +02:00
|
|
|
this->addString(m, TaggedString::Type::BTTVGlobalEmote);
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Global: FFZ Global Emotes
|
2018-06-07 13:09:33 +02:00
|
|
|
std::vector<QString> &ffzGlobalEmoteCodes = app->emotes->ffz.globalEmoteCodes;
|
2017-12-31 00:50:07 +01:00
|
|
|
for (const auto &m : ffzGlobalEmoteCodes) {
|
2018-03-30 12:06:02 +02:00
|
|
|
this->addString(m, TaggedString::Type::FFZGlobalEmote);
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Channel-specific: BTTV Channel Emotes
|
2018-06-07 13:09:08 +02:00
|
|
|
std::vector<QString> &bttvChannelEmoteCodes =
|
|
|
|
app->emotes->bttv.channelEmoteCodes[this->channelName];
|
2017-12-31 00:50:07 +01:00
|
|
|
for (const auto &m : bttvChannelEmoteCodes) {
|
2018-03-30 12:06:02 +02:00
|
|
|
this->addString(m, TaggedString::Type::BTTVChannelEmote);
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Channel-specific: FFZ Channel Emotes
|
2018-06-07 13:09:33 +02:00
|
|
|
std::vector<QString> &ffzChannelEmoteCodes =
|
|
|
|
app->emotes->ffz.channelEmoteCodes[this->channelName];
|
2017-12-31 00:50:07 +01:00
|
|
|
for (const auto &m : ffzChannelEmoteCodes) {
|
2018-03-30 12:06:02 +02:00
|
|
|
this->addString(m, TaggedString::Type::FFZChannelEmote);
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Global: Emojis
|
2018-06-05 18:53:49 +02:00
|
|
|
const auto &emojiShortCodes = app->emotes->emojis.shortCodes;
|
2017-12-31 00:50:07 +01:00
|
|
|
for (const auto &m : emojiShortCodes) {
|
2018-03-30 12:06:02 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-30 12:06:02 +02: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
|
2018-03-30 12:06:02 +02:00
|
|
|
this->emotes.insert({str + " ", type});
|
2018-03-24 11:12:24 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 17:28:00 +02:00
|
|
|
void CompletionModel::addUser(const QString &username)
|
2018-03-24 11:12:24 +01:00
|
|
|
{
|
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-03-30 12:06:02 +02:00
|
|
|
}
|
2018-05-14 17:28:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
add(username);
|
|
|
|
add("@" + username);
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
2018-03-24 12:02: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
|