Change the way Twitch accounts are stored in AccountManager

This is done in a way which should simplify abstracting it to other
types of accounts if needed in the future

Remove comment about removing singletons - we're keeping them (and probably restoring some)

IrcManager now updates its "account" reference automatically through the
AccountManager.Twitch.userChanged-signal

Remove unused IrcManager getUser-method

IrcManager::beginConnecting is no longer called asynchronously. This
might want to be reverted in a more controlled asynchronous manner.

User Accounts are now stored as Shared Pointers instead of using
references/copies everywhere
This commit is contained in:
Rasmus Karlsson
2017-12-16 02:21:06 +01:00
parent a8afdf4565
commit a372bae80d
8 changed files with 187 additions and 136 deletions
+28 -22
View File
@@ -3,6 +3,7 @@
#include "asyncexec.hpp"
#include "channel.hpp"
#include "channelmanager.hpp"
#include "debug/log.hpp"
#include "emotemanager.hpp"
#include "messages/messageparseargs.hpp"
#include "twitch/twitchmessagebuilder.hpp"
@@ -31,21 +32,18 @@ IrcManager::IrcManager(ChannelManager &_channelManager, Resources &_resources,
, resources(_resources)
, emoteManager(_emoteManager)
, windowManager(_windowManager)
, account(AccountManager::getInstance().getTwitchAnon())
, currentUser("/accounts/current")
{
this->currentUser.getValueChangedSignal().connect([](const auto &newUsername) {
// TODO: Implement
qDebug() << "Current user changed, fetch new credentials and reconnect";
AccountManager::getInstance().Twitch.userChanged.connect([this]() {
this->setUser(AccountManager::getInstance().Twitch.getCurrent());
debug::Log("[IrcManager] Reconnecting to Twitch IRC as new user {}",
this->account->getUserName());
postToThread([this] { this->connect(); });
});
}
const twitch::TwitchUser &IrcManager::getUser() const
{
return this->account;
}
void IrcManager::setUser(const twitch::TwitchUser &account)
void IrcManager::setUser(std::shared_ptr<twitch::TwitchUser> account)
{
this->account = account;
}
@@ -54,11 +52,16 @@ void IrcManager::connect()
{
disconnect();
async_exec([this] { beginConnecting(); });
// XXX(pajlada): Disabled the async_exec for now, because if we happen to run the
// `beginConnecting` function in a different thread than last time, we won't be able to connect
// because we can't clean up the previous connection properly
// async_exec([this] { beginConnecting(); });
this->beginConnecting();
}
Communi::IrcConnection *IrcManager::createConnection(bool doRead)
{
assert(this->account);
Communi::IrcConnection *connection = new Communi::IrcConnection;
if (doRead) {
@@ -68,9 +71,9 @@ Communi::IrcConnection *IrcManager::createConnection(bool doRead)
&IrcManager::privateMessageReceived);
}
QString username = this->account.getUserName();
QString oauthClient = this->account.getOAuthClient();
QString oauthToken = this->account.getOAuthToken();
QString username = this->account->getUserName();
QString oauthClient = this->account->getOAuthClient();
QString oauthToken = this->account->getOAuthToken();
if (!oauthToken.startsWith("oauth:")) {
oauthToken.prepend("oauth:");
}
@@ -79,7 +82,7 @@ Communi::IrcConnection *IrcManager::createConnection(bool doRead)
connection->setNickName(username);
connection->setRealName(username);
if (!this->account.isAnon()) {
if (!this->account->isAnon()) {
connection->setPassword(oauthToken);
this->refreshIgnoredUsers(username, oauthClient, oauthToken);
@@ -310,9 +313,11 @@ bool IrcManager::isTwitchBlockedUser(QString const &username)
bool IrcManager::tryAddIgnoredUser(QString const &username, QString &errorMessage)
{
QUrl url("https://api.twitch.tv/kraken/users/" + this->account.getUserName() + "/blocks/" +
username + "?oauth_token=" + this->account.getOAuthToken() +
"&client_id=" + this->account.getOAuthClient());
assert(this->account);
QUrl url("https://api.twitch.tv/kraken/users/" + this->account->getUserName() + "/blocks/" +
username + "?oauth_token=" + this->account->getOAuthToken() +
"&client_id=" + this->account->getOAuthClient());
QNetworkRequest request(url);
auto reply = this->networkAccessManager.put(request, QByteArray());
@@ -342,9 +347,10 @@ void IrcManager::addIgnoredUser(QString const &username)
bool IrcManager::tryRemoveIgnoredUser(QString const &username, QString &errorMessage)
{
QUrl url("https://api.twitch.tv/kraken/users/" + this->account.getUserName() + "/blocks/" +
username + "?oauth_token=" + this->account.getOAuthToken() +
"&client_id=" + this->account.getOAuthClient());
assert(this->account);
QUrl url("https://api.twitch.tv/kraken/users/" + this->account->getUserName() + "/blocks/" +
username + "?oauth_token=" + this->account->getOAuthToken() +
"&client_id=" + this->account->getOAuthClient());
QNetworkRequest request(url);
auto reply = this->networkAccessManager.deleteResource(request);