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-02 14:23:27 +02:00
|
|
|
#include "controllers/accounts/AccountController.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "controllers/commands/CommandController.hpp"
|
|
|
|
#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();
|
|
|
|
}
|
|
|
|
|
2017-12-31 00:50:07 +01:00
|
|
|
void CompletionModel::refresh()
|
|
|
|
{
|
2018-08-11 14:20:53 +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
|
2018-08-02 14:23:27 +02:00
|
|
|
if (auto account = app->accounts->twitch.getCurrent()) {
|
|
|
|
for (const auto &emote : account->accessEmotes()->allEmoteNames) {
|
2018-08-06 21:17:03 +02:00
|
|
|
// XXX: No way to discern between a twitch global emote and sub
|
|
|
|
// emote right now
|
|
|
|
this->addString(emote.string,
|
|
|
|
TaggedString::Type::TwitchGlobalEmote);
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
// // Global: BTTV Global Emotes
|
2018-08-06 21:17:03 +02:00
|
|
|
// std::vector<QString> &bttvGlobalEmoteCodes =
|
|
|
|
// app->emotes->bttv.globalEmoteNames_; for (const auto &m :
|
|
|
|
// bttvGlobalEmoteCodes) {
|
2018-08-02 14:23:27 +02:00
|
|
|
// this->addString(m, TaggedString::Type::BTTVGlobalEmote);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Global: FFZ Global Emotes
|
2018-08-06 21:17:03 +02:00
|
|
|
// std::vector<QString> &ffzGlobalEmoteCodes =
|
|
|
|
// app->emotes->ffz.globalEmoteCodes; for (const auto &m :
|
|
|
|
// ffzGlobalEmoteCodes) {
|
2018-08-02 14:23:27 +02:00
|
|
|
// this->addString(m, TaggedString::Type::FFZGlobalEmote);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Channel emotes
|
|
|
|
if (auto channel = dynamic_cast<TwitchChannel *>(
|
2018-08-06 21:17:03 +02:00
|
|
|
getApp()
|
|
|
|
->twitch2->getChannelOrEmptyByID(this->channelName_)
|
|
|
|
.get())) {
|
2018-08-11 17:15:17 +02:00
|
|
|
auto bttv = channel->bttvEmotes();
|
2018-08-02 14:23:27 +02:00
|
|
|
// auto it = bttv->begin();
|
|
|
|
// for (const auto &emote : *bttv) {
|
|
|
|
// }
|
|
|
|
// std::vector<QString> &bttvChannelEmoteCodes =
|
|
|
|
// app->emotes->bttv.channelEmoteName_[this->channelName_];
|
|
|
|
// for (const auto &m : bttvChannelEmoteCodes) {
|
|
|
|
// this->addString(m, TaggedString::Type::BTTVChannelEmote);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Channel-specific: FFZ Channel Emotes
|
2018-08-11 17:15:17 +02:00
|
|
|
for (const auto &emote : *channel->ffzEmotes()) {
|
2018-08-06 21:17:03 +02:00
|
|
|
this->addString(emote.second->name.string,
|
|
|
|
TaggedString::Type::FFZChannelEmote);
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
// 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-08-06 21:17:03 +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-08-06 21:17:03 +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) {
|
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
|