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-08-13 13:54:39 +02:00
|
|
|
#include "common/UsernameSet.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "controllers/accounts/AccountController.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "controllers/commands/CommandController.hpp"
|
2018-08-13 13:54:39 +02:00
|
|
|
#include "debug/Benchmark.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "debug/Log.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "providers/twitch/TwitchChannel.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "providers/twitch/TwitchServer.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();
|
|
|
|
}
|
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
void CompletionModel::refresh(const QString &prefix)
|
2017-12-31 00:50:07 +01:00
|
|
|
{
|
2018-08-13 13:54:39 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(this->emotesMutex_);
|
|
|
|
this->emotes_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix.length() < 2) return;
|
|
|
|
|
|
|
|
BenchmarkGuard guard("CompletionModel::refresh");
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
auto addString = [&](const QString &str, TaggedString::Type type) {
|
|
|
|
if (str.startsWith(prefix)) this->emotes_.emplace(str + " ", type);
|
|
|
|
};
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
auto _channel = getApp()->twitch2->getChannelOrEmpty(this->channelName_);
|
|
|
|
|
|
|
|
if (auto channel = dynamic_cast<TwitchChannel *>(_channel.get())) {
|
|
|
|
// account emotes
|
|
|
|
if (auto account = getApp()->accounts->twitch.getCurrent()) {
|
|
|
|
for (const auto &emote : account->accessEmotes()->allEmoteNames) {
|
|
|
|
// XXX: No way to discern between a twitch global emote and sub
|
|
|
|
// emote right now
|
|
|
|
addString(emote.string, TaggedString::Type::TwitchGlobalEmote);
|
|
|
|
}
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
// Usernames
|
|
|
|
if (prefix.length() >= UsernameSet::PrefixLength) {
|
|
|
|
auto usernames = channel->accessChatters();
|
|
|
|
|
|
|
|
for (const auto &name : usernames->subrange(Prefix(prefix))) {
|
|
|
|
addString(name, TaggedString::Type::Username);
|
|
|
|
addString("@" + name, TaggedString::Type::Username);
|
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
// Bttv Global
|
|
|
|
for (auto &emote : *channel->globalBttv().emotes()) {
|
|
|
|
addString(emote.first.string, TaggedString::Type::BTTVChannelEmote);
|
|
|
|
}
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
// Ffz Global
|
|
|
|
for (auto &emote : *channel->globalFfz().emotes()) {
|
|
|
|
addString(emote.first.string, TaggedString::Type::FFZChannelEmote);
|
|
|
|
}
|
2018-06-24 11:24:21 +02:00
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
// Bttv Channel
|
|
|
|
for (auto &emote : *channel->bttvEmotes()) {
|
|
|
|
addString(emote.first.string, TaggedString::Type::BTTVGlobalEmote);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ffz Channel
|
|
|
|
for (auto &emote : *channel->ffzEmotes()) {
|
|
|
|
addString(emote.first.string, TaggedString::Type::BTTVGlobalEmote);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emojis
|
|
|
|
if (prefix.startsWith(":")) {
|
|
|
|
const auto &emojiShortCodes = getApp()->emotes->emojis.shortCodes;
|
|
|
|
for (auto &m : emojiShortCodes) {
|
|
|
|
addString(":" + m + ":", TaggedString::Type::Emoji);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commands
|
|
|
|
for (auto &command : getApp()->commands->items.getVector()) {
|
|
|
|
addString(command.name, TaggedString::Command);
|
|
|
|
}
|
2018-06-24 13:56:56 +02:00
|
|
|
|
2018-08-13 13:54:39 +02:00
|
|
|
for (auto &command :
|
|
|
|
getApp()->commands->getDefaultTwitchCommandList()) {
|
|
|
|
addString(command, TaggedString::Command);
|
|
|
|
}
|
|
|
|
}
|
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-08-13 13:54:39 +02:00
|
|
|
auto p = this->emotes_.insert(ts);
|
2018-05-14 17:28:00 +02:00
|
|
|
if (!p.second) {
|
2018-08-06 21:17:03 +02:00
|
|
|
// No inseration was made, figure out if we need to replace the
|
|
|
|
// username.
|
2018-05-14 17:28:00 +02:00
|
|
|
|
|
|
|
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
|