mirror-chatterino2/src/providers/twitch/TwitchAccountManager.cpp

200 lines
6 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchAccountManager.hpp"
2017-12-31 00:50:07 +01:00
2018-06-26 15:33:51 +02:00
#include "common/Common.hpp"
2018-06-26 14:09:39 +02:00
#include "debug/Log.hpp"
2018-06-26 17:20:03 +02:00
#include "providers/twitch/Const.hpp"
2017-12-31 00:50:07 +01:00
namespace chatterino {
TwitchAccountManager::TwitchAccountManager()
: anonymousUser(new TwitchAccount(ANONYMOUS_USERNAME, "", "", ""))
2017-12-31 00:50:07 +01:00
{
this->currentUserChanged.connect([this] {
auto currentUser = this->getCurrent();
currentUser->loadIgnores();
});
2018-05-28 08:51:39 +02:00
this->accounts.itemRemoved.connect(
[this](const auto &acc) { this->removeUser(acc.item.get()); });
2017-12-31 00:50:07 +01:00
}
2018-02-05 15:11:50 +01:00
std::shared_ptr<TwitchAccount> TwitchAccountManager::getCurrent()
2017-12-31 00:50:07 +01:00
{
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);
2018-05-28 08:34:54 +02:00
for (const auto &user : this->accounts.getVector()) {
2017-12-31 00:50:07 +01:00
userNames.push_back(user->getUserName());
}
return userNames;
}
2018-02-05 15:11:50 +01:00
std::shared_ptr<TwitchAccount> TwitchAccountManager::findUserByUsername(
2017-12-31 00:50:07 +01:00
const QString &username) const
{
std::lock_guard<std::mutex> lock(this->mutex);
2018-05-28 08:34:54 +02:00
for (const auto &user : this->accounts.getVector()) {
2017-12-31 00:50:07 +01:00
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;
}
std::string username =
pajlada::Settings::Setting<std::string>::get("/accounts/" + uid + "/username");
std::string userID =
pajlada::Settings::Setting<std::string>::get("/accounts/" + uid + "/userID");
std::string clientID =
pajlada::Settings::Setting<std::string>::get("/accounts/" + uid + "/clientID");
std::string oauthToken =
pajlada::Settings::Setting<std::string>::get("/accounts/" + uid + "/oauthToken");
if (username.empty() || userID.empty() || clientID.empty() || oauthToken.empty()) {
continue;
}
userData.username = qS(username);
userData.userID = qS(userID);
userData.clientID = qS(clientID);
userData.oauthToken = qS(oauthToken);
switch (this->addUser(userData)) {
case AddUserResponse::UserAlreadyExists: {
2018-06-26 17:06:17 +02:00
Log("User {} already exists", userData.username);
2017-12-31 00:50:07 +01:00
// Do nothing
} break;
case AddUserResponse::UserValuesUpdated: {
2018-06-26 17:06:17 +02:00
Log("User {} already exists, and values updated!", userData.username);
2018-02-05 15:11:50 +01:00
if (userData.username == this->getCurrent()->getUserName()) {
2018-06-26 17:06:17 +02:00
Log("It was the current user, so we need to reconnect stuff!");
2018-05-06 12:52:47 +02:00
this->currentUserChanged.invoke();
2017-12-31 00:50:07 +01:00
}
} break;
case AddUserResponse::UserAdded: {
2018-06-26 17:06:17 +02:00
Log("Added user {}", userData.username);
2017-12-31 00:50:07 +01:00
listUpdated = true;
} break;
}
}
if (listUpdated) {
this->userListUpdated.invoke();
}
}
2018-04-22 14:38:10 +02:00
void TwitchAccountManager::load()
{
this->reloadUsers();
this->currentUsername.connect([this](const auto &newValue, auto) {
QString newUsername(QString::fromStdString(newValue));
auto user = this->findUserByUsername(newUsername);
if (user) {
2018-06-26 17:06:17 +02:00
Log("[AccountManager:currentUsernameChanged] User successfully updated to {}",
2018-06-26 17:20:03 +02:00
newUsername);
2018-04-22 14:38:10 +02:00
this->currentUser = user;
} else {
2018-06-26 17:20:03 +02:00
Log("[AccountManager:currentUsernameChanged] User successfully updated to anonymous");
2018-04-22 14:38:10 +02:00
this->currentUser = this->anonymousUser;
}
2018-05-06 12:52:47 +02:00
this->currentUserChanged.invoke();
2018-04-22 14:38:10 +02:00
});
}
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();
}
2018-05-28 08:51:39 +02:00
bool TwitchAccountManager::removeUser(TwitchAccount *account)
2017-12-31 00:50:07 +01:00
{
2018-05-28 08:51:39 +02:00
const auto &accs = this->accounts.getVector();
2018-05-28 08:34:54 +02:00
2018-05-28 08:51:39 +02:00
std::string userID(account->getUserId().toStdString());
if (!userID.empty()) {
pajlada::Settings::SettingManager::removeSetting("/accounts/uid" + userID);
2018-05-28 08:34:54 +02:00
}
2017-12-31 00:50:07 +01:00
2018-05-28 08:51:39 +02:00
if (account->getUserName() == qS(this->currentUsername.getValue())) {
2017-12-31 00:50:07 +01:00
// 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);
2017-12-31 00:50:07 +01:00
2018-05-28 08:51:39 +02:00
// std::lock_guard<std::mutex> lock(this->mutex);
2017-12-31 00:50:07 +01:00
2018-05-28 08:34:54 +02:00
this->accounts.insertItem(newUser);
2017-12-31 00:50:07 +01:00
return AddUserResponse::UserAdded;
}
} // namespace chatterino