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-08-11 22:23:06 +02:00
|
|
|
#include "providers/twitch/TwitchAccount.hpp"
|
2018-06-26 17:25:24 +02:00
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
2017-12-31 00:50:07 +01:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
TwitchAccountManager::TwitchAccountManager()
|
2018-07-06 19:23:47 +02:00
|
|
|
: anonymousUser_(new TwitchAccount(ANONYMOUS_USERNAME, "", "", ""))
|
2017-12-31 00:50:07 +01:00
|
|
|
{
|
2018-05-12 20:34:13 +02:00
|
|
|
this->currentUserChanged.connect([this] {
|
|
|
|
auto currentUser = this->getCurrent();
|
|
|
|
currentUser->loadIgnores();
|
|
|
|
});
|
2018-05-28 08:51:39 +02:00
|
|
|
|
2019-08-23 16:52:04 +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
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!this->currentUser_)
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->anonymousUser_;
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->currentUser_;
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QString> TwitchAccountManager::getUsernames() const
|
|
|
|
{
|
|
|
|
std::vector<QString> userNames;
|
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2019-07-31 22:29:07 +02:00
|
|
|
for (const auto &user : this->accounts)
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
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
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->mutex_);
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2019-07-31 22:29:07 +02:00
|
|
|
for (const auto &user : this->accounts)
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
|
|
|
if (username.compare(user->getUserName(), Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
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;
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &uid : keys)
|
|
|
|
{
|
|
|
|
if (uid == "current")
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
auto username = pajlada::Settings::Setting<QString>::get(
|
2018-08-06 21:17:03 +02:00
|
|
|
"/accounts/" + uid + "/username");
|
2018-11-03 13:00:07 +01:00
|
|
|
auto userID = pajlada::Settings::Setting<QString>::get("/accounts/" +
|
|
|
|
uid + "/userID");
|
|
|
|
auto clientID = pajlada::Settings::Setting<QString>::get(
|
2018-08-06 21:17:03 +02:00
|
|
|
"/accounts/" + uid + "/clientID");
|
2018-11-03 13:00:07 +01:00
|
|
|
auto oauthToken = pajlada::Settings::Setting<QString>::get(
|
2018-08-06 21:17:03 +02:00
|
|
|
"/accounts/" + uid + "/oauthToken");
|
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
if (username.isEmpty() || userID.isEmpty() || clientID.isEmpty() ||
|
|
|
|
oauthToken.isEmpty())
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
userData.username = username.trimmed();
|
|
|
|
userData.userID = userID.trimmed();
|
|
|
|
userData.clientID = clientID.trimmed();
|
|
|
|
userData.oauthToken = oauthToken.trimmed();
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
switch (this->addUser(userData))
|
|
|
|
{
|
|
|
|
case AddUserResponse::UserAlreadyExists:
|
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
log("User {} already exists", userData.username);
|
2017-12-31 00:50:07 +01:00
|
|
|
// Do nothing
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AddUserResponse::UserValuesUpdated:
|
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
log("User {} already exists, and values updated!",
|
2018-08-06 21:17:03 +02:00
|
|
|
userData.username);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (userData.username == this->getCurrent()->getUserName())
|
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
log("It was the current user, so we need to reconnect "
|
2018-08-06 21:17:03 +02:00
|
|
|
"stuff!");
|
2018-05-06 12:52:47 +02:00
|
|
|
this->currentUserChanged.invoke();
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AddUserResponse::UserAdded:
|
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
log("Added user {}", userData.username);
|
2017-12-31 00:50:07 +01:00
|
|
|
listUpdated = true;
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-12-31 00:50:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (listUpdated)
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
this->userListUpdated.invoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 14:38:10 +02:00
|
|
|
void TwitchAccountManager::load()
|
|
|
|
{
|
|
|
|
this->reloadUsers();
|
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
this->currentUsername.connect([this](const QString &newUsername) {
|
2018-04-22 14:38:10 +02:00
|
|
|
auto user = this->findUserByUsername(newUsername);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (user)
|
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
log("[AccountManager:currentUsernameChanged] User successfully "
|
2018-08-06 21:17:03 +02:00
|
|
|
"updated to {}",
|
2018-06-26 17:20:03 +02:00
|
|
|
newUsername);
|
2018-07-06 19:23:47 +02:00
|
|
|
this->currentUser_ = user;
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-11 14:20:53 +02:00
|
|
|
log("[AccountManager:currentUsernameChanged] User successfully "
|
2018-08-06 21:17:03 +02:00
|
|
|
"updated to anonymous");
|
2018-07-06 19:23:47 +02:00
|
|
|
this->currentUser_ = this->anonymousUser_;
|
2018-04-22 14:38:10 +02:00
|
|
|
}
|
|
|
|
|
2018-05-06 12:52:47 +02:00
|
|
|
this->currentUserChanged.invoke();
|
2018-04-22 14:38:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-24 15:09:56 +02:00
|
|
|
bool TwitchAccountManager::isLoggedIn() const
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!this->currentUser_)
|
|
|
|
{
|
2018-06-24 15:09:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
// Once `TwitchAccount` class has a way to check, we should also return
|
|
|
|
// false if the credentials are incorrect
|
2018-07-06 19:23:47 +02:00
|
|
|
return !this->currentUser_->isAnon();
|
2018-06-24 15:09:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-28 08:51:39 +02:00
|
|
|
bool TwitchAccountManager::removeUser(TwitchAccount *account)
|
2017-12-31 00:50:07 +01:00
|
|
|
{
|
2018-11-03 13:00:07 +01:00
|
|
|
auto userID(account->getUserId());
|
|
|
|
if (!userID.isEmpty())
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2018-11-03 13:00:07 +01:00
|
|
|
pajlada::Settings::SettingManager::removeSetting(
|
|
|
|
fS("/accounts/uid{}", userID));
|
2018-05-28 08:34:54 +02:00
|
|
|
}
|
2017-12-31 00:50:07 +01:00
|
|
|
|
2018-11-03 13:00:07 +01:00
|
|
|
if (account->getUserName() == this->currentUsername)
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
// The user that was removed is the current user, log into the anonymous
|
|
|
|
// user
|
2017-12-31 00:50:07 +01:00
|
|
|
this->currentUsername = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
this->userListUpdated.invoke();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TwitchAccountManager::AddUserResponse TwitchAccountManager::addUser(
|
|
|
|
const TwitchAccountManager::UserData &userData)
|
|
|
|
{
|
|
|
|
auto previousUser = this->findUserByUsername(userData.username);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (previousUser)
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
bool userUpdated = false;
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (previousUser->setOAuthClient(userData.clientID))
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
userUpdated = true;
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (previousUser->setOAuthToken(userData.oauthToken))
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
userUpdated = true;
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (userUpdated)
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
return AddUserResponse::UserValuesUpdated;
|
2018-10-21 13:43:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-31 00:50:07 +01:00
|
|
|
return AddUserResponse::UserAlreadyExists;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-01-18 18:20:40 +01:00
|
|
|
|
|
|
|
} // namespace chatterino
|