Skip to content

Commit

Permalink
Use common chain function to return QXmppClient::EmptyResult
Browse files Browse the repository at this point in the history
  • Loading branch information
melvo committed Mar 27, 2024
1 parent 44dba27 commit 94aa896
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
21 changes: 21 additions & 0 deletions src/base/QXmppFutureUtils_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// We mean it.
//

#include "QXmppClient.h"
#include "QXmppIq.h"
#include "QXmppPromise.h"
#include "QXmppSendResult.h"
Expand Down Expand Up @@ -215,6 +216,26 @@ auto mapSuccess(std::variant<T, Err> var, Function lambda)
std::move(var));
}

template<typename T, typename Err>
auto mapToSuccess(std::variant<T, Err> var)
{
return mapSuccess(std::move(var), [](T) {
return Success();
});
}

template<typename Input>
auto chainSuccess(QXmppTask<Input> &&source, QObject *context) -> QXmppTask<QXmppClient::EmptyResult>
{
QXmppPromise<QXmppClient::EmptyResult> promise;

source.then(context, [=](Input &&input) mutable {
promise.finish(mapToSuccess(std::move(input)));
});

return promise.task();
}

template<typename T>
static auto taskFromFuture(QFuture<T> &&future) -> QXmppTask<T>
{
Expand Down
28 changes: 3 additions & 25 deletions src/client/QXmppMixManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,7 @@ QXmppTask<QXmppMixManager::ConfigurationResult> QXmppMixManager::requestChannelC
QXmppTask<QXmppClient::EmptyResult> QXmppMixManager::updateChannelConfiguration(const QString &channelJid, QXmppMixConfigItem configuration)
{
configuration.setFormType(QXmppDataForm::Submit);

return chain<QXmppClient::EmptyResult>(d->pubSubManager->publishItem(channelJid, ns_mix_node_config.toString(), configuration), this, [](QXmppPubSubManager::PublishItemResult &&result) -> QXmppClient::EmptyResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return QXmpp::Success();
});
return chainSuccess(d->pubSubManager->publishItem(channelJid, ns_mix_node_config.toString(), configuration), this);
}

///
Expand Down Expand Up @@ -550,14 +543,7 @@ QXmppTask<QXmppMixManager::InformationResult> QXmppMixManager::requestChannelInf
QXmppTask<QXmppClient::EmptyResult> QXmppMixManager::updateChannelInformation(const QString &channelJid, QXmppMixInfoItem information)
{
information.setFormType(QXmppDataForm::Submit);

return chain<QXmppClient::EmptyResult>(d->pubSubManager->publishItem(channelJid, ns_mix_node_info.toString(), information), this, [](QXmppPubSubManager::PublishItemResult &&result) -> QXmppClient::EmptyResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return QXmpp::Success();
});
return chainSuccess(d->pubSubManager->publishItem(channelJid, ns_mix_node_info.toString(), information), this);
}

///
Expand Down Expand Up @@ -1225,15 +1211,7 @@ QXmppTask<QXmppMixManager::JidResult> QXmppMixManager::requestJids(const QString
///
QXmppTask<QXmppClient::EmptyResult> QXmppMixManager::addJidToNode(const QString &channelJid, const QString &node, const QString &jid)
{
const QXmppPubSubBaseItem item { jid };

return chain<QXmppClient::EmptyResult>(d->pubSubManager->publishItem(channelJid, node, item), this, [](QXmppPubSubManager::PublishItemResult &&result) -> QXmppClient::EmptyResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return QXmpp::Success();
});
return chainSuccess(d->pubSubManager->publishItem(channelJid, node, QXmppPubSubBaseItem { jid }), this);
}

///
Expand Down
6 changes: 0 additions & 6 deletions src/omemo/QXmppOmemoManager_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,6 @@ static void replaceChildElements(QDomElement &oldElement, const QDomElement &new
}
}

template<typename T, typename Err>
auto mapToSuccess(std::variant<T, Err> var)
{
return mapSuccess(std::move(var), [](T) { return Success(); });
}

QXmppOmemoManagerPrivate::QXmppOmemoManagerPrivate(Manager *parent, QXmppOmemoStorage *omemoStorage)
: q(parent),
omemoStorage(omemoStorage),
Expand Down

0 comments on commit 94aa896

Please sign in to comment.