Remove "CompletionManager". Completion models are now stored in Channel

Chatters list is now updated every 5 minutes
This commit is contained in:
Rasmus Karlsson 2018-03-24 12:02:07 +01:00
parent ad12a818b2
commit d9bd39e8a4
12 changed files with 65 additions and 95 deletions

View file

@ -114,7 +114,6 @@ SOURCES += \
src/singletons/accountmanager.cpp \ src/singletons/accountmanager.cpp \
src/singletons/channelmanager.cpp \ src/singletons/channelmanager.cpp \
src/singletons/commandmanager.cpp \ src/singletons/commandmanager.cpp \
src/singletons/completionmanager.cpp \
src/singletons/emotemanager.cpp \ src/singletons/emotemanager.cpp \
src/singletons/fontmanager.cpp \ src/singletons/fontmanager.cpp \
src/singletons/helper/completionmodel.cpp \ src/singletons/helper/completionmodel.cpp \
@ -208,7 +207,6 @@ HEADERS += \
src/singletons/accountmanager.hpp \ src/singletons/accountmanager.hpp \
src/singletons/channelmanager.hpp \ src/singletons/channelmanager.hpp \
src/singletons/commandmanager.hpp \ src/singletons/commandmanager.hpp \
src/singletons/completionmanager.hpp \
src/singletons/emotemanager.hpp \ src/singletons/emotemanager.hpp \
src/singletons/fontmanager.hpp \ src/singletons/fontmanager.hpp \
src/singletons/helper/chatterinosetting.hpp \ src/singletons/helper/chatterinosetting.hpp \

View file

@ -20,6 +20,7 @@ namespace chatterino {
Channel::Channel(const QString &_name) Channel::Channel(const QString &_name)
: name(_name) : name(_name)
, completionModel(this->name)
{ {
} }

View file

@ -3,6 +3,7 @@
#include "messages/image.hpp" #include "messages/image.hpp"
#include "messages/limitedqueue.hpp" #include "messages/limitedqueue.hpp"
#include "messages/message.hpp" #include "messages/message.hpp"
#include "singletons/helper/completionmodel.hpp"
#include "util/concurrentmap.hpp" #include "util/concurrentmap.hpp"
#include <QMap> #include <QMap>
@ -64,6 +65,8 @@ public:
static std::shared_ptr<Channel> getEmpty(); static std::shared_ptr<Channel> getEmpty();
singletons::CompletionModel completionModel;
protected: protected:
virtual void onConnected(); virtual void onConnected();

View file

@ -16,6 +16,7 @@
namespace chatterino { namespace chatterino {
namespace providers { namespace providers {
namespace twitch { namespace twitch {
TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection *_readConnection) TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection *_readConnection)
: Channel(channelName) : Channel(channelName)
, bttvChannelEmotes(new util::EmoteMap) , bttvChannelEmotes(new util::EmoteMap)
@ -47,6 +48,27 @@ TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection
this->messageSuffix.append(' '); this->messageSuffix.append(' ');
this->messageSuffix.append(QChar(0x206D)); this->messageSuffix.append(QChar(0x206D));
static QStringList jsonLabels = {"moderators", "staff", "admins", "global_mods", "viewers"};
auto refreshChatters = [=](QJsonObject obj) {
QJsonObject chattersObj = obj.value("chatters").toObject();
for (int i = 0; i < jsonLabels.size(); i++) {
foreach (const QJsonValue &v, chattersObj.value(jsonLabels.at(i)).toArray()) {
this->completionModel.addUser(v.toString());
}
}
};
auto doRefreshChatters = [=]() {
util::twitch::get("https://tmi.twitch.tv/group/user/" + this->name + "/chatters",
QThread::currentThread(), refreshChatters);
};
doRefreshChatters();
this->chattersListTimer = new QTimer;
QObject::connect(this->chattersListTimer, &QTimer::timeout, doRefreshChatters);
this->chattersListTimer->start(5 * 60 * 1000);
} }
TwitchChannel::~TwitchChannel() TwitchChannel::~TwitchChannel()
@ -55,6 +77,9 @@ TwitchChannel::~TwitchChannel()
this->liveStatusTimer->stop(); this->liveStatusTimer->stop();
this->liveStatusTimer->deleteLater(); this->liveStatusTimer->deleteLater();
this->chattersListTimer->stop();
this->chattersListTimer->deleteLater();
} }
bool TwitchChannel::isEmpty() const bool TwitchChannel::isEmpty() const

View file

@ -11,10 +11,13 @@
namespace chatterino { namespace chatterino {
namespace providers { namespace providers {
namespace twitch { namespace twitch {
class TwitchServer; class TwitchServer;
class TwitchChannel final : public Channel class TwitchChannel final : public Channel
{ {
QTimer *liveStatusTimer; QTimer *liveStatusTimer;
QTimer *chattersListTimer;
public: public:
~TwitchChannel(); ~TwitchChannel();
@ -69,6 +72,7 @@ private:
friend class TwitchServer; friend class TwitchServer;
}; };
} // namespace twitch } // namespace twitch
} // namespace providers } // namespace providers
} // namespace chatterino } // namespace chatterino

View file

@ -5,7 +5,6 @@
#include "providers/twitch/twitchmessagebuilder.hpp" #include "providers/twitch/twitchmessagebuilder.hpp"
#include "singletons/accountmanager.hpp" #include "singletons/accountmanager.hpp"
#include "util/posttothread.hpp" #include "util/posttothread.hpp"
#include "singletons/completionmanager.hpp"
#include <cassert> #include <cassert>
@ -84,8 +83,8 @@ void TwitchServer::privateMessageReceived(IrcPrivateMessage *message)
TwitchMessageBuilder builder(chan.get(), message, args); TwitchMessageBuilder builder(chan.get(), message, args);
auto cm = singletons::CompletionManager::getInstance().createModel(chan->name); // XXX: Thread-safety
cm->addUser(message->nick()); chan->completionModel.addUser(message->nick());
if (!builder.isIgnored()) { if (!builder.isIgnored()) {
messages::MessagePtr _message = builder.build(); messages::MessagePtr _message = builder.build();

View file

@ -1,30 +0,0 @@
#include "singletons/completionmanager.hpp"
#include "common.hpp"
#include "debug/log.hpp"
#include "singletons/channelmanager.hpp"
#include "singletons/emotemanager.hpp"
namespace chatterino {
namespace singletons {
CompletionManager &CompletionManager::getInstance()
{
static CompletionManager instance;
return instance;
}
CompletionModel *CompletionManager::createModel(const QString &channelName)
{
auto it = this->models.find(channelName);
if (it != this->models.end()) {
return it->second;
}
CompletionModel *ret = new CompletionModel(channelName);
this->models[channelName] = ret;
return ret;
}
} // namespace singletons
} // namespace chatterino

View file

@ -1,26 +0,0 @@
#pragma once
#include <QAbstractListModel>
#include <QVector>
#include <map>
#include <string>
#include "helper/completionmodel.hpp"
namespace chatterino {
namespace singletons {
class CompletionManager
{
CompletionManager() = default;
public:
static CompletionManager &getInstance();
CompletionModel *createModel(const QString &channelName);
private:
std::map<QString, CompletionModel *> models;
};
} // namespace singletons
} // namespace chatterino

View file

@ -3,13 +3,13 @@
#include "common.hpp" #include "common.hpp"
#include "debug/log.hpp" #include "debug/log.hpp"
#include "singletons/channelmanager.hpp" #include "singletons/channelmanager.hpp"
#include "singletons/completionmanager.hpp"
#include "singletons/emotemanager.hpp" #include "singletons/emotemanager.hpp"
#include <QtAlgorithms> #include <QtAlgorithms>
namespace chatterino { namespace chatterino {
namespace singletons { namespace singletons {
CompletionModel::CompletionModel(const QString &_channelName) CompletionModel::CompletionModel(const QString &_channelName)
: channelName(_channelName) : channelName(_channelName)
{ {
@ -94,5 +94,6 @@ void CompletionModel::addUser(const QString &str)
// Always add a space at the end of completions // Always add a space at the end of completions
this->emotes.insert(this->createUser(str + " ")); this->emotes.insert(this->createUser(str + " "));
} }
} // namespace singletons } // namespace singletons
} // namespace chatterino } // namespace chatterino

View file

@ -1,15 +1,16 @@
#pragma once #pragma once
#include <QAbstractListModel>
#include <set>
#include <map>
#include <string>
#include "common.hpp" #include "common.hpp"
#include <QAbstractListModel>
#include <map>
#include <set>
#include <string>
namespace chatterino { namespace chatterino {
namespace singletons { namespace singletons {
class CompletionModel : public QAbstractListModel class CompletionModel : public QAbstractListModel
{ {
public: public:
@ -50,17 +51,23 @@ private:
if (this->isEmote) { if (this->isEmote) {
if (that.isEmote) { if (that.isEmote) {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive); int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) return this->str > that.str; if (k == 0) {
else return k < 0; return this->str > that.str;
} else {
return k < 0;
}
} else } else
return true; return true;
} else { } else {
if (that.isEmote) if (that.isEmote)
return false; return false;
else { else {
int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);; int k = QString::compare(this->str, that.str, Qt::CaseInsensitive);
if (k == 0) return this->str > that.str; if (k == 0) {
else return k < 0; return this->str > that.str;
} else {
return k < 0;
}
} }
} }
} }
@ -81,5 +88,6 @@ private:
QString channelName; QString channelName;
}; };
} // namespace singletons } // namespace singletons
} // namespace chatterino } // namespace chatterino

View file

@ -1,6 +1,6 @@
#include "widgets/helper/resizingtextedit.hpp" #include "widgets/helper/resizingtextedit.hpp"
#include "common.hpp" #include "common.hpp"
#include "singletons/completionmanager.hpp" #include "singletons/helper/completionmodel.hpp"
ResizingTextEdit::ResizingTextEdit() ResizingTextEdit::ResizingTextEdit()
{ {

View file

@ -1,14 +1,13 @@
#include "widgets/helper/splitinput.hpp" #include "widgets/helper/splitinput.hpp"
#include "singletons/commandmanager.hpp" #include "singletons/commandmanager.hpp"
#include "singletons/completionmanager.hpp"
#include "singletons/ircmanager.hpp" #include "singletons/ircmanager.hpp"
#include "singletons/settingsmanager.hpp" #include "singletons/settingsmanager.hpp"
#include "singletons/thememanager.hpp" #include "singletons/thememanager.hpp"
#include "util/layoutcreator.hpp" #include "util/layoutcreator.hpp"
#include "util/urlfetch.hpp"
#include "widgets/notebook.hpp" #include "widgets/notebook.hpp"
#include "widgets/split.hpp" #include "widgets/split.hpp"
#include "widgets/splitcontainer.hpp" #include "widgets/splitcontainer.hpp"
#include "util/urlfetch.hpp"
#include <QCompleter> #include <QCompleter>
#include <QPainter> #include <QPainter>
@ -22,26 +21,14 @@ SplitInput::SplitInput(Split *_chatWidget)
{ {
this->initLayout(); this->initLayout();
// auto completion auto completer = new QCompleter(&this->chatWidget->getChannel()->completionModel);
auto completer = new QCompleter(
singletons::CompletionManager::getInstance().createModel(this->chatWidget->channelName));
auto cc = singletons::CompletionManager::getInstance().createModel(this->chatWidget->channelName);
cc->refresh();
static QStringList jsonLabels = {"moderators", "staff", "admins", "global_mods", "viewers"};
util::twitch::get("https://tmi.twitch.tv/group/user/" + this->chatWidget->channelName + "/chatters", this,
[cc](QJsonObject obj) {
QJsonObject chattersObj = obj.value("chatters").toObject();
for (int i = 0; i < jsonLabels.size(); i++) {
foreach (const QJsonValue &v,
chattersObj.value(jsonLabels.at(i)).toArray())
cc->addUser(v.toString());
}
});
this->ui.textEdit->setCompleter(completer); this->ui.textEdit->setCompleter(completer);
this->chatWidget->channelChanged.connect([this] {
auto completer = new QCompleter(&this->chatWidget->getChannel()->completionModel);
this->ui.textEdit->setCompleter(completer);
});
// misc // misc
this->installKeyPressedEvent(); this->installKeyPressedEvent();
this->themeRefreshEvent(); this->themeRefreshEvent();