Port away from commitSingleShot

This commit is contained in:
Tobias Fella
2024-06-08 15:39:27 +02:00
parent 91109ca845
commit 29972b5867
7 changed files with 197 additions and 104 deletions

View File

@@ -106,11 +106,16 @@ Controller::Controller(QObject *parent)
connect(connection, &NeoChatConnection::syncDone, this, [connection]() { connect(connection, &NeoChatConnection::syncDone, this, [connection]() {
NotificationsManager::instance().handleNotifications(connection); NotificationsManager::instance().handleNotifications(connection);
}); });
connectSingleShot(connection, &NeoChatConnection::syncDone, this, [this, connection] { connect(
if (!m_endpoint.isEmpty()) { connection,
connection->setupPushNotifications(m_endpoint); &NeoChatConnection::syncDone,
} this,
}); [this, connection] {
if (!m_endpoint.isEmpty()) {
connection->setupPushNotifications(m_endpoint);
}
},
Qt::SingleShotConnection);
} }
oldAccountCount = m_accountRegistry.size(); oldAccountCount = m_accountRegistry.size();
}); });

View File

@@ -54,14 +54,19 @@ void LoginHelper::init()
m_connection = new NeoChatConnection(); m_connection = new NeoChatConnection();
} }
m_connection->resolveServer(m_matrixId); m_connection->resolveServer(m_matrixId);
connectSingleShot(m_connection.get(), &Connection::loginFlowsChanged, this, [this]() { connect(
setHomeserverReachable(true); m_connection.get(),
m_testing = false; &Connection::loginFlowsChanged,
Q_EMIT testingChanged(); this,
m_supportsSso = m_connection->supportsSso(); [this]() {
m_supportsPassword = m_connection->supportsPasswordAuth(); setHomeserverReachable(true);
Q_EMIT loginFlowsChanged(); m_testing = false;
}); Q_EMIT testingChanged();
m_supportsSso = m_connection->supportsSso();
m_supportsPassword = m_connection->supportsPasswordAuth();
Q_EMIT loginFlowsChanged();
},
Qt::SingleShotConnection);
}); });
connect(m_connection, &Connection::connected, this, [this] { connect(m_connection, &Connection::connected, this, [this] {
Q_EMIT connected(); Q_EMIT connected();
@@ -100,9 +105,14 @@ void LoginHelper::init()
Q_EMIT Controller::instance().errorOccured(i18n("Network Error"), std::move(error)); Q_EMIT Controller::instance().errorOccured(i18n("Network Error"), std::move(error));
}); });
connectSingleShot(m_connection.get(), &Connection::syncDone, this, [this]() { connect(
Q_EMIT loaded(); m_connection.get(),
}); &Connection::syncDone,
this,
[this]() {
Q_EMIT loaded();
},
Qt::SingleShotConnection);
} }
void LoginHelper::setHomeserverReachable(bool reachable) void LoginHelper::setHomeserverReachable(bool reachable)
@@ -182,11 +192,16 @@ QUrl LoginHelper::ssoUrl() const
void LoginHelper::loginWithSso() void LoginHelper::loginWithSso()
{ {
m_connection->resolveServer(m_matrixId); m_connection->resolveServer(m_matrixId);
connectSingleShot(m_connection.get(), &Connection::loginFlowsChanged, this, [this]() { connect(
SsoSession *session = m_connection->prepareForSso(m_deviceName); m_connection.get(),
m_ssoUrl = session->ssoUrl(); &Connection::loginFlowsChanged,
Q_EMIT ssoUrlChanged(); this,
}); [this]() {
SsoSession *session = m_connection->prepareForSso(m_deviceName);
m_ssoUrl = session->ssoUrl();
Q_EMIT ssoUrlChanged();
},
Qt::SingleShotConnection);
} }
bool LoginHelper::testing() const bool LoginHelper::testing() const

View File

@@ -321,9 +321,14 @@ void NeoChatConnection::createRoom(const QString &name, const QString &topic, co
connect(job, &CreateRoomJob::failure, this, [job] { connect(job, &CreateRoomJob::failure, this, [job] {
Q_EMIT Controller::instance().errorOccured(i18n("Room creation failed: %1", job->errorString()), {}); Q_EMIT Controller::instance().errorOccured(i18n("Room creation failed: %1", job->errorString()), {});
}); });
connectSingleShot(this, &Connection::newRoom, this, [](Room *room) { connect(
RoomManager::instance().resolveResource(room->id()); this,
}); &Connection::newRoom,
this,
[](Room *room) {
RoomManager::instance().resolveResource(room->id());
},
Qt::SingleShotConnection);
} }
void NeoChatConnection::createSpace(const QString &name, const QString &topic, const QString &parent, bool setChildParent) void NeoChatConnection::createSpace(const QString &name, const QString &topic, const QString &parent, bool setChildParent)
@@ -353,9 +358,14 @@ void NeoChatConnection::createSpace(const QString &name, const QString &topic, c
connect(job, &CreateRoomJob::failure, this, [job] { connect(job, &CreateRoomJob::failure, this, [job] {
Q_EMIT Controller::instance().errorOccured(i18n("Space creation failed: %1", job->errorString()), {}); Q_EMIT Controller::instance().errorOccured(i18n("Space creation failed: %1", job->errorString()), {});
}); });
connectSingleShot(this, &Connection::newRoom, this, [](Room *room) { connect(
RoomManager::instance().resolveResource(room->id()); this,
}); &Connection::newRoom,
this,
[](Room *room) {
RoomManager::instance().resolveResource(room->id());
},
Qt::SingleShotConnection);
} }
bool NeoChatConnection::directChatExists(Quotient::User *user) bool NeoChatConnection::directChatExists(Quotient::User *user)
@@ -384,9 +394,14 @@ void NeoChatConnection::openOrCreateDirectChat(User *user)
} }
} }
requestDirectChat(user); requestDirectChat(user);
connectSingleShot(this, &Connection::directChatAvailable, this, [=](auto room) { connect(
room->activateEncryption(); this,
}); &Connection::directChatAvailable,
this,
[=](auto room) {
room->activateEncryption();
},
Qt::SingleShotConnection);
} }
qsizetype NeoChatConnection::directChatNotifications() const qsizetype NeoChatConnection::directChatNotifications() const

View File

@@ -96,23 +96,28 @@ NeoChatRoom::NeoChatRoom(Connection *connection, QString roomId, JoinState joinS
}); });
connect(this, &Room::displaynameChanged, this, &NeoChatRoom::displayNameChanged); connect(this, &Room::displaynameChanged, this, &NeoChatRoom::displayNameChanged);
connectSingleShot(this, &Room::baseStateLoaded, this, [this]() { connect(
updatePushNotificationState(QStringLiteral("m.push_rules")); this,
&Room::baseStateLoaded,
this,
[this]() {
updatePushNotificationState(QStringLiteral("m.push_rules"));
Q_EMIT canEncryptRoomChanged(); Q_EMIT canEncryptRoomChanged();
if (this->joinState() != JoinState::Invite) { if (this->joinState() != JoinState::Invite) {
return; return;
} }
auto roomMemberEvent = currentState().get<RoomMemberEvent>(localUser()->id()); auto roomMemberEvent = currentState().get<RoomMemberEvent>(localUser()->id());
QImage avatar_image; QImage avatar_image;
if (roomMemberEvent && !user(roomMemberEvent->senderId())->avatarUrl(this).isEmpty()) { if (roomMemberEvent && !user(roomMemberEvent->senderId())->avatarUrl(this).isEmpty()) {
avatar_image = user(roomMemberEvent->senderId())->avatar(128, this); avatar_image = user(roomMemberEvent->senderId())->avatar(128, this);
} else { } else {
qWarning() << "using this room's avatar"; qWarning() << "using this room's avatar";
avatar_image = avatar(128); avatar_image = avatar(128);
} }
NotificationsManager::instance().postInviteNotification(this, displayName(), htmlSafeMemberName(roomMemberEvent->senderId()), avatar_image); NotificationsManager::instance().postInviteNotification(this, displayName(), htmlSafeMemberName(roomMemberEvent->senderId()), avatar_image);
}); },
Qt::SingleShotConnection);
connect(this, &Room::changed, this, [this] { connect(this, &Room::changed, this, [this] {
Q_EMIT canEncryptRoomChanged(); Q_EMIT canEncryptRoomChanged();
Q_EMIT parentIdsChanged(); Q_EMIT parentIdsChanged();

View File

@@ -112,9 +112,14 @@ void Registration::registerAccount()
account.sync(); account.sync();
Controller::instance().addConnection(connection); Controller::instance().addConnection(connection);
Controller::instance().setActiveConnection(connection); Controller::instance().setActiveConnection(connection);
connectSingleShot(connection, &Connection::syncDone, this, []() { connect(
Q_EMIT LoginHelper::instance().loaded(); connection,
}); &Connection::syncDone,
this,
[]() {
Q_EMIT LoginHelper::instance().loaded();
},
Qt::SingleShotConnection);
m_connection = nullptr; m_connection = nullptr;
}); });
@@ -172,28 +177,33 @@ void Registration::testHomeserver()
m_connection = new NeoChatConnection(this); m_connection = new NeoChatConnection(this);
m_connection->resolveServer("@user:%1"_ls.arg(m_homeserver)); m_connection->resolveServer("@user:%1"_ls.arg(m_homeserver));
connectSingleShot(m_connection.data(), &Connection::loginFlowsChanged, this, [this]() { connect(
if (m_testServerJob) { m_connection.data(),
delete m_testServerJob; &Connection::loginFlowsChanged,
} this,
m_testServerJob = m_connection->callApi<NeoChatRegisterJob>("user"_ls, none, "user"_ls, QString(), QString(), QString(), false); [this]() {
connect(m_testServerJob.data(), &BaseJob::finished, this, [this]() { if (m_testServerJob) {
if (m_testServerJob->error() == BaseJob::StatusCode::ContentAccessError) { delete m_testServerJob;
setStatus(ServerNoRegistration);
return;
} }
if (m_testServerJob->status().code != 106) { m_testServerJob = m_connection->callApi<NeoChatRegisterJob>("user"_ls, none, "user"_ls, QString(), QString(), QString(), false);
setStatus(InvalidServer); connect(m_testServerJob.data(), &BaseJob::finished, this, [this]() {
return; if (m_testServerJob->error() == BaseJob::StatusCode::ContentAccessError) {
} setStatus(ServerNoRegistration);
if (!m_username.isEmpty()) { return;
setStatus(TestingUsername); }
testUsername(); if (m_testServerJob->status().code != 106) {
} else { setStatus(InvalidServer);
setStatus(NoUsername); return;
} }
}); if (!m_username.isEmpty()) {
}); setStatus(TestingUsername);
testUsername();
} else {
setStatus(NoUsername);
}
});
},
Qt::SingleShotConnection);
} }
void Registration::setUsername(const QString &username) void Registration::setUsername(const QString &username)

View File

@@ -150,9 +150,14 @@ void RoomManager::resolveResource(const QString &idOrUri, const QString &action)
Q_ASSERT(result == Quotient::UriResolved); Q_ASSERT(result == Quotient::UriResolved);
if (uri.type() == Uri::RoomAlias || uri.type() == Uri::RoomId) { if (uri.type() == Uri::RoomAlias || uri.type() == Uri::RoomId) {
connectSingleShot(m_connection.get(), &NeoChatConnection::newRoom, this, [this, uri](Room *room) { connect(
resolveResource(room->id()); m_connection.get(),
}); &NeoChatConnection::newRoom,
this,
[this, uri](Room *room) {
resolveResource(room->id());
},
Qt::SingleShotConnection);
} }
} }
} }
@@ -303,18 +308,32 @@ void RoomManager::joinRoom(Quotient::Connection *account, const QString &roomAli
{ {
auto job = account->joinRoom(roomAliasOrId, viaServers); auto job = account->joinRoom(roomAliasOrId, viaServers);
#if Quotient_VERSION_MINOR > 8 #if Quotient_VERSION_MINOR > 8
connectSingleShot(job.get(), &Quotient::BaseJob::finished, this, [this, account](Quotient::BaseJob *finish) { connect(
job.get(),
&Quotient::BaseJob::finished,
this,
[this, account](Quotient::BaseJob *finish) {
#else #else
connectSingleShot(job, &Quotient::BaseJob::finished, this, [this, account](Quotient::BaseJob *finish) { connect(
job,
&Quotient::BaseJob::finished,
this,
[this, account](Quotient::BaseJob *finish) {
#endif #endif
if (finish->status() == Quotient::BaseJob::Success) { if (finish->status() == Quotient::BaseJob::Success) {
connectSingleShot(account, &NeoChatConnection::newRoom, this, [this](Quotient::Room *room) { connect(
resolveResource(room->id()); account,
}); &NeoChatConnection::newRoom,
} else { this,
Q_EMIT warning(i18n("Failed to join room"), finish->errorString()); [this](Quotient::Room *room) {
} resolveResource(room->id());
}); },
Qt::SingleShotConnection);
} else {
Q_EMIT warning(i18n("Failed to join room"), finish->errorString());
}
},
Qt::SingleShotConnection);
} }
void RoomManager::knockRoom(NeoChatConnection *account, const QString &roomAliasOrId, const QString &reason, const QStringList &viaServers) void RoomManager::knockRoom(NeoChatConnection *account, const QString &roomAliasOrId, const QString &reason, const QStringList &viaServers)
@@ -325,18 +344,32 @@ void RoomManager::knockRoom(NeoChatConnection *account, const QString &roomAlias
// create it in Join state. finished() is used here instead of success() // create it in Join state. finished() is used here instead of success()
// to overtake clients that may add their own slots to finished(). // to overtake clients that may add their own slots to finished().
#if Quotient_VERSION_MINOR > 8 #if Quotient_VERSION_MINOR > 8
connectSingleShot(job.get(), &BaseJob::finished, this, [this, job, account] { connect(
job.get(),
&BaseJob::finished,
this,
[this, job, account] {
#else #else
connectSingleShot(job, &BaseJob::finished, this, [this, job, account] { connect(
job,
&BaseJob::finished,
this,
[this, job, account] {
#endif #endif
if (job->status() == Quotient::BaseJob::Success) { if (job->status() == Quotient::BaseJob::Success) {
connectSingleShot(account, &NeoChatConnection::newRoom, this, [this](Quotient::Room *room) { connect(
Q_EMIT currentRoom()->showMessage(NeoChatRoom::Info, i18n("You requested to join '%1'", room->name())); account,
}); &NeoChatConnection::newRoom,
} else { this,
Q_EMIT warning(i18n("Failed to request joining room"), job->errorString()); [this](Quotient::Room *room) {
} Q_EMIT currentRoom() -> showMessage(NeoChatRoom::Info, i18n("You requested to join '%1'", room->name()));
}); },
Qt::SingleShotConnection);
} else {
Q_EMIT warning(i18n("Failed to request joining room"), job->errorString());
}
},
Qt::SingleShotConnection);
} }
bool RoomManager::visitNonMatrix(const QUrl &url) bool RoomManager::visitNonMatrix(const QUrl &url)

View File

@@ -32,11 +32,16 @@ void SpaceHierarchyCache::cacheSpaceHierarchy()
if (neoChatRoom->isSpace()) { if (neoChatRoom->isSpace()) {
populateSpaceHierarchy(neoChatRoom->id()); populateSpaceHierarchy(neoChatRoom->id());
} else { } else {
connectSingleShot(neoChatRoom, &Room::baseStateLoaded, neoChatRoom, [this, neoChatRoom]() { connect(
if (neoChatRoom->isSpace()) { neoChatRoom,
populateSpaceHierarchy(neoChatRoom->id()); &Room::baseStateLoaded,
} neoChatRoom,
}); [this, neoChatRoom]() {
if (neoChatRoom->isSpace()) {
populateSpaceHierarchy(neoChatRoom->id());
}
},
Qt::SingleShotConnection);
} }
connect(neoChatRoom, &NeoChatRoom::unreadStatsChanged, this, [this, neoChatRoom]() { connect(neoChatRoom, &NeoChatRoom::unreadStatsChanged, this, [this, neoChatRoom]() {
@@ -97,12 +102,17 @@ void SpaceHierarchyCache::addBatch(const QString &spaceId, Quotient::GetSpaceHie
void SpaceHierarchyCache::addSpaceToHierarchy(Quotient::Room *room) void SpaceHierarchyCache::addSpaceToHierarchy(Quotient::Room *room)
{ {
connectSingleShot(room, &Quotient::Room::baseStateLoaded, this, [this, room]() { connect(
const auto neoChatRoom = static_cast<NeoChatRoom *>(room); room,
if (neoChatRoom->isSpace()) { &Quotient::Room::baseStateLoaded,
populateSpaceHierarchy(neoChatRoom->id()); this,
} [this, room]() {
}); const auto neoChatRoom = static_cast<NeoChatRoom *>(room);
if (neoChatRoom->isSpace()) {
populateSpaceHierarchy(neoChatRoom->id());
}
},
Qt::SingleShotConnection);
} }
void SpaceHierarchyCache::removeSpaceFromHierarchy(Quotient::Room *room) void SpaceHierarchyCache::removeSpaceFromHierarchy(Quotient::Room *room)