mirror-chatterino2/src/common/CompletionModel.cpp

231 lines
6.7 KiB
C++
Raw Normal View History

2018-06-26 15:33:51 +02:00
#include "common/CompletionModel.hpp"
2017-12-31 00:50:07 +01:00
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2018-06-26 15:33:51 +02:00
#include "common/Common.hpp"
2018-06-26 14:09:39 +02:00
#include "controllers/commands/CommandController.hpp"
#include "debug/Log.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Emotes.hpp"
2017-12-31 00:50:07 +01:00
#include <QtAlgorithms>
#include <utility>
2017-12-31 00:50:07 +01:00
namespace chatterino {
2018-07-06 17:42:00 +02:00
// -- TaggedString
CompletionModel::TaggedString::TaggedString(const QString &_str, Type _type)
: str(_str)
, type(_type)
, timeAdded(std::chrono::steady_clock::now())
{
}
bool CompletionModel::TaggedString::isExpired(
const std::chrono::steady_clock::time_point &now) const
{
switch (this->type) {
case Type::Username: {
static std::chrono::minutes expirationTimer(10);
return (this->timeAdded + expirationTimer < now);
} break;
default: {
return false;
} break;
}
return false;
}
bool CompletionModel::TaggedString::isEmote() const
{
return this->type > Type::EmoteStart && this->type < Type::EmoteEnd;
}
bool CompletionModel::TaggedString::operator<(const TaggedString &that) const
{
if (this->isEmote()) {
if (that.isEmote()) {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return this->str > that.str;
}
return k < 0;
}
return true;
}
if (that.isEmote()) {
return false;
}
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return false;
}
return k < 0;
}
// -- CompletionModel
2017-12-31 00:50:07 +01:00
CompletionModel::CompletionModel(const QString &_channelName)
2018-07-06 17:42:00 +02:00
: channelName_(_channelName)
2017-12-31 00:50:07 +01:00
{
}
2018-07-06 17:42:00 +02:00
int CompletionModel::columnCount(const QModelIndex &) const
{
return 1;
}
QVariant CompletionModel::data(const QModelIndex &index, int) const
{
std::lock_guard<std::mutex> lock(this->emotesMutex_);
// TODO: Implement more safely
auto it = this->emotes_.begin();
std::advance(it, index.row());
return QVariant(it->str);
}
int CompletionModel::rowCount(const QModelIndex &) const
{
std::lock_guard<std::mutex> lock(this->emotesMutex_);
return this->emotes_.size();
}
2017-12-31 00:50:07 +01:00
void CompletionModel::refresh()
{
2018-07-06 17:42:00 +02:00
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->twitch.emotes) {
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<QString> &bttvGlobalEmoteCodes = app->emotes->bttv.globalEmoteCodes;
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<QString> &ffzGlobalEmoteCodes = app->emotes->ffz.globalEmoteCodes;
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<QString> &bttvChannelEmoteCodes =
2018-07-06 17:42:00 +02:00
app->emotes->bttv.channelEmoteCodes[this->channelName_];
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<QString> &ffzChannelEmoteCodes =
2018-07-06 17:42:00 +02:00
app->emotes->ffz.channelEmoteCodes[this->channelName_];
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
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) {
this->addString(":" + m + ":", TaggedString::Type::Emoji);
2017-12-31 00:50:07 +01:00
}
2018-06-24 11:24:21 +02:00
// Commands
for (auto &command : app->commands->items.getVector()) {
this->addString(command.name, TaggedString::Command);
}
for (auto &command : app->commands->getDefaultTwitchCommandList()) {
this->addString(command, TaggedString::Command);
}
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 = ChannelManager::getInstance().getTwitchChannel(this->channelName);
2018-02-05 15:11:50 +01:00
// 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 QString &str, TaggedString::Type type)
2017-12-31 00:50:07 +01:00
{
2018-07-06 17:42:00 +02:00
std::lock_guard<std::mutex> lock(this->emotesMutex_);
2018-03-30 13:50:43 +02:00
2017-12-31 00:50:07 +01:00
// Always add a space at the end of completions
2018-07-06 17:42:00 +02:00
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
2018-07-06 17:42:00 +02:00
std::pair<std::set<TaggedString>::iterator, bool> p = this->emotes_.insert(ts);
2018-05-14 17:28:00 +02:00
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
2018-07-06 17:42:00 +02:00
this->emotes_.erase(p.first);
auto result2 = this->emotes_.insert(ts);
2018-05-14 17:28:00 +02:00
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-07-06 17:42:00 +02:00
void CompletionModel::clearExpiredStrings()
2018-03-30 13:50:43 +02:00
{
2018-07-06 17:42:00 +02:00
std::lock_guard<std::mutex> lock(this->emotesMutex_);
2018-03-30 13:50:43 +02:00
auto now = std::chrono::steady_clock::now();
2018-07-06 17:42:00 +02:00
for (auto it = this->emotes_.begin(); it != this->emotes_.end();) {
2018-03-30 13:50:43 +02:00
const auto &taggedString = *it;
2018-07-06 17:56:11 +02:00
if (taggedString.isExpired(now)) {
2018-06-26 17:06:17 +02:00
// Log("String {} expired", taggedString.str);
2018-07-06 17:42:00 +02:00
it = this->emotes_.erase(it);
2018-03-30 13:50:43 +02:00
} else {
++it;
}
}
}
2018-07-06 17:42:00 +02:00
CompletionModel::TaggedString CompletionModel::createUser(const QString &str)
{
return TaggedString{str, TaggedString::Type::Username};
}
2018-02-05 15:11:50 +01:00
} // namespace chatterino