diff --git a/src/app/notificationsmanager.cpp b/src/app/notificationsmanager.cpp index 7fe012974..f38191d28 100644 --- a/src/app/notificationsmanager.cpp +++ b/src/app/notificationsmanager.cpp @@ -60,9 +60,8 @@ void NotificationsManager::startNotificationJob(QPointer conn } if (!m_connActiveJob.contains(connection->user()->id())) { - auto job = connection->callApi(); m_connActiveJob.append(connection->user()->id()); - connect(job, &BaseJob::success, this, [this, job, connection]() { + connection->callApi().onResult([this, connection](const auto &job) { m_connActiveJob.removeAll(connection->user()->id()); processNotificationJob(connection, job, !m_oldNotifications.contains(connection->user()->id())); }); diff --git a/src/libneochat/linkpreviewer.cpp b/src/libneochat/linkpreviewer.cpp index 7b85ea6ff..d37c6cf2e 100644 --- a/src/libneochat/linkpreviewer.cpp +++ b/src/libneochat/linkpreviewer.cpp @@ -61,14 +61,7 @@ void LinkPreviewer::loadUrlPreview() return; } - BaseJob *job = nullptr; - if (conn->supportedMatrixSpecVersions().contains("v1.11"_L1)) { - job = conn->callApi(m_url); - } else { - QT_IGNORE_DEPRECATIONS(job = conn->callApi(m_url);) - } - - connect(job, &BaseJob::success, this, [this, job, conn]() { + auto onSuccess = [this, conn](const auto &job) { const auto json = job->jsonData(); m_title = json["og:title"_L1].toString().trimmed(); m_description = json["og:description"_L1].toString().trimmed().replace("\n"_L1, " "_L1); @@ -85,7 +78,13 @@ void LinkPreviewer::loadUrlPreview() Q_EMIT descriptionChanged(); Q_EMIT imageSourceChanged(); Q_EMIT loadedChanged(); - }); + }; + + if (conn->supportedMatrixSpecVersions().contains("v1.11"_L1)) { + conn->callApi(m_url); + } else { + QT_IGNORE_DEPRECATIONS(conn->callApi(m_url).onResult(onSuccess);) + } } } diff --git a/src/libneochat/models/customemojimodel.cpp b/src/libneochat/models/customemojimodel.cpp index 180e464a0..04a85b985 100644 --- a/src/libneochat/models/customemojimodel.cpp +++ b/src/libneochat/models/customemojimodel.cpp @@ -66,9 +66,7 @@ void CustomEmojiModel::addEmoji(const QString &name, const QUrl &location) { using namespace Quotient; - auto job = m_connection->uploadFile(location.toLocalFile()); - - connect(job, &BaseJob::success, this, [name, location, job, this] { + m_connection->uploadFile(location.toLocalFile()).onResult([name, location, this](const auto &job) { const auto &data = m_connection->accountData("im.ponies.user_emotes"_L1); auto json = data != nullptr ? data->contentJson() : QJsonObject(); auto emojiData = json["images"_L1].toObject(); diff --git a/src/libneochat/neochatconnection.cpp b/src/libneochat/neochatconnection.cpp index a2bc2d732..ec303f6af 100644 --- a/src/libneochat/neochatconnection.cpp +++ b/src/libneochat/neochatconnection.cpp @@ -133,8 +133,7 @@ void NeoChatConnection::connectSignals() &Connection::connected, this, [this] { - auto job = callApi(BackgroundRequest); - connect(job, &GetVersionsJob::success, this, [this, job] { + callApi(BackgroundRequest).onResult([this](const auto &job) { m_canCheckMutualRooms = job->unstableFeatures().contains("uk.half-shot.msc2666.query_mutual_rooms"_L1); Q_EMIT canCheckMutualRoomsChanged(); m_canEraseData = job->unstableFeatures().contains("org.matrix.msc4025"_L1) || job->versions().count("v1.10"_L1); @@ -237,24 +236,22 @@ bool NeoChatConnection::canCheckMutualRooms() const void NeoChatConnection::changePassword(const QString ¤tPassword, const QString &newPassword) { - auto job = callApi(newPassword, false); - connect(job, &BaseJob::result, this, [this, job, currentPassword, newPassword] { - if (job->error() == 103) { - QJsonObject replyData = job->jsonData(); - AuthenticationData authData; - authData.session = replyData["session"_L1].toString(); - authData.type = "m.login.password"_L1; - authData.authInfo["password"_L1] = currentPassword; - authData.authInfo["user"_L1] = user()->id(); - authData.authInfo["identifier"_L1] = QJsonObject{{"type"_L1, "m.id.user"_L1}, {"user"_L1, user()->id()}}; - auto innerJob = callApi(newPassword, false, authData); - connect(innerJob, &BaseJob::success, this, [this]() { - Q_EMIT passwordStatus(PasswordStatus::Success); - }); - connect(innerJob, &BaseJob::failure, this, [innerJob, this]() { - Q_EMIT passwordStatus(innerJob->jsonData()["errcode"_L1] == "M_FORBIDDEN"_L1 ? PasswordStatus::Wrong : PasswordStatus::Other); - }); - } + callApi(newPassword, false).onFailure([this, currentPassword, newPassword](const auto &job) { + QJsonObject replyData = job->jsonData(); + AuthenticationData authData; + authData.session = replyData["session"_L1].toString(); + authData.type = "m.login.password"_L1; + authData.authInfo["password"_L1] = currentPassword; + authData.authInfo["user"_L1] = user()->id(); + authData.authInfo["identifier"_L1] = QJsonObject{{"type"_L1, "m.id.user"_L1}, {"user"_L1, user()->id()}}; + auto innerJob = callApi(newPassword, false, authData) + .then( + [this]() { + Q_EMIT passwordStatus(PasswordStatus::Success); + }, + [this](const auto &job) { + Q_EMIT passwordStatus(job->jsonData()["errcode"_L1] == "M_FORBIDDEN"_L1 ? PasswordStatus::Wrong : PasswordStatus::Other); + }); }); } @@ -274,22 +271,18 @@ QString NeoChatConnection::label() const void NeoChatConnection::deactivateAccount(const QString &password, const bool erase) { - auto job = callApi(); - connect(job, &BaseJob::result, this, [this, job, password, erase] { - if (job->error() == 103) { - QJsonObject replyData = job->jsonData(); - AuthenticationData authData; - authData.session = replyData["session"_L1].toString(); - authData.authInfo["password"_L1] = password; - authData.type = "m.login.password"_L1; - authData.authInfo["user"_L1] = user()->id(); - QJsonObject identifier = {{"type"_L1, "m.id.user"_L1}, {"user"_L1, user()->id()}}; - authData.authInfo["identifier"_L1] = identifier; - auto innerJob = callApi(authData, QString{}, erase); - connect(innerJob, &BaseJob::success, this, [this]() { - logout(false); - }); - } + callApi().onFailure([password, erase, this](const auto &job) { + QJsonObject replyData = job->jsonData(); + AuthenticationData authData; + authData.session = replyData["session"_L1].toString(); + authData.authInfo["password"_L1] = password; + authData.type = "m.login.password"_L1; + authData.authInfo["user"_L1] = user()->id(); + QJsonObject identifier = {{"type"_L1, "m.id.user"_L1}, {"user"_L1, user()->id()}}; + authData.authInfo["identifier"_L1] = identifier; + callApi(authData, QString{}, erase).onResult([this]() { + logout(false); + }); }); } @@ -342,19 +335,19 @@ void NeoChatConnection::createRoom(const QString &name, const QString &topic, co }); } - const auto job = Connection::createRoom(Connection::PublishRoom, QString(), name, topic, QStringList(), {}, {}, {}, initialStateEvents); - if (!parent.isEmpty()) { - connect(job, &Quotient::CreateRoomJob::success, this, [this, parent, setChildParent, job]() { - if (setChildParent) { + Connection::createRoom(Connection::PublishRoom, QString(), name, topic, QStringList(), {}, {}, {}, initialStateEvents) + .then( + [parent, setChildParent, this](const auto &job) { + if (parent.isEmpty() || !setChildParent) { + return; + } if (auto parentRoom = room(parent)) { parentRoom->setState(u"m.space.child"_s, job->roomId(), QJsonObject{{"via"_L1, QJsonArray{domain()}}}); } - } - }); - } - connect(job, &CreateRoomJob::failure, this, [this, job] { - Q_EMIT errorOccured(i18n("Room creation failed: %1", job->errorString())); - }); + }, + [this](const auto &job) { + Q_EMIT errorOccured(i18n("Room creation failed: %1", job->errorString())); + }); } void NeoChatConnection::createSpace(const QString &name, const QString &topic, const QString &parent, bool setChildParent) @@ -371,20 +364,19 @@ void NeoChatConnection::createSpace(const QString &name, const QString &topic, c }); } - const auto job = - Connection::createRoom(Connection::UnpublishRoom, {}, name, topic, {}, {}, {}, false, initialStateEvents, {}, QJsonObject{{"type"_L1, "m.space"_L1}}); - if (!parent.isEmpty()) { - connect(job, &Quotient::CreateRoomJob::success, this, [this, parent, setChildParent, job]() { - if (setChildParent) { + Connection::createRoom(Connection::UnpublishRoom, {}, name, topic, {}, {}, {}, false, initialStateEvents, {}, QJsonObject{{"type"_L1, "m.space"_L1}}) + .then( + [parent, setChildParent, this](const auto &job) { + if (parent.isEmpty() || !setChildParent) { + return; + } if (auto parentRoom = room(parent)) { parentRoom->setState(u"m.space.child"_s, job->roomId(), QJsonObject{{"via"_L1, QJsonArray{domain()}}}); } - } - }); - } - connect(job, &CreateRoomJob::failure, this, [this, job] { - Q_EMIT errorOccured(i18n("Space creation failed: %1", job->errorString())); - }); + }, + [this](const auto &job) { + Q_EMIT errorOccured(i18n("Space creation failed: %1", job->errorString())); + }); } Quotient::ForgetRoomJob *NeoChatConnection::forgetRoom(const QString &id) diff --git a/src/libneochat/neochatroom.cpp b/src/libneochat/neochatroom.cpp index cf53e96ff..182e32d8d 100644 --- a/src/libneochat/neochatroom.cpp +++ b/src/libneochat/neochatroom.cpp @@ -513,12 +513,9 @@ QUrl NeoChatRoom::avatarMediaUrl() const void NeoChatRoom::changeAvatar(const QUrl &localFile) { - const auto job = connection()->uploadFile(localFile.toLocalFile()); - if (isJobPending(job)) { - connect(job, &BaseJob::success, this, [this, job] { - connection()->callApi(id(), "m.room.avatar"_L1, QString(), QJsonObject{{"url"_L1, job->contentUri().toString()}}); - }); - } + connection()->uploadFile(localFile.toLocalFile()).onResult([this](const auto &job) { + connection()->callApi(id(), "m.room.avatar"_L1, QString(), QJsonObject{{"url"_L1, job->contentUri().toString()}}); + }); } void NeoChatRoom::toggleReaction(const QString &eventId, const QString &reaction) @@ -1115,10 +1112,8 @@ void NeoChatRoom::setPushNotificationState(PushNotificationState::State state) const QList conditions = {pushCondition}; // Add new override rule and make sure it's enabled - auto job = connection()->callApi("override"_L1, id(), actions, QString(), QString(), conditions, QString()); - connect(job, &BaseJob::success, this, [this]() { - auto enableJob = connection()->callApi("override"_L1, id(), true); - connect(enableJob, &BaseJob::success, this, [this]() { + connection()->callApi("override"_L1, id(), actions, QString(), QString(), conditions, QString()).onResult([this]() { + connection()->callApi("override"_L1, id(), true).onResult([this]() { m_pushNotificationStateUpdating = false; }); }); @@ -1141,10 +1136,8 @@ void NeoChatRoom::setPushNotificationState(PushNotificationState::State state) // No conditions for a room rule const QList conditions; - auto setJob = connection()->callApi("room"_L1, id(), actions, QString(), QString(), conditions, QString()); - connect(setJob, &BaseJob::success, this, [this]() { - auto enableJob = connection()->callApi("room"_L1, id(), true); - connect(enableJob, &BaseJob::success, this, [this]() { + connection()->callApi("room"_L1, id(), actions, QString(), QString(), conditions, QString()).onResult([this]() { + connection()->callApi("room"_L1, id(), true).onResult([this]() { m_pushNotificationStateUpdating = false; }); }); @@ -1172,10 +1165,8 @@ void NeoChatRoom::setPushNotificationState(PushNotificationState::State state) const QList conditions; // Add new room rule and make sure enabled - auto setJob = connection()->callApi("room"_L1, id(), actions, QString(), QString(), conditions, QString()); - connect(setJob, &BaseJob::success, this, [this]() { - auto enableJob = connection()->callApi("room"_L1, id(), true); - connect(enableJob, &BaseJob::success, this, [this]() { + connection()->callApi("room"_L1, id(), actions, QString(), QString(), conditions, QString()).onResult([this]() { + connection()->callApi("room"_L1, id(), true).onResult([this]() { m_pushNotificationStateUpdating = false; }); }); @@ -1242,11 +1233,8 @@ void NeoChatRoom::updatePushNotificationState(QString type) void NeoChatRoom::reportEvent(const QString &eventId, const QString &reason) { - auto job = connection()->callApi(id(), eventId, -50, reason); - connect(job, &BaseJob::finished, this, [this, job]() { - if (job->error() == BaseJob::Success) { - Q_EMIT showMessage(MessageType::Positive, i18n("Report sent successfully.")); - } + auto job = connection()->callApi(id(), eventId, -50, reason).onResult([this]() { + Q_EMIT showMessage(MessageType::Positive, i18n("Report sent successfully.")); }); } @@ -1541,13 +1529,11 @@ void NeoChatRoom::download(const QString &eventId, const QUrl &localFilename) void NeoChatRoom::mapAlias(const QString &alias) { - auto getLocalAliasesJob = connection()->callApi(id()); - connect(getLocalAliasesJob, &BaseJob::success, this, [this, getLocalAliasesJob, alias] { - if (getLocalAliasesJob->aliases().contains(alias)) { + connection()->callApi(id()).onResult([this, alias](const auto &job) { + if (job->aliases().contains(alias)) { return; } else { - auto setRoomAliasJob = connection()->callApi(alias, id()); - connect(setRoomAliasJob, &BaseJob::success, this, [this, alias] { + connection()->callApi(alias, id()).onResult([this, alias] { auto newAltAliases = altAliases(); newAltAliases.append(alias); setLocalAliases(newAltAliases); @@ -1639,23 +1625,25 @@ void NeoChatRoom::downloadEventFromServer(const QString &eventId) Q_EMIT extraEventLoaded(eventId); return; } - auto job = connection()->callApi(id(), eventId); - connect(job, &BaseJob::success, this, [this, job, eventId] { - // The event may have arrived in the meantime so check it's not in the timeline. - if (findInTimeline(eventId) != historyEdge()) { - Q_EMIT extraEventLoaded(eventId); - return; - } + connection() + ->callApi(id(), eventId) + .then( + [this, eventId](const auto &job) { + // The event may have arrived in the meantime so check it's not in the timeline. + if (findInTimeline(eventId) != historyEdge()) { + Q_EMIT extraEventLoaded(eventId); + return; + } - event_ptr_tt event = fromJson>(job->jsonData()); - m_extraEvents.push_back(std::move(event)); - Q_EMIT extraEventLoaded(eventId); - }); - connect(job, &BaseJob::failure, this, [this, job, eventId] { - if (job->error() == BaseJob::NotFound) { - Q_EMIT extraEventNotFound(eventId); - } - }); + event_ptr_tt event = fromJson>(job->jsonData()); + m_extraEvents.push_back(std::move(event)); + Q_EMIT extraEventLoaded(eventId); + }, + [this, eventId](const auto &job) { + if (job->error() == BaseJob::NotFound) { + Q_EMIT extraEventNotFound(eventId); + } + }); } std::pair NeoChatRoom::getEvent(const QString &eventId) const diff --git a/src/libneochat/spacehierarchycache.cpp b/src/libneochat/spacehierarchycache.cpp index 074717685..9fe5cd248 100644 --- a/src/libneochat/spacehierarchycache.cpp +++ b/src/libneochat/spacehierarchycache.cpp @@ -61,13 +61,12 @@ void SpaceHierarchyCache::populateSpaceHierarchy(const QString &spaceId) } m_nextBatchTokens[spaceId] = QString(); - auto job = m_connection->callApi(spaceId, std::nullopt, std::nullopt, std::nullopt, *m_nextBatchTokens[spaceId]); + m_connection->callApi(spaceId, std::nullopt, std::nullopt, std::nullopt, *m_nextBatchTokens[spaceId]) + .onResult([this, spaceId](const auto &job) { + addBatch(spaceId, job); + }); auto group = KConfigGroup(KSharedConfig::openStateConfig("SpaceHierarchy"_L1), "Cache"_L1); m_spaceHierarchy.insert(spaceId, group.readEntry(spaceId, QStringList())); - - connect(job, &BaseJob::success, this, [this, job, spaceId]() { - addBatch(spaceId, job); - }); } void SpaceHierarchyCache::addBatch(const QString &spaceId, Quotient::GetSpaceHierarchyJob *job) @@ -90,10 +89,10 @@ void SpaceHierarchyCache::addBatch(const QString &spaceId, Quotient::GetSpaceHie const auto nextBatchToken = job->nextBatch(); if (!nextBatchToken.isEmpty() && nextBatchToken != *m_nextBatchTokens[spaceId] && m_connection) { *m_nextBatchTokens[spaceId] = nextBatchToken; - auto nextJob = m_connection->callApi(spaceId, std::nullopt, std::nullopt, std::nullopt, *m_nextBatchTokens[spaceId]); - connect(nextJob, &BaseJob::success, this, [this, nextJob, spaceId]() { - addBatch(spaceId, nextJob); - }); + m_connection->callApi(spaceId, std::nullopt, std::nullopt, std::nullopt, *m_nextBatchTokens[spaceId]) + .onResult([this, spaceId](const auto &nextJob) { + addBatch(spaceId, nextJob); + }); } else { m_nextBatchTokens[spaceId].reset(); } diff --git a/src/messagecontent/pollhandler.cpp b/src/messagecontent/pollhandler.cpp index 418c4a48f..50c64c4b3 100644 --- a/src/messagecontent/pollhandler.cpp +++ b/src/messagecontent/pollhandler.cpp @@ -49,8 +49,7 @@ void PollHandler::checkLoadRelations() return; } - auto job = m_room->connection()->callApi(m_room->id(), pollStartEvent->id()); - connect(job, &BaseJob::success, this, [this, job]() { + m_room->connection()->callApi(m_room->id(), pollStartEvent->id()).onResult([this](const auto &job) { for (const auto &event : job->chunk()) { handleEvent(event.get()); } diff --git a/src/settings/models/devicesmodel.cpp b/src/settings/models/devicesmodel.cpp index 163df6111..0a763d328 100644 --- a/src/settings/models/devicesmodel.cpp +++ b/src/settings/models/devicesmodel.cpp @@ -22,15 +22,15 @@ DevicesModel::DevicesModel(QObject *parent) void DevicesModel::fetchDevices() { - if (m_connection) { - auto job = m_connection->callApi(); - connect(job, &BaseJob::success, this, [this, job]() { - beginResetModel(); - m_devices = job->devices(); - endResetModel(); - Q_EMIT countChanged(); - }); + if (!m_connection) { + return; } + m_connection->callApi().onResult([this](const auto &job) { + beginResetModel(); + m_devices = job->devices(); + endResetModel(); + Q_EMIT countChanged(); + }); } QVariant DevicesModel::data(const QModelIndex &index, int role) const @@ -100,27 +100,20 @@ void DevicesModel::logout(const QString &deviceId, const QString &password) for (index = 0; m_devices[index].deviceId != deviceId; index++) ; - auto job = m_connection->callApi(m_devices[index].deviceId); - - connect(job, &BaseJob::result, this, [this, job, password, index] { - auto onSuccess = [this, index]() { - beginRemoveRows(QModelIndex(), index, index); - m_devices.remove(index); - endRemoveRows(); - Q_EMIT countChanged(); - }; - if (job->error() != BaseJob::Success) { - QJsonObject replyData = job->jsonData(); - AuthenticationData authData; - authData.session = replyData["session"_L1].toString(); - authData.authInfo["password"_L1] = password; - authData.type = "m.login.password"_L1; - authData.authInfo["identifier"_L1] = QJsonObject{{"type"_L1, "m.id.user"_L1}, {"user"_L1, m_connection->user()->id()}}; - auto innerJob = m_connection->callApi(m_devices[index].deviceId, authData); - connect(innerJob.get(), &BaseJob::success, this, onSuccess); - } else { - onSuccess(); - } + auto onSuccess = [this, index]() { + beginRemoveRows(QModelIndex(), index, index); + m_devices.remove(index); + endRemoveRows(); + Q_EMIT countChanged(); + }; + m_connection->callApi(m_devices[index].deviceId).then(onSuccess, [password, this, index, onSuccess](const auto &job) { + QJsonObject replyData = job->jsonData(); + AuthenticationData authData; + authData.session = replyData["session"_L1].toString(); + authData.authInfo["password"_L1] = password; + authData.type = "m.login.password"_L1; + authData.authInfo["identifier"_L1] = QJsonObject{{"type"_L1, "m.id.user"_L1}, {"user"_L1, m_connection->user()->id()}}; + m_connection->callApi(m_devices[index].deviceId, authData).onResult(onSuccess); }); } diff --git a/src/timeline/models/pinnedmessagemodel.cpp b/src/timeline/models/pinnedmessagemodel.cpp index 5768202aa..2c5c09a23 100644 --- a/src/timeline/models/pinnedmessagemodel.cpp +++ b/src/timeline/models/pinnedmessagemodel.cpp @@ -53,8 +53,7 @@ void PinnedMessageModel::fill() const auto events = m_room->pinnedEventIds(); for (const auto &event : std::as_const(events)) { - auto job = m_room->connection()->callApi(m_room->id(), event); - connect(job, &BaseJob::success, this, [this, job] { + m_room->connection()->callApi(m_room->id(), event).then([this](const auto &job) { beginInsertRows({}, m_pinnedEvents.size(), m_pinnedEvents.size()); m_pinnedEvents.push_back(std::move(fromJson>(job->jsonData()))); Q_EMIT newEventAdded(m_pinnedEvents.back().get());