expire non-recent chatters

This commit is contained in:
Rasmus Karlsson 2018-03-30 13:50:43 +02:00
parent d4f37f786b
commit 89d7b7db87
4 changed files with 91 additions and 24 deletions

View file

@ -22,11 +22,19 @@ Channel::Channel(const QString &_name)
: name(_name) : name(_name)
, completionModel(this->name) , completionModel(this->name)
{ {
this->clearCompletionModelTimer = new QTimer;
QObject::connect(this->clearCompletionModelTimer, &QTimer::timeout, [this]() {
this->completionModel.ClearExpiredStrings(); //
});
this->clearCompletionModelTimer->start(60 * 1000);
} }
Channel::~Channel() Channel::~Channel()
{ {
this->destroyed.invoke(); this->destroyed.invoke();
this->clearCompletionModelTimer->stop();
this->clearCompletionModelTimer->deleteLater();
} }
bool Channel::isEmpty() const bool Channel::isEmpty() const

View file

@ -7,6 +7,7 @@
#include "util/concurrentmap.hpp" #include "util/concurrentmap.hpp"
#include <QString> #include <QString>
#include <QTimer>
#include <boost/signals2.hpp> #include <boost/signals2.hpp>
#include <memory> #include <memory>
@ -18,6 +19,8 @@ struct Message;
class Channel : public std::enable_shared_from_this<Channel> class Channel : public std::enable_shared_from_this<Channel>
{ {
QTimer *clearCompletionModelTimer;
public: public:
explicit Channel(const QString &_name); explicit Channel(const QString &_name);
virtual ~Channel(); virtual ~Channel();

View file

@ -81,12 +81,13 @@ void CompletionModel::refresh()
void CompletionModel::addString(const std::string &str, TaggedString::Type type) void CompletionModel::addString(const std::string &str, TaggedString::Type type)
{ {
// Always add a space at the end of completions this->addString(qS(str), type);
this->emotes.insert({qS(str + " "), type});
} }
void CompletionModel::addString(const QString &str, TaggedString::Type type) void CompletionModel::addString(const QString &str, TaggedString::Type type)
{ {
std::lock_guard<std::mutex> lock(this->emotesMutex);
// Always add a space at the end of completions // Always add a space at the end of completions
this->emotes.insert({str + " ", type}); this->emotes.insert({str + " ", type});
} }
@ -108,4 +109,22 @@ void CompletionModel::addUser(const QString &str)
} }
} }
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;
}
}
}
} // namespace chatterino } // namespace chatterino

View file

@ -4,6 +4,8 @@
#include <QAbstractListModel> #include <QAbstractListModel>
#include <chrono>
#include <mutex>
#include <set> #include <set>
#include <string> #include <string>
@ -12,9 +14,6 @@ namespace chatterino {
class CompletionModel : public QAbstractListModel class CompletionModel : public QAbstractListModel
{ {
struct TaggedString { struct TaggedString {
QString str;
// Type will help decide the lifetime of the tagged strings
enum Type { enum Type {
Username, Username,
@ -26,7 +25,38 @@ class CompletionModel : public QAbstractListModel
TwitchGlobalEmote, TwitchGlobalEmote,
TwitchSubscriberEmote, TwitchSubscriberEmote,
Emoji, Emoji,
} type; };
TaggedString(const QString &_str, Type _type)
: str(_str)
, type(_type)
, timeAdded(std::chrono::steady_clock::now())
{
}
QString str;
// Type will help decide the lifetime of the tagged strings
Type type;
std::chrono::steady_clock::time_point timeAdded;
bool HasExpired(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 IsEmote() const bool IsEmote() const
{ {
@ -40,45 +70,49 @@ class CompletionModel : public QAbstractListModel
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive); int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) { if (k == 0) {
return this->str > that.str; return this->str > that.str;
} else { }
return k < 0; return k < 0;
} }
} else {
return true; return true;
} }
} else {
if (that.IsEmote()) { if (that.IsEmote()) {
return false; return false;
} else { }
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive); int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) { if (k == 0) {
return false; return false;
} else { }
return k < 0; return k < 0;
} }
}
}
}
}; };
public: public:
CompletionModel(const QString &_channelName); CompletionModel(const QString &_channelName);
virtual int columnCount(const QModelIndex &) const override int columnCount(const QModelIndex &) const override
{ {
return 1; return 1;
} }
virtual QVariant data(const QModelIndex &index, int) const override QVariant data(const QModelIndex &index, int) const override
{ {
std::lock_guard<std::mutex> lock(this->emotesMutex);
// TODO: Implement more safely // TODO: Implement more safely
auto it = this->emotes.begin(); auto it = this->emotes.begin();
std::advance(it, index.row()); std::advance(it, index.row());
return QVariant(it->str); return QVariant(it->str);
} }
virtual int rowCount(const QModelIndex &) const override int rowCount(const QModelIndex &) const override
{ {
std::lock_guard<std::mutex> lock(this->emotesMutex);
return this->emotes.size(); return this->emotes.size();
} }
@ -88,12 +122,15 @@ public:
void addUser(const QString &str); void addUser(const QString &str);
void ClearExpiredStrings();
private: private:
TaggedString createUser(const QString &str) TaggedString createUser(const QString &str)
{ {
return TaggedString{str, TaggedString::Type::Username}; return TaggedString{str, TaggedString::Type::Username};
} }
mutable std::mutex emotesMutex;
std::set<TaggedString> emotes; std::set<TaggedString> emotes;
QString channelName; QString channelName;