Modernize job handling

This commit is contained in:
Tobias Fella
2025-08-29 17:12:34 +02:00
committed by Tobias Fella
parent 8f7be9993c
commit 5d5295d06d
9 changed files with 123 additions and 157 deletions

View File

@@ -60,9 +60,8 @@ void NotificationsManager::startNotificationJob(QPointer<NeoChatConnection> conn
}
if (!m_connActiveJob.contains(connection->user()->id())) {
auto job = connection->callApi<GetNotificationsJob>();
m_connActiveJob.append(connection->user()->id());
connect(job, &BaseJob::success, this, [this, job, connection]() {
connection->callApi<GetNotificationsJob>().onResult([this, connection](const auto &job) {
m_connActiveJob.removeAll(connection->user()->id());
processNotificationJob(connection, job, !m_oldNotifications.contains(connection->user()->id()));
});

View File

@@ -61,14 +61,7 @@ void LinkPreviewer::loadUrlPreview()
return;
}
BaseJob *job = nullptr;
if (conn->supportedMatrixSpecVersions().contains("v1.11"_L1)) {
job = conn->callApi<GetUrlPreviewAuthedJob>(m_url);
} else {
QT_IGNORE_DEPRECATIONS(job = conn->callApi<GetUrlPreviewJob>(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<GetUrlPreviewAuthedJob>(m_url);
} else {
QT_IGNORE_DEPRECATIONS(conn->callApi<GetUrlPreviewJob>(m_url).onResult(onSuccess);)
}
}
}

View File

@@ -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();

View File

@@ -133,8 +133,7 @@ void NeoChatConnection::connectSignals()
&Connection::connected,
this,
[this] {
auto job = callApi<GetVersionsJob>(BackgroundRequest);
connect(job, &GetVersionsJob::success, this, [this, job] {
callApi<GetVersionsJob>(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 &currentPassword, const QString &newPassword)
{
auto job = callApi<ChangePasswordJob>(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<ChangePasswordJob>(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<ChangePasswordJob>(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<ChangePasswordJob>(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<DeactivateAccountJob>();
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<DeactivateAccountJob>(authData, QString{}, erase);
connect(innerJob, &BaseJob::success, this, [this]() {
logout(false);
});
}
callApi<DeactivateAccountJob>().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<DeactivateAccountJob>(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)

View File

@@ -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<SetRoomStateWithKeyJob>(id(), "m.room.avatar"_L1, QString(), QJsonObject{{"url"_L1, job->contentUri().toString()}});
});
}
connection()->uploadFile(localFile.toLocalFile()).onResult([this](const auto &job) {
connection()->callApi<SetRoomStateWithKeyJob>(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<PushCondition> conditions = {pushCondition};
// Add new override rule and make sure it's enabled
auto job = connection()->callApi<SetPushRuleJob>("override"_L1, id(), actions, QString(), QString(), conditions, QString());
connect(job, &BaseJob::success, this, [this]() {
auto enableJob = connection()->callApi<SetPushRuleEnabledJob>("override"_L1, id(), true);
connect(enableJob, &BaseJob::success, this, [this]() {
connection()->callApi<SetPushRuleJob>("override"_L1, id(), actions, QString(), QString(), conditions, QString()).onResult([this]() {
connection()->callApi<SetPushRuleEnabledJob>("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<PushCondition> conditions;
auto setJob = connection()->callApi<SetPushRuleJob>("room"_L1, id(), actions, QString(), QString(), conditions, QString());
connect(setJob, &BaseJob::success, this, [this]() {
auto enableJob = connection()->callApi<SetPushRuleEnabledJob>("room"_L1, id(), true);
connect(enableJob, &BaseJob::success, this, [this]() {
connection()->callApi<SetPushRuleJob>("room"_L1, id(), actions, QString(), QString(), conditions, QString()).onResult([this]() {
connection()->callApi<SetPushRuleEnabledJob>("room"_L1, id(), true).onResult([this]() {
m_pushNotificationStateUpdating = false;
});
});
@@ -1172,10 +1165,8 @@ void NeoChatRoom::setPushNotificationState(PushNotificationState::State state)
const QList<PushCondition> conditions;
// Add new room rule and make sure enabled
auto setJob = connection()->callApi<SetPushRuleJob>("room"_L1, id(), actions, QString(), QString(), conditions, QString());
connect(setJob, &BaseJob::success, this, [this]() {
auto enableJob = connection()->callApi<SetPushRuleEnabledJob>("room"_L1, id(), true);
connect(enableJob, &BaseJob::success, this, [this]() {
connection()->callApi<SetPushRuleJob>("room"_L1, id(), actions, QString(), QString(), conditions, QString()).onResult([this]() {
connection()->callApi<SetPushRuleEnabledJob>("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<ReportContentJob>(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<ReportContentJob>(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<GetLocalAliasesJob>(id());
connect(getLocalAliasesJob, &BaseJob::success, this, [this, getLocalAliasesJob, alias] {
if (getLocalAliasesJob->aliases().contains(alias)) {
connection()->callApi<GetLocalAliasesJob>(id()).onResult([this, alias](const auto &job) {
if (job->aliases().contains(alias)) {
return;
} else {
auto setRoomAliasJob = connection()->callApi<SetRoomAliasJob>(alias, id());
connect(setRoomAliasJob, &BaseJob::success, this, [this, alias] {
connection()->callApi<SetRoomAliasJob>(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<GetOneRoomEventJob>(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<GetOneRoomEventJob>(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<RoomEvent> event = fromJson<event_ptr_tt<RoomEvent>>(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<RoomEvent> event = fromJson<event_ptr_tt<RoomEvent>>(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<const Quotient::RoomEvent *, bool> NeoChatRoom::getEvent(const QString &eventId) const

View File

@@ -61,13 +61,12 @@ void SpaceHierarchyCache::populateSpaceHierarchy(const QString &spaceId)
}
m_nextBatchTokens[spaceId] = QString();
auto job = m_connection->callApi<GetSpaceHierarchyJob>(spaceId, std::nullopt, std::nullopt, std::nullopt, *m_nextBatchTokens[spaceId]);
m_connection->callApi<GetSpaceHierarchyJob>(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<GetSpaceHierarchyJob>(spaceId, std::nullopt, std::nullopt, std::nullopt, *m_nextBatchTokens[spaceId]);
connect(nextJob, &BaseJob::success, this, [this, nextJob, spaceId]() {
addBatch(spaceId, nextJob);
});
m_connection->callApi<GetSpaceHierarchyJob>(spaceId, std::nullopt, std::nullopt, std::nullopt, *m_nextBatchTokens[spaceId])
.onResult([this, spaceId](const auto &nextJob) {
addBatch(spaceId, nextJob);
});
} else {
m_nextBatchTokens[spaceId].reset();
}

View File

@@ -49,8 +49,7 @@ void PollHandler::checkLoadRelations()
return;
}
auto job = m_room->connection()->callApi<GetRelatingEventsJob>(m_room->id(), pollStartEvent->id());
connect(job, &BaseJob::success, this, [this, job]() {
m_room->connection()->callApi<GetRelatingEventsJob>(m_room->id(), pollStartEvent->id()).onResult([this](const auto &job) {
for (const auto &event : job->chunk()) {
handleEvent(event.get());
}

View File

@@ -22,15 +22,15 @@ DevicesModel::DevicesModel(QObject *parent)
void DevicesModel::fetchDevices()
{
if (m_connection) {
auto job = m_connection->callApi<GetDevicesJob>();
connect(job, &BaseJob::success, this, [this, job]() {
beginResetModel();
m_devices = job->devices();
endResetModel();
Q_EMIT countChanged();
});
if (!m_connection) {
return;
}
m_connection->callApi<GetDevicesJob>().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<DeleteDeviceJob>(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<DeleteDeviceJob>(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<DeleteDeviceJob>(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<DeleteDeviceJob>(m_devices[index].deviceId, authData).onResult(onSuccess);
});
}

View File

@@ -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<GetOneRoomEventJob>(m_room->id(), event);
connect(job, &BaseJob::success, this, [this, job] {
m_room->connection()->callApi<GetOneRoomEventJob>(m_room->id(), event).then([this](const auto &job) {
beginInsertRows({}, m_pinnedEvents.size(), m_pinnedEvents.size());
m_pinnedEvents.push_back(std::move(fromJson<event_ptr_tt<RoomEvent>>(job->jsonData())));
Q_EMIT newEventAdded(m_pinnedEvents.back().get());