mirror-chatterino2/src/accountmanager.cpp

219 lines
6.3 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "accountmanager.hpp"
#include "common.hpp"
#include "debug/log.hpp"
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-04-12 17:46:44 +02:00
namespace {
inline QString getEnvString(const char *target)
{
char *val = std::getenv(target);
if (val == nullptr) {
return QString();
}
return QString(val);
}
} // namespace
std::shared_ptr<twitch::TwitchUser> TwitchAccountManager::getCurrent()
2017-04-12 17:46:44 +02:00
{
if (!this->currentUser) {
return this->anonymousUser;
}
return this->currentUser;
2017-04-12 17:46:44 +02:00
}
std::vector<QString> TwitchAccountManager::getUsernames() const
{
std::vector<QString> userNames;
std::lock_guard<std::mutex> lock(this->mutex);
2017-09-24 18:14:22 +02:00
for (const auto &user : this->users) {
userNames.push_back(user->getUserName());
}
return userNames;
}
std::shared_ptr<twitch::TwitchUser> TwitchAccountManager::findUserByUsername(
const QString &username) const
{
std::lock_guard<std::mutex> lock(this->mutex);
for (const auto &user : this->users) {
if (username.compare(user->getUserName(), Qt::CaseInsensitive) == 0) {
return user;
2017-09-24 18:14:22 +02:00
}
}
return nullptr;
}
bool TwitchAccountManager::userExists(const QString &username) const
2017-04-12 17:46:44 +02:00
{
return this->findUserByUsername(username) != nullptr;
2017-04-12 17:46:44 +02:00
}
2017-04-13 16:06:23 +02:00
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: {
debug::Log("User {} already exists", userData.username);
// Do nothing
} break;
case AddUserResponse::UserValuesUpdated: {
debug::Log("User {} already exists, and values updated!", userData.username);
if (userData.username == this->getCurrent()->getNickName()) {
debug::Log("It was the current user, so we need to reconnect stuff!");
this->userChanged.invoke();
}
} break;
case AddUserResponse::UserAdded: {
debug::Log("Added user {}", userData.username);
listUpdated = true;
} break;
}
}
if (listUpdated) {
this->userListUpdated.invoke();
}
}
bool TwitchAccountManager::removeUser(const QString &username)
{
if (!this->userExists(username)) {
return false;
}
this->mutex.lock();
this->users.erase(std::remove_if(this->users.begin(), this->users.end(), [username](auto user) {
if (user->getNickName() == username) {
std::string userID(user->getUserId().toStdString());
assert(!userID.empty());
pajlada::Settings::SettingManager::removeSetting("/accounts/uid" + userID);
return true;
}
return false;
}));
this->mutex.unlock();
if (username == qS(this->currentUsername.getValue())) {
// 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->getOAuthClient().compare(userData.clientID) != 0) {
previousUser->setOAuthClient(userData.clientID);
userUpdated = true;
}
if (previousUser->getOAuthToken().compare(userData.oauthToken) != 0) {
previousUser->setOAuthToken(userData.oauthToken);
userUpdated = true;
}
if (userUpdated) {
return AddUserResponse::UserValuesUpdated;
} else {
return AddUserResponse::UserAlreadyExists;
}
}
auto newUser = std::make_shared<twitch::TwitchUser>(userData.username, userData.oauthToken,
userData.clientID);
// Set users User ID without the uid prefix
newUser->setUserId(userData.userID.mid(3));
std::lock_guard<std::mutex> lock(this->mutex);
this->users.push_back(newUser);
return AddUserResponse::UserAdded;
}
AccountManager::AccountManager()
{
this->Twitch.anonymousUser.reset(new twitch::TwitchUser("justinfan64537", "", ""));
this->Twitch.currentUsername.connect([this](const auto &newValue, auto) {
QString newUsername(QString::fromStdString(newValue));
auto user = this->Twitch.findUserByUsername(newUsername);
if (user) {
debug::Log("[AccountManager:currentUsernameChanged] User successfully updated to {}",
newUsername);
this->Twitch.currentUser = user;
} else {
debug::Log(
"[AccountManager:currentUsernameChanged] User successfully updated to anonymous");
this->Twitch.currentUser = this->Twitch.anonymousUser;
}
this->Twitch.userChanged.invoke();
});
}
void AccountManager::load()
2017-04-13 16:06:23 +02:00
{
this->Twitch.reloadUsers();
auto currentUser = this->Twitch.findUserByUsername(
QString::fromStdString(this->Twitch.currentUsername.getValue()));
if (currentUser) {
this->Twitch.currentUser = currentUser;
} else {
this->Twitch.currentUser = this->Twitch.anonymousUser;
}
this->Twitch.userChanged.invoke();
2017-04-12 17:46:44 +02:00
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino