Skip to content

Commit

Permalink
Connection: load supported room versions
Browse files Browse the repository at this point in the history
A part of quotient-im#236.
  • Loading branch information
KitsuneRal committed Feb 14, 2019
1 parent 9a0f5ce commit e12fc32
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
58 changes: 53 additions & 5 deletions lib/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -92,6 +93,9 @@ class Connection::Private
QString userId;
int syncLoopTimeout = -1;

GetCapabilitiesJob* capabilitiesJob = nullptr;
GetCapabilitiesJob::Capabilities capabilities;

SyncJob* syncJob = nullptr;

bool cacheState = true;
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
8 changes: 8 additions & 0 deletions lib/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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(); }
Expand Down Expand Up @@ -501,6 +508,7 @@ namespace QMatrixClient
void resolveError(QString error);

void homeserverChanged(QUrl baseUrl);
void capabilitiesLoaded();

void connected();
void reconnected(); //< \deprecated Use connected() instead
Expand Down

0 comments on commit e12fc32

Please sign in to comment.