Refactor accountmanager

This commit is contained in:
Tobias Fella
2025-08-24 16:50:38 +02:00
parent 77ac811498
commit abffeec938

View File

@@ -25,10 +25,10 @@ AccountManager::AccountManager(bool testMode, QObject *parent)
loadAccountsFromCache(); loadAccountsFromCache();
}); });
} else { } else {
auto c = new NeoChatConnection(QUrl(u"https://localhost:1234"_s), this); auto connection = new NeoChatConnection(QUrl(u"https://localhost:1234"_s), this);
c->assumeIdentity(u"@user:localhost:1234"_s, u"device_1234"_s, u"token_1234"_s); connection->assumeIdentity(u"@user:localhost:1234"_s, u"device_1234"_s, u"token_1234"_s);
m_accountRegistry->add(c); m_accountRegistry->add(connection);
c->syncLoop(); connection->syncLoop();
} }
} }
@@ -39,22 +39,20 @@ Quotient::AccountRegistry *AccountManager::accounts()
void AccountManager::loadAccountsFromCache() void AccountManager::loadAccountsFromCache()
{ {
const auto accounts = Quotient::SettingsGroup("Accounts"_L1).childGroups(); for (const auto &accountId : Quotient::SettingsGroup("Accounts"_L1).childGroups()) {
for (const auto &accountId : accounts) {
Quotient::AccountSettings account{accountId}; Quotient::AccountSettings account{accountId};
m_accountsLoading += accountId; m_accountsLoading += accountId;
Q_EMIT accountsLoadingChanged(); Q_EMIT accountsLoadingChanged();
if (!account.homeserver().isEmpty()) { if (account.homeserver().isEmpty()) {
continue;
}
auto accessTokenLoadingJob = loadAccessTokenFromKeyChain(account.userId()); auto accessTokenLoadingJob = loadAccessTokenFromKeyChain(account.userId());
connect(accessTokenLoadingJob, &QKeychain::Job::finished, this, [accountId, this, accessTokenLoadingJob](QKeychain::Job *) { connect(accessTokenLoadingJob, &QKeychain::Job::finished, this, [accountId, this, accessTokenLoadingJob](QKeychain::Job *) {
Quotient::AccountSettings account{accountId}; if (accessTokenLoadingJob->error() != QKeychain::Error::NoError) {
QString accessToken;
if (accessTokenLoadingJob->error() == QKeychain::Error::NoError) {
accessToken = QString::fromLatin1(accessTokenLoadingJob->binaryData());
} else {
return; return;
} }
Quotient::AccountSettings account{accountId};
auto connection = new NeoChatConnection(account.homeserver()); auto connection = new NeoChatConnection(account.homeserver());
m_connectionsLoading[accountId] = connection; m_connectionsLoading[accountId] = connection;
connect(connection, &NeoChatConnection::connected, this, [this, connection, accountId] { connect(connection, &NeoChatConnection::connected, this, [this, connection, accountId] {
@@ -78,11 +76,10 @@ void AccountManager::loadAccountsFromCache()
Qt::SingleShotConnection); Qt::SingleShotConnection);
} }
}); });
connection->assumeIdentity(account.userId(), account.deviceId(), accessToken); connection->assumeIdentity(account.userId(), account.deviceId(), QString::fromLatin1(accessTokenLoadingJob->binaryData()));
}); });
} }
} }
}
QStringList AccountManager::accountsLoading() const QStringList AccountManager::accountsLoading() const
{ {
@@ -207,8 +204,7 @@ void AccountManager::dropConnection(const QString &userId)
if (dropConnectionLoading(m_connectionsLoading.value(userId, nullptr))) { if (dropConnectionLoading(m_connectionsLoading.value(userId, nullptr))) {
return; return;
} }
const auto connection = dynamic_cast<NeoChatConnection *>(m_accountRegistry->get(userId)); if (const auto connection = dynamic_cast<NeoChatConnection *>(m_accountRegistry->get(userId))) {
if (connection) {
dropRegistry(connection); dropRegistry(connection);
} }
} }