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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2018-07-06 18:10:21 +02:00
|
|
|
CompletionModel::CompletionModel(const QString &channelName)
|
|
|
|
: 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
|
|
|
|
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 =
|
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) {
|
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 =
|
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) {
|
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
|
|
|
}
|
|
|
|
|
2018-06-24 11:24:21 +02:00
|
|
|
// Commands
|
|
|
|
for (auto &command : app->commands->items.getVector()) {
|
|
|
|
this->addString(command.name, TaggedString::Command);
|
|
|
|
}
|
|
|
|
|
2018-06-24 13:56:56 +02:00
|
|
|
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
|
2018-06-28 19:38:57 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
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-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-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
|
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-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-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
|