diff --git a/src/base/QXmppFutureUtils_p.h b/src/base/QXmppFutureUtils_p.h index e2bf6caab..7ffc6704a 100644 --- a/src/base/QXmppFutureUtils_p.h +++ b/src/base/QXmppFutureUtils_p.h @@ -224,6 +224,12 @@ auto mapToSuccess(std::variant var) }); } +template +auto mapTaskToSuccess(std::variant var, Converter task) +{ + return mapSuccess(std::move(var), task); +} + template auto chainSuccess(QXmppTask &&source, QObject *context) -> QXmppTask { @@ -236,6 +242,18 @@ auto chainSuccess(QXmppTask &&source, QObject *context) -> QXmppTask +auto chainOnSuccess(QXmppTask &&source, QObject *context, Converter task) -> QXmppTask +{ + QXmppPromise promise; + + source.then(context, [=](Input &&input) mutable { + promise.finish(mapTaskToSuccess(std::move(input), task)); + }); + + return promise.task(); +} + template static auto taskFromFuture(QFuture &&future) -> QXmppTask { diff --git a/src/client/QXmppMixManager.cpp b/src/client/QXmppMixManager.cpp index 9508a3eff..8ea7a183d 100644 --- a/src/client/QXmppMixManager.cpp +++ b/src/client/QXmppMixManager.cpp @@ -431,12 +431,7 @@ QXmppTask QXmppMixManager::createChannel(const /// QXmppTask QXmppMixManager::requestChannelJids(const QString &serviceJid) { - return chain(d->discoveryManager->requestDiscoItems(serviceJid), this, [](QXmppDiscoveryManager::ItemsResult &&result) -> ChannelJidResult { - if (auto *error = std::get_if(&result)) { - return std::move(*error); - } - - const auto &items = std::get>(result); + return chainOnSuccess(d->discoveryManager->requestDiscoItems(serviceJid), this, [](QList &&items) { return transform>(items, [](const QXmppDiscoveryIq::Item &item) { return item.jid(); }); @@ -452,17 +447,10 @@ QXmppTask QXmppMixManager::requestChannelJids /// QXmppTask QXmppMixManager::requestChannelNodes(const QString &channelJid) { - return chain(d->discoveryManager->requestDiscoItems(channelJid, MIX_SERVICE_DISCOVERY_NODE.toString()), this, [](QXmppDiscoveryManager::ItemsResult &&result) -> ChannelNodeResult { - if (auto *error = std::get_if(&result)) { - return std::move(*error); - } - - const auto &items = std::get>(result); - const auto nodes = transform>(items, [](const QXmppDiscoveryIq::Item &item) { + return chainOnSuccess(d->discoveryManager->requestDiscoItems(channelJid, MIX_SERVICE_DISCOVERY_NODE.toString()), this, [](QList &&items) { + return listToMixNodes(transform>(items, [](const QXmppDiscoveryIq::Item &item) { return item.node(); - }); - - return listToMixNodes(nodes); + })); }); } @@ -475,12 +463,8 @@ QXmppTask QXmppMixManager::requestChannelNod /// QXmppTask QXmppMixManager::requestChannelConfiguration(const QString &channelJid) { - return chain(d->pubSubManager->requestItems(channelJid, ns_mix_node_config.toString()), this, [](QXmppPubSubManager::ItemsResult &&result) -> ConfigurationResult { - if (auto *error = std::get_if(&result)) { - return std::move(*error); - } - - return std::get>(result).items.takeFirst(); + return chainOnSuccess(d->pubSubManager->requestItems(channelJid, ns_mix_node_config.toString()), this, [](QXmppPubSubManager::Items &&items) { + return items.items.takeFirst(); }); } @@ -520,12 +504,8 @@ QXmppTask QXmppMixManager::updateChannelConfiguration( /// QXmppTask QXmppMixManager::requestChannelInformation(const QString &channelJid) { - return chain(d->pubSubManager->requestItems(channelJid, ns_mix_node_info.toString()), this, [](QXmppPubSubManager::ItemsResult &&result) -> InformationResult { - if (auto *error = std::get_if(&result)) { - return std::move(*error); - } - - return std::get>(result).items.takeFirst(); + return chainOnSuccess(d->pubSubManager->requestItems(channelJid, ns_mix_node_info.toString()), this, [](QXmppPubSubManager::Items &&items) { + return items.items.takeFirst(); }); } @@ -885,12 +865,8 @@ QXmppTask QXmppMixManager::unbanAllJids(const QString /// QXmppTask QXmppMixManager::requestParticipants(const QString &channelJid) { - return chain(d->pubSubManager->requestItems(channelJid, ns_mix_node_participants.toString()), this, [](QXmppPubSubManager::ItemsResult &&result) -> ParticipantResult { - if (auto *error = std::get_if(&result)) { - return std::move(*error); - } - - return std::get>(std::move(result)).items; + return chainOnSuccess(d->pubSubManager->requestItems(channelJid, ns_mix_node_participants.toString()), this, [](QXmppPubSubManager::Items &&items) { + return items.items; }); } @@ -1186,13 +1162,8 @@ QXmppTask QXmppMixManager::joinChannel(QXmppMixI /// QXmppTask QXmppMixManager::requestJids(const QString &channelJid, const QString &node) { - return chain(d->pubSubManager->requestItems(channelJid, node), this, [](QXmppPubSubManager::ItemsResult &&result) -> JidResult { - if (auto error = std::get_if(&result)) { - return std::move(*error); - } - - const auto &items = std::get>(result).items; - return transform>(items, [](const QXmppPubSubBaseItem &item) { + return chainOnSuccess(d->pubSubManager->requestItems(channelJid, node), this, [](QXmppPubSubManager::Items &&items) { + return transform>(items.items, [](const QXmppPubSubBaseItem &item) { return item.id(); }); });