2019-09-08 22:27:57 +02:00
|
|
|
#include "providers/twitch/TwitchAccountManager.hpp"
|
|
|
|
|
|
|
|
#include "common/Common.hpp"
|
2020-11-21 16:20:10 +01:00
|
|
|
#include "common/QLogging.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "providers/twitch/TwitchAccount.hpp"
|
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
2020-03-14 12:13:57 +01:00
|
|
|
#include "providers/twitch/api/Helix.hpp"
|
|
|
|
#include "providers/twitch/api/Kraken.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
TwitchAccountManager::TwitchAccountManager()
|
2020-02-23 19:49:45 +01:00
|
|
|
: accounts(SharedPtrElementLess<TwitchAccount>{})
|
|
|
|
, anonymousUser_(new TwitchAccount(ANONYMOUS_USERNAME, "", "", ""))
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
this->currentUserChanged.connect([this] {
|
|
|
|
auto currentUser = this->getCurrent();
|
2021-02-14 14:01:13 +01:00
|
|
|
currentUser->loadBlocks();
|
2019-09-08 22:27:57 +02:00
|
|
|
});
|
|
|
|
|
2020-11-08 12:02:19 +01:00
|
|
|
this->accounts.itemRemoved.connect([this](const auto &acc) {
|
2019-09-08 22:27:57 +02:00
|
|
|
this->removeUser(acc.item.get());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<TwitchAccount> TwitchAccountManager::getCurrent()
|
|
|
|
{
|
|
|
|
if (!this->currentUser_)
|
|
|
|
{
|
|
|
|
return this->anonymousUser_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->currentUser_;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QString> TwitchAccountManager::getUsernames() const
|
|
|
|
{
|
|
|
|
std::vector<QString> userNames;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
|
|
|
|
|
|
|
for (const auto &user : this->accounts)
|
|
|
|
{
|
|
|
|
userNames.push_back(user->getUserName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return userNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<TwitchAccount> TwitchAccountManager::findUserByUsername(
|
|
|
|
const QString &username) const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
|
|
|
|
|
|
|
for (const auto &user : this->accounts)
|
|
|
|
{
|
|
|
|
if (username.compare(user->getUserName(), Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchAccountManager::userExists(const QString &username) const
|
|
|
|
{
|
|
|
|
return this->findUserByUsername(username) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchAccountManager::reloadUsers()
|
|
|
|
{
|
|
|
|
auto keys = pajlada::Settings::SettingManager::getObjectKeys("/accounts");
|
|
|
|
|
|
|
|
UserData userData;
|
|
|
|
|
|
|
|
bool listUpdated = false;
|
|
|
|
|
|
|
|
for (const auto &uid : keys)
|
|
|
|
{
|
|
|
|
if (uid == "current")
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto username = pajlada::Settings::Setting<QString>::get(
|
|
|
|
"/accounts/" + uid + "/username");
|
|
|
|
auto userID = pajlada::Settings::Setting<QString>::get("/accounts/" +
|
|
|
|
uid + "/userID");
|
|
|
|
auto clientID = pajlada::Settings::Setting<QString>::get(
|
|
|
|
"/accounts/" + uid + "/clientID");
|
|
|
|
auto oauthToken = pajlada::Settings::Setting<QString>::get(
|
|
|
|
"/accounts/" + uid + "/oauthToken");
|
|
|
|
|
|
|
|
if (username.isEmpty() || userID.isEmpty() || clientID.isEmpty() ||
|
|
|
|
oauthToken.isEmpty())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
userData.username = username.trimmed();
|
|
|
|
userData.userID = userID.trimmed();
|
|
|
|
userData.clientID = clientID.trimmed();
|
|
|
|
userData.oauthToken = oauthToken.trimmed();
|
|
|
|
|
|
|
|
switch (this->addUser(userData))
|
|
|
|
{
|
2019-09-26 00:51:05 +02:00
|
|
|
case AddUserResponse::UserAlreadyExists: {
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoTwitch)
|
|
|
|
<< "User" << userData.username << "already exists";
|
2019-09-08 22:27:57 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
break;
|
2019-09-26 00:51:05 +02:00
|
|
|
case AddUserResponse::UserValuesUpdated: {
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoTwitch)
|
|
|
|
<< "User" << userData.username
|
|
|
|
<< "already exists, and values updated!";
|
2019-09-08 22:27:57 +02:00
|
|
|
if (userData.username == this->getCurrent()->getUserName())
|
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoTwitch)
|
|
|
|
<< "It was the current user, so we need to "
|
|
|
|
"reconnect stuff!";
|
2019-09-08 22:27:57 +02:00
|
|
|
this->currentUserChanged.invoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-09-26 00:51:05 +02:00
|
|
|
case AddUserResponse::UserAdded: {
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoTwitch) << "Added user" << userData.username;
|
2019-09-08 22:27:57 +02:00
|
|
|
listUpdated = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listUpdated)
|
|
|
|
{
|
|
|
|
this->userListUpdated.invoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchAccountManager::load()
|
|
|
|
{
|
|
|
|
this->reloadUsers();
|
|
|
|
|
|
|
|
this->currentUsername.connect([this](const QString &newUsername) {
|
|
|
|
auto user = this->findUserByUsername(newUsername);
|
|
|
|
if (user)
|
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoTwitch)
|
|
|
|
<< "Twitch user updated to" << newUsername;
|
2020-03-14 12:13:57 +01:00
|
|
|
getHelix()->update(user->getOAuthClient(), user->getOAuthToken());
|
|
|
|
getKraken()->update(user->getOAuthClient(), user->getOAuthToken());
|
2019-09-08 22:27:57 +02:00
|
|
|
this->currentUser_ = user;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoTwitch) << "Twitch user updated to anonymous";
|
2019-09-08 22:27:57 +02:00
|
|
|
this->currentUser_ = this->anonymousUser_;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->currentUserChanged.invoke();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchAccountManager::isLoggedIn() const
|
|
|
|
{
|
|
|
|
if (!this->currentUser_)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once `TwitchAccount` class has a way to check, we should also return
|
|
|
|
// false if the credentials are incorrect
|
|
|
|
return !this->currentUser_->isAnon();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchAccountManager::removeUser(TwitchAccount *account)
|
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
static const QString accountFormat("/accounts/uid%1");
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
auto userID(account->getUserId());
|
|
|
|
if (!userID.isEmpty())
|
|
|
|
{
|
|
|
|
pajlada::Settings::SettingManager::removeSetting(
|
2020-01-03 20:51:37 +01:00
|
|
|
accountFormat.arg(userID).toStdString());
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (account->getUserName() == this->currentUsername)
|
|
|
|
{
|
|
|
|
// The user that was removed is the current user, log into the anonymous
|
|
|
|
// user
|
|
|
|
this->currentUsername = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
this->userListUpdated.invoke();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TwitchAccountManager::AddUserResponse TwitchAccountManager::addUser(
|
|
|
|
const TwitchAccountManager::UserData &userData)
|
|
|
|
{
|
|
|
|
auto previousUser = this->findUserByUsername(userData.username);
|
|
|
|
if (previousUser)
|
|
|
|
{
|
|
|
|
bool userUpdated = false;
|
|
|
|
|
|
|
|
if (previousUser->setOAuthClient(userData.clientID))
|
|
|
|
{
|
|
|
|
userUpdated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousUser->setOAuthToken(userData.oauthToken))
|
|
|
|
{
|
|
|
|
userUpdated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userUpdated)
|
|
|
|
{
|
|
|
|
return AddUserResponse::UserValuesUpdated;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return AddUserResponse::UserAlreadyExists;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto newUser =
|
|
|
|
std::make_shared<TwitchAccount>(userData.username, userData.oauthToken,
|
|
|
|
userData.clientID, userData.userID);
|
|
|
|
|
|
|
|
// std::lock_guard<std::mutex> lock(this->mutex);
|
|
|
|
|
2020-02-23 19:44:13 +01:00
|
|
|
this->accounts.insert(newUser);
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
return AddUserResponse::UserAdded;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|