expire non-recent chatters

This commit is contained in:
Rasmus Karlsson
2018-03-30 13:50:43 +02:00
committed by fourtf
parent e34cbd741c
commit 45841953da
4 changed files with 91 additions and 24 deletions
+59 -22
View File
@@ -4,6 +4,8 @@
#include <QAbstractListModel>
#include <chrono>
#include <mutex>
#include <set>
#include <string>
@@ -12,9 +14,6 @@ namespace chatterino {
class CompletionModel : public QAbstractListModel
{
struct TaggedString {
QString str;
// Type will help decide the lifetime of the tagged strings
enum Type {
Username,
@@ -26,7 +25,38 @@ class CompletionModel : public QAbstractListModel
TwitchGlobalEmote,
TwitchSubscriberEmote,
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
{
@@ -40,45 +70,49 @@ class CompletionModel : public QAbstractListModel
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return this->str > that.str;
} else {
return k < 0;
}
} else {
return true;
}
} else {
if (that.IsEmote()) {
return false;
} else {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) {
return false;
} else {
return k < 0;
}
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;
}
};
public:
CompletionModel(const QString &_channelName);
virtual int columnCount(const QModelIndex &) const override
int columnCount(const QModelIndex &) const override
{
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
auto it = this->emotes.begin();
std::advance(it, index.row());
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();
}
@@ -88,12 +122,15 @@ public:
void addUser(const QString &str);
void ClearExpiredStrings();
private:
TaggedString createUser(const QString &str)
{
return TaggedString{str, TaggedString::Type::Username};
}
mutable std::mutex emotesMutex;
std::set<TaggedString> emotes;
QString channelName;