Get 3PID binds on startup

Get binds on startup and update the staus properly after bind/unbinding 3PIDs
This commit is contained in:
James Graham
2024-06-08 08:42:34 +00:00
parent 8e5ccb5461
commit 91109ca845
4 changed files with 98 additions and 2 deletions

View File

@@ -3,6 +3,12 @@
#include "threepidmodel.h"
#include <QCryptographicHash>
#include <QNetworkReply>
#include <Quotient/csapi/openid.h>
#include <Quotient/networkaccessmanager.h>
#include "neochatconnection.h"
ThreePIdModel::ThreePIdModel(NeoChatConnection *connection)
@@ -30,6 +36,9 @@ QVariant ThreePIdModel::data(const QModelIndex &index, int role) const
if (role == MediumRole) {
return m_threePIds.at(index.row()).medium;
}
if (role == IsBoundRole) {
return m_bindings.contains(m_threePIds.at(index.row()).address);
}
return {};
}
@@ -45,6 +54,7 @@ QHash<int, QByteArray> ThreePIdModel::roleNames() const
return {
{AddressRole, QByteArrayLiteral("address")},
{MediumRole, QByteArrayLiteral("medium")},
{IsBoundRole, QByteArrayLiteral("isBound")},
};
}
@@ -57,8 +67,80 @@ void ThreePIdModel::refreshModel()
beginResetModel();
m_threePIds = threePIdJob->threepids();
endResetModel();
refreshBindStatus();
});
}
}
void ThreePIdModel::refreshBindStatus()
{
const auto connection = dynamic_cast<NeoChatConnection *>(this->parent());
if (connection == nullptr || !connection->hasIdentityServer()) {
return;
}
const auto openIdJob = connection->callApi<Quotient::RequestOpenIdTokenJob>(connection->userId());
connect(openIdJob, &Quotient::BaseJob::success, this, [this, connection, openIdJob]() {
const auto requestUrl = QUrl(connection->identityServer().toString() + QStringLiteral("/_matrix/identity/v2/account/register"));
if (!(requestUrl.scheme() == QStringLiteral("https") || requestUrl.scheme() == QStringLiteral("http"))) {
return;
}
QNetworkRequest request(requestUrl);
auto newRequest = Quotient::NetworkAccessManager::instance()->post(request, QJsonDocument(openIdJob->jsonData()).toJson());
connect(newRequest, &QNetworkReply::finished, this, [this, connection, newRequest]() {
QJsonObject replyJson = QJsonDocument::fromJson(newRequest->readAll()).object();
const auto identityServerToken = replyJson[QLatin1String("token")].toString();
const auto requestUrl = QUrl(connection->identityServer().toString() + QStringLiteral("/_matrix/identity/v2/hash_details"));
if (!(requestUrl.scheme() == QStringLiteral("https") || requestUrl.scheme() == QStringLiteral("http"))) {
return;
}
QNetworkRequest hashRequest(requestUrl);
hashRequest.setRawHeader("Authorization", "Bearer " + identityServerToken.toLatin1());
auto hashReply = Quotient::NetworkAccessManager::instance()->get(hashRequest);
connect(hashReply, &QNetworkReply::finished, this, [this, connection, identityServerToken, hashReply]() {
QJsonObject replyJson = QJsonDocument::fromJson(hashReply->readAll()).object();
const auto lookupPepper = replyJson[QLatin1String("lookup_pepper")].toString();
const auto requestUrl = QUrl(connection->identityServer().toString() + QStringLiteral("/_matrix/identity/v2/lookup"));
if (!(requestUrl.scheme() == QStringLiteral("https") || requestUrl.scheme() == QStringLiteral("http"))) {
return;
}
QNetworkRequest lookupRequest(requestUrl);
lookupRequest.setRawHeader("Authorization", "Bearer " + identityServerToken.toLatin1());
QJsonObject requestData = {
{QLatin1String("algorithm"), QLatin1String("none")},
{QLatin1String("pepper"), lookupPepper},
};
QJsonArray idLookups;
for (const auto &id : m_threePIds) {
idLookups += QStringLiteral("%1 %2").arg(id.address, id.medium);
}
requestData[QLatin1String("addresses")] = idLookups;
auto lookupReply = Quotient::NetworkAccessManager::instance()->post(lookupRequest, QJsonDocument(requestData).toJson(QJsonDocument::Compact));
connect(lookupReply, &QNetworkReply::finished, this, [this, connection, lookupReply]() {
beginResetModel();
m_bindings.clear();
QJsonObject mappings = QJsonDocument::fromJson(lookupReply->readAll()).object()[QLatin1String("mappings")].toObject();
for (const auto &id : mappings.keys()) {
if (mappings[id] == connection->userId()) {
m_bindings += id.section(u' ', 0, 0);
}
}
endResetModel();
});
});
});
});
}
#include "moc_threepidmodel.cpp"