aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/connection.cpp58
-rw-r--r--lib/connection.h8
2 files changed, 61 insertions, 5 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp
index 2a2d4822..6d1763ee 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -24,6 +24,7 @@
#include "room.h"
#include "settings.h"
#include "csapi/login.h"
+#include "csapi/capabilities.h"
#include "csapi/logout.h"
#include "csapi/receipts.h"
#include "csapi/leaving.h"
@@ -92,6 +93,9 @@ class Connection::Private
QString userId;
int syncLoopTimeout = -1;
+ GetCapabilitiesJob* capabilitiesJob = nullptr;
+ GetCapabilitiesJob::Capabilities capabilities;
+
SyncJob* syncJob = nullptr;
bool cacheState = true;
@@ -244,6 +248,29 @@ void Connection::connectWithToken(const QString& userId,
[=] { d->connectWithToken(userId, accessToken, deviceId); });
}
+void Connection::reloadCapabilities()
+{
+ d->capabilitiesJob = callApi<GetCapabilitiesJob>(BackgroundRequest);
+ connect(d->capabilitiesJob, &BaseJob::finished, this, [this] {
+ if (d->capabilitiesJob->error() == BaseJob::Success)
+ d->capabilities = d->capabilitiesJob->capabilities();
+ else if (d->capabilitiesJob->error() == BaseJob::IncorrectRequestError)
+ qCDebug(MAIN) << "Server doesn't support /capabilities";
+
+ if (d->capabilities.roomVersions.omitted())
+ {
+ qCWarning(MAIN) << "Pinning supported room version to 1";
+ d->capabilities.roomVersions = { "1", {{ "1", "stable" }} };
+ } else {
+ qCDebug(MAIN) << "Room versions:"
+ << defaultRoomVersion() << "is default, full list:"
+ << availableRoomVersions();
+ }
+ Q_ASSERT(!d->capabilities.roomVersions.omitted());
+ emit capabilitiesLoaded();
+ });
+}
+
void Connection::Private::connectWithToken(const QString& user,
const QString& accessToken,
const QString& deviceId)
@@ -256,7 +283,7 @@ void Connection::Private::connectWithToken(const QString& user,
<< "by user" << userId << "from device" << deviceId;
emit q->stateChanged();
emit q->connected();
-
+ q->reloadCapabilities();
}
void Connection::checkAndConnect(const QString& userId,
@@ -1259,9 +1286,30 @@ void QMatrixClient::Connection::setLazyLoading(bool newValue)
void Connection::getTurnServers()
{
- auto job = callApi<GetTurnServerJob>();
- connect( job, &GetTurnServerJob::success, [=] {
- emit turnServersChanged(job->data());
- });
+ auto job = callApi<GetTurnServerJob>();
+ connect(job, &GetTurnServerJob::success,
+ this, [=] { emit turnServersChanged(job->data()); });
+}
+
+QString Connection::defaultRoomVersion() const
+{
+ Q_ASSERT(!d->capabilities.roomVersions.omitted());
+ return d->capabilities.roomVersions->defaultVersion;
+}
+QStringList Connection::stableRoomVersions() const
+{
+ Q_ASSERT(!d->capabilities.roomVersions.omitted());
+ QStringList l;
+ const auto& allVersions = d->capabilities.roomVersions->available;
+ for (auto it = allVersions.begin(); it != allVersions.end(); ++it)
+ if (it.value() == "stable")
+ l.push_back(it.key());
+ return l;
+}
+
+const QHash<QString, QString>& Connection::availableRoomVersions() const
+{
+ Q_ASSERT(!d->capabilities.roomVersions.omitted());
+ return d->capabilities.roomVersions->available;
}
diff --git a/lib/connection.h b/lib/connection.h
index 8c938df2..e5bce52e 100644
--- a/lib/connection.h
+++ b/lib/connection.h
@@ -102,6 +102,7 @@ namespace QMatrixClient
Q_PROPERTY(QString localUserId READ userId NOTIFY stateChanged)
Q_PROPERTY(QString deviceId READ deviceId NOTIFY stateChanged)
Q_PROPERTY(QByteArray accessToken READ accessToken NOTIFY stateChanged)
+ Q_PROPERTY(QString defaultRoomVersion READ defaultRoomVersion NOTIFY capabilitiesLoaded)
Q_PROPERTY(QUrl homeserver READ homeserver WRITE setHomeserver NOTIFY homeserverChanged)
Q_PROPERTY(bool cacheState READ cacheState WRITE setCacheState NOTIFY cacheStateChanged)
Q_PROPERTY(bool lazyLoading READ lazyLoading WRITE setLazyLoading NOTIFY lazyLoadingChanged)
@@ -257,6 +258,10 @@ namespace QMatrixClient
Q_INVOKABLE QString token() const;
Q_INVOKABLE void getTurnServers();
+ QString defaultRoomVersion() const;
+ QStringList stableRoomVersions() const;
+ const QHash<QString, QString>& availableRoomVersions() const;
+
/**
* Call this before first sync to load from previously saved file.
*
@@ -365,6 +370,8 @@ namespace QMatrixClient
const QString& deviceId = {});
void connectWithToken(const QString& userId, const QString& accessToken,
const QString& deviceId);
+ /** Explicitly request capabilities from the server */
+ void reloadCapabilities();
/** @deprecated Use stopSync() instead */
void disconnectFromServer() { stopSync(); }
@@ -501,6 +508,7 @@ namespace QMatrixClient
void resolveError(QString error);
void homeserverChanged(QUrl baseUrl);
+ void capabilitiesLoaded();
void connected();
void reconnected(); //< \deprecated Use connected() instead