Skip to content

Commit

Permalink
Merge #836(kitsune): Another round of modernization and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
KitsuneRal authored Nov 26, 2024
2 parents e579e0b + 20185fa commit 688915f
Show file tree
Hide file tree
Showing 76 changed files with 338 additions and 354 deletions.
6 changes: 3 additions & 3 deletions Quotient/application-service/definitions/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ template <>
struct JsonObjectConverter<ThirdPartyLocation> {
static void dumpTo(QJsonObject& jo, const ThirdPartyLocation& pod)
{
addParam<>(jo, "alias"_L1, pod.alias);
addParam<>(jo, "protocol"_L1, pod.protocol);
addParam<>(jo, "fields"_L1, pod.fields);
addParam(jo, "alias"_L1, pod.alias);
addParam(jo, "protocol"_L1, pod.protocol);
addParam(jo, "fields"_L1, pod.fields);
}
static void fillFrom(const QJsonObject& jo, ThirdPartyLocation& pod)
{
Expand Down
20 changes: 10 additions & 10 deletions Quotient/application-service/definitions/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ template <>
struct JsonObjectConverter<FieldType> {
static void dumpTo(QJsonObject& jo, const FieldType& pod)
{
addParam<>(jo, "regexp"_L1, pod.regexp);
addParam<>(jo, "placeholder"_L1, pod.placeholder);
addParam(jo, "regexp"_L1, pod.regexp);
addParam(jo, "placeholder"_L1, pod.placeholder);
}
static void fillFrom(const QJsonObject& jo, FieldType& pod)
{
Expand Down Expand Up @@ -49,9 +49,9 @@ template <>
struct JsonObjectConverter<ProtocolInstance> {
static void dumpTo(QJsonObject& jo, const ProtocolInstance& pod)
{
addParam<>(jo, "desc"_L1, pod.desc);
addParam<>(jo, "fields"_L1, pod.fields);
addParam<>(jo, "network_id"_L1, pod.networkId);
addParam(jo, "desc"_L1, pod.desc);
addParam(jo, "fields"_L1, pod.fields);
addParam(jo, "network_id"_L1, pod.networkId);
addParam<IfNotEmpty>(jo, "icon"_L1, pod.icon);
}
static void fillFrom(const QJsonObject& jo, ProtocolInstance& pod)
Expand Down Expand Up @@ -96,11 +96,11 @@ template <>
struct JsonObjectConverter<ThirdPartyProtocol> {
static void dumpTo(QJsonObject& jo, const ThirdPartyProtocol& pod)
{
addParam<>(jo, "user_fields"_L1, pod.userFields);
addParam<>(jo, "location_fields"_L1, pod.locationFields);
addParam<>(jo, "icon"_L1, pod.icon);
addParam<>(jo, "field_types"_L1, pod.fieldTypes);
addParam<>(jo, "instances"_L1, pod.instances);
addParam(jo, "user_fields"_L1, pod.userFields);
addParam(jo, "location_fields"_L1, pod.locationFields);
addParam(jo, "icon"_L1, pod.icon);
addParam(jo, "field_types"_L1, pod.fieldTypes);
addParam(jo, "instances"_L1, pod.instances);
}
static void fillFrom(const QJsonObject& jo, ThirdPartyProtocol& pod)
{
Expand Down
6 changes: 3 additions & 3 deletions Quotient/application-service/definitions/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ template <>
struct JsonObjectConverter<ThirdPartyUser> {
static void dumpTo(QJsonObject& jo, const ThirdPartyUser& pod)
{
addParam<>(jo, "userid"_L1, pod.userid);
addParam<>(jo, "protocol"_L1, pod.protocol);
addParam<>(jo, "fields"_L1, pod.fields);
addParam(jo, "userid"_L1, pod.userid);
addParam(jo, "protocol"_L1, pod.protocol);
addParam(jo, "fields"_L1, pod.fields);
}
static void fillFrom(const QJsonObject& jo, ThirdPartyUser& pod)
{
Expand Down
56 changes: 28 additions & 28 deletions Quotient/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include <QtNetwork/QDnsLookup>
#include <qt6keychain/keychain.h>

#include <ranges>

using namespace Quotient;

namespace {
Expand Down Expand Up @@ -835,7 +837,7 @@ QFuture<Room*> Connection::getDirectChat(const QString& otherUserId)
continue;
qCDebug(MAIN) << "Requested direct chat with" << otherUserId
<< "is already available as" << r->id();
return QtFuture::makeReadyFuture(r);
return makeReadyValueFuture(r);
}
if (auto ir = invitation(roomId)) {
Q_ASSERT(ir->id() == roomId);
Expand Down Expand Up @@ -1123,7 +1125,7 @@ QVector<Room*> Connection::allRooms() const
{
QVector<Room*> result;
result.resize(d->roomMap.size());
std::copy(d->roomMap.cbegin(), d->roomMap.cend(), result.begin());
std::ranges::copy(d->roomMap, result.begin());
return result;
}

Expand Down Expand Up @@ -1203,9 +1205,8 @@ QStringList Connection::tagNames() const
QVector<Room*> Connection::roomsWithTag(const QString& tagName) const
{
QVector<Room*> rooms;
std::copy_if(d->roomMap.cbegin(), d->roomMap.cend(),
std::back_inserter(rooms),
[&tagName](Room* r) { return r->tags().contains(tagName); });
std::ranges::copy_if(d->roomMap, std::back_inserter(rooms),
[&tagName](Room* r) { return r->tags().contains(tagName); });
return rooms;
}

Expand Down Expand Up @@ -1657,31 +1658,30 @@ void Connection::enableDirectChatEncryption(bool enable)
emit directChatsEncryptionChanged(enable);
}

inline bool roomVersionLess(const Connection::SupportedRoomVersion& v1,
const Connection::SupportedRoomVersion& v2)
{
bool ok1 = false, ok2 = false;
const auto vNum1 = v1.id.toFloat(&ok1);
const auto vNum2 = v2.id.toFloat(&ok2);
return ok1 && ok2 ? vNum1 < vNum2 : v1.id < v2.id;
}

QVector<Connection::SupportedRoomVersion> Connection::availableRoomVersions() const
{
QVector<SupportedRoomVersion> result;
if (d->capabilities.roomVersions) {
const auto& allVersions = d->capabilities.roomVersions->available;
result.reserve(allVersions.size());
for (auto it = allVersions.begin(); it != allVersions.end(); ++it)
result.push_back({ it.key(), it.value() });
// Put stable versions over unstable; within each group,
// sort numeric versions as numbers, the rest as strings.
const auto mid =
std::partition(result.begin(), result.end(),
std::mem_fn(&SupportedRoomVersion::isStable));
std::sort(result.begin(), mid, roomVersionLess);
std::sort(mid, result.end(), roomVersionLess);
}
if (!d->capabilities.roomVersions)
return {};

// Can't stuff QKeyValueRange in a std:: view directly because it's not move-assignable and
// most views require that - using std::views::all to go around this
// TODO: use std::ranges::to() when all toolchains support it
const auto allVersions = d->capabilities.roomVersions->available.asKeyValueRange();
auto allVersionsView = std::views::all(allVersions) | std::views::transform([](const auto& p) {
return SupportedRoomVersion{ p.first, p.second };
});
QVector<SupportedRoomVersion> result(allVersionsView.begin(), allVersionsView.end());
// Put stable versions over unstable; for
std::ranges::sort(result, [](const SupportedRoomVersion& v1, const SupportedRoomVersion& v2) {
if (const auto stable1 = v1.isStable(), stable2 = v2.isStable(); stable1 != stable2)
return stable1 && !stable2; // Put all stable versions over unstable
// For two versions with the same stability, if both versions are numeric order them as
// numbers, otherwise compare strings.
bool ok1 = false, ok2 = false;
const auto vNum1 = v1.id.toFloat(&ok1);
const auto vNum2 = v2.id.toFloat(&ok2);
return ok1 && ok2 ? vNum1 < vNum2 : v1.id < v2.id;
});
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion Quotient/connectiondata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void ConnectionData::submit(BaseJob* job)
{
job->setStatus(BaseJob::Pending);
if (!d->rateLimiter.isActive()) {
QTimer::singleShot(0, job, &BaseJob::sendRequest);
QMetaObject::invokeMethod(job, &BaseJob::sendRequest, Qt::QueuedConnection);
return;
}
d->jobs[size_t(job->isBackground())].emplace(job);
Expand Down
8 changes: 3 additions & 5 deletions Quotient/connectionencryptiondata_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ QFuture<void> setupPicklingKey(Connection* connection,
qDebug(E2EE) << "Successfully loaded pickling key from keychain";
encryptionData = std::make_unique<ConnectionEncryptionData>(
connection, PicklingKey::fromByteArray(std::move(data)));
return QtFuture::makeReadyFuture<Job*>(nullptr);
return makeReadyValueFuture<Job*>(nullptr);
}
qCritical(E2EE)
<< "The pickling key loaded from" << keychainId << "has length"
Expand Down Expand Up @@ -592,8 +592,7 @@ void ConnectionEncryptionData::handleDevicesList(
<< device.userId << user;
continue;
}
if (!std::all_of(device.algorithms.cbegin(),
device.algorithms.cend(), isSupportedAlgorithm)) {
if (!std::ranges::all_of(device.algorithms, isSupportedAlgorithm)) {
qWarning(E2EE) << "Unsupported encryption algorithms found"
<< device.algorithms;
continue;
Expand Down Expand Up @@ -755,8 +754,7 @@ std::pair<QByteArray, QByteArray> doDecryptMessage(const QOlmSession& session,
{
const auto expectedMessage = session.decrypt(message);
if (expectedMessage) {
const auto result =
std::make_pair(*expectedMessage, session.sessionId());
const auto result = std::pair{ *expectedMessage, session.sessionId() };
andThen();
return result;
}
Expand Down
22 changes: 11 additions & 11 deletions Quotient/csapi/administrative_contact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Post3PIDsJob::Post3PIDsJob(const ThreePidCredentials& threePidCreds)
: BaseJob(HttpVerb::Post, u"Post3PIDsJob"_s, makePath("/_matrix/client/v3", "/account/3pid"))
{
QJsonObject _dataJson;
addParam<>(_dataJson, "three_pid_creds"_L1, threePidCreds);
addParam(_dataJson, "three_pid_creds"_L1, threePidCreds);
setRequestData({ _dataJson });
}

Expand All @@ -28,8 +28,8 @@ Add3PIDJob::Add3PIDJob(const QString& clientSecret, const QString& sid,
{
QJsonObject _dataJson;
addParam<IfNotEmpty>(_dataJson, "auth"_L1, auth);
addParam<>(_dataJson, "client_secret"_L1, clientSecret);
addParam<>(_dataJson, "sid"_L1, sid);
addParam(_dataJson, "client_secret"_L1, clientSecret);
addParam(_dataJson, "sid"_L1, sid);
setRequestData({ _dataJson });
}

Expand All @@ -38,10 +38,10 @@ Bind3PIDJob::Bind3PIDJob(const QString& clientSecret, const QString& idServer,
: BaseJob(HttpVerb::Post, u"Bind3PIDJob"_s, makePath("/_matrix/client/v3", "/account/3pid/bind"))
{
QJsonObject _dataJson;
addParam<>(_dataJson, "client_secret"_L1, clientSecret);
addParam<>(_dataJson, "id_server"_L1, idServer);
addParam<>(_dataJson, "id_access_token"_L1, idAccessToken);
addParam<>(_dataJson, "sid"_L1, sid);
addParam(_dataJson, "client_secret"_L1, clientSecret);
addParam(_dataJson, "id_server"_L1, idServer);
addParam(_dataJson, "id_access_token"_L1, idAccessToken);
addParam(_dataJson, "sid"_L1, sid);
setRequestData({ _dataJson });
}

Expand All @@ -52,8 +52,8 @@ Delete3pidFromAccountJob::Delete3pidFromAccountJob(const QString& medium, const
{
QJsonObject _dataJson;
addParam<IfNotEmpty>(_dataJson, "id_server"_L1, idServer);
addParam<>(_dataJson, "medium"_L1, medium);
addParam<>(_dataJson, "address"_L1, address);
addParam(_dataJson, "medium"_L1, medium);
addParam(_dataJson, "address"_L1, address);
setRequestData({ _dataJson });
addExpectedKey(u"id_server_unbind_result"_s);
}
Expand All @@ -65,8 +65,8 @@ Unbind3pidFromAccountJob::Unbind3pidFromAccountJob(const QString& medium, const
{
QJsonObject _dataJson;
addParam<IfNotEmpty>(_dataJson, "id_server"_L1, idServer);
addParam<>(_dataJson, "medium"_L1, medium);
addParam<>(_dataJson, "address"_L1, address);
addParam(_dataJson, "medium"_L1, medium);
addParam(_dataJson, "address"_L1, address);
setRequestData({ _dataJson });
addExpectedKey(u"id_server_unbind_result"_s);
}
Expand Down
8 changes: 4 additions & 4 deletions Quotient/csapi/administrative_contact.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ template <>
struct QUOTIENT_API JsonObjectConverter<Post3PIDsJob::ThreePidCredentials> {
static void dumpTo(QJsonObject& jo, const Post3PIDsJob::ThreePidCredentials& pod)
{
addParam<>(jo, "client_secret"_L1, pod.clientSecret);
addParam<>(jo, "id_server"_L1, pod.idServer);
addParam<>(jo, "id_access_token"_L1, pod.idAccessToken);
addParam<>(jo, "sid"_L1, pod.sid);
addParam(jo, "client_secret"_L1, pod.clientSecret);
addParam(jo, "id_server"_L1, pod.idServer);
addParam(jo, "id_access_token"_L1, pod.idAccessToken);
addParam(jo, "sid"_L1, pod.sid);
}
};
QT_WARNING_POP
Expand Down
2 changes: 1 addition & 1 deletion Quotient/csapi/appservice_room_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ UpdateAppserviceRoomDirectoryVisibilityJob::UpdateAppserviceRoomDirectoryVisibil
false)
{
QJsonObject _dataJson;
addParam<>(_dataJson, "visibility"_L1, visibility);
addParam(_dataJson, "visibility"_L1, visibility);
setRequestData({ _dataJson });
}
6 changes: 3 additions & 3 deletions Quotient/csapi/authed-content-repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ auto queryToGetContentThumbnailAuthed(int width, int height, const QString& meth
qint64 timeoutMs, std::optional<bool> animated)
{
QUrlQuery _q;
addParam<>(_q, u"width"_s, width);
addParam<>(_q, u"height"_s, height);
addParam(_q, u"width"_s, width);
addParam(_q, u"height"_s, height);
addParam<IfNotEmpty>(_q, u"method"_s, method);
addParam<IfNotEmpty>(_q, u"timeout_ms"_s, timeoutMs);
addParam<IfNotEmpty>(_q, u"animated"_s, animated);
Expand Down Expand Up @@ -96,7 +96,7 @@ GetContentThumbnailAuthedJob::GetContentThumbnailAuthedJob(const QString& server
auto queryToGetUrlPreviewAuthed(const QUrl& url, std::optional<qint64> ts)
{
QUrlQuery _q;
addParam<>(_q, u"url"_s, url);
addParam(_q, u"url"_s, url);
addParam<IfNotEmpty>(_q, u"ts"_s, ts);
return _q;
}
Expand Down
4 changes: 2 additions & 2 deletions Quotient/csapi/banning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ BanJob::BanJob(const QString& roomId, const QString& userId, const QString& reas
: BaseJob(HttpVerb::Post, u"BanJob"_s, makePath("/_matrix/client/v3", "/rooms/", roomId, "/ban"))
{
QJsonObject _dataJson;
addParam<>(_dataJson, "user_id"_L1, userId);
addParam(_dataJson, "user_id"_L1, userId);
addParam<IfNotEmpty>(_dataJson, "reason"_L1, reason);
setRequestData({ _dataJson });
}
Expand All @@ -18,7 +18,7 @@ UnbanJob::UnbanJob(const QString& roomId, const QString& userId, const QString&
makePath("/_matrix/client/v3", "/rooms/", roomId, "/unban"))
{
QJsonObject _dataJson;
addParam<>(_dataJson, "user_id"_L1, userId);
addParam(_dataJson, "user_id"_L1, userId);
addParam<IfNotEmpty>(_dataJson, "reason"_L1, reason);
setRequestData({ _dataJson });
}
2 changes: 1 addition & 1 deletion Quotient/csapi/capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ template <>
struct JsonObjectConverter<BooleanCapability> {
static void dumpTo(QJsonObject& jo, const BooleanCapability& pod)
{
addParam<>(jo, "enabled"_L1, pod.enabled);
addParam(jo, "enabled"_L1, pod.enabled);
}
static void fillFrom(const QJsonObject& jo, BooleanCapability& pod)
{
Expand Down
6 changes: 3 additions & 3 deletions Quotient/csapi/content-repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ auto queryToGetContentThumbnail(int width, int height, const QString& method, bo
qint64 timeoutMs, bool allowRedirect, std::optional<bool> animated)
{
QUrlQuery _q;
addParam<>(_q, u"width"_s, width);
addParam<>(_q, u"height"_s, height);
addParam(_q, u"width"_s, width);
addParam(_q, u"height"_s, height);
addParam<IfNotEmpty>(_q, u"method"_s, method);
addParam<IfNotEmpty>(_q, u"allow_remote"_s, allowRemote);
addParam<IfNotEmpty>(_q, u"timeout_ms"_s, timeoutMs);
Expand Down Expand Up @@ -151,7 +151,7 @@ GetContentThumbnailJob::GetContentThumbnailJob(const QString& serverName, const
auto queryToGetUrlPreview(const QUrl& url, std::optional<qint64> ts)
{
QUrlQuery _q;
addParam<>(_q, u"url"_s, url);
addParam(_q, u"url"_s, url);
addParam<IfNotEmpty>(_q, u"ts"_s, ts);
return _q;
}
Expand Down
12 changes: 6 additions & 6 deletions Quotient/csapi/create_room.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,19 @@ template <>
struct QUOTIENT_API JsonObjectConverter<CreateRoomJob::Invite3pid> {
static void dumpTo(QJsonObject& jo, const CreateRoomJob::Invite3pid& pod)
{
addParam<>(jo, "id_server"_L1, pod.idServer);
addParam<>(jo, "id_access_token"_L1, pod.idAccessToken);
addParam<>(jo, "medium"_L1, pod.medium);
addParam<>(jo, "address"_L1, pod.address);
addParam(jo, "id_server"_L1, pod.idServer);
addParam(jo, "id_access_token"_L1, pod.idAccessToken);
addParam(jo, "medium"_L1, pod.medium);
addParam(jo, "address"_L1, pod.address);
}
};

template <>
struct QUOTIENT_API JsonObjectConverter<CreateRoomJob::StateEvent> {
static void dumpTo(QJsonObject& jo, const CreateRoomJob::StateEvent& pod)
{
addParam<>(jo, "type"_L1, pod.type);
addParam<>(jo, "content"_L1, pod.content);
addParam(jo, "type"_L1, pod.type);
addParam(jo, "content"_L1, pod.content);
addParam<IfNotEmpty>(jo, "state_key"_L1, pod.stateKey);
}
};
Expand Down
2 changes: 1 addition & 1 deletion Quotient/csapi/definitions/client_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ template <>
struct JsonObjectConverter<Device> {
static void dumpTo(QJsonObject& jo, const Device& pod)
{
addParam<>(jo, "device_id"_L1, pod.deviceId);
addParam(jo, "device_id"_L1, pod.deviceId);
addParam<IfNotEmpty>(jo, "display_name"_L1, pod.displayName);
addParam<IfNotEmpty>(jo, "last_seen_ip"_L1, pod.lastSeenIp);
addParam<IfNotEmpty>(jo, "last_seen_ts"_L1, pod.lastSeenTs);
Expand Down
6 changes: 3 additions & 3 deletions Quotient/csapi/definitions/cross_signing_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ template <>
struct JsonObjectConverter<CrossSigningKey> {
static void dumpTo(QJsonObject& jo, const CrossSigningKey& pod)
{
addParam<>(jo, "user_id"_L1, pod.userId);
addParam<>(jo, "usage"_L1, pod.usage);
addParam<>(jo, "keys"_L1, pod.keys);
addParam(jo, "user_id"_L1, pod.userId);
addParam(jo, "usage"_L1, pod.usage);
addParam(jo, "keys"_L1, pod.keys);
addParam<IfNotEmpty>(jo, "signatures"_L1, pod.signatures);
}
static void fillFrom(const QJsonObject& jo, CrossSigningKey& pod)
Expand Down
Loading

0 comments on commit 688915f

Please sign in to comment.