2017-06-11 09:31:45 +02:00
|
|
|
|
#include "accountmanager.hpp"
|
2017-07-28 19:46:53 +02:00
|
|
|
|
#include "common.hpp"
|
2017-12-16 02:21:06 +01:00
|
|
|
|
#include "debug/log.hpp"
|
2017-07-23 14:16:13 +02:00
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
|
namespace chatterino {
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
2017-05-30 18:24:55 +02:00
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
inline QString getEnvString(const char *target)
|
|
|
|
|
{
|
|
|
|
|
char *val = std::getenv(target);
|
|
|
|
|
if (val == nullptr) {
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QString(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
std::shared_ptr<twitch::TwitchUser> TwitchAccountManager::getCurrent()
|
2017-04-12 17:46:44 +02:00
|
|
|
|
{
|
2017-12-16 02:21:06 +01:00
|
|
|
|
if (!this->currentUser) {
|
|
|
|
|
return this->anonymousUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this->currentUser;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
std::vector<QString> TwitchAccountManager::getUsernames() const
|
2017-07-28 19:46:53 +02:00
|
|
|
|
{
|
2017-12-16 02:21:06 +01:00
|
|
|
|
std::vector<QString> userNames;
|
2017-07-28 19:46:53 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
std::lock_guard<std::mutex> lock(this->mutex);
|
2017-09-24 18:14:22 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
for (const auto &user : this->users) {
|
|
|
|
|
userNames.push_back(user->getUserName());
|
|
|
|
|
}
|
2017-07-28 19:46:53 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
return userNames;
|
|
|
|
|
}
|
2017-07-28 19:46:53 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
std::shared_ptr<twitch::TwitchUser> TwitchAccountManager::findUserByUsername(
|
|
|
|
|
const QString &username) const
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(this->mutex);
|
2017-07-28 19:46:53 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
for (const auto &user : this->users) {
|
|
|
|
|
if (username.compare(user->getUserName(), Qt::CaseInsensitive) == 0) {
|
|
|
|
|
return user;
|
2017-09-24 18:14:22 +02:00
|
|
|
|
}
|
2017-07-28 19:46:53 +02:00
|
|
|
|
}
|
2017-12-16 02:21:06 +01:00
|
|
|
|
|
|
|
|
|
return nullptr;
|
2017-07-28 19:46:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
bool TwitchAccountManager::userExists(const QString &username) const
|
2017-04-12 17:46:44 +02:00
|
|
|
|
{
|
2017-12-16 02:21:06 +01:00
|
|
|
|
return this->findUserByUsername(username) != nullptr;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
}
|
2017-04-13 16:06:23 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
bool TwitchAccountManager::addUser(std::shared_ptr<twitch::TwitchUser> user)
|
2017-05-30 18:24:55 +02:00
|
|
|
|
{
|
2017-12-16 02:21:06 +01:00
|
|
|
|
if (this->userExists(user->getNickName())) {
|
|
|
|
|
// User already exists in user list
|
|
|
|
|
return false;
|
2017-05-30 18:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
std::lock_guard<std::mutex> lock(this->mutex);
|
2017-08-19 15:37:56 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
this->users.push_back(user);
|
2017-08-19 15:37:56 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
return true;
|
2017-05-30 18:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
AccountManager::AccountManager()
|
2017-08-19 15:37:56 +02:00
|
|
|
|
{
|
2017-12-16 02:21:06 +01:00
|
|
|
|
this->Twitch.anonymousUser.reset(new twitch::TwitchUser("justinfan64537", "", ""));
|
|
|
|
|
|
2017-12-17 03:37:46 +01:00
|
|
|
|
this->Twitch.currentUsername.connect([this](const auto &newValue, auto) {
|
2017-12-16 02:21:06 +01:00
|
|
|
|
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;
|
2017-12-18 21:25:19 +01:00
|
|
|
|
} else {
|
|
|
|
|
debug::Log(
|
|
|
|
|
"[AccountManager:currentUsernameChanged] User successfully updated to anonymous");
|
|
|
|
|
this->Twitch.currentUser = this->Twitch.anonymousUser;
|
2017-12-16 02:21:06 +01:00
|
|
|
|
}
|
2017-12-18 21:25:19 +01:00
|
|
|
|
|
|
|
|
|
this->Twitch.userChanged.invoke();
|
2017-12-16 02:21:06 +01:00
|
|
|
|
});
|
2017-08-19 15:37:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
void AccountManager::load()
|
2017-04-13 16:06:23 +02:00
|
|
|
|
{
|
2017-12-16 02:21:06 +01:00
|
|
|
|
auto keys = pajlada::Settings::SettingManager::getObjectKeys("/accounts");
|
2017-06-11 12:00:53 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
for (const auto &uid : keys) {
|
|
|
|
|
if (uid == "current") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-04-13 16:06:23 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
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");
|
2017-04-13 16:06:23 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
if (username.empty() || userID.empty() || clientID.empty() || oauthToken.empty()) {
|
|
|
|
|
continue;
|
2017-04-13 16:06:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
auto user =
|
|
|
|
|
std::make_shared<twitch::TwitchUser>(qS(username), qS(oauthToken), qS(clientID));
|
2017-04-13 16:06:23 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
this->Twitch.addUser(user);
|
2017-04-13 16:06:23 +02:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
printf("Adding user %s(%s)\n", username.c_str(), userID.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto currentUser = this->Twitch.findUserByUsername(
|
|
|
|
|
QString::fromStdString(this->Twitch.currentUsername.getValue()));
|
2017-12-18 21:25:19 +01:00
|
|
|
|
|
2017-12-16 02:21:06 +01:00
|
|
|
|
if (currentUser) {
|
|
|
|
|
this->Twitch.currentUser = currentUser;
|
2017-12-18 21:25:19 +01:00
|
|
|
|
} else {
|
|
|
|
|
this->Twitch.currentUser = this->Twitch.anonymousUser;
|
2017-12-16 02:21:06 +01:00
|
|
|
|
}
|
2017-12-18 21:25:19 +01:00
|
|
|
|
|
|
|
|
|
this->Twitch.userChanged.invoke();
|
2017-04-12 17:46:44 +02:00
|
|
|
|
}
|
2017-05-27 17:45:40 +02:00
|
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
|
} // namespace chatterino
|