Skip to content

Commit

Permalink
Add common chain function to return result or error
Browse files Browse the repository at this point in the history
  • Loading branch information
melvo committed Apr 8, 2024
1 parent 5e4bc4b commit c954065
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
8 changes: 8 additions & 0 deletions src/base/QXmppFutureUtils_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ auto chainSuccess(QXmppTask<Input> &&source, QObject *context) -> QXmppTask<std:
});
}

template<typename Result, typename Input, typename Converter>
auto chainOnSuccess(QXmppTask<Input> &&source, QObject *context, Converter convert) -> QXmppTask<Result>
{
return chain<Result>(std::move(source), context, [convert](Input &&input) {
return mapSuccess(std::move(input), convert);
});
}

} // namespace QXmpp::Private

#endif // QXMPPFUTUREUTILS_P_H
53 changes: 12 additions & 41 deletions src/client/QXmppMixManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,7 @@ QXmppTask<QXmppMixManager::CreationResult> QXmppMixManager::createChannel(const
///
QXmppTask<QXmppMixManager::ChannelJidResult> QXmppMixManager::requestChannelJids(const QString &serviceJid)
{
return chain<ChannelJidResult>(d->discoveryManager->requestDiscoItems(serviceJid), this, [](QXmppDiscoveryManager::ItemsResult &&result) -> ChannelJidResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

const auto &items = std::get<QList<QXmppDiscoveryIq::Item>>(result);
return chainOnSuccess<ChannelJidResult>(d->discoveryManager->requestDiscoItems(serviceJid), this, [](QList<QXmppDiscoveryIq::Item> &&items) {
return transform<QVector<ChannelJid>>(items, [](const QXmppDiscoveryIq::Item &item) {
return item.jid();
});
Expand All @@ -452,17 +447,10 @@ QXmppTask<QXmppMixManager::ChannelJidResult> QXmppMixManager::requestChannelJids
///
QXmppTask<QXmppMixManager::ChannelNodeResult> QXmppMixManager::requestChannelNodes(const QString &channelJid)
{
return chain<ChannelNodeResult>(d->discoveryManager->requestDiscoItems(channelJid, MIX_SERVICE_DISCOVERY_NODE.toString()), this, [](QXmppDiscoveryManager::ItemsResult &&result) -> ChannelNodeResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

const auto &items = std::get<QList<QXmppDiscoveryIq::Item>>(result);
const auto nodes = transform<QVector<QString>>(items, [](const QXmppDiscoveryIq::Item &item) {
return chainOnSuccess<ChannelNodeResult>(d->discoveryManager->requestDiscoItems(channelJid, MIX_SERVICE_DISCOVERY_NODE.toString()), this, [](QList<QXmppDiscoveryIq::Item> &&items) {
return listToMixNodes(transform<QVector<QString>>(items, [](const QXmppDiscoveryIq::Item &item) {
return item.node();
});

return listToMixNodes(nodes);
}));
});
}

Expand All @@ -475,12 +463,8 @@ QXmppTask<QXmppMixManager::ChannelNodeResult> QXmppMixManager::requestChannelNod
///
QXmppTask<QXmppMixManager::ConfigurationResult> QXmppMixManager::requestChannelConfiguration(const QString &channelJid)
{
return chain<ConfigurationResult>(d->pubSubManager->requestItems<QXmppMixConfigItem>(channelJid, ns_mix_node_config.toString()), this, [](QXmppPubSubManager::ItemsResult<QXmppMixConfigItem> &&result) -> ConfigurationResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return std::get<QXmppPubSubManager::Items<QXmppMixConfigItem>>(result).items.takeFirst();
return chainOnSuccess<ConfigurationResult>(d->pubSubManager->requestItems<QXmppMixConfigItem>(channelJid, ns_mix_node_config.toString()), this, [](QXmppPubSubManager::Items<QXmppMixConfigItem> &&items) {
return items.items.takeFirst();
});
}

Expand Down Expand Up @@ -520,12 +504,8 @@ QXmppTask<QXmppClient::EmptyResult> QXmppMixManager::updateChannelConfiguration(
///
QXmppTask<QXmppMixManager::InformationResult> QXmppMixManager::requestChannelInformation(const QString &channelJid)
{
return chain<InformationResult>(d->pubSubManager->requestItems<QXmppMixInfoItem>(channelJid, ns_mix_node_info.toString()), this, [](QXmppPubSubManager::ItemsResult<QXmppMixInfoItem> &&result) -> InformationResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return std::get<QXmppPubSubManager::Items<QXmppMixInfoItem>>(result).items.takeFirst();
return chainOnSuccess<InformationResult>(d->pubSubManager->requestItems<QXmppMixInfoItem>(channelJid, ns_mix_node_info.toString()), this, [](QXmppPubSubManager::Items<QXmppMixInfoItem> &&items) {
return items.items.takeFirst();
});
}

Expand Down Expand Up @@ -885,12 +865,8 @@ QXmppTask<QXmppClient::EmptyResult> QXmppMixManager::unbanAllJids(const QString
///
QXmppTask<QXmppMixManager::ParticipantResult> QXmppMixManager::requestParticipants(const QString &channelJid)
{
return chain<ParticipantResult>(d->pubSubManager->requestItems<QXmppMixParticipantItem>(channelJid, ns_mix_node_participants.toString()), this, [](QXmppPubSubManager::ItemsResult<QXmppMixParticipantItem> &&result) -> ParticipantResult {
if (auto *error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

return std::get<QXmppPubSubManager::Items<QXmppMixParticipantItem>>(std::move(result)).items;
return chainOnSuccess<ParticipantResult>(d->pubSubManager->requestItems<QXmppMixParticipantItem>(channelJid, ns_mix_node_participants.toString()), this, [](QXmppPubSubManager::Items<QXmppMixParticipantItem> &&items) {
return items.items;
});
}

Expand Down Expand Up @@ -1187,13 +1163,8 @@ QXmppTask<QXmppMixManager::JoiningResult> QXmppMixManager::joinChannel(QXmppMixI
///
QXmppTask<QXmppMixManager::JidResult> QXmppMixManager::requestJids(const QString &channelJid, const QString &node)
{
return chain<JidResult>(d->pubSubManager->requestItems(channelJid, node), this, [](QXmppPubSubManager::ItemsResult<QXmppPubSubBaseItem> &&result) -> JidResult {
if (auto error = std::get_if<QXmppError>(&result)) {
return std::move(*error);
}

const auto &items = std::get<QXmppPubSubManager::Items<QXmppPubSubBaseItem>>(result).items;
return transform<QVector<Jid>>(items, [](const QXmppPubSubBaseItem &item) {
return chainOnSuccess<JidResult>(d->pubSubManager->requestItems(channelJid, node), this, [](QXmppPubSubManager::Items<QXmppPubSubBaseItem> &&items) {
return transform<QVector<Jid>>(items.items, [](const QXmppPubSubBaseItem &item) {
return item.id();
});
});
Expand Down

0 comments on commit c954065

Please sign in to comment.